A cosine wave is made of one frequency (actually, two frequencies if you include the negative frequency). Let's generate a cosine wave and calculate its frequency using fft(), as shown in Listing 8-5.
Listing 8-5. Fourier Transform of a Cosine Wave
F=25 #a wave at 25 Hz t = arange(N)/float(N) # sampled over 1 second x = cos(2*pi*t*F) # the signal subplot(2, l)
title('A cosine wave')
title('Fourier transform of a cosine wave')
I first defined a few parameters: N is the number of points in the signal, and f is the frequency of the cosine wave. I then created a time vector, t, which is made of evenly spaced samples between 0 and 1, representing 1 second. I then calculated the sampled cosine wave and plotted it along with its Fourier transform. I've chosen to plot the absolute of the transformed signal, seeing as Fourier transforms return complex values (albeit in this case those complex values are zero). Figure 8-12 shows the results.
■ Note There's a frequency content at 25 Hz (the left spike), but there's also another one at 487 Hz. That's really the value corresponding to -25 Hz, that is, 512 - 25. If you'd like to view the frequency domain centered around 0 Hz, use the function fftshift().
Was this article helpful?