0
votes

I have generated the following time signal: enter image description here

Now I want to perform a Discrete Fourier Transform by using the matlab command fft

Here is my code:

function [ xdft, omega ] = FastFourier( t, fs )

%% Inputs from other functions %%
[P_mean, x, u] = MyWay( t )   %From here comes my signal x(t)

%% FFT %%
xdft1 = fft(x);                  % Perform FFT
xdft2 = abs(xdft1);              % Take the absolute value of the fft results
xdft = xdft2(1:length(x)/2+1);   % FFT is symmetric, second half is not needed
freq = 0:fs/length(x):fs/2;      % frequency axis

plot (freq(1:100),xdft(1:100));

end

And here is the plot that I get: enter image description here

And what is puzzling to me is the y axis? Shouldn't the y axis represent the amplitudes of the frequency components? Is there a way to get the amplitudes of all the frequency components?

Thanks!

EDIT: I have found that some people do the following:

n = size(x,2)/2;      %The number of components and second half can be neglected again
xdft2 = abs(xdft1)/n;   

This way I seem to get the amplitude spectrum, but why do I have to divide the absolute value by n?

3
Additional answer may be found at the related question (dsp.stackexchange.com/questions/14636/…)Lutz Lehmann

3 Answers

2
votes

FFT gives you a complex pair in each Frequency Bin. The first bin in the FFT is like the DC part of your signal (around 0 Hz), the second bin is Fs / N, where Fs is the sample rate and Nis the windowsize of the FFT, next bin is 2 * Fs / N and so on. What you calc with the abs() of such a pair is the power contained in a bin.

you might also want to check this out: Understanding Matlab FFT example

1
votes

Most (not all) FFT libraries preserve total energy (Parseval's theorem), which means that the magnitude has to get bigger for longer FFT windows (longer stationary waveform -> more energy). So you have to divide the result by N to get a more "natural" looking magnitude height of sinewaves in the spectrum.

0
votes

If you want the amplitudes of the harmonics, then you need to plot real(xdft1) and imag(xdft1). Real(xdft1) gives you coefficients of all the cosine harmonics present in your signal, from -Fs/2 to +Fs/2, (we assume your Fs is large enough to cover all frequencies in the signal) and the imag(xdft) give the amplitudes of the sines. What you are doing is giving you the magnitude of the signal, which is the RMS value of the total energy at a bin in both the real and imaginary frequency component. Its often the item of most interest to people looking at a spectrum.

Basics of this: (https://www.youtube.com/watch?v=ZKNzMyS9Z6s&t=1629s)