adc-testing: Georgia Papadakis's documents

parent 0374d6da
y=0
x=input("x: ")
while x>=0:
y=y+x
x=input("x: ")
print y
from numpy import*
from math import sin, pi, atan
import matplotlib.pyplot as p
def sine_samples(n):
"""Produce a vector of samples of sine in [0..2 pi]"""
samples = []
for i in range(n):
samples.append(sin(2*pi*i/n))
return samples
x=input("give number of samples :")
vec=sine_samples(x)
for element in vec:
print element
b=fft.fft(vec)
print("now i'll show you the FFT of it")
print(b)
c=atan(b[0].imag/b[0].real)
d=atan(b[x-1].imag/b[x-1].real)
print("modulus of the first harmonic:")
print(abs(b[0]))
print("argument of the first harmonic:")
print(c)
print("modulus of the last harmonic:")
print(abs(b[x-1]))
print("argument of the last harmonic:")
print(d)
#if you put x=9 it gives you d=1.57079632679 but my calculator gives d=1.431169...so,where is the mistake?
a=[]
print "give a number"
x=input("x: ")
while x>=0:
a.append(x)
print "give a number"
x=input("x: ")
print a[:]
#f_name=input()
#print(f_name)
question = "What is the name of the file?"
print (question)
answer = raw_input()
print ("ok,so the name of the file is " + answer + "let me show it")
f=open(answer)
for line in f:
print line
import math
for i in range (101):
x = 2 * math.pi * i / 100
print math.sin(x)
import math
samples = input('Number of divisions: ')
i = 0
while i <= samples:
# do sample i
x = 2 * math.pi * i / samples
print math.sin(x)
i = i + 1
from math import sin, pi
from sys import argv
def sine_samples(n):
"""Produce a vector of samples of sine in [0..2 pi]"""
samples = []
for i in range(n+1):
samples.append(sin(2*pi*i/n))
return samples
def print_vector(v):
for element in v:
print element
if __name__ == '__main__': # the usual idiom for a main program
# samples = input("give a number: ")
# samples = int(argv[1])
if len(argv)==1:
samples = input("give a number: ")
else:
samples = int(argv[1])
vec = sine_samples(samples)
print_vector(vec)
from numpy import*
from math import sin, pi
import matplotlib.pyplot as p
def sine_samples(n):
"""Produce a vector of samples of sine in [0..2 pi]"""
samples = []
for i in range(n):
samples.append(sin(2*pi*i/n))
return samples
x=input("give number of samples :")
vec=sine_samples(x)
for element in vec:
print element
print("and now the FFT of it:")
print fft.fft(vec)
a = range(x)
b=fft.fft(vec)
p.plot(a, vec, a, abs(b))
p.show()
a=input(
from numpy import*
from math import sin, pi
import matplotlib.pyplot as p
def sine_samples(n):
"""Produce a vector of samples of sine in [0..2 pi]"""
samples = []
for i in range(n):
samples.append(sin(2*pi*i/n))
return samples
x=input("give number of samples :")
vec=sine_samples(x)
for element in vec:
print element
print("and now the FFT of it:")
print fft.fft(vec)
from numpy import*
from math import sin, pi
def argument(n,sampl_rate,fr,ph):
#produce a vector of the arguments of the sinewave#
arg=[]
for i in range(n):
arg.append(2*pi*fr*i/sampl_rate+ph*2*pi)
return arg
def make_sinewave (n,v,amp):
#produces the values if the sinewave#
b=[]
for i in range (n):
b.append(amp*sin(v[i]))
return b
a=input("please give amplitude:")
f=input("please give the sinewave's frequency:")
p=input("please give phase(rad):")
sr=input("please give sampling rate:")
s=input("please give number of samples:")
vector=argument(s,sr,f,p)
print("these are the arguments of the sinewave")
print vector[:]
print ("these are the values of the sinewave")
sine=make_sinewave(s,vector,a)
print sine[:]
from numpy import*
from math import *
import matplotlib.pyplot as pl
def adc_transfer(fsr, bits,inl):
#MAKING THE TRANSFER FUNCTION
a=float(math.pow(2,bits))
lsb=float(fsr/a)
print("the LSB is"),lsb
digital=[]
analog=[]
plot_analog=[]
plot_digital=[]
straight_line=[]
for i in range (a):
b=lsb*i
analog.append(b)
#THE INL ERROR HAS AN INFLUANCE ON THE Y-AXES(thus,on "digital"and "plot_digital")
digital.append(i+inl)
dnl=input ("WHAT IS THE DNL ERROR AT THIS STEP? : ")
d=(dnl*lsb)
for w in range(a):
#THE DNL ERROR HAS AN INFLUANCE ON THE Y-AXES(thus,on "analog" and "plot_analog")
plot_analog.append((w/a+i)*lsb+d)
plot_digital.append(digital[w])
if (w==a/2):
straight_line.append((w/a+i)*lsb)
print ("THE ANALOG VALUES ARE: "),analog [:]
print ("THE DIGITAL VALUES ARE: "),digital[:]
print ("plot_analog: "),plot_analog[:]
print ("plot_digital: "),plot_digital[:]
print ("THE STRAIGHT LINE VECTOR IS: "),straight_line[:]
#PLOTTING THE TRANSFER FUNCTION
# pl.plot(plot_analog, plot_digital)
# pl.show()
#PLOTTING THE STRAIGHT LINE OF THE TRANSFER FUNCTION
# pl.plot(straight_line,digital)
# pl.show()
pl.plot(plot_analog,plot_digital)
pl.show()
return analog,digital
full_scale_range=input("GIVE ME THE FULL SCALE RANGE: ")
num_of_bits=input("GIVE ME THE NUMBER OF BITS: ")
inl_error=float(input("PLEASE GIVE ME THE INL ERROR: "))
adc_transfer(full_scale_range,num_of_bits,inl_error)
from numpy import*
x=input("give number of samples: ")
b=array(random.random(x)*)
print b[:]
from numpy import*
from math import sin, pi
import matplotlib.pyplot as pl
def argument(n,sampl_rate,fr,ph):
#produce a vector of the arguments of the sinewave#
arg=[]
for i in range(n):
arg.append(2*pi*fr*i/sampl_rate+ph*2*pi)
return arg
def make_sinewave (n,v,amp):
#produces the values if the sinewave#
b=[]
for i in range (n):
b.append(amp*sin(v[i]))
return b
a=input("please give amplitude:")
f=input("please give the sinewave's frequency:")
p=input("please give phase(rad):")
sr=input("please give sampling rate:")
s=input("please give number of samples:")
vector=argument(s,sr,f,p)
print("these are the arguments of the sinewave")
print vector[:]
print ("these are the values of the sinewave")
sine=make_sinewave(s,vector,a)
sine=array(sine)
print sine[:]
b=array(random.random(s)-1)
print ("this is the noise")
print b[:]
c=array(b+sine)
print("the sum of the sinewave and the noise is:")
print c[:]
#SNR calculation#
sine_rss=0
noise_rss=0
for i in range (s):
sine_rss=sine_rss+sine[i]*sine[i]
noise_rss=noise_rss+b[i]*b[i]
SNR=sine_rss/noise_rss
print ("the SNR is" ) ,10*log10(SNR)
#creation of the plot#
g=range(s)
pl.plot(g, c)
pl.show()
import matplotlib.pyplot as pl
def plot_routine(arr,n):
#plots the contents of arr#
#creation of the plot#
g=range(n)
pl.plot(g, arr)
pl.show()
a=[]
s=input("give me the size of the array: ")
answer="y"
i=0
while (answer=="y") and (i in range (s)):
b=input("give me element: ")
a.append(b)
print("would you like to continue?")
answer=raw_input()
i=i+1
plot_routine(a,s)
from numpy import*
from math import *
import matplotlib.pyplot as pl
#def Denary2Binary(n):
# #convert denary integer n to binary string bStr
# bStr = ''
# if n < 0: raise ValueError, "must be a positive integer"
# if n == 0: return '0'
# while n > 0:
# bStr = str(n % 2) + bStr
# n = n >> 1
# return bStr
def adc_transfer(v, fsr, bits):
#MAKING THE TRANSFER FUNCTION
a=2.0**bits
lsb=float(fsr/a)
print("the LSB is"),lsb
digital=[]
analog=[]
for i in range (a):
b=lsb*i
analog.append(b)
digital.append(i)
print ("THE ANALOG VALUES ARE: "),analog [:]
print ("THE DIGITAL VALUES ARE: "),digital[:]
#PLOTTING THE TRANSFER FUNCTION
#pl.plot(analog, digital)
#pl.show()
#AND NOW THE CORRESPONDANCE!!!
i=0
if (v>=analog[0]) and (v<=analog[int(math.pow(2,bits)-1)]):
while i<=a:
if (v>=analog[i]) and (v<analog[i+1]):
return digital[i]
i=i+1
elif (v>analog[int(math.pow(2,bits)-1)]) :
return digital[int(math.pow(2,bits)-1)]
else:
return digital[0]
voltage_value=float(input("GIVE ME THE VOLTAGE VALUE: "))
full_scale_range=input("GIVE ME THE FULL SCALE RANGE: ")
num_of_bits=input("GIVE ME THE NUMBER OF BITS: ")
digital_value=adc_transfer(voltage_value,full_scale_range,num_of_bits)
print ("THE DIGITAL VALUE CORRESPONDING TO THE ANALOG IS :"),digital_value
from numpy import*
from math import *
import matplotlib.pyplot as pl
def adc_transfer(fsr, bits):
#MAKING THE TRANSFER FUNCTION
a=float(math.pow(2,bits))
lsb=float(fsr/a)
print("the LSB is"),lsb
digital=[]
analog=[]
plot_analog=[]
plot_digital=[]
for i in range (a):
b=lsb*i
analog.append(b)
digital.append(i)
for j in range(a):
plot_digital.append(digital[i])
for w in range(a):
plot_analog.append((w/a+i)*lsb)
print ("THE ANALOG VALUES ARE: "),analog [:]
print ("THE DIGITAL VALUES ARE: "),digital[:]
print ("plot_analog: "),plot_analog[:]
print ("plot_digital: "),plot_digital[:]
#PLOTTING THE TRANSFER FUNCTION
pl.plot(plot_analog, plot_digital)
pl.show()
return analog,digital
full_scale_range=input("GIVE ME THE FULL SCALE RANGE: ")
num_of_bits=input("GIVE ME THE NUMBER OF BITS: ")
adc_transfer(full_scale_range,num_of_bits)
from numpy import*
from math import *
import matplotlib.pyplot as pl
def adc_transfer(fsr, bits,inl):
#MAKING THE TRANSFER FUNCTION
a=float(math.pow(2,bits))
lsb=float(fsr/a)
print("the LSB is"),lsb
digital=[]
analog=[]
plot_analog=[]
plot_digital=[]
straight_line=[]
for i in range (a):
b=lsb*i
analog.append(b)
#THE INL ERROR HAS AN INFLUANCE ON THE Y-AXES(thus,on "digital"and "plot_digital")
digital.append(i+inl)
dnl=input ("WHAT IS THE DNL ERROR AT THIS STEP? : ")
d=(dnl*lsb)
for w in range(a):
#THE DNL ERROR HAS AN INFLUANCE ON THE Y-AXES(thus,on "analog" and "plot_analog")
plot_analog.append((w/a+i)*lsb+d)
plot_digital.append(digital[i])
if (w==a/2):
straight_line.append((w/a+i)*lsb)
print ("THE ANALOG VALUES ARE: "),analog [:]
print ("THE DIGITAL VALUES ARE: "),digital[:]
print ("plot_analog: "),plot_analog[:]
print ("plot_digital: "),plot_digital[:]
print ("THE STRAIGHT LINE VECTOR IS: "),straight_line[:]
#PLOTTING THE TRANSFER FUNCTION
# pl.plot(plot_analog, plot_digital)
# pl.show()
#PLOTTING THE STRAIGHT LINE OF THE TRANSFER FUNCTION
# pl.plot(straight_line,digital)
# pl.show()
pl.plot(plot_analog,plot_digital)
pl.show()
return analog,digital
full_scale_range=input("GIVE ME THE FULL SCALE RANGE: ")
num_of_bits=input("GIVE ME THE NUMBER OF BITS: ")
inl_error=float(input("PLEASE GIVE ME THE INL ERROR: "))
adc_transfer(full_scale_range,num_of_bits,inl_error)
from numpy import*
from numpy import bartlett
import matplotlib.pyplot as plt
n=input("the number of samples is :")
d=bartlett(n)
print d[:]
#plt.plot(d)
#plt.show()
e=abs(fft.fft(d))
e=array(e)
maximum=max(e)
f=e/maximum
g=log10(f)
h=20*g
i=fft.fftshift(h)
plt.plot(i)
plt.show()
from math import *
from numpy import *
from matplotlib import pyplot as plt
name=raw_input("give me the name of the file :")
f=open(name)
a=[]
for line in f:
value=int(line)
a.append(value)
b=fft.fft(a)
c=abs(b)
#n=b.size
#c=abs(b)
#h=[]
#i=[]
#h,i=histogram(c,n)
#plt.hist(c,n)
#plt.show()
from numpy import*
from numpy import bartlett
import matplotlib.pyplot as plt
n=input("the number of samples is :")
d=bartlett(n)
print d[:]
plt.plot(d)
plt.show()
e=fft.fft(d)
f=abs(e)
n=f.size
print n
from math import *
from numpy import *
from matplotlib import pyplot as plt
##################### OPEN THE FILE ###################################
name=raw_input("GIVE ME THE NAME OF THE FILE:")
f=open(name)
a=[]
for line in f:
value=int(line)
a.append(value)
#################### MAKING THE FFT ###################################
a=a-mean(a)
a=array(a)
asize=a.size
x=log2(asize)
print ("SIZE OF a IS: "),asize
print ("THE SIZE OF a EQUALS TO: 2 TO THE "),x
b=abs(fft.fft(a))
maxb=max(b)
d=b/maxb
e=log10(d)
f=20*e
h=fft.fftshift(f)
m=h.size
################ I 'LL MAKE A COUPLE:VECTOR h AND VECTOR freq #################################
freq=[]
for i in range (m):
freq.append(-50000000+i*100000000/m)
######## I'LL MAKE ANOTHER COUPLE:Vector hhalfh AND VECTOR freq_shift ##########################
hhalf=[]
freq_shift=[]
for i in range(m/2,m):
freq_shift.append(freq[i])
hhalf.append(h[i])
hhalf=array(hhalf)
freq_shift=array(freq_shift)
ser=hhalf.size
sep=freq_shift.size
print ("SIZE OF hhalf IS:"),ser
########## FINDING THE POSITION OF THE MAXMUM OF hhalf=>EXPECTING THE INPUT FREQUENCY #########
for i in range (ser):
maximum=hhalf[i]
for j in range (ser):
if hhalf[j]>maximum:
maximum=hhalf[j]
sinefreq=freq_shift[j]
print("the frequency of the sinewave is "),sinefreq
print("maximum of hhalf is "),maximum
#************************************* HARMONICS *********************************************#
# 2 important vectors: 1) position :has the position of the harmonics , 2) distortion :has the values of hhalf at the position of the harmonics (those two have the same size)
position=[]
distortion=[]
if x%2==0 :
################## 64 ##########################(x=12)
for i in range(int(2**(x/2))):
vector=[]
######################### 32 ###################
for j in range(int(2**((x/2)-1))):
vector.append(hhalf[j+i*(2**((x/2)-1))])
vector=array(vector)
maxh=max(vector)
for j in range(int(2**((x/2)-1))):
if vector[j]>=maxh:
maxh=vector[j]
position.append(freq_shift[j+i*(2**((x/2)-1))])
distortion.append(vector[j])
else :
########################## 128 #################(x=13)
for i in range(int((2**((x+1)/2)))):
vector=[]
############################ 32 ################
for j in range(int(2**(((x-1)/2)-1))):
vector.append(hhalf[j+i*(2**(((x-1)/2)-1))])
vector=array(vector)
maxh=max(vector)
for j in range(int(2**(((x-1)/2)-1))):
if vector[j]>=maxh:
maxh=vector[j]
position.append(freq_shift[j+i*(2**(((x-1)/2)-1))])
distortion.append(vector[j])
###### i will calculate the mean value of distortion and save the values in a vector called: realdistortion and the position of them(frequencies) in a vector called: realposition
distortion=array(distortion)
position=array(position)
mean=mean(distortion)
realdistortion=[]
realposition=[]
if x%2==0 :
for i in range(int(2**(x/2))):
if distortion[i]>=mean:
realdistortion.append(distortion[i])
realposition.append(position[i])
else:
for i in range(int((2**((x+1)/2)))):
if distortion[i]>=mean:
realdistortion.append(distortion[i])
realposition.append(position[i])
print("THE DISTORTION VECTOR :"),realdistortion[:]
print("THE POSITION OF THE HARMONICS :"),realposition[:]
plt.plot(freq_shift,hhalf)
plt.title("The FFT of the signal")
plt.xlabel("frequency")
plt.ylabel("dB")
plt.show()
from math import *
from numpy import *
from matplotlib import pyplot as plt
##################### OPEN THE FILE ###################################
name=raw_input("GIVE ME THE NAME OF THE FILE:")
f=open(name)
a=[]
for line in f:
value=int(line)
a.append(value)
#################### MAKING THE FFT ###################################
a=a-mean(a)
a=array(a)
asize=a.size
x=log2(asize)
print ("SIZE OF a IS: "),asize
print ("THE SIZE OF a EQUALS TO: 2 TO THE "),x
b=abs(fft.fft(a))
maxb=max(b)
d=b/maxb
e=log10(d)
f=20*e
h=fft.fftshift(f)
m=h.size
################ I 'LL MAKE A COUPLE:VECTOR h AND VECTOR freq #################################
freq=[]
for i in range (m):
freq.append(-50000000+i*100000000/m)
######## I'LL MAKE ANOTHER COUPLE:Vector hhalfh AND VECTOR freq_shift ##########################
hhalf=[]
freq_shift=[]
for i in range(m/2,m):
freq_shift.append(freq[i])
hhalf.append(h[i])
hhalf=array(hhalf)
freq_shift=array(freq_shift)
ser=hhalf.size
print ("SIZE OF hhalf IS:"),ser
########## FINDING THE POSITION OF THE MAXMUM OF hhalf=>EXPECTING THE INPUT FREQUENCY #########
for i in range (ser):
maximum=hhalf[i]
for j in range (ser):
if hhalf[j]>maximum:
maximum=hhalf[j]
sinefreq=freq_shift[j]
print("the frequency of the sinewave is "),sinefreq
print("maximum of hhalf is "),maximum
#************************************************************************HARMONICS**********************************************************************************
#############WINDOW FUNCTION################
nbart=asize/2
d=hamming(nbart)
print d[:]
#e=abs(fft.fft(d))
#e=array(e)
#maximum=max(e)
#f=e/maximum
#g=log10(f)
#h=20*g
#i=fft.fftshift(h)
#plt.plot(i)
#plt.show()
gerog=a[0:(asize/2)]
print ("size of gerog is:"),gerog.size
gerog=array(gerog)
d=array(d)
mult=d*gerog
mult=array(mult)
mult1=abs(fft.fft(mult))
maxmult1=max(mult1)
mult2=mult1/maxmult1
mult3=log10(mult2)
mult4=20*mult3
mult5=fft.fftshift(mult4)
multsize=mult5.size
freq_shift1=[]
for i in range (gerog.size):
freq_shift1.append(-50000000+i*100000000/nbart)
freq_shift1=array(freq_shift1)
plt.plot(freq_shift1,mult5)
plt.title("The FFT of the signal")
plt.xlabel("frequency")
plt.ylabel("dB")
plt.show()
from math import *
from numpy import *
from matplotlib import pyplot as plt
##################### OPEN THE FILE ###################################
name=raw_input("GIVE ME THE NAME OF THE FILE:")
f=open(name)
a=[]
for line in f:
value=int(line)
a.append(value)
#################### MAKING THE FFT ###################################
a=a-mean(a)
a=array(a)
asize=a.size
x=log2(asize)
print ("SIZE OF a IS: "),asize
print ("THE SIZE OF a EQUALS TO: 2 TO THE "),x
b=abs(fft.fft(a))
maxb=max(b)
d=b/maxb
e=log10(d)
f=20*e
h=fft.fftshift(f)
m=h.size
################ I 'LL MAKE A COUPLE:VECTOR h AND VECTOR freq #################################
freq=[]
for i in range (m):
freq.append(-50000000+i*100000000/m)
######## I'LL MAKE ANOTHER COUPLE:Vector hhalfh AND VECTOR freq_shift ##########################
hhalf=[]
freq_shift=[]
for i in range(m/2,m):
freq_shift.append(freq[i])
hhalf.append(h[i])
hhalf=array(hhalf)
freq_shift=array(freq_shift)
ser=hhalf.size
print ("SIZE OF hhalf IS:"),ser
print ("freq_shift is :"),freq_shift
########## FINDING THE POSITION OF THE MAXMUM OF hhalf=>EXPECTING THE INPUT FREQUENCY #########
for i in range (ser):
maximum=hhalf[i]
for j in range (ser):
if hhalf[j]>maximum:
maximum=hhalf[j]
sinefreq=freq_shift[j]
print("the frequency of the sinewave is "),sinefreq
print("maximum of hhalf is "),maximum
#*****************************************************************************HARMONICS**************************************************************
position=[]
distortion=[]
for i in range (ser):
#I TRY TO FIND THE POSITION OF THE HARMONICS BY SEARCHING IN freq_shift VEVTOR FOR FREQUENCIES CLOSE TO THE:f=n*sinefreq #
if (freq_shift[i]%sinefreq==0) :#if you find a harmonic
if x%2==0 :
magnitude=[]
helpfreq=[]
for j in range (int(-2**(((x-2)/2)-1)),int(2**(((x-2)/2)-1))):
magnitude.append(hhalf[i+j])
helpfreq.append(freq_shift[i+j])
magnitude=array(magnitude)
helpfreq=array(helpfreq)
maxmag=max(magnitude)
sizemag=magnitude.size
for w in range(sizemag):
if magnitude[w]>=maxmag:
maxmag=magnitude[w]
position.append(helpfreq[w])
distortion.append(magnitude[w])
else:
magnitude=[]
helpfreq=[]
for j in range(int(-2**(((x-3)/2)-1)),int(2**(((x-3)/2)-1))): #(if doubled sampling frequency is used)
magnitude.append(hhalf[i+j])
helpfreq.append(freq_shift[i+j])
magnitude=array(magnitude)
helpfreq=array(helpfreq)
maxmag=max(magnitude)
sizemag=magnitude.size
for w in range(sizemag):
if magnitude[w]>=maxmag:
maxmag=magnitude[w]
position.append(helpfreq[w])
distortion.append(magnitude[w])
print ("The position vector is:"),position[:]
print ("The distortion vector is:"),distortion[:]
distortion=array(distortion)
distsize=distortion.size
position=array(position)
meandistortion=mean(distortion)
print("the mean value of distort is :"),meandistortion
##################
realdistortion=[]
realposition=[]
for i in range(distsize):
if distortion[i]>=meandistortion:
realdistortion.append(distortion[i])
realposition.append(position[i])
print("the REAL position of distortion is: "),realposition[:]
print("the REAL distortion is: "),realdistortion[:]
########
plt.plot(freq_shift,hhalf)
plt.title("The FFT of the signal")
plt.xlabel("frequency")
plt.ylabel("dB")
plt.show()
from math import *
from numpy import *
from matplotlib import pyplot as plt
##################### OPEN THE FILE ###################################
name=raw_input("GIVE ME THE NAME OF THE FILE:")
f=open(name)
a=[]
for line in f:
value=int(line)
a.append(value)
#################### MAKING THE FFT ###################################
a=a-mean(a)
a=array(a)
asize=a.size
x=log2(asize)
print ("SIZE OF a IS: "),asize
print ("THE SIZE OF a EQUALS TO: 2 TO THE "),x
b=abs(fft.fft(a))
maxb=max(b)
d=b/maxb
e=log10(d)
f=20*e
h=fft.fftshift(f)
m=h.size
################ I 'LL MAKE A COUPLE:VECTOR h AND VECTOR freq #################################
freq=[]
for i in range (m):
freq.append(-50000000+i*100000000/m)
######## I'LL MAKE ANOTHER COUPLE:Vector hhalfh AND VECTOR freq_shift ##########################
hhalf=[]
freq_shift=[]
for i in range(m/2,m):
freq_shift.append(freq[i])
hhalf.append(h[i])
hhalf=array(hhalf)
freq_shift=array(freq_shift)
ser=hhalf.size
sep=freq_shift.size
print ("SIZE OF hhalf IS:"),ser
########## FINDING THE POSITION OF THE MAXMUM OF hhalf=>EXPECTING THE INPUT FREQUENCY #########
for i in range (ser):
maximum=hhalf[i]
for j in range (ser):
if hhalf[j]>maximum:
maximum=hhalf[j]
sinefreq=freq_shift[j]
print("the frequency of the sinewave is "),sinefreq
print("maximum of hhalf is "),maximum
#************************************* HARMONICS *********************************************#
position=[]
distortion=[]
if x%2==0 :
################## 64 ##########################(x=12)
for i in range(int(2**(x/2))):
vector=[]
######################### 32 ###################
for j in range(int(2**((x/2)-1))):
vector.append(hhalf[j+i*(2**((x/2)-1))])
vector=array(vector)
maxh=max(vector)
for j in range(int(2**((x/2)-1))):
if vector[j]>=maxh:
maxh=vector[j]
position.append(freq_shift[j+i*(2**((x/2)-1))])
distortion.append(vector[j])
else :
########################## 128 #################(x=13)
for i in range(int((2**((x+1)/2)))):
vector=[]
############################ 32 ################
for j in range(int(2**(((x-1)/2)-1))):
vector.append(hhalf[j+i*(2**(((x-1)/2)-1))])
vector=array(vector)
maxh=max(vector)
for j in range(int(2**(((x-1)/2)-1))):
if vector[j]>=maxh:
maxh=vector[j]
position.append(freq_shift[j+i*(2**(((x-1)/2)-1))])
distortion.append(vector[j])
print("THE POSITION OF THE HARMONICS ARE :"),position[:]
plt.plot(freq_shift,hhalf)
plt.title("The FFT of the signal")
plt.xlabel("frequency")
plt.ylabel("dB")
plt.show()
from math import *
from numpy import *
from matplotlib import pyplot as plt
##################### OPEN THE FILE ###################################
name=raw_input("GIVE ME THE NAME OF THE FILE:")
f=open(name)
a=[]
for line in f:
value=int(line)
a.append(value)
#################### MAKING THE FFT ###################################
a=a-mean(a)
a=array(a)
asize=a.size
x=log2(asize)
print ("SIZE OF a IS: "),asize
print ("THE SIZE OF a EQUALS TO: 2 TO THE "),x
b=abs(fft.fft(a))
maxb=max(b)
d=b/maxb
e=log10(d)
f=20*e
h=fft.fftshift(f)
m=h.size
################ I 'LL MAKE A COUPLE:VECTOR h AND VECTOR freq #################################
freq=[]
for i in range (m):
freq.append(-50000000+i*100000000/m)
######## I'LL MAKE ANOTHER COUPLE:Vector hhalfh AND VECTOR freq_shift ##########################
hhalf=[]
freq_shift=[]
for i in range(m/2,m):
freq_shift.append(freq[i])
hhalf.append(h[i])
hhalf=array(hhalf)
freq_shift=array(freq_shift)
ser=hhalf.size
print ("SIZE OF hhalf IS:"),ser
print ("freq_shift is :"),freq_shift
########## FINDING THE POSITION OF THE MAXMUM OF hhalf=>EXPECTING THE INPUT FREQUENCY #########
for i in range (ser):
maximum=hhalf[i]
for j in range (ser):
if hhalf[j]>maximum:
maximum=hhalf[j]
sinefreq=freq_shift[j]
print("the frequency of the sinewave is "),sinefreq
print("maximum of hhalf is "),maximum
#*****************************************************************************HARMONICS**************************************************************
position=[]
distortion=[]
for i in range (ser):
#I TRY TO FIND THE POSITION OF THE HARMONICS BY SEARCHING IN freq_shift VEVTOR FOR FREQUENCIES CLOSE TO THE:f=n*sinefreq #
if (freq_shift[i]%sinefreq==0) :#if you find a harmonic
if x%2==0 :
magnitude=[]
helpfreq=[]
for j in range (int(-2**(((x-2)/2)-1)),int(2**(((x-2)/2)-1))):
magnitude.append(hhalf[i+j])
helpfreq.append(freq_shift[i+j])
magnitude=array(magnitude)
helpfreq=array(helpfreq)
maxmag=max(magnitude)
sizemag=magnitude.size
for w in range(sizemag):
if magnitude[w]>=maxmag:
maxmag=magnitude[w]
position.append(helpfreq[w])
distortion.append(magnitude[w])
else:
magnitude=[]
helpfreq=[]
for j in range(int(-2**(((x-3)/2)-1)),int(2**(((x-3)/2)-1))): #(if doubled sampling frequency is used)
magnitude.append(hhalf[i+j])
helpfreq.append(freq_shift[i+j])
magnitude=array(magnitude)
helpfreq=array(helpfreq)
maxmag=max(magnitude)
sizemag=magnitude.size
for w in range(sizemag):
if magnitude[w]>=maxmag:
maxmag=magnitude[w]
position.append(helpfreq[w])
distortion.append(magnitude[w])
print ("The position vector is:"),position[:]
print ("The distortion vector is:"),distortion[:]
distortion=array(distortion)
distsize=distortion.size
position=array(position)
meandistortion=mean(distortion)
print("the mean value of distort is :"),meandistortion
##################
realdistortion=[]
realposition=[]
noise1=[]
for i in range(distsize):
if distortion[i]>=meandistortion:
realdistortion.append(distortion[i])
realposition.append(position[i])
for i in range (ser):
if hhalf[i]<meandistortion:
noise1.append(hhalf[i])
print("the REAL position of distortion is: "),realposition[:]
print("the REAL distortion is: "),realdistortion[:]
noise1=array(noise1)
noisesize=noise1.size
noise2=[]
for i in range(noisesize):
if noise1[i]>-140.0:
noise2.append(noise1[i])
print ("THE NOISE FLOOR IS :"),mean(noise2)
snr=maximum-mean(noise2)
print ("SNR IS (quick and dirty) :" ),snr
########
plt.plot(freq_shift,hhalf)
plt.title("The FFT of the signal")
plt.xlabel("frequency")
plt.ylabel("dB")
plt.show()
from math import *
from numpy import *
from matplotlib import pyplot as plt
##################### OPEN THE FILE ###################################
name=raw_input("GIVE ME THE NAME OF THE FILE:")
f=open(name)
a=[]
for line in f:
value=int(line)
a.append(value)
#################### MAKING THE FFT ###################################
a=a-mean(a)
a=array(a)
asize=a.size
x=log2(asize)
print ("SIZE OF a IS: "),asize
print ("THE SIZE OF a EQUALS TO: 2 TO THE "),x
b=abs(fft.fft(a))
maxb=max(b)
d=b/maxb
e=log10(d)
f=20*e
h=fft.fftshift(f)
m=h.size
################ I 'LL MAKE A COUPLE:VECTOR h AND VECTOR freq #################################
freq=[]
for i in range (m):
freq.append(-50000000+i*100000000/m)
######## I'LL MAKE ANOTHER COUPLE:Vector hhalfh AND VECTOR freq_shift ##########################
hhalf=[]
freq_shift=[]
for i in range(m/2,m):
freq_shift.append(freq[i])
hhalf.append(h[i])
hhalf=array(hhalf)
freq_shift=array(freq_shift)
ser=hhalf.size
print ("SIZE OF hhalf IS:"),ser
print ("freq_shift is :"),freq_shift
########## FINDING THE POSITION OF THE MAXMUM OF hhalf=>EXPECTING THE INPUT FREQUENCY #########
for i in range (ser):
maximum=hhalf[i]
for j in range (ser):
if hhalf[j]>maximum:
maximum=hhalf[j]
sinefreq=freq_shift[j]
print("the frequency of the sinewave is "),sinefreq
print("maximum of hhalf is "),maximum
#############WINDOW FUNCTION################
nbart=asize/2
d=hamming(nbart)
print d[:]
#e=abs(fft.fft(d))
#e=array(e)
#maximum=max(e)
#f=e/maximum
#g=log10(f)
#h=20*g
#i=fft.fftshift(h)
#plt.plot(i)
#plt.show()
gerog=a[0:(asize/2)]
print ("size of gerog is:"),gerog.size
gerog=array(gerog)
d=array(d)
mult=d*gerog
mult=array(mult)
mult1=abs(fft.fft(mult))
maxmult1=max(mult1)
mult2=mult1/maxmult1
mult3=log10(mult2)
mult4=20*mult3
mult5=fft.fftshift(mult4)
multsize=mult5.size
freq_shift1=[]
for i in range (gerog.size):
freq_shift1.append(-50000000+i*100000000/nbart)
for i in range (gerog.size):
if freq_shift1[i]==0:
flag=i
fourievector=mult5[flag:multsize]
freqvector=freq_shift1[flag:multsize] #the distance between two elements of freqvector is 2fs/M and not fs/M. -(THE SAME HAPPENS FOR freq_shift1[])
################################################ CALCULATION OF THE DISTORTION WHEN THERE IS ALSO PROCESS GAIN SO THE HARMONICS ARE A LITTLE SPREAD ############################################################################
#################################################### THE ARRAYS THAT ARE GOING TO BE USED ARE: fourievector and freqvector #####################################################################################################
position=[]
distortion=[]
for i in range (fourievector.size):
#I TRY TO FIND THE POSITION OF THE HARMONICS BY SEARCHING IN freq_shift VECTOR FOR FREQUENCIES CLOSE TO THE:f=n*sinefreq #
if (freqvector[i]%sinefreq==0) :#if you find a harmonic
if x%2==0 :
magnitude=[]
helpfreq=[]
if i>=2**(((x-2)/2)-1) and i<=fourievector.size-2**(((x-2)/2)-1)-1:
for j in range (int(-2**(((x-2)/2)-1)),int(2**(((x-2)/2)-1))):
magnitude.append(fourievector[i+j])
helpfreq.append(freqvector[i+j])
elif i<2**(((x-2)/2)-1):
for j in range (0,int(i+2**(((x-2)/2)-1))):
magnitude.append(fourievector[j])
helpfreq.append(freqvector[j])
else:
for j in range (int(i-2**(((x-2)/2)-1)),fourievector.size):
magnitude.append(fourievector[j])
helpfreq.append(freqvector[j])
magnitude=array(magnitude)
helpfreq=array(helpfreq)
maxmag=max(magnitude)
sizemag=magnitude.size
for w in range(sizemag):
if magnitude[w]>=maxmag:
maxmag=magnitude[w]
position.append(helpfreq[w])
distortion.append(magnitude[w])
else:
magnitude=[]
helpfreq=[]
for j in range(int(-2**(((x-3)/2)-1)),int(2**(((x-3)/2)-1))):
magnitude.append(fourievector[i+j])
helpfreq.append(freqvector[i+j])
magnitude=array(magnitude)
helpfreq=array(helpfreq)
maxmag=max(magnitude)
sizemag=magnitude.size
for w in range(sizemag):
if magnitude[w]>=maxmag:
maxmag=magnitude[w]
position.append(helpfreq[w])
distortion.append(magnitude[w])
############################################# I ASSUMED THAT WHEN THERE IS A SIGNAL LEACAGE,ONLY THE HIGHEST PEAK IS A HARMONIC AND THE REST OF THE PEAKS ARE NOTHING,NOT EVEN NOISE ###################
distortion=array(distortion)
distsize=distortion.size
position=array(position)
meandistortion=mean(distortion)
realdistortion=[]
realposition=[]
for i in range(distsize):
if distortion[i]>=meandistortion:
realdistortion.append(distortion[i])
realposition.append(position[i])
print("the REAL position of distortion is: "),realposition[:]
print("the REAL distortion is: "),realdistortion[:]
freqvector=array(freqvector)
fourievector=array(fourievector)
plt.plot(freqvector,fourievector)
plt.title("The FFT of the signal")
plt.xlabel("frequency")
plt.ylabel("dB")
plt.show()
from math import *
from numpy import *
from matplotlib import pyplot as plt
##################### OPEN THE FILE ###########################################################
num_of_bits=input("GIVE ME THE NUMBER OF BITS :")
real_amplit=input("GIVE ME THE INPUT AMPLITUDE :") #(I NEED both real_amplit AND full_scale_amplitud
full_scale_amplitude=input("GIVE ME THE FULLSCALE AMPLITUDE :") # IN ORDER TO MEASURE ENOB )
name=raw_input("GIVE ME THE NAME OF THE FILE:")
f=open(name)
a=[]
for line in f:
value=int(line)
a.append(value)
#################### MAKING THE FFT ###########################################################
a=a-mean(a)
a=array(a)
asize=a.size
x=log2(asize)
print ("NUMBER OF SAMPLES ARE(-SIZE OF a IS) : "),asize
print ("THE SIZE OF a EQUALS TO: 2 TO THE "),x
b=abs(fft.fft(a))
maxb=max(b)
d=b/maxb
e=log10(d)
f=20*e
h=fft.fftshift(f)
m=h.size
################ I 'LL MAKE A COUPLE:VECTOR h AND VECTOR freq #################################
freq=[]
for i in range (m):
freq.append(-50000000+i*100000000/m)
######## I'LL MAKE ANOTHER COUPLE:Vector hhalfh AND VECTOR freq_shift #########################
hhalf=[]
freq_shift=[]
for i in range(m/2,m):
freq_shift.append(freq[i])
hhalf.append(h[i])
hhalf=array(hhalf)
freq_shift=array(freq_shift)
ser=hhalf.size
print ("SIZE OF hhalf IS:"),ser
########## SEARCH FOR THE POSITION OF THE MAXMUM OF hhalf=>EXPECTING THE INPUT FREQUENCY #########
for i in range (ser):
maximum=hhalf[i]
for j in range (ser):
if hhalf[j]>maximum:
maximum=hhalf[j]
sinefreq=freq_shift[j]
print("the frequency of the sinewave is "),sinefreq
print("maximum of hhalf is "),maximum
#############WINDOW FUNCTION################
nbart=asize/2
d=hamming(nbart)
print d[:]
#e=abs(fft.fft(d))
#e=array(e)
#maximum=max(e)
#f=e/maximum
#g=log10(f)
#h=20*g
#i=fft.fftshift(h)
#plt.plot(i)
#plt.show()
gerog=a[0:(asize/2)]
gerog=array(gerog)
d=array(d)
mult=d*gerog
mult=array(mult)
mult1=abs(fft.fft(mult))
maxmult1=max(mult1)
mult2=mult1/maxmult1
mult3=log10(mult2)
mult4=20*mult3
mult5=fft.fftshift(mult4)
multsize=mult5.size
freq_shift1=[]
for i in range (gerog.size):
freq_shift1.append(-50000000+i*100000000/nbart)
for i in range (gerog.size):
if freq_shift1[i]==0:
flag=i
fourievector=mult5[flag:multsize]
freqvector=freq_shift1[flag:multsize] #the distance between two elements of freqvector is 2fs/M and not fs/M. -(THE SAME HAPPENS FOR freq_shift1[])
###
#helpvect=mult2[flag:multsize]
################################################ CALCULATION OF THE DISTORTION WHEN THERE IS ALSO PROCESS GAIN SO THE HARMONICS ARE A LITTLE SPREAD ############################################################################
############################################################# THE ARRAYS THAT ARE GOING TO BE USED ARE: fourievector and freqvector ############################################################################################
position=[]
distortion=[]
for i in range (fourievector.size):
#I TRY TO FIND THE POSITION OF THE HARMONICS BY SEARCHING IN freq_shift VECTOR FOR FREQUENCIES CLOSE TO THE:f=n*sinefreq #
if (freqvector[i]%sinefreq==0) :#if you find a harmonic
if x%2==0 :
magnitude=[]
helpfreq=[]
if i>=2**(((x-2)/2)-1) and i<=fourievector.size-2**(((x-2)/2)-1)-1:
for j in range (int(-2**(((x-2)/2)-1)),int(2**(((x-2)/2)-1))):
magnitude.append(fourievector[i+j])
helpfreq.append(freqvector[i+j])
elif i<2**(((x-2)/2)-1):
for j in range (0,int(i+2**(((x-2)/2)-1))):
magnitude.append(fourievector[j])
helpfreq.append(freqvector[j])
else:
for j in range (int(i-2**(((x-2)/2)-1)),fourievector.size):
magnitude.append(fourievector[j])
helpfreq.append(freqvector[j])
magnitude=array(magnitude)
helpfreq=array(helpfreq)
maxmag=max(magnitude)
sizemag=magnitude.size
for w in range(sizemag):
if magnitude[w]>=maxmag:
maxmag=magnitude[w]
position.append(helpfreq[w])
distortion.append(magnitude[w])
else:
magnitude=[]
helpfreq=[]
for j in range(int(-2**(((x-3)/2)-1)),int(2**(((x-3)/2)-1))):
magnitude.append(fourievector[i+j])
helpfreq.append(freqvector[i+j])
magnitude=array(magnitude)
helpfreq=array(helpfreq)
maxmag=max(magnitude)
sizemag=magnitude.size
for w in range(sizemag):
if magnitude[w]>=maxmag:
maxmag=magnitude[w]
position.append(helpfreq[w])
distortion.append(magnitude[w])
############################################# I ASSUMED THAT WHEN THERE IS A SIGNAL LEACAGE,ONLY THE HIGHEST PEAK IS A HARMONIC AND THE REST OF THE PEAKS ARE NOTHING,NOT EVEN NOISE ###################
distortion=array(distortion)
distsize=distortion.size
position=array(position)
meandistortion=mean(distortion)
realdistortion=[]
realposition=[]
noise1=[]
for i in range(distsize):
if (distortion[i]>=meandistortion) and (position[i]>sinefreq):
realdistortion.append(distortion[i])
realposition.append(position[i])
#*********************************************************** C A L C U L A T I O N O F N O I S E ==> SNR *********************************************************************************************
for i in range (ser):
if hhalf[i]<meandistortion:
noise1.append(hhalf[i])
noise1=array(noise1)
noisesize=noise1.size
noise2=[]
for i in range(noisesize):
if noise1[i]>-140.0:
noise2.append(noise1[i])
print ("THE NOISE FLOOR IS :"),mean(noise2)
snr=maximum-mean(noise2)-10*log10(asize/2) # there is also the process gain:10*log10(asize/2) #
snr_theoret=6.02*num_of_bits+1.76
print("******************************* SNR ********************************************")
print ("SNR IS (quick and dirty) :" ),snr
print ("SNRtheoretical IS :"),snr_theoret
#*********************************************************************************************************************************************************************************************************
#**************************************************************** C A L C U L A T I O N O F D I S T O R T I O N ==> THD ******************************************************************************
print("******************************* THD*********************************************")
print("the REAL position of distortion is: "),realposition[:]
print("the REAL distortion is: "),realdistortion[:]
realposition=array(realposition)
realdistortion=array(realdistortion)
thd1=[]
for i in range (realdistortion.size):
thd1.append(realdistortion[i]/20.0)
thd1=array(thd1)
thd2=thd1*2
thd2=array(thd2)
thd3=10**(thd2)
thd4=0
for i in range (realdistortion.size-1):
if thd3[i]!=1 :
thd4=thd4+thd3[i]
thd=10*log10(thd4)
print ("THD IS :"),thd
#********************************************************************************************************************************************************************************************************
#*********************************************************************************************** C A L C U L A T I O N O F SINAD *************************************************************************
print("**************************** SINAD ***********************************************")
sinad1=-(snr)/10.0
sinad2=10**(sinad1)
sinad3=-abs(thd)/10.0
sinad4=10**(sinad3)
sinad5=sinad2+sinad4
sinad=-10*log10(sinad5)
print("SINAD IS :"),sinad
#************************************************************************************************************************************************************************************************************
#****************************************************************************************** C A L C U L A T I O N O F ENOB ********************************************************************************
print("**************************** ENOB ************************************************")
factor=20*log10(full_scale_amplitude/real_amplit)
enob=(sinad-1.76+factor)/6.02
print ("ENOB IS :"),enob
#*************************************************************************************************************************************************************************************************************
#******************************************************************************************** PLOTTING *******************************************************************************************************
freqvector=array(freqvector)
fourievector=array(fourievector)
plt.plot(freqvector,fourievector)
plt.title("The FFT of the signal")
plt.xlabel("frequency")
plt.ylabel("dB")
plt.show()
from math import *
from matplotlib import pyplot as plt
from numpy import *
##########MAKING THE HISTOGRAM##################
def histo(bits, samples):
c=[]
d=[]
c,d=histogram(samples,2**bits)
plt.hist(samples,2**bits)
# plt.show()
return c
b=input("give number of bits :")
name=raw_input("give me the name of the file :")
f=open(name)
a=[]
for line in f:
value=int(line)
a.append(value)
#########CALCULATE THE NUMBER OF SAMPLES###########
a=array(a)
mt=a.size
print ("number of samples is"),mt
###################################################
e=[]
e=histo(b,a)
print ("the number of occurencies per bin are "),e[:]
h_first=e[0]
last=int(2**b-1)
h_last=e[last]
#########CALCULATING THE ESTIMATED AMPLITUDE########
fsr=input("give me fsr: ")
g=(math.pi/2)
help1=float(mt+h_first+h_last)
help=(mt/help1)
w=(g*(help))
k=float(math.sin(w))
amp=float(fsr/k)
####################################################
##########CALCULATING THE p(n)######################
p=[]
print("the amplitude is "),amp
for i in range(2**b):
twobits = 2**b
twobits1 = 2**(b-1)
pirecip = 1/math.pi
p.append(pirecip * (math.asin(fsr*(i -twobits1)/(amp*twobits))
-math.asin(fsr*(i-1-twobits1)/(amp*twobits))))
print p[:]
####################################################
h=p*mt
############CALCULATING DNL ERROR###################
dnl=[]
for i in range (2**b):
dnl.append(e[i]/h[i]-1)
print ("T H E DNL E R R O R S A R E :"),dnl[:]
####################################################
from math import *
from matplotlib import pyplot as plt
from numpy import *
##########MAKING THE HISTOGRAM##################
def histo(bits, samples):
c=[]
d=[]
c,d=histogram(samples,2**bits)
plt.hist(samples,2**bits)
# plt.show()
return c
b=input("give number of bits :")
name=raw_input("give me the name of the file :")
f=open(name)
a=[]
for line in f:
value=int(line)
a.append(value)
#########CALCULATE THE NUMBER OF SAMPLES###########
a=array(a)
mt=a.size
print ("number of samples is"),mt
###################################################
e=[]
e=histo(b,a)
print ("the number of occurencies per bin are "),e[:]
h_first=e[0]
last=int(2**b-1)
h_last=e[last]
#########CALCULATING THE ESTIMATED AMPLITUDE########
fsr=input("give me fsr: ")
g=(math.pi/2)
help1=float(mt+h_first+h_last)
help=(mt/help1)
w=(g*(help))
k=float(math.sin(w))
amp=float(fsr/k)
####################################################
##########CALCULATING THE p(n)######################
p=[]
print("the amplitude is "),amp
for i in range(2**b):
twobits = 2**b
twobits1 = 2**(b-1)
pirecip = 1/math.pi
p.append(pirecip * (math.asin(fsr*(i -twobits1)/(amp*twobits))
-math.asin(fsr*(i-1-twobits1)/(amp*twobits))))
print p[:]
####################################################
h=p*mt
inl=0
############CALCULATING DNL ERROR###################
dnl=[]
for i in range (2**b):
dnl.append(e[i]/h[i]-1)
inl=inl+dnl[i]
print ("T H E DNL E R R O R S A R E :"),dnl[:]
####################################################
print ("T H E INL E R R O R S A R E :"),inl
from math import *
from matplotlib import pyplot as plt
from numpy import *
def histo(bits, samples):
#making the histogram of the digital samples of a sinewave#
c=[]
d=[]
c,d=histogram(samples,2**bits)
plt.hist(samples,2**bits)
plt.show()
return c
b=input("give number of bits :")
name=raw_input("give me the name of the file :")
f=file(name)
a=[]
for line in f:
value=int(line)
a.append(value)
e=[]
e=histo(b,a)
print e[:]
from numpy import*
from math import *
import matplotlib.pyplot as plt
##############################MAKING THE SINEWAVE######################
def argument(n,sampl_rate,fr,ph):
#produce a vector of the arguments of the sinewave#
arg=[]
for i in range(n):
arg.append(2*pi*fr*i/sampl_rate+ph*2*pi)
return arg
def make_sinewave (n,v,amp):
#produces the values if the sinewave#
b=[]
for i in range (n):
b.append(amp*sin(v[i]))
return b
###############################MAKING THE TRANSFER FUNCTION###########
def adc_transfer(v, fsr, bits,num_of_samples):
#MAKING THE TRANSFER FUNCTION
c=2**bits
a=float(2**bits)
lsb=(fsr/a)
print("the LSB is"),lsb
digital=[]
analog=[]
for i in range (c):
b=lsb*i
analog.append(b)
digital.append(i)
#PLOTTING THE TRANSFER FUNCTION
#pl.plot(analog, digital)
#pl.show()
#AND NOW THE CORRESPONDANCE!!!
vdig=[]
for j in range(num_of_samples):
i=0
if (v[j]>=analog[0]) and (v[j]<=analog[int(math.pow(2,bits)-1)]): #if the value given is in tha full scale range#
while i<c:
if (v[j]>=analog[i]) and (v[j]<analog[i+1]):
flag=digital[i]
vdig.append(flag)
i=i+1
elif (v[j]>analog[int(math.pow(2,bits)-1)]) :
vdig.append(digital[int(math.pow(2,bits)-1)])
else:
vdig.append(digital[0])
return vdig[:]
a=input("please give amplitude:")
f=input("please give the sinewave's frequency:")
p=input("please give phase:")
sr=input("please give sampling rate:")
s=input("please give number of samples:")
vector=argument(s,sr,f,p)
print ("these are the values of the sinewave")
sine=make_sinewave(s,vector,a)
print sine[:]
############################################################################
full_scale_range=input("GIVE ME THE FULL SCALE RANGE: ")
num_of_bits=input("GIVE ME THE NUMBER OF BITS: ")
digital_value=adc_transfer(sine,full_scale_range,num_of_bits,s)
print ("THE DIGITAL VALUE CORRESPONDING TO THE ANALOG IS :"),digital_value[:]
####################DNL AND INL ERRORS####################################
##########MAKING THE HISTOGRAM##################
def histo(bits, samples):
c=[]
d=[]
c,d=histogram(samples,2**bits)
plt.hist(samples,2**bits)
# plt.show()
return c
b=num_of_bits
a=[]
for i in range (s):
value=digital_value[i]
a.append(value)
#########CALCULATE THE NUMBER OF SAMPLES###########
a=array(a)
mt=s
###################################################
e=[]
e=histo(b,a)
print ("the number of occurencies per bin are "),e[:]
h_first=e[0]
last=int(2**b-1)
h_last=e[last]
#########CALCULATING THE ESTIMATED AMPLITUDE########
fsr=full_scale_range
g=(math.pi/2)
help1=float(mt+h_first+h_last)
help=(mt/help1)
w=(g*(help))
k=float(math.sin(w))
amp=float(fsr/k)
####################################################
##########CALCULATING THE p(n)######################
p=[]
print("the amplitude is "),amp
for i in range(2**b):
twobits = 2**b
twobits1 = 2**(b-1)
pirecip = 1/math.pi
p.append(pirecip * (math.asin(fsr*(i -twobits1)/(amp*twobits))
-math.asin(fsr*(i-1-twobits1)/(amp*twobits))))
print p[:]
####################################################
h=p*mt
inl=0
############CALCULATING DNL ERROR###################
dnl=[]
for i in range (2**b):
dnl.append(e[i]/h[i]-1)
inl=inl+dnl[i]
print ("T H E DNL E R R O R S A R E :"),dnl[:]
####################################################
print ("T H E INL E R R O R S A R E :"),inl
from math import *
from numpy import *
from matplotlib import pyplot as plt
##################### OPEN THE FILE ###########################################################
num_of_bits=input("GIVE ME THE NUMBER OF BITS :")
real_amplit=input("GIVE ME THE INPUT AMPLITUDE :") #(I NEED both real_amplit AND full_scale_amplitud
full_scale_amplitude=input("GIVE ME THE FULLSCALE AMPLITUDE :") # IN ORDER TO MEASURE ENOB )
name=raw_input("GIVE ME THE NAME OF THE FILE:")
f=open(name)
a=[]
for line in f:
value=int(line)
a.append(value)
#################### MAKING THE FFT ###########################################################
a=a-mean(a)
a=array(a)
asize=a.size
x=log2(asize)
print ("NUMBER OF SAMPLES ARE(-SIZE OF a IS) : "),asize
print ("THE SIZE OF a EQUALS TO: 2 TO THE "),x
b=abs(fft.fft(a))
maxb=max(b)
d=b/maxb
e=log10(d)
f=20*e
h=fft.fftshift(f)
m=h.size
################ I 'LL MAKE A COUPLE:VECTOR h AND VECTOR freq #################################
freq=[]
for i in range (m):
freq.append(-50000000+i*100000000/m)
######## I'LL MAKE ANOTHER COUPLE:Vector hhalfh AND VECTOR freq_shift #########################
hhalf=[]
freq_shift=[]
for i in range(m/2,m):
freq_shift.append(freq[i])
hhalf.append(h[i])
hhalf=array(hhalf)
freq_shift=array(freq_shift)
ser=hhalf.size
print ("SIZE OF hhalf IS:"),ser
########## SEARCH FOR THE POSITION OF THE MAXMUM OF hhalf=>EXPECTING THE INPUT FREQUENCY #########
for i in range (ser):
maximum=hhalf[i]
for j in range (ser):
if hhalf[j]>maximum:
maximum=hhalf[j]
sinefreq=freq_shift[j]
print("the frequency of the sinewave is "),sinefreq
print("maximum of hhalf is "),maximum
#############WINDOW FUNCTION################
nbart=asize/2
d=hamming(nbart)
print d[:]
#e=abs(fft.fft(d))
#e=array(e)
#maximum=max(e)
#f=e/maximum
#g=log10(f)
#h=20*g
#i=fft.fftshift(h)
#plt.plot(i)
#plt.show()
gerog=a[0:(asize/2)]
gerog=array(gerog)
d=array(d)
mult=d*gerog
mult=array(mult)
mult1=abs(fft.fft(mult))
maxmult1=max(mult1)
mult2=mult1/maxmult1
mult3=log10(mult2)
mult4=20*mult3
mult5=fft.fftshift(mult4)
multsize=mult5.size
freq_shift1=[]
for i in range (gerog.size):
freq_shift1.append(-50000000+i*100000000/nbart)
for i in range (gerog.size):
if freq_shift1[i]==0:
flag=i
fourievector=mult5[flag:multsize]
freqvector=freq_shift1[flag:multsize] #the distance between two elements of freqvector is 2fs/M and not fs/M. -(THE SAME HAPPENS FOR freq_shift1[])
###
#helpvect=mult2[flag:multsize]
################################################ CALCULATION OF THE DISTORTION WHEN THERE IS ALSO PROCESS GAIN SO THE HARMONICS ARE A LITTLE SPREAD ############################################################################
############################################################# THE ARRAYS THAT ARE GOING TO BE USED ARE: fourievector and freqvector ############################################################################################
position=[]
distortion=[]
for i in range (fourievector.size):
#I TRY TO FIND THE POSITION OF THE HARMONICS BY SEARCHING IN freq_shift VECTOR FOR FREQUENCIES CLOSE TO THE:f=n*sinefreq #
if (freqvector[i]%sinefreq==0) :#if you find a harmonic
if x%2==0 :
magnitude=[]
helpfreq=[]
if i>=2**(((x-2)/2)-1) and i<=fourievector.size-2**(((x-2)/2)-1)-1:
for j in range (int(-2**(((x-2)/2)-1)),int(2**(((x-2)/2)-1))):
magnitude.append(fourievector[i+j])
helpfreq.append(freqvector[i+j])
elif i<2**(((x-2)/2)-1):
for j in range (0,int(i+2**(((x-2)/2)-1))):
magnitude.append(fourievector[j])
helpfreq.append(freqvector[j])
else:
for j in range (int(i-2**(((x-2)/2)-1)),fourievector.size):
magnitude.append(fourievector[j])
helpfreq.append(freqvector[j])
magnitude=array(magnitude)
helpfreq=array(helpfreq)
maxmag=max(magnitude)
sizemag=magnitude.size
for w in range(sizemag):
if magnitude[w]>=maxmag:
maxmag=magnitude[w]
position.append(helpfreq[w])
distortion.append(magnitude[w])
else:
magnitude=[]
helpfreq=[]
for j in range(int(-2**(((x-3)/2)-1)),int(2**(((x-3)/2)-1))):
magnitude.append(fourievector[i+j])
helpfreq.append(freqvector[i+j])
magnitude=array(magnitude)
helpfreq=array(helpfreq)
maxmag=max(magnitude)
sizemag=magnitude.size
for w in range(sizemag):
if magnitude[w]>=maxmag:
maxmag=magnitude[w]
position.append(helpfreq[w])
distortion.append(magnitude[w])
############################################# I ASSUMED THAT WHEN THERE IS A SIGNAL LEACAGE,ONLY THE HIGHEST PEAK IS A HARMONIC AND THE REST OF THE PEAKS ARE NOTHING,NOT EVEN NOISE ###################
distortion=array(distortion)
distsize=distortion.size
position=array(position)
meandistortion=mean(distortion)
realdistortion=[]
realposition=[]
noise1=[]
for i in range(distsize):
if (distortion[i]>=meandistortion) and (position[i]>sinefreq):
realdistortion.append(distortion[i])
realposition.append(position[i])
#*********************************************************** C A L C U L A T I O N O F N O I S E ==> SNR *********************************************************************************************
for i in range (ser):
if hhalf[i]<meandistortion:
noise1.append(hhalf[i])
noise1=array(noise1)
noisesize=noise1.size
noise2=[]
for i in range(noisesize):
if noise1[i]>-140.0:
noise2.append(noise1[i])
print ("THE NOISE FLOOR IS :"),mean(noise2)
snr=maximum-mean(noise2)-10*log10(asize/2) # there is also the process gain:10*log10(asize/2) #
snr_theoret=6.02*num_of_bits+1.76
print("******************************* SNR ********************************************")
print ("SNR IS (quick and dirty) :" ),snr
print ("SNRtheoretical IS :"),snr_theoret
#*********************************************************************************************************************************************************************************************************
#**************************************************************** C A L C U L A T I O N O F D I S T O R T I O N ==> THD ******************************************************************************
print("******************************* THD*********************************************")
print("the REAL position of distortion is: "),realposition[:]
print("the REAL distortion is: "),realdistortion[:]
realposition=array(realposition)
realdistortion=array(realdistortion)
thd1=[]
for i in range (realdistortion.size):
thd1.append(realdistortion[i]/20.0)
thd1=array(thd1)
thd2=thd1*2
thd2=array(thd2)
thd3=10**(thd2)
thd4=0
for i in range (realdistortion.size-1):
if thd3[i]!=1 :
thd4=thd4+thd3[i]
thd=10*log10(thd4)
print ("THD IS :"),thd
#********************************************************************************************************************************************************************************************************
#*********************************************************************************************** C A L C U L A T I O N O F SINAD *************************************************************************
print("**************************** SINAD ***********************************************")
sinad1=-(snr)/10.0
sinad2=10**(sinad1)
sinad3=-abs(thd)/10.0
sinad4=10**(sinad3)
sinad5=sinad2+sinad4
sinad=-10*log10(sinad5)
print("SINAD IS :"),sinad
#************************************************************************************************************************************************************************************************************
#****************************************************************************************** C A L C U L A T I O N O F ENOB ********************************************************************************
print("**************************** ENOB ************************************************")
factor=20*log10(full_scale_amplitude/real_amplit)
enob=(sinad-1.76+factor)/6.02
print ("ENOB IS :"),enob
#*************************************************************************************************************************************************************************************************************
#******************************************************************************************** PLOTTING *******************************************************************************************************
freqvector=array(freqvector)
fourievector=array(fourievector)
plt.plot(freqvector,fourievector)
plt.title("The FFT of the signal")
plt.xlabel("frequency")
plt.ylabel("dB")
#plt.plot(a[0:500])
plt.show()
from math import *
from matplotlib import pyplot as plt
from numpy import *
#################################################################### CREATING THE DISTORTED SINEWAVE #################################################
def make_sinewaveforunipolar (n,sampl_rate,fr,ph,amp,fullsr):
#produces the values of the sinewave#
b=[]
maj=[]
for i in range (n):
b.append(fullsr/2.0+amp*sin(2.0*pi*fr*i/sampl_rate+ph*2.0*pi))
#b.append(fullsr/2+amp*sin(2*pi*fr*i/sampl_rate+ph*2*pi)+0.02*amp*sin(4.44*pi*fr*i/sampl_rate+ph*2*pi)+0.09*amp*sin(6.66*pi*fr*i/sampl_rate+ph*2*pi))
return b
def make_sinewaveforbipolar (n,sampl_rate,fr,ph,amp,fullsr):
#produces the values of the sinewave#
b=[]
for i in range (n):
b.append(amp*sin(2.0*pi*fr*i/sampl_rate+ph*2.0*pi))
#b.append(amp*sin(2.0*pi*fr*i/sampl_rate+ph*2.0*pi)+0.02*amp*sin(4.44*pi*fr*i/sampl_rate+ph*2*pi)+0.09*amp*sin(6.66*pi*fr*i/sampl_rate+ph*2*pi))
return b
#################################################################### ANALOG TO DIGITAL ################################################################
def adc_transferunipolar(v, fsr, bits):
#MAKING THE TRANSFER FUNCTION FOR UNIPOLAR ADC
a=2.0**bits
lsb=float(fsr/a)
digital=[]
analog=[]
for i in range (int(a)):
b=lsb*i
analog.append(b)
digital.append(i)
#PLOTTING THE TRANSFER FUNCTION
#pl.plot(analog, digital)
#pl.show()
#AND NOW THE CORRESPONDENCE!!!
i=0
if (v>=analog[0]) and (v<=analog[int(math.pow(2,bits)-1)]):
while i<=a-1:
if (v>=analog[i]) and (v<analog[i+1]):
d=(digital[i])
i=i+1
elif (v>analog[int(math.pow(2,bits)-1)]) :
d=( digital[int(math.pow(2,bits)-1)])
else:
d=(digital[0])
return d
def adc_transferbipolar(v, fsr, bits,vfs):
#MAKING THE TRANSFER FUNCTION FOR BIPOLAR ADC
a=2.0**bits
lsb=float(fsr/a)
digital=[]
analog=[]
for i in range (int(a)):
b=-vfs+lsb*i
analog.append(b)
digital.append(i)
#PLOTTING THE TRANSFER FUNCTION
#plt.plot(analog, digital)
#plt.show()
#AND NOW THE CORRESPONDENCE!!!
i=0
if (v>=analog[0]) and (v<=analog[int(math.pow(2,bits)-1)]):
while i<=a-1:
if (v>=analog[i]) and (v<analog[i+1]):
d=(digital[i])
i=i+1
elif (v>analog[int(math.pow(2,bits)-1)]) :
d=( digital[int(math.pow(2,bits)-1)])
else:
d=(digital[0])
return d
#################################################################### MAKING THE HISTOGRAM #############################################################
#def histo(bits, samples):
# c=[]
# d=[]
# c,d=histogram(samples,2**bits)
# plt.hist(samples,2**bits)
# plt.show()
# return c
def histo(bits,samples):
samples=array(samples)
c=[]
for i in range (2**bits):
c.append(0)
i=0
for j in range (2**bits):
for i in range(samples.size):
if (samples[i]==j):
c[j]=c[j]+1
return c
##################################################################### THE INPUTS #######################################################################
#b=input("give number of bits :")
#fsr=input("give me fsr(volts): ")
#a=input("please give amplitude(volts):")
#f=input("please give the sinewave's frequency(Hertz):")
#p=input("please give phase:")
#sr=input("please give sampling rate(Hertz):")
#s=input("please give number of samples:")
#bipolar=input("if the ADC is bipolar then press 1,else press 0 :")
b=8
fsr=10
a=5
f=0.31415164576
p=0
sr=8000
s=100000
bipolar=1
#######################################
fs=fsr/2
samples=[] #making the sinewave's samples
if (bipolar==0):
samples=make_sinewaveforunipolar(s,sr,f,p,a,fsr)
elif (bipolar==1):
samples=make_sinewaveforbipolar(s,sr,f,p,a,fsr)
samples=array(samples)
dig_samples=[] #the convertion
if (bipolar==0):
for i in range (s):
dig_samples.append(adc_transferunipolar(samples[i], fsr, b))
elif (bipolar==1):
for i in range (s):
dig_samples.append(adc_transferbipolar(samples[i], fsr, b, fs))
dig_samples=array(dig_samples)
#to be sure that the frequency of the sinewave and the sampling frequency are not correlated,i need dig_samples to have a lot of different from each other values
e=[]
e=histo(b,dig_samples)
#print ("the number of occurencies per bin are "),e[:]
print (" ")
e=array(e)
ala=0 #this variable shows the number of different values
for i in range (e.size):
if (e[i]!=0):
ala=ala+1
h_first=e[0]
last=e.size
h_last=e[last-1]
#########CALCULATING THE ESTIMATED AMPLITUDE########
lsb=fsr/(2.0**b)
print("the lsb is :"),lsb
print (" ")
g=(math.pi/2)
help1=float(s+h_first+h_last)
help2=float((s/help1))
w=float((g*(help2)))
k=float(math.sin(float(w)))
amp=float(fs/k)
print("the estimated amplitude is "),amp
print (" ")
####################################################
##########CALCULATING THE p(n)######################
p=[]
twobits = 2.0**b
twobits1 = 2.0**(b-1)
pirecip = 1.0/math.pi
for i in range(1,int(twobits)+1):
p.append(pirecip*( math.asin(2*fs*(i -twobits1)/(amp*twobits)) - math.asin(2*fs*(i-1-twobits1)/(amp*twobits)) ))
p=array(p)
print (" ")
##########CALCULATING h(n)theoretical##############
h=[]
for i in range(p.size):
h.append(p[i]*s)
#print(" ")
h=array(h)
#print("the theoretical number of samples is :"),sum(h)
############CALCULATING DNL ERROR###################
dnl=[]
help4=0.0
help5=0.0
#for i in range (1,2**b+-1):
for i in range (0,2**b):
help4=(e[i]/h[i])
help5=help4-1.0
dnl.append(help5)
dnl=array(dnl)
alala=0
for i in range ((2**b)-2):
if (dnl[i]!=-1.0):
alala=alala+1 #this variable calculates how many different values dnl vector has
##print ("T H E DNL E R R O R S A R E :"),dnl[:]
##########CALCULATING INL ERROR#####################
dnl=dnl[1:dnl.size-2]
inl=[]
for i in range (dnl.size):
inl.append(0)
for i in range (dnl.size):
for j in range (i+1):
inl[i]=inl[i]+dnl[j]
print(inl[0])
print (" The maximum deviation of the transfer function is :") ,max(dnl) ,("LSBs.Which means :"), ((max(dnl)/(2**b))*100),("%FSR.")
####################################################
rang=range(dig_samples.size)
plt.plot(rang,samples,rang,dig_samples)
plt.plot(inl)
plt.plot(dig_samples)
plt.show()
from math import *
from matplotlib import pyplot as plt
from numpy import *
name=raw_input("give me the name of the file :")
f=file(name)
a=[]
b=[]
i=0
for line in f:
value=int(line)
a.append(value)
b.append(i)
i=i+1
plt.plot(b,a)
plt.show()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment