DSP Lab Experiment 4 UET Lahore.

You might also like

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

Name: Rooshan Khan

Roll Number: 2021-EE-067


Section: B
Teacher: Sir Khalid Butt
Course: Digital Signal Processing Lab

Functions from previous Lab experiments:


function [y, ny] = conv_m(x, nx, h, nh)
% Modified convolution routine for signal processing
% --------------------------------------------------
% [y, ny] = conv_m(x, nx, h, nh)
% [y, ny] = result of convolution
% [x, nx] = first signal
% [h, nh] = second signal
%
nyb = nx(1) + nh(1);
nye = nx(length(x)) + nh(length(h));
ny = nyb:nye;
%
y = conv(x,h);
end
function [y, ny] = my_conv(x, nx, h, nh)
% Implements y[n] = x1[n] ∗ x2[n]
% ------------------------------------
% [y, ny] = my_conv(x1, n1, x2, n2)
% [y, ny] = result of convolution
% [x, nx] = first signal
% [h, nh] = second signal
%
% Time indices for the convolution of x1 and x2
nyb = nx(1) + nh(1);
nye = nx(length(x)) + nh(length(h));
ny = nyb:nye;
Prev_Product=[0];
nP_P=[0];

for k = nx
[H, nH] = sig_shift(h, nh, k);
if k<nx(1)||k>nx(length(nx))
c=0;
else
c=x(k+(1-nx(1)));
end
Product=c*H;
[Product_Sum, nPS] = sig_add(Prev_Product, nP_P, Product, nH);
Prev_Product=Product_Sum;
nP_P=nPS;
end
y =Prev_Product;
ny=nP_P;
%
% -------------------------------------------------------------------
% Write your code here to generate y from x1, x2.
% Use the functions sig_fold.m, sig_shift.m and sig_mult.m to perform
% the convolution
% -------------------------------------------------------------------
end
function [y, n] = sig_shift(x, m, k)
% Implements y[n] = x [n–k]
% -----------------------------
% [y, n] = sig_shift(x, m, k)
%
n= m + k; y = x;
end
function [x, n] = step_seq(n0, n1, n2)
n = n1:n2; x = (n-n0) >= 0;
end
function [y, n] = sig_fold(x, n)
% Implements y[n] = x[-n]
% -------------------------
% [y, n] = sig_fold(x, n)
%
y = fliplr(x); n = -fliplr(n);
end
function [y, n] = sig_add(x1, n1, x2, n2)
n = min(min(n1), min(n2)):max(max(n1), max(n2));
y1 = zeros(1, length(n));
y2 = zeros(1, length(n));
for i = 1:length(n)
for j = 1:length(n1)
if n(i) == n1(j)
y1(i) = x1(j);
end
end
for k = 1:length(n2)
if n(i) == n2(k)
y2(i) = x2(k);
end
end
end
y = y1 + y2;
end
function [y, n] = sig_mul(x1, n1, x2, n2)
n = min(min(n1), min(n2)):max(max(n1), max(n2));
y1 = zeros(1, length(n));
y2 = zeros(1, length(n));
for i = 1:length(n)
for j = 1:length(n1)
if n(i) == n1(j)
y1(i) = x1(j);
end
end
for k = 1:length(n2)
if n(i) == n2(k)
y2(i) = x2(k);
end
end
end
y = y1 .* y2;
end

Task1:
Code:
[step1, ns1] = step_seq(-3, 0, 10);
[step2, ns2] = step_seq(5, 0, 10);
[x, nx] = sig_add(step1, ns1,-1* step2, ns2);
[step3, ns3] = step_seq(2, 0, 10);
ne=0:10;
xe=(0.2).^ne;
[h, nh] = sig_mul(step3, ns3, xe, ne);
[y1, ny1] = conv_m(x, nx, h, nh);
[y2, ny2] = conv_m(h, nh, x, nx);
% Plot signals x[n], h[n], y1[n], and y2[n]
figure;
subplot(2, 2, 1); % 1st subplot
stem(nx, x, 'r', 'LineWidth', 1.5, 'Marker', 's');
title('x[n]');
xlabel('n');
ylabel('Amplitude');
subplot(2, 2, 2); % 2nd subplot
stem(nh, h, 'b', 'LineWidth', 1.5, 'Marker', 's');
title('h[n]');
xlabel('n');
ylabel('Amplitude');
subplot(2, 2, 3); % 3rd subplot
stem(ny1, y1, 'g', 'LineWidth', 1.5, 'Marker', 's');
title('y1[n]');
xlabel('n');
ylabel('Amplitude');
subplot(2, 2, 4); % 4th subplot
stem(ny2, y2, 'm', 'LineWidth', 1.5, 'Marker', 's');
title('y2[n]');
xlabel('n');
ylabel('Amplitude');
%%
Code Description:
I have used the functions from previous lab experiments. I was asked to verify the commutative
property of convolution for LTI systems. I called my conv_m function twice. For the two function
calls the order of the signals was different.
Figure:

