Skip to content

Instantly share code, notes, and snippets.

@PiJoules
Last active August 29, 2015 14:24
Show Gist options
  • Save PiJoules/cae4321693638e082495 to your computer and use it in GitHub Desktop.
Save PiJoules/cae4321693638e082495 to your computer and use it in GitHub Desktop.
Cosine Fourier Transformation
clear all; close all; clc;
W = 4;
Fs = 100; % large frequency to replicate continuous data (dt)
t = 0:1/Fs:W-1/Fs;
N = length(t);
x = cos(2*pi*t);
% Fourier transform
f = -10:1/Fs:10;
X = zeros(1,length(f));
for i1 = 1:length(f)
X(i1) = sum( x.*exp(-1i*f(i1).*t) )/Fs;
end
% Inverse Fourier Transform
xr = zeros(1,length(t));
for i1 = 1:length(t)
xr(i1) = sum( X.*exp(1i.*f*t(i1)) )/2/pi/Fs;
end
figure(1);
subplot(3,1,1);
plot(t,x);
grid on;
title('x(t)=cos(2\pit)');
xlabel('Time (s)');
ylabel('Magnitude');
subplot(3,1,2);
plot(f,X);
grid on;
title('X(\omega)');
ylabel('Magnitude');
xlabel('Frequency (\omega)');
subplot(3,1,3);
plot(t,xr);
grid on;
title('Reconstructed x(t)');
xlabel('Time (s)');
ylabel('Magnitude');
ylim([-1 1]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment