Professional Documents
Culture Documents
Experiment No.: 04
Experiment Name: Frequency Domain Analysis of DT Signals and
Systems.
Submitted to
Submitted by
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
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;
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:
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:
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;
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
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;
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;
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
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
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;
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.