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

Digital Signal Processing

Lab Experiment 2

Name: Rooshan Khan

Roll Number: 2021-EE-067

Task 1:

Code:

n1 = -2:3;
x1 = [1, 2, 0, -2, 2, 1];
n2 = -5:4;
x2 = [2, 3, 0, -1, 5, 0, -2, 4, 1, 4];

[y, n, y1, y2] = sig_add(x1, n1, x2, n2);


disp([y1;y2;y; n]);
% Create subplots
subplot(3, 1, 1); % Top subplot
stem(n, y1, 'r', 'LineWidth', 1.5, 'Marker', 'o');
title('x1[n]');
xlabel('n');
ylabel('Amplitude');

subplot(3, 1, 2); % Middle subplot


stem(n, y2, 'g', 'LineWidth', 1.5, 'Marker', 's');
title('x2[n]');
xlabel('n');
ylabel('Amplitude');

subplot(3, 1, 3); % Bottom subplot


stem(n, y, 'b', 'LineWidth', 1.5, 'Marker', 'x');
title('y[n] = x1[n] + x2[n]');
xlabel('n');
ylabel('Amplitude');

function [y, n, y1, y2] = 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

Code Description:

This MATLAB code defines a signal addition function sig_add and uses it to add two discrete
signals, x1 and x2, with corresponding time indices n1 and n2. The resulting signals y1, y2,
and y are then displayed in subplots along with their respective time indices.

Figure:

Figure Description:

Top Subplot (x1[n]): Displays the signal x1 with time indices n. The stem plot uses red color, and
markers ('o') are placed at the sample points. This subplot represents the first input signal.

Middle Subplot (x2[n]): Displays the signal x2 with time indices n. The stem plot uses green color,
and markers ('s') indicate the sample points. This subplot represents the second input signal.

Bottom Subplot (y[n] = x1[n] + x2[n]): Shows the result of adding x1 and x2, represented by the
signal y, with time indices n. The stem plot uses blue color, and markers ('x') mark the sample points.
This subplot illustrates the summation of the two input signals.
Task 2:

Code:

n1 = -2:3;
x1 = [1, 2, 0, -2, 2, 1];
n2 = -5:4;
x2 = [2, 3, 0, -1, 5, 0, -2, 4, 1, 4];

[y, n, y1, y2] = sig_mul(x1, n1, x2, n2);


disp([y1;y2;y; n]);
% Create subplots
subplot(3, 1, 1); % Top subplot
stem(n, y1, 'r', 'LineWidth', 1.5, 'Marker', 'o');
title('x1[n]');
xlabel('n');
ylabel('Amplitude');

subplot(3, 1, 2); % Middle subplot


stem(n, y2, 'g', 'LineWidth', 1.5, 'Marker', 's');
title('x2[n]');
xlabel('n');
ylabel('Amplitude');

subplot(3, 1, 3); % Bottom subplot


stem(n, y, 'b', 'LineWidth', 1.5, 'Marker', 'x');
title('y[n] = x1[n]x2[n]');
xlabel('n');
ylabel('Amplitude');

function [y, n, y1, y2] = 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

Code Description:

This MATLAB code defines a signal multiplication function sig_mul and utilizes it to multiply two
discrete signals, x1 and x2, with corresponding time indices n1 and n2. The resulting signals y1, y2,
and y are then displayed in subplots along with their respective time indices.

Figure:

Figure Description:

Top Subplot (x1[n]): Displays the signal x1 with time indices n. The stem plot uses red color, and
markers ('o') are placed at the sample points. This subplot represents the first input signal.

Middle Subplot (x2[n]): Displays the signal x2 with time indices n. The stem plot uses green color,
and markers ('s') indicate the sample points. This subplot represents the second input signal.

Bottom Subplot (y[n] = x1[n] * x2[n]): Shows the result of multiplying x1 and x2, represented by the
signal y, with time indices n. The stem plot uses blue color, and markers ('x') mark the sample points.
This subplot illustrates the element-wise multiplication of the two input signals.

Task 3:
Code:

x=[-2,-1,0,1,2];
m=[-2,-1,0,1,2];
k=2;
[y1, n1] = sig_shift(x, m, k);
[y2, n2] = sig_fold(x, m);
[y5, n5, y3, y4] = sig_add(2*x, m, -3*y1, n1);
[y8, n8, y6, y7] = sig_add(y5, n5, 5*y2, n2);

% Create subplots
subplot(4, 1, 1); % Top subplot
stem(m, x, 'r', 'LineWidth', 1.5, 'Marker', 'o');
title('x[n]');
xlabel('n');
ylabel('Amplitude');

subplot(4, 1, 2); % 2nd subplot


stem(n1, y1, 'g', 'LineWidth', 1.5, 'Marker', 's');
title('x[n-2]');
xlabel('n');
ylabel('Amplitude');

subplot(4, 1, 3); % 3rd subplot


stem(n2, y2, 'b', 'LineWidth', 1.5, 'Marker', 'x');
title('x[-n]');
xlabel('n');
ylabel('Amplitude');

subplot(4, 1, 4); % Bottom subplot


stem(n2, y2, 'b', 'LineWidth', 1.5, 'Marker', 'x');
title('y[n] = 2x[n]- 3x[n-2]+ 5x[-n]');
xlabel('n');
ylabel('Amplitude');

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, y1, y2] = 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_shift(x, m, k)


% Implements y[n] = x [n–k]
% -----------------------------
% [y, n] = sig_shift(x, m, k)
%
n= m + k; y = x;
end

Code Description:

This MATLAB code performs signal operations, including shifting, folding, addition, and plotting of the
resulting signals. It uses three custom functions: sig_shift, sig_fold, and sig_add.

Figure:
Figure Description:

Top Subplot (`x[n]`): Displays the original signal `x` with time indices `m`. The stem plot uses red
color, and markers ('o') are placed at the sample points.

2nd Subplot (`x[n-2]`): Shows the result of shifting `x` to the right by 2 units. The stem plot uses
green color, and markers ('s') indicate the sample points.

3rd Subplot (`x[-n]`): Represents the folded version of the original signal `x`. The stem plot uses blue
color, and markers ('x') are placed at the sample points.

Bottom Subplot (`y[n] = 2x[n] - 3x[n-2] + 5x[-n]`): Displays the result of a combination of shifted,
folded, and scaled versions of the original signal `x`. The stem plot uses blue color, and markers ('x')
mark the sample points. This subplot illustrates the final signal obtained through the specified
operations

Task 4:

Code:

[x, n] = step_seq(0, -10, 10);

[x1, n1] = sig_fold(x, n);


[x2, n_even] = sig_add(x, n, x1, n1);
y_even=x2/2;
[x3, n_odd] = sig_add(x, n, -1*x1, n1);
y_odd=x3/2;

% Create subplots
subplot(3, 1, 1); % Top subplot
stem(n, x, 'r', 'LineWidth', 1.5, 'Marker', 'o');
title('x[n]');
xlabel('n');
ylabel('Amplitude');

subplot(3, 1, 2); % Middle subplot


stem(n_even, y_even, 'g', 'LineWidth', 1.5, 'Marker', 's');
title('x_e[n]');
xlabel('n');
ylabel('Amplitude');

subplot(3, 1, 3); % Bottom subplot


stem(n_odd, y_odd, 'b', 'LineWidth', 1.5, 'Marker', 'x');
title('x_o[n]');
xlabel('n');
ylabel('Amplitude');

function [x, n] = step_seq(n0, n1, n2)


n = n1:n2; x = (n-n0) >= 0;
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_fold(x, n)
% Implements y[n] = x[-n]
% -------------------------
% [y, n] = sig_fold(x, n)
%
y = fliplr(x); n = -fliplr(n);
end

Code Description:

This MATLAB code generates a step sequence `x[n]` using the `step_seq` function, performs signal
folding using `sig_fold`, and then separates the even and odd components of the folded signal. The
subplots visualize the original sequence, the even part (`x_e[n]`), and the odd part (`x_o[n]`).

Figure:
Figure Description:

Top Subplot (`x[n]`): Displays the step sequence `x[n]` with time indices `n`. The stem plot uses red
color, and markers ('o') are placed at the sample points.

Middle Subplot (`x_e[n]`): Represents the even part of the folded signal. The stem plot uses green
color, and markers ('s') indicate the sample points.

Bottom Subplot (`x_o[n]`): Illustrates the odd part of the folded signal. The stem plot uses blue color,
and markers ('x') are placed at the sample points. This subplot shows the decomposition of the
folded signal into even and odd components.

Task 5:

Code:

x=[-2,-1,0,1,2];
n=[-2,-1,0,1,2];
[y, n2, y1, y2] = sig_mul(x, n, x, n);

E_x=sum(y(1:length(n2)));
disp(["Signal Energy";E_x]);

function [y, n, y1, y2] = 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

Code Description:

This MATLAB code calculates the energy of a signal by computing the sum of the squared values of
the signal. It uses the sig_mul function to multiply a signal by itself, and then it sums the energy of
the resulting signal.

Output:

Output Description:

The output displays the energy of the signal, which is calculated as the sum of the squared values of
the signal obtained through element-wise multiplication. The result is shown as a single value under
the label "Signal Energy."

Exercise:

Code:

n1=-10;
n2=10;
[D1, nD1] = imp_seq(-2, n1, n2);
[D2, nD2] = imp_seq(1, n1, n2);
[D3, nD3] = imp_seq(-3, n1, n2);
[D4, nD4] = imp_seq(8, n1, n2);
[Di1, nDi1] = sig_add(3*D1, nD1, 2*D2, nD2);
[Di2, nDi2] = sig_add(Di1, nDi1, D3, nD3);
[Di3, nDi3] = sig_add(Di2, nDi2, -4*D4, nD4);
k=-20:20;
abs(k)
k2=-5:5;

