Error in FFT calculation

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)|")

 Accepted Answer

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
L = 7500
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);
Warning: Integer operands are required for colon operator when used as index.
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

Indeed it worked ,one question how about zero padding in this case ? is it right to use in this case or when it should be used?
L = 2*length(x1);
Y1 = fft(X1,L); % instead of Y1 = fft(X1);
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.
.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2023b

Community Treasure Hunt

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

Start Hunting!