Figure Description:
The two resulting signals y1[n] and y2[n] are the same. We have successfully verified the
commutative property of convolution for LTI systems.

Task2:
Code:
[step1, ns1] = step_seq(-3, 0, 10);
[step2, ns2] = step_seq(5, 0, 10);
[x, nx] = sig_add(step1, ns1,-1* step2, ns2);
[step3, ns3] = step_seq(2, 0, 10);
ne=0:10;
xe=(0.2).^ne;
[h1, nh1] = sig_mul(step3, ns3, xe, ne);
[step4, ns4] = step_seq(1, 0, 10);
[step5, ns5] = step_seq(-3, 0, 10);
[h2, nh2] = sig_add(step4, ns4,-1* step5, ns5);
[B1, nB1] = conv_m(h1, nh1, h2, nh2);
[y1, ny1] = conv_m(x, nx, B1, nB1);
[B2, nB2] = conv_m(x, nx, h1, nh1);
[y2, ny2] = conv_m(B2, nB2, h2, nh2);
% Plot signals x[n], h[n], y1[n], and y2[n]
figure;
subplot(1, 2, 1); % 1st subplot
stem(ny1, y1, 'g', 'LineWidth', 1.5, 'Marker', 's');
title('y1[n]');
xlabel('n');
ylabel('Amplitude');
subplot(1, 2, 2); % 2nd subplot
stem(ny2, y2, 'm', 'LineWidth', 1.5, 'Marker', 's');
title('y2[n]');
xlabel('n');
ylabel('Amplitude');

Code Description:
This Matlab code generates and processes various signals. It creates step sequences and
performs addition and multiplication operations on them. Then, it computes the responses
y1[n] = x[n] ∗ (h1[n] ∗ h2[n]) and y2[n] = (x[n] ∗ h1[n]) ∗ h2[n] using the function conv_m.m..

Finally, it plots the resulting signals, namely y1[n] and y2[n].

Figure:

Figure Description:
We were asked to write a MATLAB script file to verify the associative law for the LTI systems

having impulse response h1[n] =0.2nu[n], 0 ≤ n ≤ 10 and h2[n] = u[n − 1] − u[n + 3].
Both figures are same so the the law has been verified.
Block Diagram:
Task3:
Code:
% Impulse Response of Task1
[step3, ns3] = step_seq(2, 0, 10);
ne=0:10;
xe=(0.2).^ne;
[h, nh] = sig_mul(step3, ns3, xe, ne);
% Impulse Response 1 of Task2
[step3, ns3] = step_seq(2, -10, 10);
ne=0:10;
xe=(0.2).^ne;
[h1, nh1] = sig_mul(step3, ns3, xe, ne);
% Impulse Response 2 of Task2
[step4, ns4] = step_seq(1, -10, 10);
[step5, ns5] = step_seq(-3, -10, 10);
[h2, nh2] = sig_add(step4, ns4,-1* step5, ns5);
disp("Impulse Response of Task1");
m = Causality_Checker(h, nh);
disp(m);
disp("Impulse Response 1 of Task2");
m1 = Causality_Checker(h1, nh1);
disp(m1);
disp("Impulse Response 2 of Task2");
m2 = Causality_Checker(h2, nh2);
disp(m2);
function [x, n] = imp_seq(n0, n1, n2)
% Generates x(n) = delta(n-n0); n1 <= n <= n2
% [x, n] = imp_seq(n0, n1, n2)
%
n = n1:n2;
x = (n-n0) == 0;
end
function b = Causality_Checker(h, n)
b=1;
for i=1:length(n)
if (n(i)<0)
if (h(i)~=0)
b=0;
end
end
end
end

