0% found this document useful (0 votes)
13 views31 pages

DSP Report

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 31

BANGLADESH UNIVERSITY OF ENGINEERING AND

TECHNOLOGY

Department of Electrical and Electronic


Engineering

Course No. : EEE 312


Course Title: Digital Signal Processing I Laboratory

Experiment No: 03 and 04

Name of the Experiment: Z-transform and Its Application and Frequency domain
analysis of DT signal and systems
Date of Performance: __/01/2024
Date of Submission: 03/03/2024

Submitted by:

Name: Md: Refat Khan


ID: 2006131

Level: 3 Term: 1
Department: EEE
Section: C1
Question 1

Code

%% C4

a=[1 -1/2];
b=[1 1/3];
n=0:10;
x1=(n==0);
y1=filter(b,a,x1);
figure(4)
stem(n,y1),title("Impulse Response");
figure(5)
zplane(b,a);

output:
Input Code: C5
clc;
clear;
close all;

b=[3 -4];
a=[1 -3.5 1.5];
figure(1)
zplane(b,a)
[r p c]=residuez(b,a)

Outputs:
For the given system function, poles were found to be and 3. ROC and h[n]for
different condition are shown.

Causal condition:

h[n] = (( )n + 3n)u[n] ROC : |z| > 3


Anti-causal condition:
h ROC: |z| < 0.5
Non-causal condition:
h ROC:0.5 < |z| < 3
The system is stable only for the non-causal condition, since for this case, the ROC
includes the unit circle.
Input Code: C6

clc;
clear;
close all;

b=[2 16 44 56 32];
a=[3 3 -15 18 -12];
zplane(b,a)
[r p c]=residuez(b,a)

Outputs:
r=

-0.0177 + 0.0000i

9.4914 + 0.0000i

-3.0702 + 2.3398i

-3.0702 - 2.3398i

p=

-3.2361 + 0.0000i

1.2361 + 0.0000i

0.5000 + 0.8660i

0.5000 - 0.8660i

c=

-2.6667
the factored form the system function is

ROC for different cases are

Causal: |z| > 3.236


Anti-causal: |z| < 1
Non-causal: 1 < |z| < 1.236 or 1.236 < |z| < 3.236
Input Code: C7

clc;
clear;
close all;

a=[1 -5/6 1/6];


b=[1];
n=0:10;
x=[1 -1/3 0 0 0 0 0 0 0 0 0];
y=filter(b,a,x);
figure(1)
zplane(b,a)
figure(2)
subplot(211);
stem(n,x);title('input');
subplot(212);
stem(n,y);title('Output Response')

%Reconstruction from output response


h = zeros(1,length(n));
h(1) = y(1) ;
for i=2:10
h(i) = y(i) + 1/3*h(i-1);
end
figure(3)
stem(n,h);
title("Reconstructed System Response");
Outputs:
It is possible to reconstruct the system from the output response.
Input Code: C8

clc;
clear;
close all;
b=[1];
a=[1 -2.5 1];
[r p c]=residuez(b,a)

Outputs:
r=

1.3333

-0.3333

p=

2.0000

0.5000

c=

[]

