[英]Something wrong with my fft() in python
我有一個函數可以繪制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)應約為55 dB。 第二對邊帶是60 dB時的最大幅度。 然后,第五對邊帶為20 dB點,因此約為40 dB。 誰能發現這段解釋我為什么不顯示此代碼的代碼有問題嗎?
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次,並且確定該信號采樣位置的變量是不同的尺寸。 我對這個很迷失...
您的錯誤可能來自定義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執行第一行時,它將使用當前值t( scipy.linspace(0.01, 32/390625, samples, False)
)為變量x_t分配給您公式的值。 它不會將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的函數?)
不知道您是否仍在進行此操作。 我確實找到了這篇文章: Matlab中的FFT和numpy / scipy給出了不同的結果 ,可能會有所啟發 。
該帖子建議在Matlab將fft應用到矩陣的列上,在numpy中將fft應用到最后一個軸(行)上,因為Matlab在Scipy中使用了轉置
希望能有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.