Plot of fft of data is mirrored, unsure how to fix it

9 views (last 30 days)
I have two sets of data that I have to compare by plotting each with fft on logarithmic axes. The data, however, keeps resulting in being mirrored which as far as I understand is due to not respecting the Nyquist limit.
My sample rate is 40,000 samples/sec and the time is 1 second. I have tried to use fftshift but it just shifts it to the mirroring point (around 20kHz). No matter if the x-axis data is time or freq (with array of 40000), it the y axis data plotted stays the same shape leading me to believe I need to do something to the y axis data?
My current code:
load data.dat
load data2.dat
t = data(4,:);
x = data(3,:);
y = data(1,:);
t2 = data2(4,:);
x2 = data2(3,:);
y2 = data2(1,:);
Fs = 40000;
Fn = Fs/2;
f = (0:length(t)-1)*Fs/length(t);
subplot(2,1,1)
h = fft(x);
semilogx(f,h)
subplot(2,1,2)
h2 = fft(x2/2);
semilogx(f,h2,"r");

Accepted Answer

Star Strider
Star Strider on 11 Nov 2022
It respects the Nyquist limit. If you use the fftshift function you will see the result as negative frequencies and positive frequencies (although you have to create the frequency vector to go from -Fn to +Fn, where ‘Fn’.is the Nyquist frequency).
To get a one-sided Fourier transform, I do something like this —
Fs = 1000; % Sampling Frequency
t = linspace(0, Fs-1, Fs)/Fs; % Time Vector
s = sum(sin([1:50:460].'*2*pi*t)).'; % Signal
t = t(:);
L = numel(t);
Fn = Fs/2; % Nyquist Frequency
NFFT = 2^nextpow2(L); % For Efficiency
FTs = fft(s.*hamming(L),NFFT)/L; % Windowed Fourier Transform
Fv = linspace(0, 1, NFFT/2+1)*Fn; % Frequency Vector
Iv = 1:numel (Fv); % Index Vector
figure
plot(Fv, abs(FTs(Iv))*2)
grid
xlabel('Frequency')
ylabel('Magnitude')
Make appropriate changes to work with your signals.
.
  6 Comments
Clare Leverage
Clare Leverage on 11 Nov 2022
I had only used t=t(:) previously but also using it on my signal helped!! Thank you so much it completely solved the issue. It works perfectly now (:

Sign in to comment.

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!