You are on page 1of 33

Shahjalal University of Science & Technology, Sylhet

Department of Electrical and Electronic Engineering


Course no: EEE 332
Course Name: Digital Signal Processing I Lab

Experiment No.: 04
Experiment Name: Frequency Domain Analysis of DT Signals and
Systems.

Submitted to

Md. Ishfak Tahmid,


Assistant Professor,
Department of Electrical and Electronic Engineering.

Submitted by

Name: Md. Tawsiful Alam


Registration No.: 2018338009
Group No.: 01
Session: 2018-19
Date of Submission: 13/04/2022
Experiment No: 04

Experiment Name: Frequency Domain Analysis of DT Signals and


Systems.

Theory:
The prominent methods in the field of analysis of the signal and system are
Fourier Series (for periodic signal) and Fourier Transform (for aperiodic signal).
In this process, the signal is decomposed into various sinusoidal components of
different frequencies and amplitude. These decompositions are important for the
analysis of the LTI system.
For Fourier Series, the signal is broken down into 2 parts: amplitude spectrum
and phase spectrum. The generalized way to represent a periodic signal in
Fourier Series is:
N−1 − j 2 πkn
1
c k=
N
∑ x (n) e N

n=0

For Fourier Transform, the basic way to represent an aperiodic signal:



X (ω)= ∑ x ( n ) e− jωn
n=−∞

Code:
Question:
Find the mean square error from the Fourier Series of a rectangular pulse
train.
The required code to solve the given problem is as follows:
clc;
close all;
clear all;

% Generation of Input Signal


Fs = 100e3;
dt = 1/Fs;
T = 1e-3;
D = .1;
PW = D*T;
f = 1/T;
t = -T/2:dt:T/2;
n = t/dt;
L = PW/dt;
x = zeros(1,length(t));
x(find(abs(n)<=L/2)) = 1.1 ;

%Taking different values of Nc/N


ah = 0.01:0.04:1;

%Calculation of Fourier Series Coefficients


N = length(x);
for f = 1:length(ah)
Nc =ah(f)*N;
if mod(Nc,2)== 0
k = -Nc/2:Nc/2-1;
else
k = -(Nc-1)/2:(Nc-1)/2;
end

c = zeros(1,length(k));
for i1 = 1:length(k)
for i2 = 1:length(x)
c(i1) = c(i1)+1/N*x(i2)*exp(-i*2*pi*k(i1)*n(i2)/N);
end
end

t_remax = T/2;
t_re=-t_remax:dt:t_remax;
n_re = t_re/dt;
x_re = zeros(1, length(n_re));
for i1 = 1:length(k)
for i2 = 1:length(x_re)
x_re(i2)= x_re(i2)+c(i1)*exp(i*2*pi*k(i1)*n_re(i2)/N);
end
end
% Determination of Mean Square Error
er = ((x-x_re)/x)^2;
g(f) = mean(er);
end

plot(1:length(ah),g), xlabel('Nc/N'),ylabel('MSE'),
title('Mean Square Error vs. Total no. of Coefficient');

Result:
From the graph (MSE vs Nc/N) with the increase in the number of coefficients
in the Fourier Series, the error in the reconstruction of the given signal
decreases.

Explanation:
First, an input signal i.e., a rectangular pulse train is generated. Then, for
various values of Nc, the signal is converted into Fourier Series. From the
series, the signal is reconstructed. For a value of Nc, the error between points of
the original and reconstructed signal is determined. Then, the values are squared
and the mean of the sum of the squared values is stored in the g matrix. A
similar task is done for other values of Nc. In this way, we can have a set of
values that can be plotted against corresponding Nc/N.

Question:

Note that in example 1, the time


interval is defined from –T/2 to
T/2, Change the interval to 0
to T and–T/4 to 3T/4. Do you
observe any change in the
magnitude spectrum?
Note that in example 1, the time interval is defined from –T/2 to T/2, Change the
interval to 0 to T and to –T/4 to 3T/4. Do you observe any change in the
magnitude spectrum? Try to explain the observation. Change the interval from
–T to T. Explain the observation.
.
Code:
clc;
close all;
clear all;
Fs = 100e3;
dt = 1/Fs;
T = 1e-3;
D = .1;
PW = D*T;
f = 1/T;
a = [ -1/2,0,-1/4,-1];
b = [ 1/2,1,3/4,1];

for m = 1:4
t = a(m)*T:dt:b(m)*T;
n = t/dt;
L = PW/dt;
x = zeros(1,length(t));
x(find(abs(n)<=L/2)) = 1.1 ;
figure(1), subplot(2,2,m), plot(t,x,'k'), title('Original
Continuous Signal')

N = length(x);
Nc =N;
if mod(Nc,2)== 0;
k = -Nc/2:Nc/2-1;
else
k = -(Nc-1)/2:(Nc-1)/2;
end

c = zeros(1,length(k));
for i1 = 1:length(k)
for i2 = 1:length(x)
c(i1) = c(i1)+1/N*x(i2)*exp(-i*2*pi*k(i1)*n(i2)/N);
end
end
subplot(2,2,m),stem(k,abs(c),'.k'),title(m,'Fourier Series
Coefficient');

end
Result:
For different time intervals of the input signal, different magnitude spectrums
are found.

Explanation:
The input signal is different for different time intervals though their shape and
size may look alike. So, the distribution of the value of the coefficient is
different from one another. As a result, the magnitude spectrum is different.

Question:

We have stated that c


k+N
=c
k
. Modify the code for example
1 and see whether you have a
periodic
repetition of c
k
.
/5637

9)234+74
>#,

>#3#7,3#7
/
We have stated that c
k+N
=c
k
. Modify the code for example
1 and see whether you a
periodic
repetition of c
k
.
/5637

9)234+74
>#,

>#3#7,3#7
/
We have stated that c
k+N
=c
k
. Modify the code for example
1 and see whether you a
periodic
repetition of c
k
.
We have stated that ck+N =ck. Modify the code for example 1 and see
whether you a periodic repetition of ck.

N = length(x);
If mod(N,2) == 0
K = -N:N;
else
K = -(N-1):(N-1);
end

Code:
clc;
close all;
clear all;
Fs = 100e3;
dt = 1/Fs;
T = 1e-3;
D = .1;
PW = D*T;
f = 1/T;
t = -T/2:dt:T/2;
n = t/dt;
L = PW/dt;
x = zeros(1,length(t));
x(find(abs(n)<=L/2)) = 1.1 ;

N = length(x);
if mod(N,2)== 0
k = -2*N:2*N;
else
k = -2*(N-1):2*(N-1);
end

