Asked by William Osborne
on 17 Oct 2019 at 12:33

Hello all,

I have been trying to figure out what each step is in the FFT process as shown below. It is to perform an FFT on a WAV file.

Essentially I want to know why this is how to do it, rather than just taking it for what it is. Step by step.

[x,fs] = audioread(filename);

%% FFT

lpad = length(x);

xdft = fft(x,lpad);

xdft = xdft(1:lpad/2+1);

xdft = xdft/length(x);

xdft(2:end-1) = 2*xdft(2:end-1);

freq = 0:fs/lpad:fs/2;

amplitude= abs(xdft);

Answer by Adam
on 17 Oct 2019 at 13:11

Edited by Adam
on 17 Oct 2019 at 14:27

Accepted Answer

The lpad part is a little suspicious. If you have an odd length of signal it will not work as is.

Apart from that though:

xdft = fft(x,lpad);

The fft line simply runs the fft. Passing in the length of signal as the number of points is superfluous, but if you were using a different number of points (e.g. next power of 2, which is common) then that would be input there.

xdft = xdft(1:lpad/2+1);

fft results in a 2-sided spectrum, with negative frequencies (or equiv nyquist to sample frequency) as well as positive frequencies (if you want to know more about this look for a resource online). Generally we aren't interested in the negative spectrum so we clip that off to give a one-sided spectrum.

fft returns its results as [0, positiveFrequencies, nyquist, negativeFrequencies]. We want to keep the nyquist frequency, hence n/2 + 1 rather than n/2.

xdft = xdft/length(x);

scales the result to maintain the amplitudes of the time domain. I always forget exactly what this step should be, but I am assuming you have it right here. Certainly this is scaling the result, whether correctly or not.

xdft(2:end-1) = 2*xdft(2:end-1);

Because we threw away half the spectrum we multiply the remaining half by 2 to maintain the same total power.

freq = 0:fs/lpad:fs/2;

In order to view the spectrum meaningfully you need to create your vector of frequencies. Obviously it needs to be the same length as the result and runs from 0 to Nyquist (which, if you remember is the range we kept from the spectrum).

amplitude= abs(xdft);

The result of the fft is a complex one. To view the power spectrum you want the absolute value. Often this is also squared, but just taking the abs is also fine, depending what you wish to interpret from it.

William Osborne
on 17 Oct 2019 at 14:16

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.