The code below uses the signal below as an example:
y=5*sin(10*2*pi*x)+3*sin(22*2*pi*x)+cos(13*2*pi*x)+7;
Now the Code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% A simple example of how to reconstruct | |
% the frequency components from an FFT | |
%% Clean up the workspace | |
clear all; | |
close all; | |
clc; | |
%% Create the initial parameters | |
Window=5; %Set how long in time to sample + and - | |
Fs=100; %Set the sampling frequency | |
% Fs should be set atleast 2x max | |
% frequency in the signal | |
% With the signal below, the max frequency is | |
% 22Hz meaning we want to sample atleast at 44Hz | |
%% Generate the time domain signals | |
x=-Window:1/Fs:Window; %Create the time signal | |
y=5*sin(10*2*pi*x)+3*sin(22*2*pi*x)+cos(13*2*pi*x)+7; | |
% Plot the time domain signal | |
figure(1); | |
plot(x,y); | |
%% Compute and plot the frequency domain signal | |
figure(2); | |
% Extract the max frequency that the FFT | |
% can compute from the Fs we have specified | |
fmax=Fs/2; %Application of Nyquist Theorem | |
% Compute the magnitude of the fft of y | |
N=441; | |
FFTy=fftshift(abs(fft(y,N))); | |
% The frequency axis is generated using linspace | |
f_axix=linspace(-fmax,fmax,N-1); | |
% Plot the FFT using stem | |
stem(f_axix,FFTy(2:end)./length(FFTy)); |
Now the result:
Hope you find this useful.
I don't have matlab installed so I cannot verify... But I believe this also works for getting the frequency axis:
ReplyDeletedeltat = x(2)-x(1);
fmax = 1/(2*deltat);
deltaf = 1/(N*deltat);
f_axix = -fmax:deltaf:fmax;
Hi,
ReplyDeletejust in case someone still reads this. You can also go to the help of the "fft" command inside Matlab and check the example code at the bottom. This shows exactly how to do that.
Best
Martin