Tuesday, January 24, 2012
Fourier Transform for Dummies: Matlab Implementation
The following code implements a simple Fourier Transform using cos and sin functions. This project was inspired by a short pdf I stumbled onto during an internet search on this topic. Here is a link to the original: FT for Dummies (I will gladly credit the author if contacted, I just have no idea who wrote the original document as it is not cited)
The following Matlab code, implements a simple Fourier Transform manually in Matlab.
% Fourier Transform for Dummies
% Matlab Implementation
% James Eastham
% Member, IEEE
% 1/22/2012
clear all; %clear all variables
close all; %close all figures
fs = 100; % sample freq
Ts=1/fs; % sample period
n=0:1*fs; % time window (fs=samples per second), 1 second
tn=n*Ts; % sample index
sig_a=5*sin(2*pi*5*tn+pi/3)+2*sin(2*pi*30*tn+pi/2)+2.3*sin(2*pi*8*tn); %create complex signal
sig_a = sig_a + 2*randn(size(tn)); % add noise
figure('Color',[1 1 1]); % plot orignial signal in time domain
subplot(2,1,1);hold on;
plot(tn,sig_a,'-r');
title('Time Domain Signal w/ Noise');
grid on;
% The following loop multiplies the original signal
% by sin and cos at each freq betwen 1 and fs/2
% each sample is then summed, after the loop
% the total sum for each freq is computed
for i=1:fs/2;
detect_sin=sin(2*pi*i*tn);
detect_cos=cos(2*pi*i*tn);
mult_sig_sin=detect_sin.*sig_a;
mult_sig_cos=detect_cos.*sig_a;
total_freq_sin(i)=sum(mult_sig_sin);
total_freq_cos(i)=sum(mult_sig_cos);
end;
total_freq_sin=total_freq_sin/(fs/2);
total_freq_cos=total_freq_cos/(fs/2);
total_freq=sqrt(total_freq_sin.^2+total_freq_cos.^2);
subplot(2,1,2);
stem(total_freq);
xlabel('Frequency');
ylabel('Amplitude');
Title('Frequency Domain');
axis('tight');
grid on;
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment