2013-05-19 37 views
1

我想要得到純正弦函數的譜圖。再加上我想展示整個信號fft的情節。我期望峯值處於相同的頻率,因爲它們處理的是相同的信號,這個信號在時間上是固定的。用matplotlib繪製純竇譜圖

代碼

samplingFrequency = 32. 
frequency = 4       #frequency of the sinus wave 
t = arange(0,20,1/samplingFrequency) #time intervals with period 1/sampling frequency 
y = cos(2*pi*frequency*t) 

Y = fft.fft(y)       #standard fft on the whole signal 
frequencyAxis = fft.fftfreq(len(Y),1/samplingFrequency)   #adjusting the x axis 

#PLOTTING 
fig, (ax1,ax2) = plt.subplots(nrows=2, ncols=1) 
ax1.specgram(y, Fs = samplingFrequency) 
ax2.stem(frequencyAxis,Y,linefmt='r--', markerfmt='ro') 

情節 enter image description here

如預期與4的峯值在整個信號的FFT然而頻譜圖繪製的線12上觀上,其中錯誤是?

更新 使用以下版本中:

  • matplotlib '1.1.1'
  • numpy的 '1.6.2'
  • 蟒蛇2.7.3
+2

我不能複製這個結果(Numpy 1.7.1&matplotlib 1.2.1),因爲譜圖對我來說是4。順便說一句,你的腳本中有一個錯字; 'freqency'代替,如果'frequency'在第4行。 – hooy

+3

在Python 2.7.4中,numpy 1.7.1,matplotlib 1.2.1,它可以正常工作 –

+0

它在更新後工作。如果你要在這裏形成你所說的答案,我會接受它並將問題標記爲已解決。 – TheMeaningfulEngineer

回答

2

關於Python 2.7.5與numpy 1.7.1和matplotlib 1.2.1你的代碼完全按照預期工作。嘗試將您的numpy和matplotlib安裝更新到最新版本。