How to plot Fourier transform against frequency?
3 views (last 30 days)
Show older comments
Jacob Duryee-Feeney
on 9 Apr 2022
Commented: Star Strider
on 9 Apr 2022
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
0 Comments
Accepted Answer
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
Df=1/((Ntotal)*L/n)
f_axis=(0:1:(Ntotal-1))*Df
.
4 Comments
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.
More Answers (0)
See Also
Categories
Find more on Spectral Measurements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!