Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

This is a question about an example for the software Matlab, but I still chose to ask it here, since I suspect that the question is more about the math involved than the software itself.

I want to use Matlab's Fast Fourier Transform (FFT) algorithm to find out how the frequencies are distributed in a soundsample. However, there is one thing that is bugging me in the example Mathworks has on their website over here. It's probably really obvious, but I just can't seem to understand why they are doing this one thing.

In the example they first create a sample of two sinusoidal waves together with simple white noise. Then they try to extract to frequencies of these two waves from the sample with the help of the FFT-algorithm:

1    NFFT = 2^nextpow2(L);     % Next power of 2 from length of y
2    Y = fft(y,NFFT)/L;
3    f = Fs/2*linspace(0,1,NFFT/2+1);
4        % Plot single-sided amplitude spectrum.
5    plot(f,2*abs(Y(1:NFFT/2+1)))
6    title('Single-Sided Amplitude Spectrum of y(t)')
7    xlabel('Frequency (Hz)')
8    ylabel('|Y(f)|')

Now, here is what I don't understand. On line 2 they divide the the transform by L, the amount of samples used, to get Cn, the amplitude of the wave e^inx, right? Then why do they need to multiply this amplitude by 2 on line 5 before they can get the original amplitudes of the two waves.

share|improve this question

1 Answer 1

I think it is a normalization. They halved the number of sample points they used, so they doubled the amplitude to keep the "integral" normalized.

share|improve this answer
    
I don't know, it never had to do with any integral or sum of predefined value. I just think they didn't want to show the whole spectrum since there wouldn't be much of value after 500 Hz - the peaks at the waves might even disappear because of the resolution. If they hadn't halved the number of samples they still would've needed to multiply the vector by 2 to get the same result amplitudes as now. –  Laph Feb 10 '13 at 16:54
    
@Laph: why would they have had to do that? The plot would have looked the same regardless, save for the ticks on the ordinate. You are right: the spectrum has little value after $512$ Hz because they zero-padded to get the power of two they supposedly needed (although I thought this was no longer needed after the FFTW algorithm gained prominence). –  Ron Gordon Feb 10 '13 at 17:02
    
Well, if I've understood the whole thing correctly, after the FFT and division by L they have a vector Y showing the spectum of frequencies and corresponding amplitudes from 0 to 1024 Hz. They only choose to plot the first half of that vector, but exluding the second half doesn't change the values of the first half. So, in order to have the peaks as high as now, would they have chosen to plot the whole Y, they'd still need those values in Y multiplied by 2. I'm not familiar with the FFTW algorithm, and Matlab seems to have a function for that one as well. I'll have a look at what it does. –  Laph Feb 10 '13 at 17:39
    
I had Matlab do as they did, but instead NOT halve the samples plotted and NOT multiply by 2. The end result was a diagram where the peaks were about half the height in their example, as i suspected. However the second half, the missing half in their example, was the first half mirrored. This is something i don't really understand. Maybe the algorithm behaves like this because of optimization reasons, since the values over N/2 never really should be used to avoid aliasing? Maybe not so likely.. –  Laph Feb 10 '13 at 17:46
    
FFTW performs a FFT for datasets of arbitrary size without zero padding. Details here. fftw.org/faq/section1.html#whatisfftw –  Ron Gordon Feb 10 '13 at 17:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.