Is this a mistake in documentation or my misunderstanding of FFT?

1 次查看(过去 30 天)
In MATLAB Help Center for FFT here; there is one example whose code is given below.
My question is why at line 6 from below (in the following code) it is P1(2:end-1) = 2*P1(2:end-1); instead of P1(2:end) = 2*P1(2:end)?
My rational is that while calculating the Single sided Amplitude spectrum from Double sided spectrum it is only the DC component to be excluded from multiplying by 2. (All AC component is supposed to be multiplied by 2.) If so then why did the 5001 element in vector P1 (which is an AC component) is keep off from getting multiplied by 2 ?
regards
Fs = 1000; % Sampling frequency
T = 1/Fs; % Sampling period
L = 1500; % Length of signal
t = (0:L-1)*T; % Time vector
S = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t);
X = S + 2*randn(size(t));
plot(1000*t(1:50),X(1:50))
title("Signal Corrupted with Zero-Mean Random Noise")
xlabel("t (milliseconds)")
ylabel("X(t)")
Y = fft(X);
P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);
f = Fs*(0:(L/2))/L;
figure
plot(f,P1)
title("Single-Sided Amplitude Spectrum of X(t)")
xlabel("f (Hz)")
ylabel("|P1(f)|")
Y = fft(S);
P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1); % Why is it NOT P1(2:end) = 2*P1(2:end);
figure
plot(f,P1)
title("Single-Sided Amplitude Spectrum of S(t)")
xlabel("f (Hz)")
ylabel("|P1(f)|")

采纳的回答

Bruno Luong
Bruno Luong 2023-2-15
编辑:Bruno Luong 2023-2-15
"Single sided Amplitude spectrum from Double sided spectrum it is only the DC component to be excluded from multiplying by 2"
No if L is even, the nyquist frequency is Y(L/2+1) or P1(end) is shared between one-sided spectrum and the "negative" frequency so it must not devided by 2. So TMW code is correct.
On the otherhand if L is odd the Niquist frequency is in between 2 bins and skip. The correct code is
P1 = P2(1:(L+1)/2)
P1(2:end) = 2*P1(2:end);
Of course one can do the same code for both, a little bit more complicated
P1 = P2(1:ceil((L+1)/2));
P1(2:end-mod(L+1,2)) = 2*P1(2:end-mod(L+1,2));
  2 个评论
Paul
Paul 2023-2-16
@Bruno Luong is correct, of course, wrt to amplitude at the freuqency samples. But there's another subtlety to consider.
If you don't multiply the first and last points by two, then you have to be careful about how to interpret what's happening between the first two points and between the last two points if you're going to "connect the dots" to estimate the Discrete Time Fourier Transform. That's because every point in between the first two and between the last two should be multiplied by two as well, but if you connect the dots between the first and second points, the connecting line won't give correct interpolation. Same for the last two points.
Fahad
Fahad 2023-2-16
Oh, so spectrum is already adding up from positive and negative frequencies at P1(end) (or Nyquist frequency: fs/2).
Cool ! How come I was never aware of this subtle and key concept in all my 20 years with DSP.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

标签

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by