x2 = ((exp(1)).^(-abs(k)/6));
x3=x2*11;
k3=-10:10;
[us1, usn1] = step_seq(-2, -10, 10);
[us2, usn2] = step_seq(1, -10, 10);
[us3, usn3] = step_seq(-3, -10, 10);
[us4, usn4] = step_seq(8, -10, 10);
[ys1, ns1] = sig_add(5*us1, usn1, -2*us2, usn2);
[ys2, ns2] = sig_add(ys1, ns1, us3, usn3);
[ys3, ns3] = sig_add(ys2, ns2, 2*us4, usn4);

k4=-10:20;
[us5, usn5] = step_seq(-10, -10, 20);
[us6, usn6] = step_seq(20, -10, 20);
e1=(exp(1)).^(0.2*k4);
[yd1, nd1] = sig_add(us5, usn5, -1*us6, usn6);
[yd2, nd2] = sig_mul(e1, k4, yd1, usn6)
nd3=-200:200;
A=10*(cos(0.495*pi*nd3)+cos(0.505*pi*nd3));
% Create subplots
subplot(5, 1, 1); % Top subplot
stem(nDi3, Di3, 'r', 'LineWidth', 1.5, 'Marker', 'o');
title('𝑥1[𝑛] = 3𝛿[𝑛 + 2] + 2𝛿[𝑛 − 1] + 𝛿[𝑛 + 3] − 4𝛿[𝑛 − 8]');
xlabel('n');
ylabel('Amplitude');

subplot(5, 1, 2); % 2nd subplot


stem(k, x3, 'g', 'LineWidth', 1.5, 'Marker', 's');
title('x2[n]');
xlabel('n');
ylabel('Amplitude');

subplot(5, 1, 3); % 3rd subplot


stem(ns3, ys3, 'b', 'LineWidth', 1.5, 'Marker', 'x');
title('𝑥3[𝑛] = 5𝑢[𝑛 + 2] − 2𝑢[𝑛 − 1] + 𝑢[𝑛 + 3] + 2𝑢[𝑛 − 8]');
xlabel('n');
ylabel('Amplitude');

subplot(5, 1, 4); % 4th subplot


stem(nd2, yd2, 'b', 'LineWidth', 1.5, 'Marker', 'x');
title('𝑥4[𝑛] = 𝑒0.2𝑛(𝑢[𝑛 + 10] − 𝑢[𝑛 − 20])');
xlabel('n');
ylabel('Amplitude');

subplot(5, 1, 5); % Bottom subplot


stem(nd3, A, 'b', 'LineWidth', 1.5, 'Marker', 'x');
title('𝑥5[𝑛] = 10(cos(0.495𝜋𝑛) + cos(0.505𝜋𝑛))');
xlabel('n');
ylabel('Amplitude');

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 [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 [x, n] = step_seq(n0, n1, n2)
% Generates x(n) = u(n-n0); n1 <= n <= n2
% [x, n] = step_seq(n0 ,n1, n2)
%
n = n1:n2; x = (n-n0) >= 0;
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 [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_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

Code Descroiption:

This MATLAB code generates and plots multiple signals and their combinations using functions such
as imp_seq, step_seq, sig_add, sig_shift, sig_fold, and sig_mul. The code covers various signal
operations and functions to create a diverse set of signals.

Figure:
Figure Description:

1. Top Subplot (`𝑥1[𝑛] = 3𝛿 [𝑛 + 2] + 2𝛿 [𝑛 − 1] + 𝛿 [𝑛 + 3] − 4𝛿 [𝑛 − 8]`):

- Displays the signal `𝑥1[𝑛]` generated by adding shifted and scaled impulse sequences.

- Stem plot with red color and markers ('o').

2. 2nd Subplot (`x2[n]`):

- Displays the signal `x2[n]` generated using an exponential function.

- Stem plot with green color and markers ('s').

3. 3rd Subplot (`𝑥3[𝑛] = 5𝑢[𝑛 + 2] − 2𝑢[𝑛 − 1] + 𝑢[𝑛 + 3] + 2𝑢[𝑛 − 8]`):

- Displays the signal `𝑥3[𝑛]` obtained by adding and scaling step sequences.

- Stem plot with blue color and markers ('x').

4. 4th Subplot (`𝑥4[𝑛] = 𝑒^0.2𝑛(𝑢[𝑛 + 10] − 𝑢[𝑛 − 20])`):

- Displays the signal `𝑥4[𝑛]` obtained by combining an exponential function and step sequences.

- Stem plot with blue color and markers ('x').

5. Bottom Subplot (`𝑥5[𝑛] = 10(cos(0.495𝜋𝑛) + cos(0.505𝜋𝑛))`):


- Displays the signal `𝑥5[𝑛]` generated using a cosine function.

- Stem plot with blue color and markers ('x').

Conclusion:

From this Lab Experiment, I learnt how to write functions in MATLAB and how to use them. Writing
functions for Signal Addition and Signal Multiplication saved a lot of my time that would have been
wasted otherwise. Function calls allowed me to perform same task repeatedly for different signals in
short time.

You might also like