numpy - Something wrong with my fft() in python -
i have function supposed plot amplitude spectrum of between 10mhz , 11.4mhz. function: cos(6.72*(10**7*t) + 3.2*sin(5*(10**5*t))) know plot supposed like, mine wrong. i'm pretty sure it's programming error, not math/theory error because i'm doing matlab example showing, feel python doing i'm not understanding. i'm supposed sample function 8192 times @ 10ns apart, multiply hamming window, , plot amplitude spectrum in db between 10mhz , 11.4mhz. carrier (10.7mhz) should around 55 db. second pair of sidebands greatest amplitude @ 60 db. then, fifth pair of sidebands 20 db point, around 40 db. can spot wrong code explains why mine doesn't show this?
import numpy import scipy import scipy.fftpack import matplotlib import matplotlib.pyplot plt scipy import pi import pylab pylab import * import cmath import sys sys.setrecursionlimit(10000) samples = 8192 #defining test function t = scipy.linspace(0.01, 32/390625, samples, false) #sample samples times @ 10ns apart #the samples non-inclusive, goes 0 samples-1. x_t = cos(6.72*(10**7*t) + 3.2*sin(5*(10**5*t))) #define x(t) acc = lambda t: (x_t) #define x[t] in terms of t variable in python's eyes signal = acc(t) #make signal function of t plt.subplot(211) #set plot plt.xlabel('ohmega (hz)') #label x axis plt.ylabel('amplitude of sampled x(t) (db)') #label y axis window = numpy.hamming(samples) #make hamming window w = scipy.linspace(0, 100*10**6, samples, false) #create ohmega between 1/(10ns) signal = signal * window #multiply hamming window signal = scipy.fft(signal) #take fft signal = abs(20*log10(signal)) #get magnitude in db scale plt.xlabel('ohmega') #label x axis plt.ylabel('|f[w]|') #label y axis #marker, stemlines, baseline = stem(w,signal, 'b-..') #plot stemlines plot(w,signal, 'b-') plt.xlim(10*10**6, 11.4*10**6) #set x-limits plt.show() #show plot
thanks help!
edit:
my code now:
import numpy import scipy import scipy.fftpack import matplotlib import matplotlib.pyplot plt scipy import pi import pylab pylab import * import cmath import sys sys.setrecursionlimit(10000) samples = 8192 #defining test function t = scipy.linspace(0.01, 32/390625, samples, false) #sample samples times @ 10ns apart #the samples non-inclusive, goes 0 samples-1. x_t = cos(6.72*(10**7*t) + 3.2*sin(5*(10**5*t))) #define x(t) acc = lambda t: cos(6.72*(10**7*t) + 3.2*sin(5*(10**5*t))) #define x[t] in terms of t variable in python's eyes #signal = acc(t) #make signal function of t plt.subplot(211) #set plot plt.xlabel('ohmega (hz)') #label x axis plt.ylabel('amplitude of sampled x(t) (db)') #label y axis window = numpy.hamming(samples) #make hamming window w = scipy.linspace(0.01, 100*10**6, samples, false) #create ohmega between 1/(10ns) signal = lambda t: abs(20*log10(scipy.fft(acc*window))) #acc = acc * window #multiply hamming window #acc = scipy.fft(acc) #take fft #acc = abs(20*log10(acc)) #get magnitude in db scale plt.xlabel('ohmega') #label x axis plt.ylabel('|f[w]|') #label y axis #marker, stemlines, baseline = stem(w,signal, 'b-..') #plot stemlines plot(w,signal, 'b-') plt.xlim(10*10**6, 11.4*10**6) #set x-limits plt.show() #show plot
and error...:
traceback (most recent call last): file "/home/hollis/documents/elen 322/elen_322_#2.py", line 39, in <module> plot(w,signal, 'b-') file "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 2467, in plot ret = ax.plot(*args, **kwargs) file "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 3893, in plot line in self._get_lines(*args, **kwargs): file "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 322, in _grab_next_args seg in self._plot_args(remaining, kwargs): file "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 300, in _plot_args x, y = self._xy_from_xy(x, y) file "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 240, in _xy_from_xy raise valueerror("x , y must have same first dimension") valueerror: x , y must have same first dimension
while i've seen , fixed error before, have no idea how fix now. apparently signal sampled 8192 times , variable determining sampled different dimensions. i'm lost on one...
your error might come way define x(t)
:
x_t = cos(6.72*(10**7*t) + 3.2*sin(5*(10**5*t))) #define x(t) acc = lambda t: (x_t) #define x[t] in terms of t variable in python's eyes
when python executes first line, assigns variable x_t value of formula you're given using current value of t ( scipy.linspace(0.01, 32/390625, samples, false)
). doesn't interpret t
variable.
to fix change second line :
acc = lambda t: cos(6.72*(10**7*t) + 3.2*sin(5*(10**5*t)))
edit : noticed did same signal
. writing : signal = acc(t)
(even if fixed acc
) assign result of function acc(t)
(with current value of t) variable signal. won't function. (why don't use acc instead of signal acc function of t anyway ?)
Comments
Post a Comment