Clear Filters
Clear Filters

How to plot Fourier transform against frequency?

3 views (last 30 days)
I am trying to plot the fourrier transform against frequency rather than bins. How do I do this? I belive the mistake is in creating f_axis
n = 400 %number of points
F1 = 3 %frequency 2
F2 = 2 %frequency 1
NT = 1.33 %number of periods
T = 2*pi/(F1-F2) %one period
L = NT*T %length of signal
m = zeros(1,n/20)
p = zeros(1,n/20)
for N = 20:20:n
t = 0:L/N:L-L/N % time axis given by N points counting from 0 to NT periods
y = cos(t.*(F1+F2)).*cos(t.*(F1-F2)) %signal
%%% Windowing %%%
win = gausswin(N)
y = y'.*win
%%% Zero padding %%%
Nz = 1000 % number of zeros to pad with
y = [y' zeros(1,Nz)]
%%% Fourier Transform %%%
Y = fftshift(fft(y))
%%% Extract the maxima %%%
[pks,locs] = findpeaks(abs(Y))
m(N/20) = m(N/20)+locs(3)
p(N/20) = p(N/20)+locs(4)
end
%%% plot against frequecy rather than bins %%%
Ntotal = Nz + n
Df=1/((Ntotal)*L/n)
f_axis=(0:1:(Ntotal-1))*Df

Accepted Answer

Star Strider
Star Strider on 9 Apr 2022
I am not certain what you want to do.
Try this —
n = 400; %number of points
F1 = 3; %frequency 2
F2 = 2; %frequency 1
NT = 1.33; %number of periods
T = 2*pi/(F1-F2); %one period
L = NT*T; %length of signal
m = zeros(1,n/20);
p = zeros(1,n/20);
Nz = 1000; % number of zeros to pad with
for N = 20:20:n
t = 0:L/N:L-L/N; % time axis given by N points counting from 0 to NT periods
y = cos(t.*(F1+F2)).*cos(t.*(F1-F2)); %signal
%%% Windowing %%%
win = gausswin(N);
y = y'.*win;
%%% Zero padding %%%
% Nz = 1000 % number of zeros to pad with
y = [y' zeros(1,Nz)];
%%% Fourier Transform %%%
Y = fftshift(fft(y));
Fs = N/L; % Sampling Frequency
Fn = Fs/2; % Nyquist Frequency
Fv = linspace(-Fn, Fn, numel(t)+Nz); % Frequency Vector (Assuming 'fftshift')
figure % Plot Fourier Transforms
plot(Fv, abs(Y))
grid
xlabel('Frequency')
ylabel('Amplitude')
title(sprintf('N = %d',N))
%%% Extract the maxima %%%
[pks,locs] = findpeaks(abs(Y));
m(N/20) = m(N/20)+locs(3);
p(N/20) = p(N/20)+locs(4);
end
%%% plot against frequecy rather than bins %%%
Ntotal = Nz + n
Ntotal = 1400
Df=1/((Ntotal)*L/n)
Df = 0.0342
f_axis=(0:1:(Ntotal-1))*Df
f_axis = 1×1400
0 0.0342 0.0684 0.1026 0.1368 0.1710 0.2051 0.2393 0.2735 0.3077 0.3419 0.3761 0.4103 0.4445 0.4787 0.5129 0.5470 0.5812 0.6154 0.6496 0.6838 0.7180 0.7522 0.7864 0.8206 0.8548 0.8889 0.9231 0.9573 0.9915
.
  4 Comments
Jacob Duryee-Feeney
Jacob Duryee-Feeney on 9 Apr 2022
thank you. I am still not sure the FFT shows the correct frequencies though. Why is it showing a peak at f<1 when F1 and F2 are 2 and 3 respectively.
Star Strider
Star Strider on 9 Apr 2022
My plkeasuer.
It is showing the frequencies you defined (calculatted from the length of ‘t’ and ‘L’ and ‘N’) and since you want a double-sided Fourier transform, the frequency vector ‘Fv’ reflects that. The ‘locs’ vector are the absolute indices of the peaks. To experess them as frequencies, use:
PeakFreqs = Fv(locs);
Those frequencies should correspond to the double-sided Fourier transform plots.
This plots them, as requested. Experiment with it to get the result you want.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!