Wednesday, October 18, 2017

[Matlab] FFT (Fast Fourier Transform)

https://www.mathworks.com/help/matlab/ref/fft.html

Matlab computes DFT(Discrete Fourier Transform) using a FFT algorithm.

Example
-------------------------------------
% Discrete Fourier Transform (DFT)
function handles = DFTPlotFnc(hObject,eventdata,handles)

% Load the data
result = handles.result;
% Retrieve x data
xi = get(handles.xVariables,'Value');
t = result{xi,1};
% Retrieve y data
yn = get(handles.VariablesList,'String');
yi = get(handles.VariablesList,'Value');
yn = yn{yi,1}; y = result{yi,1};

% Sampling time Ts and sampling frequency Fs
Ts = t(2) - t(1);
Fs = 1/Ts;

% Data length L
L = length(y);
% Number of FFT points,
% nextpow2 returns the exponents for the smallest powers of two that satisfy 
nfft = 2^nextpow2(L);

% Frequency
freq = (Fs/2) * linspace(0,1,nfft/2+1);
% Discrete Fourier Transform(DFT)
normalizedFFT = fft(y,nfft)/L; % with zero-padding 

% Plot
pline = plot(handles.axes2,freq,2*abs(normalizedFFT(1:nfft/2+1)),'DisplayName',yn);
dftleg = legend(handles.axes2,'-DynamicLegend');
set(dftleg,'interpreter','none');

xlabel(handles.axes2,'Frequency [Hz]');
ylabel(handles.axes2,'FFT |Y(f)|','interpreter','none');
grid(handles.axes2,'on');

originaline = plot(handles.axes3,t,y,'DisplayName',yn);
orileg = legend(handles.axes3,'-DynamicLegend');
set(orileg,'interpreter','none');
xlabel(handles.axes3,'Time [sec]');
ylabel(handles.axes3,[yn ' Original'],'interpreter','none');
grid(handles.axes3,'on');

No comments:

Post a Comment