I don't know if this is a programming or math question, but I have put together some short examples of FFT. I load in a 440hz wave and add some sine waves on top, but for some reason, the spectrum has a 'wave' that I don't understand. The spectrum, as I understand it, should have the same |Y(freq)| value for all frequencies.
from pylab import plot, show, xlabel, ylabel, subplot
from scipy import fft, arange
from numpy import linspace, array
# from scipy.io.wavfile import read,write
import scikits.audiolab as audio
import math
def plotSpectru(y,Fs):
n = len(y) # lungime semnal
k = arange(n)
T = n/Fs
frq = k/T # two sides frequency range
frq = frq[range(n/2)] # one side frequency range
Y = fft(y)/n # fft computing and normalization
Y = Y[range(n/2)]
plot(frq,abs(Y),'r') # plotting the spectrum
xlabel('Freq (Hz)')
ylabel('|Y(freq)|')
Fs = 44100; # sampling rate
# (data, rate, bits) = audio.wavread('440Hz_44100Hz_16bit_05sec.wav')
(data, rate, bits) = audio.wavread('250Hz_44100Hz_16bit_05sec.wav')
for n in xrange(0,4*120, 4):
n=n/40.
data = array([x+math.sin(n*idx) for idx,x in enumerate(data)])
y=data[:]
lungime=len(y)
timp=len(y)/44100.
t=linspace(0,timp,len(y))
subplot(2,1,1)
plot(t,y, color="green")
xlabel('Time')
ylabel('Amplitude')
subplot(2,1,2)
plotSpectru(y,Fs)
show()