Code Description:
We have used impulse responses from Tasks 1 and 2. This code checks for causality using a
custom function Causality_Checker, which iterates through the sequence and verifies if any
values exist for negative time indices. Finally, it displays the causality check results for each
impulse response.
Output:

Output Description:
The system in Task1 is causal. The first system in Task2 is causal and the second system in
Task2 is not causal.

Task4:
Code:
% Impulse Response of Task1
[step3, ns3] = step_seq(2, 0, 10);
ne=0:10;
xe=(0.2).^ne;
[h, nh] = sig_mul(step3, ns3, xe, ne);
% Impulse Response 1 of Task2
[step3, ns3] = step_seq(2, -10, 10);
ne=0:10;
xe=(0.2).^ne;
[h1, nh1] = sig_mul(step3, ns3, xe, ne);
% Impulse Response 2 of Task2
[step4, ns4] = step_seq(1, -10, 10);
[step5, ns5] = step_seq(-3, -10, 10);
[h2, nh2] = sig_add(step4, ns4,-1* step5, ns5);
disp("Impulse Response of Task1");
m = Stability_Checker(h);
disp(m);
disp("Impulse Response 1 of Task2");
m1 = Stability_Checker(h1);
disp(m1);
disp("Impulse Response 2 of Task2");
m2 = Stability_Checker(h2);
disp(m2);

function b = Stability_Checker(h)
b=0;
if (sum(abs(h))<Inf)
b=1;
end
end

Code Description:
We have used impulse responses from Tasks 1 and 2. This code checks for stability using a
custom function Stability_Checker, which examines the sum of absolute values of the impulse
response. Finally, it displays the stability check results for each impulse response.
Output:

Output Description:
The system in Task1 and both the systems in Task2 are stable because their impulse
responses are absolutely summable.

Task5:
Code:
x=[-2,-1,0,1,2];
nx=[0,1,2,3,4];
[x1, nx1] = sig_shift(x, nx, 5);
w= randn(1,length(x));
nw=nx1;
[y, ny] = sig_add(x1, nx1, w, nw);
figure;
subplot(2, 2, 1); % 1st subplot
stem(nx, x, 'r', 'LineWidth', 1.5, 'Marker', 's');
title('x[n]');
xlabel('n');
ylabel('Amplitude');
subplot(2, 2, 2); % 2nd subplot
stem(nw, w, 'b', 'LineWidth', 1.5, 'Marker', 's');
title('w[n]');
xlabel('n');
ylabel('Amplitude');
subplot(2, 2, 3); % 3rd subplot
stem(ny, y, 'g', 'LineWidth', 1.5, 'Marker', 's');
title('y[n]');
xlabel('n');
ylabel('Amplitude');
disp("From the given function cross-correlation between vectors x and y
is:");
xcorr(x,y)
disp("From the function I wrote the cross-correlation between vectors x and y
is:");
[rxy, l] = my_corr(x, nx, y, ny)
function [rxy, l] = my_corr(x, nx, y, ny)
% Implements y[n] = x1[n] ∗ x2[-n]
% ----------------------------------
% [rxy, l] = my_corr (x, nx, y, ny)
% [rxy, l] = result of correlation
% [x, nx] = first signal
% [y, ny] = second signal
[y1, ny1] = sig_fold(y, ny);
[rxy, l] = conv_m(x, nx, y1, ny1);
% -------------------------------------------------
% Write your code here to generate rxy from x and w
% -------------------------------------------------
end

Code Description:
This MATLAB code generates two signals, `x` and `w`, and adds them together to produce `y`.
It then plots these signals along with their corresponding indices. Additionally, it calculates and
displays the cross-correlation between vectors `x` and `y` using both MATLAB's built-in function
`xcorr` and a custom function `my_corr`. The `my_corr` function implements cross-correlation by
first folding one of the signals and then performing convolution.
Figure and Output:
Figure Description and Output Description:
From the output we can see that the cross-correlation calculated using my function is the same
as the one calculated using the given function.

Exercise_Q1:
Code:
% x[n] = 3u[n − 5] − 3u[n − 25]: a rectangular pulse
[U1, nU1] = step_seq(5, 0, 60);
[U2, nU2] = step_seq(25, 0, 60);
[x1, nx1] = sig_add(3*U1, nU1, -3*U2, nU2);
% x[n] = n(u[n] − u[n − 15]) + (30 − n)(u[n − 15] − u[n − 30]): a triangular pulse
[U3, nU3] = step_seq(0, 0, 30);
[U4, nU4] = step_seq(15, 0, 30);
[U5, nU5] = step_seq(15, 0, 30);
[U6, nU6] = step_seq(30, 0, 30);
[UD1, nUD1] = sig_add(U3, nU3, -1*U4, nU4);
[UD2, nUD2] = sig_add(U5, nU5, -1*U6, nU6);
nS=0:30;
S1 = nS.*(UD1);
S2= (nS-30).*UD2;
[x2, nx2] = sig_add(S1, nS, S2, nS);
% x[n] = sin (πn20) (u[n] − u[n − 100]): a sinusoidal pulse
nS3=0:100;
S3=sin((pi*nS3)/20);
[U7, nU7] = step_seq(0, 0, 110);
[U8, nU8] = step_seq(0, 90, 110);
[S4, nS4] = sig_add(U7, nU7, -1*U8, nU8);
[x3, nx3] = sig_mul(S3, nS3, S4, nS4);
b=[1];
a=[1,-1];
h1 = impz(b, a, nx1);
[y1,ny1] = conv_m(x1, nx1, h1, nx1);
h2 = impz(b, a, nx2);
[y2,ny2] = conv_m(x2, nx2, h2, nx2);
h3 = impz(b, a, nx3);
[y3,ny3] = conv_m(x3, nx3, h3, nx3);
figure;
stem(ny1, y1, 'r', 'LineWidth', 1.5, 'Marker', 's');
title('y1[n]');
xlabel('n');
ylabel('Amplitude');
figure;
stem(ny2, y2, 'b', 'LineWidth', 1.5, 'Marker', 's');
title('y2[n]');
xlabel('n');
ylabel('Amplitude');
figure;
stem(ny3, y3, 'g', 'LineWidth', 1.5, 'Marker', 's');
title('y3[n]');
xlabel('n');
ylabel('Amplitude');

Code Description:
This MATLAB code generates three different types of signals: a rectangular pulse, a triangular
pulse, and a sinusoidal pulse. It then convolves each signal with an impulse response
generated using the impz function. Finally, it plots the resulting convolved signals y1[n], y2[n],
and y3[n] for visualization.
Figures:
Figure Description:
These signals resemble their corresponding input signals.
Exercise_Q2:
Code:
% x[n] = cos(0.2πn) + 0.5 cos(0.6πn)
Num_samples=200;
nS=0:(Num_samples-1);
S1=cos(0.2*pi*nS);
S2=0.5*cos(0.6*pi*nS);
[x, nx] = sig_add(S1, nS, S2, nS);
% y[n] = x[n] − αx[n − k], α = 0.1, k = 50.
b=[1,0];
alpha=0.1;
k = 500;
array_length = k + 1;
a = zeros(1, array_length);
a(1) = 1;
a(k + 1) = -1*alpha;
[h,nh] = impz(b, a);
[y,ny] = conv_m(x, nx, h, nh);
[ryx, l] = my_corr(y, ny, x, nx);
figure;
stem(l, ryx, 'r', 'LineWidth', 1.5, 'Marker', 's');
title('ryx[l]');
xlabel('l');
ylabel('Amplitude');

Code Description:
This MATLAB code generates a signal (x[n]) consisting of a sum of two cosine functions. Then,
it creates a new signal (y[n]) by applying the difference equation ( y[n] = x[n] - (alpha)x[n - k]),
where (alpha = 0.1) and ( k = 50). It computes the impulse response (h[n]) of the system using
the `impz` function, convolves (x[n]) with (h[n]) using the `conv_m` function, and calculates the
cross-correlation between (x[n]) and (y[n]) using a custom function `my_corr`. Finally, it plots the
cross-correlation result (ryx[l]) for visualization.
Figure:

Figure Description:
Cross_correlation rxy(l) has non_zero values from n=-200 to about n=200. After this rxy(l) is
zero for all values of l. Changing k stretches or compresses rxy(l) while changing alpha changes
the shape of rxy(l).

Conclusion:
From this experiment, I learnt how to find cross correlation in matlab. I proved some convolution
properties of LTI systems. I learnt some new builtin functions in MATLAB such as impz, filter
etc.

You might also like