You are on page 1of 10

Fourier Transform

Code for exp(-5t)*u(t):

clc;
close all;
clear all;

% Plotting x(t)
t = 0:0.001:30;
x = exp(-5*t);
y2 = zeros(size(x));
y2 = y2(2:end);
x = [y2, x];
t = -30:0.001:30;
subplot(4, 1, 1)
plot(t, x);
title('x(t)=exp(-5t)*u(t)')

%Plotting Fourier Transform( |X(w)| ) of x(t)


subplot(4, 1, 2);
w = -6:0.001:6;
ft = zeros(size(w));
i = 1;
for wi = w
ft(i) = fourier_transform(wi);
i = i+1;
end
plot(w, abs(ft));
title('|X(w)|');

% Plotting angle(X(w)) of x(t)


subplot(4, 1, 3);
plot(w, angle(ft));
title('Arg(X(w))')

% Plotting theoretical values of |X(w)| for checking


subplot(4, 1, 4)
ind = 1;
for wr=w
m = (5+(1i*wr));
ch(ind) = 1/m;
ind = ind+1;
end
plot(w, abs(ch), 'r')
title('Theoretical |X(w)|')

% Function to get fourier transform


function s=fourier_transform(w)
handler = @(t) signal1(t)*exp(-1i*w*t);
s = integral(handler, -Inf, Inf, 'ArrayValued', true);
end

% Function to get signal values


function s=signal1(x)
if x < 0
s = 0;
elseif x >=0
s = exp(-5*x);
else
s = 0;
end
end
Code for exp(-5|t|):

clc;
close all;
clear all;

% Plotting x(t)
t = 0:0.001:30;
x = exp(-5*t);
t = -30:0.001:30;
x2 = fliplr(x);
x = [x2(1:end-1),x];
subplot(4, 1, 1);
plot(t, x);
title('x(t)=exp(-5|t|)')

%Plotting Fourier Transform( |X(w)| ) of x(t)


subplot(4, 1, 2);
w = -6:0.01:6;
ft = zeros(size(w));
i = 1;
for wi = w
ft(i) = fourier_transform(wi);
i = i+1;
end
plot(w, abs(ft));
title('|X(w)|');

% Plotting angle(X(w)) of x(t)


subplot(4, 1, 3);
plot(w, angle(ft));
title('Arg(X(w))')
ylim([-1 1])

% Plotting theoretical values of |X(w)| for checking


subplot(4, 1, 4);
ind = 1;
for wr=w
m = (25+((wr).^2));
ch(ind) = 10/m;
ind = ind+1;
end
plot(w, abs(ch), 'r')
title('Theoretical |X(w)|')
% Function to get fourier transform
function s=fourier_transform(w)
handler = @(t) signal1(t)*exp(-1i*w*t);
s = integral(handler, -Inf, Inf, 'ArrayValued', true);
end

% Function to get signal values


function s=signal1(x)
x = abs(x);
s = exp(-5*abs(x));
end
Code for Square Wave:
clc;
clear all;
close all;

%Plotting x(t)
subplot(3, 1, 1)
t = -10:0.001:10;
sig = zeros(size(t));
i = 1;
for ti=t
sig(i) = signal(ti);
i = i + 1;
end
plot(t, sig)
title('x(t)')
ylim([0 1.25])

%Plotting Fourier Transform(X(w)) of x(t)


subplot(2, 1, 2);
w = -7:0.01:7;
ft = zeros(size(w));
i = 1;
for wi = w
ft(i) = fourier_transform(wi);
i = i+1;
end
plot(w, ft);
line(xlim, [0,0], 'Color', 'k');
title('X(w)');

% Function to get fourier transform


function s=fourier_transform(w)
handler = @(t) signal(t)*exp(-1i*w*t);
s = integral(handler, -Inf, Inf, 'ArrayValued', true);
end

% Function to get the signal


function s=signal(x)
if x>=-4 && x<=4
s = 1;
else
s = 0;
end
end
Code for Sinc Wave:

clc;
clear all;
close all;

%Plotting x(t)
subplot(2, 1, 1)
t = -10:0.001:10;
sig = zeros(size(t));
i = 1;
for ti=t
sig(i) = signal(ti);
i = i + 1;
end
plot(t, sig)
line(xlim, [0,0], 'Color', 'k');
title('x(t)')

%Plotting Fourier Transform(X(w)) of x(t)


subplot(2, 1, 2);
w = -7:0.01:7;
ft = zeros(size(w));
i = 1;
for wi = w
ft(i) = fourier_transform(wi);
i = i+1;
end
plot(w, ft);
line(xlim, [0,0], 'Color', 'k');
title('X(w)');

% Function to get fourier transform


function s=fourier_transform(w)
handler = @(t) signal(t)*exp(-1i*w*t);
s = integral(handler, -100, 100, 'ArrayValued', true);
end

% Function to get the signal


function s=signal(x)
if x==0
s = 2/pi;
else
s = sin(2*x)/(pi*x);
end
end

You might also like