Skip to content

Instantly share code, notes, and snippets.

@PiJoules
Created July 10, 2015 20:42
Show Gist options
  • Save PiJoules/7340c42995b03db2fc9e to your computer and use it in GitHub Desktop.
Save PiJoules/7340c42995b03db2fc9e to your computer and use it in GitHub Desktop.
Cosine Inverse Fourier Transform Example
clear all; close all; clc;
W = 5;
fs = 100;
t = -W:1/fs:W;
N = length(t);
x = cos(2*pi*t);
% Fourier transform
bin_inc = fs/N;
f = (-N/2:1:N/2-1)*bin_inc;
X = zeros(1, length(f));
for i = 1:length(f)
X = X + x(i)*exp(-j*2*pi*f(i)*t)/N;
end
figure(1);
subplot(3,1,1);
plot(t,x);
grid on;
title('g(t)=cos(2\pit)');
xlabel('Time (s)');
ylabel('Magnitude');
figure(1);
subplot(3,1,2);
stem(f,X);
xlim([-2 2]);
grid on;
title('Fourier Transform of g(t)');
ylabel('Magnitude');
xlabel('Frequency (f)');
figure(1);
subplot(3,1,3);
grid on;
hold on;
result = zeros(1,length(t));
for i = 1:length(f)
result = result + X(i)*exp(j*2*pi*f(i)*t);
end
plot(t, real(result));
title('Reconstructed wave from IFT');
ylabel('Magnitude');
xlabel('Time (s)');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment