Skip to content

Instantly share code, notes, and snippets.

@grodtron
Created May 18, 2012 14:43
Show Gist options
  • Save grodtron/2725603 to your computer and use it in GitHub Desktop.
Save grodtron/2725603 to your computer and use it in GitHub Desktop.
A Matlab script to calculate the discrete Fourier Transform of a signal using matrix multiplication.
clear;
clc;
% This is a test
% This x[n] is a simple square pulse centered at the origin
% it can be replaced with any finite-duration signal, as long as
% n is updated to match
x = [ zeros(1,8) ones(1,5) zeros(1,8) ];
n = -10:10;
stepsize = pi/20;
W = -pi:stepsize:pi;
% We have to create the first column of the matrix outside of the
% loop. There may be a better way of doing this, I'm not sure.
matrix = exp(-j*W(1)*n)';
% We build up the matrix by successively concatenating columns
for w = W(2:length(W))
matrix = [ matrix exp(-j*w*n)' ];
end
% The product of the original signal and the matrix gives the
% Fourier transform of the signal!
transform = x * matrix;
% plot it
subplot(2,1,1);
stem(n,x);
title('x[n]');
subplot(2,1,2);
plot(W, abs(transform));
title('Fourier transform of x[n] (over one period)');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment