FFT example Syntax what does it means P1(2:end-1) = 2*P1(2:end-1); ?

53 vues (au cours des 30 derniers jours)
Andrea Ciufo
Andrea Ciufo le 23 Fév 2017
Commenté : John D'Errico le 2 Mar 2022
In the Matlab example for the FFT they describe how to plot the FFT.
"Compute the Fourier transform of the signal.
Y = fft(X);
Compute the two-sided spectrum P2. Then compute the single-sided spectrum P1 based on P2 and the even-valued signal length L.
P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);
Why P1(2:end-1)= 2*P1(2:end-1)? What does it means (2:end-1)? I tried to deduce creating other example with some matrix, but i don't understand Why (1:L/2+1)?
_ Define the frequency domain f and plot the single-sided amplitude spectrum P1. The amplitudes are not exactly at 0.7 and 1, as expected, because of the added noise. On average, longer signals produce better frequency approximations. _
f = Fs*(0:(L/2))/L;
plot(f,P1)
title('Single-Sided Amplitude Spectrum of X(t)')
xlabel('f (Hz)')
ylabel('|P1(f)|')
This is my first time I approach to the FFT, and I have a civil engineer background, so is my first time with signal analysis, never studied Signal Theory before.
My interest for the FFT is to define the best low pass filter (example apply a butterworth but I don’t know how to choose the filter order and cutoff frequency.
Thanks in advance :)

Réponses (3)

Jonas
Jonas le 18 Mai 2021
Modifié(e) : Jonas le 18 Mai 2021
P1(2:end-1)= 2*P1(2:end-1)
means that all values of the spectrum get doubled because of the conversion from two sided spectrum (which is symmetrical and has half signal frequency amplitude at positive and negative frequency) to one sided spectrum, which adds the spectrum amplitude of negative frequencies to the amplitude of the positive frequencies. the first value is not doubled, because it is the DC value (the mean of the signal, frequency 0Hz) which occurs only once in the spectrum and has already the true amplitude. the last spectrum frequency (index end) is also not doubled because it occurs also only once in the spectrum
e.g. the spectrum's x axis for the fft output is [0 1 2 -3 -2 1] Hz and we want a full one sided spectrum and we catch the amplitudes with index 1 to 4 (which is (1:L/2+1) by the way) which are the amplitudes of frequencies 0 1 2 -3. first and last frequency occur only once so we dont double them. want the tutorial does not say is that the default output of fft does not have the 0 frequency in the center like we want to see that for a two sided spectrum, the desired view is achieved with a fftshift(), which circularily shifts the frequency by half the entries, which results into the order of [-3 -2 -1 0 1 2], but since you would then have to find the index of frequency zero and double the right half of the values and matlab wants to avoid unnecessary steps and does not want to shift and truncate the left half afterwards. so the tutorial is fast but too fast to understand that correctly
the decision which filter you should choose depends fully on your signal and what frequencies are interesting at the end
  4 commentaires
Francesca Pa
Francesca Pa le 19 Mai 2021
P1(2:end-1) = 2*P1(2:end-1); when i use that, and after i want the magnitude of the fft in db (20*log10(abs(P1))) I need to subtract 3dB to have the correct level? Thank you
Jonas
Jonas le 20 Mai 2021
no, you don't need to subtract 3 dB afterwards. is there a reason why you think that is necessary? it's ok to calculate the dB from the doubled spectrum because the two sided spectrum shows only half the amplitude at the frequencies (e.g. two sided spectrum of a 5Hz sine with amplitude 1 will give you anplitude 0.5 at -5 and +5 Hz, by doubling it and using only one side you then get amplitude 1 at +5Hz which is the correct amplitude)

Connectez-vous pour commenter.


Waldemiro Kubucama
Waldemiro Kubucama le 14 Déc 2021
Means that it initializes from column 2 to the second-to-last column (end-1)

Nidhi Singh
Nidhi Singh le 2 Mar 2022
Modifié(e) : Nidhi Singh le 2 Mar 2022
How can I substrate that 0 Hz dc value it shows a very high amplitude. Actually I tried to substrate the mean of the signal from orginal one and than did fft. But didn't get appropriate results.
  1 commentaire
John D'Errico
John D'Errico le 2 Mar 2022
Please don't ask a question as an answer to another question. And this has nothing to do with the original question.

Connectez-vous pour commenter.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by