Question

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()

Wired sine wave through spectrum

Was it helpful?

Solution

I do not fully understand how you want to calculate your sine wave? Here is a small example how to add a sine wave with numpy.

duration = len(data)/float(rate)
t = np.linspace(0, len(data), rate*duration)
sinewave = np.sin(2*np.pi*440*t)

data += sinewave

EDIT

Sorry, I rered your question and recognized my answer did't matched your question. Even of you really add all positive frequencies (that are analyzed by the fft), you get no uniform |Y(freq)|.

duration = len(data)/float(rate)
t = np.linspace(0, len(data), rate*duration)
allfreqs = np.fft.fftfreq(len(data), 1.0/rate)

for f in allfreqs[:len(allfreqs)/2]:
    data += np.sin(2*np.pi*f*t)

enter image description here

As far as I understand it's because of interference. If you add so many sine waves, it's likely, that some become weaker, some become stronger.

If you assign a random phase to each wave, things look different:

duration = len(data)/float(rate)
t = np.linspace(0, len(data), rate*duration)
allfreqs = np.fft.fftfreq(len(data), 1.0/rate)

for f in allfreqs[:len(allfreqs)/2]:
    data += np.sin(2*np.pi*f*t + np.random.rand()*np.pi)

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top