[英]x and y must have same first dimension ERROR in python
我是python的新手,正在尝试绘制具有不同频率的正弦波的FFT。 我得到了正弦波图,但发现很难绘制信号的FFT。
A = 2.0
fs = 5000
nsample = 200
T=1.0/fs
ff = np.array([200, 600, 1000]) # different freq under test
#Generating the test sinusoidals
x = np.arange(0,nsample)
normalisedFreq = 2*np.pi*ff/fs
# create the three sinusoids and generating waveform
y = np.zeros(nsample)
for w in normalisedFreq :
y=y+A*np.sin(w*x)
# generating Fast Fourier Transform
n_fft=len(y)
magnitude = 2*np.abs(scipy.fftpack.fft(y,n_fft))/n_fft
cutoff = m.ceil((n_fft+1)/2) #nyquist frequency
freq=np.array((0,cutoff-1)*(fs/n_fft)) #frequency vector
plt.subplot (211)
plt.plot(x,y)
plt.subplot(212)
plt.plot(freq,magnitude)
plt.show()
我收到以下错误:
ValueError: x and y must have same first dimension
当我尝试绘制频率和幅值时会发生此错误。
请帮助,谢谢
我不确定如何解决它,但似乎错误表明频率长度和幅度长度不同。
我认为您要么需要创建与幅度相同长度的频率,要么截断幅度数组以匹配频率
编译len(magnitude)
和len(freg)
可能是调试问题的合理第一步。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.