Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

1802037

Task 01:
Code:
clc;
clear all;
close all;
Fs = 1000; % Sampling frequency
T = 1/Fs; % Sampling period
L = 1500; % Length of signal
t = (0:L-1)*T; % Time vector
S = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t);
X = S + 2*randn(size(t));
subplot(3,1,1)
plot(1000*t(1:50),X(1:50),'m','Linewidth',3)
title('Signal Corrupted with Zero-Mean Random Noise')
xlabel('t (milliseconds)')
ylabel('X(t)')

Output:

Analysis:
A corrupted signal with zero mean random noise is plotted. The frequency components of a signal buried in
noise is retrieved through Fourier Transforms. 1 KHz sampling frequency and 1.5 seconds signal duration is
specified. A signal containing a 50 Hz sinusoid of amplitude 0.7 and a 120 Hz sinusoid of amplitude 1 is
formed first.

Page 1 of 7
1802037

Task 02:
Code:
Y = fft(X);
P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);
f = Fs*(0:(L/2))/L;
subplot(3,1,2)
plot(f,P1,'r','Linewidth',1)
title('Single-Sided Amplitude Spectrum of X(t)')
xlabel('f (Hz)')
ylabel('|P1(f)|')

Output:

Analysis:
It is difficult to identify the frequency components by looking at the original signal. Converting the
frequency domain the discrete Fourier transform of the noisy signal ‘y’ is found by taking the fast Fourier
transform(FFT).

Page 2 of 7
1802037

Task 03:
Code:
Y = fft(S); %Taking the Fourier transform of the original,
uncorrupted signal
and retrieve the exact amplitudes, 0.7 and 1.0.
P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);
subplot(3,1,3)
plot(f,P1,'b','Linewidth',3)
title('Single-Sided Amplitude Spectrum of S(t)')
xlabel('f (Hz)')
ylabel('|P1(f)|')

Output:

Analysis:
The amplitude of the original signal is 0.7 and 1.0. The exact amplitudes are retrieved by taking the Fourier
transform of the original, uncorrupted signal.

Page 3 of 7
1802037

Task 04:
Code:
function [Xk] = dfs(xn,N)
% Computes Discrete Fourier Series Coefficients
% ---------------------------------------------
% [Xk] = dfs(xn,N)
% Xk = DFS coeff. array over 0 <= k <= N-1
% xn = One period of periodic signal over 0 <= n <= N-1
% N = Fundamental period of xn
n = [0:1:N-1]; % row vector for n
k = [0:1:N-1]; % row vecor for k
WN = exp(-j*2*pi/N); % Wn factor
nk = n'*k; % creates a N by N matrix of nk values
WNnk = WN .^ nk; % DFS matrix
Xk = xn * WNnk; % row vector for DFS coefficients
end

Output:

Analysis:
The DFS is a numerically computable representation. An efficient implementation in MATLAB would be to
use a matrix-vector multiplication. Let ˜x and X˜ denote column vectors corresponding to the primary
periods of sequences ˜ x(n) and X˜(k), respectively. The matrix WN is a square matrix and is called a DFS
matrix..

Page 4 of 7
1802037

Task 05:
Code:
function [xn] = idfs(Xk,N)
% Computes Inverse Discrete Fourier Series
% ----------------------------------------
% [xn] = idfs(Xk,N)
% xn = One period of periodic signal over 0 <= n <= N-1
% Xk = DFS coeff. array over 0 <= k <= N-1
% N = Fundamental period of Xk%
n = [0:1:N-1]; % row vector for n
k = [0:1:N-1]; % row vecor for k
WN = exp(-j*2*pi/N); % Wn factor
nk = n'*k; % creates a N by N matrix of nk values
WNnk = WN .^ (-nk); % IDFS matrix
xn = (Xk * WNnk)/N; % row vector for IDFS values
end

Output:

Analysis:
The IDF function implements the synthesis equation. It computes inverse discrete Fourier series.

Page 5 of 7
1802037

Task 06:
Code:
clc;
clear all
close all
L = 5;
N = 20;
k = [-N/2:N/2];
xn = [ones(1,L), zeros(1,N-L)];
Xk = dfs(xn,N);
magXk = abs([Xk(N/2+1:N) Xk(1:N/2+1)]);
%subplot(2,2,1);
stem(k,magXk);
axis([-N/2,N/2,-0.5,5.5])
xlabel('k');
ylabel('Xtilde(k)')
title('DFS of SQ. wave: L=5, N=20')
Output:

Analysis:
The envelopes of the DFS coefficients of square waves look like “sinc” functions. The amplitude at k = 0 is
equal to L, while the zeros of the functions are at multiples of N/L, which is the reciprocal of the duty cycle.
Since X˜(k) is periodic, the plots are shown from -N/2 to N/2.

Page 6 of 7
1802037

Task 07:
Code:
clc
clear all
close all
Nmax=2048;
fft_time=zeros(1,Nmax);
for n=1:1:Nmax
x=rand(1,n);
t=clock;fft(x);fft_time(n)=etime(clock,t);
end
n=[1:1:Nmax];
plot(n,fft_time,'.')
xlabel('N');
ylabel('Time inSec.')
title('FFT execution times')
Output:

Analysis:
To determine the execution time, MATLAB provides two functions. The clock function provides the
instantaneous clock reading, while the “etime(t1,t2)” function computes the elapsed time between two time
marks t1 and t2. To determine the execution time, we have generated random vectors from length 1 through
2048, computed their FFTs, and saved the computation time in an array. Finally, we have plotted this
execution time versus N. This plot is very informative. The points in the plot do not show one clear function
but appear to group themselves into various trends.

Page 7 of 7

You might also like