2013-05-04 104 views
2

我有一個函數,我應該繪製10MHz和11.4MHz之間的幅度頻譜。這是一個函數:cos(6.72 *(10 ** 7 * t)+ 3.2 * sin(5 *(10 ** 5 * t)))我知道情節應該是什麼樣子,但我的完全錯誤。我很確定這是一個編程錯誤,而不是數學/理論錯誤,因爲我正在做一個MatLab示例正在展示的內容,但是我覺得Python正在做一些我不理解的事情。我應該在相距10ns的情況下對該函數進行8192次採樣,乘以漢明窗口,並在10MHz和11.4MHz之間繪製以dB爲單位的幅度譜。載波(10.7MHz)應該在55dB左右。第二對邊帶是60 dB處的最大幅度。然後,第五對邊帶是20 dB點,因此大約爲40 dB。任何人都可以發現這個代碼錯了,解釋了爲什麼我的不顯示這個?我的fft()在Python中有些問題

import numpy 
import scipy 
import scipy.fftpack 
import matplotlib 
import matplotlib.pyplot as plt 
from scipy import pi 
import pylab 
from pylab import * 
import cmath 
import sys 
sys.setrecursionlimit(10000) 

samples = 8192 

#Defining the test function 
t = scipy.linspace(0.01, 32/390625, samples, False) #Sample samples times at 10ns apart 
#The samples is non-inclusive, it goes from 0 to 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 as a variable in Python's eyes 

signal = acc(t)     #Make signal a function of t 
plt.subplot(211)    #Set up a plot 
plt.xlabel('Ohmega (Hz)')  #Label x axis 
plt.ylabel('Amplitude of sampled x(t) (dB)') #Label y axis 

window = numpy.hamming(samples) #Make a hamming window 


w = scipy.linspace(0, 100*10**6, samples, False) #Create Ohmega between 1/(10ns) 
signal = signal * window  #Multiply by the hamming window 
signal = scipy.fft(signal)  #Take the FFT 
signal = abs(20*log10(signal)) #Get the 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 with stemlines 
plot(w,signal, 'b-') 
plt.xlim(10*10**6, 11.4*10**6) #Set x-limits 

plt.show()      #Show the plot 

感謝您的幫助!

編輯:

現在我的代碼:

import numpy 
import scipy 
import scipy.fftpack 
import matplotlib 
import matplotlib.pyplot as plt 
from scipy import pi 
import pylab 
from pylab import * 
import cmath 
import sys 
sys.setrecursionlimit(10000) 

samples = 8192 

#Defining the test function 
t = scipy.linspace(0.01, 32/390625, samples, False)   #Sample samples times at 10ns apart 
#The samples is non-inclusive, it goes from 0 to 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 as a variable in Python's eyes 

#signal = acc(t)     #Make signal a function of t 
plt.subplot(211)    #Set up a plot 
plt.xlabel('Ohmega (Hz)')  #Label x axis 
plt.ylabel('Amplitude of sampled x(t) (dB)') #Label y axis 

window = numpy.hamming(samples) #Make a 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 by the hamming window 
#acc = scipy.fft(acc)  #Take the FFT 
#acc = abs(20*log10(acc)) #Get the 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 with stemlines 

plot(w,signal, 'b-') 
plt.xlim(10*10**6, 11.4*10**6) #Set x-limits 

plt.show()      #Show the plot 

和錯誤...:

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 
    for line in self._get_lines(*args, **kwargs): 
    File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 322, in _grab_next_args 
    for 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 and y must have same first dimension") 
ValueError: x and y must have same first dimension 

雖然我已經看到了之前修正了這個錯誤,我不知道現在如何解決它。顯然,一個採樣8192次的信號和確定採樣位置的變量是不同的尺寸。我這樣就失去了在這一個...

+0

32/390625 == 0,它是整數除法。寫32.0/390625 – 2013-05-04 20:44:55

回答

1

你的錯誤可能來自你的方式定義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 as a variable in Python's eyes 

當Python執行的第一線,其分配給變量X_T的價值使用當前值t(scipy.linspace(0.01, 32/390625, samples, False))給出公式。它不會將t解釋爲變量。

要通過這種修復這種變化的第二行:

acc = lambda t: cos(6.72*(10**7*t) + 3.2*sin(5*(10**5*t))) 

編輯:只注意到你做了同樣用signal。編寫:signal = acc(t)(即使您固定acc)只會將函數acc(t)(與當前值t)的結果分配給變量信號。它不會是一個功能。 (你爲什麼不用acc來代替信號,因爲acc是t的函數呢?)

+0

這是有道理的,但它仍然無法正常工作。我基本上看到一個20分貝的水平線,如果這有助於任何事情...... – 2013-05-04 15:29:22

+0

請閱讀我的編輯。 (你對信號做的是一樣的,所以信號是一個常數值,而不是一個函數) – halflings 2013-05-04 15:30:00

+0

這給了我所有的錯誤......由於函數和float的非法操作「*」不能乘以窗口,因此不會採用acc:Traceback(最近一次調用最後一個)的fft(): 文件「/ home/hollis/Documents/ELEN 322/ELEN_322_#2.py「,第31行,在 acc = scipy.fft(acc)#Take 文件」/usr/lib/python2.7/dist-packages/numpy/fft/fftpack.py「, line 164,in fft return _raw_fft(a,n,axis,fftpack.cffti,fftpack.cfftf,_fft_cache) 文件「/usr/lib/python2.7/dist-packages/numpy/fft/fftpack.py」, line 48,in _raw_fft n = a.shape [axis] IndexE元組索引超出範圍 – 2013-05-04 15:40:39

0

不知道你是否還在爲此工作。我確實發現了這個帖子:FFT in Matlab and numpy/scipy give different results這可能會有所幫助。

的職位表明,使用內SciPy的轉置作爲Matlab的在矩陣的列應用FFT,numpy的默認

希望幫助應用在過去的軸(行)的FFT。