Error in FFT calculation
9 views (last 30 days)
Show older comments
Why the FFT value shows higher value in the cut signal when compared to whole signal, it should be opposite right?
close all
clear all
clc
load("allSignals.mat");
Fs = 50e6; % Sampling frequency
T = 1/Fs; % Sampling period
L = 7500; % change as required
t = (0:L-1)*T; % Time vector
X = allSignals(1,:);
plot(t,X)
title("Signal ")
xlabel("t ")
ylabel("X(t)")
% fft
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;
plot(f,P1)
title("Single-Sided Amplitude Spectrum of X(t)")
xlabel("f (Hz)")
ylabel("|P1(f)|")
figure ; % after cut
X1 = allSignals(1,500:1100);
L1 = length(X1);
Y1 = fft(X1);
P21 = abs(Y1/L1);
P11 = P21(1:L1/2+1);
P11(2:end-1) = 2*P11(2:end-1);
f1 = Fs*(0:(L1/2))/L1;
plot(f1,P11)
title("Single-Sided Amplitude Spectrum of X(t)")
xlabel("f (Hz)")
ylabel("|P1(f)|")
0 Comments
Accepted Answer
Star Strider
on 3 Oct 2023
The value for ‘L’ is 7500 and for ‘L1’ is 601.
If you change:
P21 = abs(Y1/L1);
to
P21 = abs(Y1/L);
you will get the expected result (magnitude of about 40 rather than about 500).
load("allSignals.mat");
Fs = 50e6; % Sampling frequency
T = 1/Fs; % Sampling period
L = 7500; % change as required
t = (0:L-1)*T; % Time vector
X = allSignals(1,:);
plot(t,X)
title("Signal ")
xlabel("t ")
ylabel("X(t)")
% fft
Y = fft(X);
L = length(X) % <— ADDED
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;
plot(f,P1)
title("Single-Sided Amplitude Spectrum of X(t)")
xlabel("f (Hz)")
ylabel("|P1(f)|")
figure ; % after cut
X1 = allSignals(1,500:1100);
L1 = length(X1);
Y1 = fft(X1);
% P21 = abs(Y1/L1);
P21 = abs(Y1/L); % <— CHANGED
P11 = P21(1:L1/2+1);
P11(2:end-1) = 2*P11(2:end-1);
f1 = Fs*(0:(L1/2))/L1;
plot(f1,P11)
title("Single-Sided Amplitude Spectrum of X(t)")
xlabel("f (Hz)")
ylabel("|P1(f)|")
.
2 Comments
Star Strider
on 3 Oct 2023
Zero-padding is appropriate.
It would be better to use this:
L = length(X1);
NFFT = 2^nextpow2(L);
Y1 = fft(X1,NFFT);
or better yet, this:
Y1 = fft(X1(:).*hann(L),NFFT);
since the window function corrects for the fft being finite, providing a more accurate result, and a power-of-2 fft length significantly improves its efficiency. Zero-padding it also improves the frequency resolution.
.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