Observations:
We can see that the system has two poles in |𝑧| = 2 and |𝑧| = 0.5
So, for the system being causal and stable, it must have no poles in the region |𝑧| > 1.
We can achieve that by cascading a system with H1 = 1 – 2z-1
For the system being anti-causal and stable, it must have no poles in the region |𝑧| < 1.
We can also achieve this by cascading a system with H2 = 1 – 0.5z-1
Making the system stable while it is
(a) causal (b) anti-causal
clc
clear
clear all
b = [1];
a = [1 -2.5 1];
n1 = conv(b, [1 -2]);
n2 = conv(b, [1 -0.5]);
subplot(3,2,1),zplane(b,a),title("The Main System");
subplot(3,2,2),stem(impz(b,a)),title("Impulse Response for The Main System");
subplot(3,2,3),zplane(n1,a),title('System with Causal Stability');
subplot(3,2,4),stem(impz(n1,a)),title("Impulse Response for System with Causal
Stability");
subplot(3,2,5),zplane(n2,a),title("System with Anti-Causal Stability");
subplot(3,2,6),stem(impz(n2,a)),title("Impulse Response for System with AntiCausal
Stability");
Question 2:
Generalized code for schur cohn stability test:
function isStable = schurCohnStabilityTest(coefficients)
% Input:
% coefficients: Coefficients of the characteristic polynomial in descending
order (highest degree to lowest)

% Check if the input coefficients are provided


if nargin < 1
error('Please provide the coefficients of the characteristic polynomial.');
end

% Ensure the coefficients are a row vector


coefficients = coefficients(:).';

% Check if the first coefficient is non-zero (leading coefficient must be non-


zero for stability)
if coefficients(1) == 0
error('The leading coefficient of the polynomial must be non-zero for the
Schur-Cohn Stability Test.');
end

% Initialize the first row of the Schur matrix


schurMatrix = diag(coefficients);

% Initialize a flag for stability


isStable = true;

% Perform the Schur-Cohn Stability Test


for i = 1:length(coefficients)-1
% Construct the next row of the Schur matrix
nextRow = [0, fliplr(coefficients)];
nextRow = nextRow(1:end-1);

% Update the Schur matrix


schurMatrix = [nextRow; schurMatrix];

% Check the determinant of the submatrix


submatrix = schurMatrix(1:i+1, 1:i+1);
determinant = det(submatrix);

% If determinant is non-positive, the system is unstable


if determinant <= 0
isStable = false;
break;
end

% Update coefficients for the next iteration


coefficients = nextRow;
end

% Display the result


if isStable
disp('The system is stable according to the Schur-Cohn Stability Test.');
else
disp('The system is unstable according to the Schur-Cohn Stability Test.');
end
end

usring the function :


clc
clear all
close all

%% Example: Check stability for the polynomial 1 - 2.5z^(-1) + z^(-2)


coefficients = [1, -2.5, 1];

% Call the function


isStable = schurCohnStabilityTest(coefficients);

% Display the result


if isStable
disp('The system is stable according to the Schur-Cohn Stability Test.');
else
disp('The system is unstable according to the Schur-Cohn Stability Test.');
end

result :

The system is unstable according to the Schur-Cohn Stability Test.

The system is unstable according to the Schur-Cohn Stability Test.

>>

Question : 3

Problem 2.1

Code :
%% Problem for Example 2
clc
clear
close all
%% GENERATING A SINC PULSE
f_c = 1/8; %DEFINING FREQUENCY VARIABLE FOR SINC PULSE
n = -131:131; %DEFINING THE INDEX FOR SINC PULSE
x = sinc(2*f_c*n);
%END OF SINC PULSE
figure(1);stem(n, x, '.k');
xlabel('n'),ylabel('x(n)'),title('Discrete time signal');
M = 101; %NUMBER OF POINTS IN DIGITAL FREQUENCY GRID
w = linspace(-0,2*pi,M); %DEFINING THE DIGITAL FREQUENCY GRID
dw = w(2) - w(1);%RESOLUTION OF DIGITAL FREQUENCY
X = zeros(1,M);%INITIALIZING THE DTFT OF x(n)
for i1 = 1:M
for i2 = 1:length(x)
X(i1) = X(i1) + x(i2)*exp(-i*w(i1)*n(i2));
end
end
figure(2), plot(w,abs(X), 'k', 'linewidth', 2);
xlabel('Frequency (rad/sec)'), ylabel('X(w)');
title('FREQUENCY SPECTRA');
%RECONSTRUCTION OF SIGNAL
n_re = -131:131;
x_re = zeros(1,length(n_re)); %INITIALIZING RECONSTRUCTED SIGNAL
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(3), stem(n_re, x_re,'.k', 'linewidth',1);
xlabel('t'), ylabel('x_{reconstructed}');
title('Reconstructed signal');

output :
Example 2.2

Code :
%% Problem for Example 2
% problem 3
clc
clear
close all
%% GENERATING A SINC PULSE
f_c = 1/8; %DEFINING FREQUENCY VARIABLE FOR SINC PULSE
n = -131:131; %DEFINING THE INDEX FOR SINC PULSE
x = sinc(2*f_c*n);
%END OF SINC PULSE
figure(1);stem(n, x, '.k');
xlabel('n'),ylabel('x(n)'),title('Discrete time signal');
M = 101; %NUMBER OF POINTS IN DIGITAL FREQUENCY GRID
w = linspace(-2*pi,2*pi,M); %DEFINING THE DIGITAL FREQUENCY GRID
dw = w(2) - w(1);%RESOLUTION OF DIGITAL FREQUENCY
X = zeros(1,M);%INITIALIZING THE DTFT OF x(n)
for i1 = 1:M
for i2 = 1:length(x)
X(i1) = X(i1) + x(i2)*exp(-i*w(i1)*n(i2));
end
end
figure(2), plot(w,abs(X), 'k', 'linewidth', 2);
xlabel('Frequency (rad/sec)'), ylabel('X(w)');
title('FREQUENCY SPECTRA');
%RECONSTRUCTION OF SIGNAL
n_re = -131:131;
x_re = zeros(1,length(n_re)); %INITIALIZING RECONSTRUCTED SIGNAL
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(3), stem(n_re, x_re,'.k', 'linewidth',1);
xlabel('t'), ylabel('x_{reconstructed}');
title('Reconstructed signal');

output:
Example2.3:
Problem 03: Verify the effect of changing index (n) to -40:120, -40:80. In each case observe the
reconstructed signal.

Matlab Code:
%% Problem for Example 2
% problem 3
clc
clear
close all
%% GENERATING A SINC PULSE
f_c = 1/8; %DEFINING FREQUENCY VARIABLE FOR SINC PULSE
n = -40:120; %DEFINING THE INDEX FOR SINC PULSE
x = sinc(2*f_c*n);
%END OF SINC PULSE
figure(1);stem(n, x, '.k');
xlabel('n'),ylabel('x(n)'),title('Discrete time signal');
M = 101; %NUMBER OF POINTS IN DIGITAL FREQUENCY GRID
w = linspace(-2*pi,2*pi,M); %DEFINING THE DIGITAL FREQUENCY GRID
dw = w(2) - w(1);%RESOLUTION OF DIGITAL FREQUENCY
X = zeros(1,M);%INITIALIZING THE DTFT OF x(n)
for i1 = 1:M
for i2 = 1:length(x)
X(i1) = X(i1) + x(i2)*exp(-i*w(i1)*n(i2));
end
end
figure(2), plot(w,abs(X), 'k', 'linewidth', 2);
xlabel('Frequency (rad/sec)'), ylabel('X(w)');
title('FREQUENCY SPECTRA');
%RECONSTRUCTION OF SIGNAL
n_re = -40:40;
x_re = zeros(1,length(n_re)); %INITIALIZING RECONSTRUCTED SIGNAL
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(3), stem(n_re, x_re,'.k', 'linewidth',1);
xlabel('t'), ylabel('x_{reconstructed}');
title('Reconstructed signal');

output
Example 2.4

Code
%% Problem for Example 2
% problem 3
clc
clear
close all
%% GENERATING A SINC PULSE
f_c = 1/8; %DEFINING FREQUENCY VARIABLE FOR SINC PULSE
n = -131:131; %DEFINING THE INDEX FOR SINC PULSE
x = sinc(2*f_c*n);
%END OF SINC PULSE
figure(1);stem(n, x, '.k');
xlabel('n'),ylabel('x(n)'),title('Discrete time signal');
M = 101; %NUMBER OF POINTS IN DIGITAL FREQUENCY GRID
w = linspace(-2*pi,2*pi,M); %DEFINING THE DIGITAL FREQUENCY GRID
dw = w(2) - w(1);%RESOLUTION OF DIGITAL FREQUENCY
X = zeros(1,M);%INITIALIZING THE DTFT OF x(n)
for i1 = 1:M
for i2 = 1:length(x)
X(i1) = X(i1) + x(i2)*exp(-i*w(i1)*n(i2));
end
end
figure(2), plot(w,abs(X), 'k', 'linewidth', 2);
xlabel('Frequency (rad/sec)'), ylabel('X(w)');
title('FREQUENCY SPECTRA');
%RECONSTRUCTION OF SIGNAL
n_re = -240:240;
x_re = zeros(1,length(n_re)); %INITIALIZING RECONSTRUCTED SIGNAL
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(3), stem(n_re, x_re,'.k', 'linewidth',1);
xlabel('t'), ylabel('x_{reconstructed}');
title('Reconstructed signal');

output :
Here, again the number of points is taken more than it’s needed. We had built our code in this sense that
the period N is infinity, thus the signal is aperiodic. However, our code doesn’t know that and works as if
N is a finite value. Thus, values beyond N will result in periodic signal.

Example 2.5

Code:
%% Problem for Example 2
% problem 5
clc
clear
close all
%% Constructing the given signal
n=-0.02:0.0005:0.02;
x=zeros(1,length(n));
for i5=1:length(n)
if n(i5)<=0.005 && n(i5)>=-0.005
x(i5)=sin(2*pi*n(i5)/0.010);
end
end
figure(1)
subplot(211);plot(n, x, 'k', 'linewidth', 2)
xlabel('t'), ylabel('x(n)')
title('Continous time signal');
subplot(212);stem(n, x, '.k', 'linewidth', 2)
xlabel('n'), ylabel('x(n)')
title('Discrete time signal');
%% Fourier
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(-1i*w(i1)*n(i2));
end
end
figure(2)
subplot(211),plot(w,abs(X), 'k', 'linewidth', 2);
xlabel('Frequency (rad/sec)');ylabel('|X(w)|')
title('Magnitude Spectrum');
subplot(212),plot(w,angle(X)*180/pi, 'k', 'linewidth', 2);
xlabel('Frequency (rad/sec)');ylabel('angle(X(w))')
title('Phase Spectrum');
figure(3)
plot(w,X, 'k', 'linewidth', 2);
xlabel('Frequency (rad/sec)');ylabel('X(w)')
title('X(w)');
output :
Problem 06:

Verify the duality property of DTFT with rectangular pulse and sinc function.

Code
clc
clear
close all
%% RECTANGULAR PULSE
n = -40:40; %DEFINING THE INDEX FOR RECTANGULAR PULSE
x = [zeros(1,20) ones(1,41) zeros(1,20)];
%END OF RECTANGULAR PULSE
figure(1);subplot(211); stem(n, x, '.k');
xlabel('n'),ylabel('x(n)'),title('Discrete time signal');grid on;
M = 1001; %NUMBER OF POINTS IN DIGITAL FREQUENCY GRID
w = linspace(-pi,pi,M); %DEFINING THE DIGITAL FREQUENCY GRID
dw = w(2) - w(1);%RESOLUTION OF DIGITAL FREQUENCY
X = zeros(1,M);%INITIALIZING THE DTFT OF x(n)
for i1 = 1:M
for i2 = 1:length(x)
X(i1) = X(i1) + x(i2)*exp(-i*w(i1)*n(i2));
end
end
subplot(212), plot(w,X, 'k', 'linewidth', 2);
xlabel('Frequency (rad/sec)'), ylabel('X(w)');
title('FREQUENCY SPECTRA');grid on;

output
Sinc pulse

Matlab Code:
clc
clear
close all
%% SINC PULSE
f_c = 1/8; %DEFINING FREQUENCY VARIABLE FOR SINC PULSE
n = -40:40; %DEFINING THE INDEX FOR SINC PULSE
x = sinc(2*f_c*n);
%END OF SINC PULSE
figure(2);
subplot(211);
stem(n, x, '.k');
xlabel('n');
ylabel('x(n)');
title('Discrete time signal');
M = 10001; %NUMBER OF POINTS IN DIGITAL FREQUENCY GRID
w = linspace(-pi,pi,M); %DEFINING THE DIGITAL FREQUENCY GRID
dw = w(2) - w(1);%RESOLUTION OF DIGITAL FREQUENCY
X = zeros(1,M);%INITIALIZING THE DTFT OF x(n)
for i1 = 1:M
for i2 = 1:length(x)
X(i1) = X(i1) + x(i2)*exp(-i*w(i1)*n(i2));
end
end
subplot(212);
plot(w,abs(X), 'k', 'linewidth', 2);
xlabel('Frequency (rad/sec)');
ylabel('X(w)');
title('FREQUENCY SPECTRA');

output

You might also like