c = zeros(1,length(k));
for i1 = 1:length(k)
for i2 = 1:length(x)
c(i1) = c(i1)+1/N*x(i2)*exp(-i*2*pi*k(i1)*n(i2)/N);
end
end
figure(2), stem(k,abs(c),'.k'),title('Fourier Series
Coefficient');
Result:
A graph of periodic repetition of ck is plotted.

Explanation:
Using N, we get a periodic repetition of c k because in the Fourier Series ck is a
periodic sequence with period N.

Question:
Take a sine wave and determine the spectrum.

Code:
clc;
close all;
clear all;
Fs = 100e3;
dt = 1/Fs;
T = 1e-3;
f = 1/T;
t = -T/2:dt:T/2;
x = sin(2*pi*(t/T));
n = t/dt;

N = length(x);
Nc =N;
if mod(Nc,2)== 0;
k = -Nc/2:Nc/2-1;
else
k = -(Nc-1)/2:(Nc-1)/2;
end

c = zeros(1,length(k));
for i1 = 1:length(k)
for i2 = 1:length(x)
c(i1) = c(i1)+1/N*x(i2)*exp(-i*2*pi*k(i1)*n(i2)/N);
end
end
figure(2), subplot(211),stem(k,abs(c),'.k'),title('Fourier
Series Coefficient');
subplot(212), stem(k,angle(c)*180/pi,'.k'), title('angle of
Fourier Series')

Result:
The spectrums of a sine wave are shown in the graph.
Explanation:
Through Fourier Series, a sine wave is decomposed into amplitude spectrum
and phase spectrum.

Question:
Derive the power density spectrum for a rectangular pulse train.
Code:
clc;
close all;
clear all;

%generation of input signal


Fs = 100e3;
dt = 1/Fs;
T = 1e-3;
D = .1;
PW = D*T;
f = 1/T;
t = -T/2:dt:T/2;
n = t/dt;
L = PW/dt;
x = zeros(1,length(t));
x(find(abs(n)<=L/2)) = 1.1 ;

%Calculation of Fourier Coefficient


N = length(x);
Nc =N;
if mod(Nc,2)== 0;
k = -Nc/2:Nc/2-1;
else
k = -(Nc-1)/2:(Nc-1)/2;
end

c = zeros(1,length(k));
for i1 = 1:length(k)
for i2 = 1:length(x)
c(i1) = c(i1)+1/N*x(i2)*exp(-i*2*pi*k(i1)*n(i2)/N);
end
end

%plotting of power density spectrum


h = (abs(c)).^2;
stem(k,h,'.k'),title('Power Density Spectrum')
Result:
The graph shows the power density spectrum for a rectangular pulse train
Explanation:
The square of the coefficient of the Fourier Series gives the power for that
frequency. The power is plotted against the value of k.

Question:
Change the range of the frequency grid to [0,2 π ]and [- 2 π , 2 π ]. Observe
and explain the effect.
Code:
clc;
close all;
clear all;

%generation of input signal


f_c = 1/8;
n = -40:40;
x = sinc(2*f_c*n);

%rannge of the frquency grid


a = [-1 0 -2];
b = [1 2 2];

M=101;
for f = 1: length(a)
w = linspace(a(f)*pi,b(f)*pi,M);
dw = w(2)-w(1);
X = zeros(1,M);

for i1 = 1:M
for i2 = 1:length(x)
X(i1) = X(i1)+x(i2)*exp(-i*w(i1)*n(i2));
end
end
figure(1),subplot(3,1,f), plot(w,abs(X)), title(f,'Frequency
Spectra');
end
Result:
The graph shows the periodicity of X (ω).

Explanation:
In Fourier transform, X (ω) is periodic with period 2 π extending from - π to
π.As a result, at a different interval other than from [- π ,π ], we multiple
waves of X(ω).

Question:
Verify the effect of changing index (n) to -40:120, -40:80. In each case observe
the reconstructed signal.
Code:
clc;
close all;
clear all;
f_c = 1/8;

a = [ -40 -40 -40];


b = [ 40 80 120];

for f = 1:length(a)
n = a(f):b(f);
x = sinc(2*f_c*n);

M=101;
w = linspace(-pi,pi,M);
dw = w(2)-w(1);
X = zeros(1,M);

for i1 = 1:M
for i2 = 1:length(x)
X(i1) = X(i1)+x(i2)*exp(-i*w(i1)*n(i2));
end
end

n_re = -80:80;
x_re = zeros(1,length(n_re));

for i1 = 1:M
for i2 = 1:length(x_re)
x_re(i2) = x_re(i2)+1/(2*pi)*X(i1)*exp(-
i*w(i1)*n_re(i2))*dw;
end
end
figure(1);
subplot(3,1,f),stem(n_re,x_re), title(f,'reconstructed
signal');
end
Result:
For the range whose length is less than n_re, the difference is filled with
zero. For the other range, the graph continues as a repetition of the sinc
function.
Explanation:
If the range of n is less than n_re, then the gap between the range is filled
with zero between two consecutive reconstructed waves.

Question:
If you reconstruct the signal in n_re=-240:240, why do you get a repetition of
the aperiodic signal?

Code:
clc;
close all;
clear all;
f_c = 1/8;
n = -40:40;
x = sinc(2*f_c*n);

M=101;
w = linspace(-pi,pi,M);
dw = w(2)-w(1);
X = zeros(1,M);

for i1 = 1:M
for i2 = 1:length(x)
X(i1) = X(i1)+x(i2)*exp(-i*w(i1)*n(i2));
end
end

a = [ -240];
b = [ 240];

for f = 1:length(a)
n_re = a(f):b(f);
x_re = zeros(1,length(n_re));

for i1 = 1:M
for i2 = 1:length(x_re)
x_re(i2) = x_re(i2)+1/(2*pi)*X(i1)*exp(-
i*w(i1)*n_re(i2))*dw;
end
end

figure(2),subplot(length(a),1,f), stem(n_re,x_re),
title(f,'reconstructed signaL');
end

Result:
The graph shows the repetition of the aperiodic signal.

Explanation:
When the aperiodic signal is Fourier transformed for a certain range, the
phase of the signal becomes periodic as if that portion of the aperiodic
signal existed as a periodic signal. So, when the signal is reconstructed
from the phase of the Fourier transform, we get a repetition of the
aperiodic signal.

Question:
Consider the continuous-time signal shown in figure 12. Sample it and
then find out the Fourier transform in terms of the analogue frequency.
Code:
clc;
close all;
clear all;

T = 0.01;
dt = 0.0001;
t = -0.005:dt:0.005;
x = sin(2*pi*(t/T));
n =t/dt;
figure(1), stem(n,x,'.k'),title('Discrete time signal');

M=101;
w = linspace(-pi,pi,M);
dw = w(2)-w(1);
X = zeros(1,M);

for i1 = 1:M
for i2 = 1:length(x)
X(i1) = X(i1)+x(i2)*exp(-i*w(i1)*n(i2));
end
end

n_re = -100:100;
x_re = zeros(1,length(n_re));

for i1 = 1:M
for i2 = 1:length(x_re)
x_re(i2) = x_re(i2)+1/(2*pi)*X(i1)*exp(-
i*w(i1)*n_re(i2))*dw;
end
end

figure(2), plot(w,abs(X)), title('Frequency Spectra');


figure (3), stem(n_re,x_re), title('reconstructed signaL');
Result:
The Fourier Transform of the given signal is plotted in the graph along
with its sampling.

Question:
Say, x1=rand (1,51) % random signal
x2(n)=sin(2л (1000) n/10000); % 1000 Hz signal
Find PSD of x(n) where x(n) = x1(n) + x2(n).

Code:
clc;
clear all;
close all;

x1 = rand(1,51);
n= -25:25;
x2 = sin(2*pi*n/10);
x = x2 + x1;
plot(n,x)

N = length(x);
xdft = fft(x);
xdft = xdft(1:N);
psdx = (1/(2*pi*N)) * abs(xdft).^2;
psdx(2:end-1) = 2*psdx(2:end-1);
freq = -pi:(2*pi)/(N-1):pi;
length(freq)

figure()
plot(freq/pi,10*log10(psdx))
title('Periodogram Using FFT')
Result:
The power spectral density vs n graph is plotted.

Question:
F s 0.5
w x =2 π ( + )
4 N

x(n) = cos (wx n)


Perform windowing (Hann) and again plot the data sequence and magnitude
spectrum.

Code:
clear all;
close all;

fs = 8000;
ts = 1/fs;
N = [ 150];
w0 = 2*pi*((fs/4)+0.5/N);

for i = 1: length(N)
t = (1:N(i))*ts;
x = cos (w0*t);
f = (0:length(t)-1)/(length(t)*ts)-1/(2*ts);
figure(1)
plot(t,x)

L = [ 50 16 37 32 64];
for h = 1:length(L)
windowLength = L(h);
w = hann(windowLength);
index = length(t) - L(h);
windowSequence = [zeros(1,index-1) w' zeros(1,length(x)-index-
length(w)+1)];
winOut = x.*windowSequence;

figure(2)
subplot(5,2,2*h-1),stem(t,winOut),title(L(h),'x[n] vs time'),
subplot(5,2,2*h), stem(f,abs(fftshift(fft(winOut)))),
title(L(h),'|X| vs frequency')
end
end
Result:
The data sequence and magnitude spectrum of the given signal after windowing
is plotted.
Explanation:
The given sinusoid signal does not have an integer number of cycles within N.
So, the frequency of the sinusoid is no longer an integer multiple of the bin size
(1/N). Hence, it is not localized in the DFT bins. Power, which was supposed to
be confined to a single frequency component, spreads into the entire
frequency range even for 2n multiple.

Question:
64 samples 125µs apart are available for x(t) = sin (2лf 1t) + 0.05sin (2 лf2t),
where f1 and f2 are 1062.5 Hz and 1625 Hz respectively.
a) Plot the data sequence and magnitude spectrum. (use stem () )
b) Perform windowing (Hann) and again plot the data sequence and
magnitude spectrum. Comment on your result.
Code:
clc;
clear all;
close all;
f1 = 1062.5;
f2 = 1625;
fs = 8000;
ts = 1/fs;
N = [ 64];

for i = 1: length(N)
t = (1:N(i))*ts;
x = sin (2*pi*f1*t) + 0.05*sin(2*pi *f2*t);
f = (0:length(t)-1)/(length(t)*ts)-1/(2*ts);
windowLength = 56;
w = hann(windowLength);
index = 8;

windowSequence = [zeros(1,index-1) w' zeros(1,length(x)-


index-length(w)+1)];
winOut = x.*windowSequence;

subplot(211),stem(t,winOut),title(N(i),'time vs
x[n]'),xlabel('Time(seconds)'),ylabel('x[n]')
subplot(212), stem(f,abs(fftshift(fft(winOut)))),
title(N(i),'frequency vs |X|'),xlabel('frequency'),ylabel('|
X|')
figure()
subplot(211),stem(t,x),title(N(i),'time vs
x[n]'),xlabel('Time(seconds)'),ylabel('x[n]')
subplot(212), stem(f,abs(fftshift(fft(x)))),
title(N(i),'frequency vs |X|'),xlabel('frequency'),ylabel('|
X|')
end
Result:
Data sequence and magnitude spectrum for the given sequence and after
windowing are plotted in the graph.

Explanation:
After windowing, a certain part of the given signal is allowed. The rest of the
parts are eliminated i.e., their value is reduced to zero. For the magnitude
spectrum, the weak components are not masked rather the strong spectral
component at a certain frequency becomes distinct.

Question:
“Ability to resolve closely spaced spectral lines of different frequencies is
limited by the window main lobe width” --- Justify this statement by proper
explanation and results of the following problem.
Let, x(n) = cos won + cos w1n + cos w2n where wo = 0.2л , w1 = 0.22л and w2 =
0.6л. Consider, window lengths L = 25,50,100. Comment on your result.

Code:

clc;
clear all;
close all;
w0 = 0.2*pi;
w1 = 0.22*pi;
w2 = 0.6*pi;
fs = 8000;
ts = 1/fs;
N = [ 150];
for i = 1: length(N)
t = (1:N(i))*ts;
x = cos (2*w0*t) + cos (2*w1*t)+cos (2*w2*t);
f = (0:length(t)-1)/(length(t)*ts)-1/(2*ts);

L = [ 25 50 100];
for h = 1:length(L)
windowLength = L(h);
w = hann(windowLength);
index = length(t) - L(h);
windowSequence = [zeros(1,index-1) w' zeros(1,length(x)-index-
length(w)+1)];
winOut = x.*windowSequence;

subplot(3,1,h), stem(f,abs(fftshift(fft(winOut)))),
title(L(h),'frequency vs |X|')
end
end
Result:
The magnitude spectrum for different window main lobe widths is plotted.
Explanation:
It is seen that, for increasing window width, the strong component in the spectra
becomes more distinct. For low width, there is leakage from the main spectral
peak which causes the weak sinusoid to be masked. So, the mentioned statement
is true.

Discussion:
There were errors in the calculation and experiment since approximation was
used to determine the final output. But the errors were negligible.

You might also like