0% found this document useful (0 votes)
10 views

DSP Lab Report 3

Uploaded by

madnir99
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

DSP Lab Report 3

Uploaded by

madnir99
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Convolution of Discrete-Time

equences

3 / 13 / 24

03
EE-20-A

Muhammad Raza Madni 210401034


Riyan Abdullah 210401013
Lab # 03
Objectives:
By the end of this lab, students will be able to understand even and odd component decomposition of
signal. Also, they will be familiarized to get output from LTI systems by convolving input x(n) with impulse
response h(n). This will add to their understanding of LTI system and its operation.

Equipment’s:
• Laptop
• MATLAB (software)

Introduction to MATLAB
MATLAB is a high-level programming language that has been used extensively to solve complex
engineering problems.
Task # 1
Write a function (in MATLAB) for decomposition of discrete time sequence to distinguish its even
and odd components.
Answers:
Following is the MATLAB function code.
function [even, odd] = decompose_sequence(x)
N = length(x);
n = 0:N-1;

% Decompose the sequence into even and odd components


even = 0.5*(x + fliplr(x));
odd = 0.5*(x - fliplr(x));
end

Following is the MATLAB code as well as answers.


% Taking discrete time sequence
x = input('Enter the DT sequence: ');

% Decompose the sequence into even and odd components


[even, odd] = decompose_sequence(x);

% Verificaion
sum = even + odd;

% Display the results


disp('Original Sequence:');disp(x);
Original Sequence:
1 2 3 5 3 6
disp('Even Component:');disp(even);
Even Component:
3.5000 2.5000 4.0000 4.0000 2.5000 3.5000
disp('Odd Component:');disp(odd);
Odd Component:
-2.5000 -0.5000 -1.0000 1.0000 0.5000 2.5000
disp('verification:');disp(sum);
verification:
1 2 3 5 3 6
subplot 221
stem(even);legend('even');grid on;title Even-Components
subplot 222
stem(odd);legend('odd');grid on;title Odd-Components
subplot 223
stem(x);legend('orignal');grid on;title Orignal-Sequence
subplot 224
stem(sum);legend('verification');grid on;title Verification

Observations:
On this task we made a function named “decompose_sequence” in which we have created our code to
separate even and component from the sequence taken by the user in the second part of the code.

Task # 2
Write a function convolution in MATLAB that performs 1D linear convolution by the steps
mentioned above using any method. You can process the sequence to distinguish the convolution results of
your function and the MATLAB built-in function “conv”.
Answers:
Following is the MATLAB function code.
function result = y_convolution(x, h)
% Length of input sequences
N = length(x);
M = length(h);

% Zero-pad sequences to ensure the length of both sequences are same


padded_x = [x, zeros(1, M-1)];
padded_h = [h, zeros(1, N-1)];

% Perform linear convolution


result = zeros(1, N + M - 1); %let result be variable to store all the convolve
values, the length must be the sum of lenght of the input sequence

for n = 1:N+M-1 %index of result


for k = max(1, n-M+1):min(n, N) % max(1, n-M+1) ensures that the index k does not
go below 1, min(n, N) ensures that the index k does not exceed the length of x

result(n) = result(n) + padded_x(k) * padded_h(n-k+1); %Computes the


convolution sum by multiplying and summing the appropriately aligned elements of padded_x
and padded_h
end
end
end

Following is the MATLAB code as well as answers.


% Example usage and comparison with built-in conv function
x = [1 2];
h = [3 7 8];
result_my_conv = y_convolution(x, h); % my convolution funcyion
result_builtin_conv = conv(x, h); % built in convolution function

disp('Result of my convolution function:');


Result of my convolution function:
disp(result_my_conv);
3 13 22 16
disp('Result of MATLAB built-in convolution function:');
Result of MATLAB built-in convolution function:
disp(result_builtin_conv);
3 13 22 16

% Plotting both results for comparison


subplot 211;
stem(result_my_conv);
title('Result of my convolution function');
xlabel('Index');
ylabel('Value');

subplot 212;
stem(result_builtin_conv);
title('Result of MATLAB built-in convolution function');
xlabel('Index');
ylabel('Value');
Conclusion:
In the end we have created a function that works the same as built in function named “conv”.
Task # 3
You should have noticed that “conv” command calculates convolution assuming both input
sequences are starting from origin (i-e no values on –ve n-axis). This is not always the case; we do have
sequences which have values for n<0. Write a function conv_m to describe, analyze, and distinguish the
“conv” command that would remove this limitation in the built-in function “conv”.
Answers:
Following is the MATLAB function code.
function y = conv_m(x, h)
% Check if either sequence has negative indices
% any() is used to check if any element in the resulting logical array is true.
% find(x, 1) returns the indices of elements in x that are equal to 1.
if any(find(x, 1) < 1) || any(find(h, 1) < 1)
% Pad sequences with zeros to include negative indices
% abs(find(x, 1))-1 calculates the number of zeros needed to pad the sequence x at
the beginning.
x = [zeros(1, abs(find(x, 1))-1), x];
h = [zeros(1, abs(find(h, 1))-1), h];
end

% Calculate convolution
y = conv(x, h);
end
Following is the MATLAB code as well as answers.
% Define the input sequences with values for n < 0
x = [-1 4 -3 -2 1 0 2]; % n>0
n_x = -2:4;
h = [1 -1 2]; % n<0
n_h = -1:1;

% Perform convolution using custom conv_m function


y_conv_custom = conv_m(x, h);

% Display the input sequences and the output of convolution


disp('Input Sequence x[n]:');disp(x);
Input Sequence x[n]:
-1 4 -3 -2 1 0 2
disp('Input Sequence h[n]:');disp(h);
Input Sequence h[n]:
1 -1 2
disp('Output of Convolution y[n]:');disp(y_conv_custom);
Output of Convolution y[n]:
-1 5 -9 9 -3 -5 4 -2 4

%ploting the sequences


subplot 411
stem(n_x,x);legend('input sequence x[n]');grid on;title input_sequence
subplot 412
stem(n_h,h);legend('input sequence h[n]');grid on;
subplot 413
stem(y_conv_custom);legend('output');grid on;title output-Sequence
y = conv(x,h);
subplot 414
stem(y);legend('verification');grid on;title verify
Conclusion:
In the end we have removed the limitations i.e., the negative values of the sequence.
Task # 4
Analyze the following sequences to convolve them using MATLAB Function “conv” and your
function “conv_m” and plot the input, impulse response, and output in one figure using “subplot” to
distinguish the results of built-in function and user-defined function:
• x[n] = [1 2 1], n=[0 1 2] h[n] = [1 1 1], n= [0 1 2]
• x[n] = [-1 4 -3 -2 1 0 2], n=[-2:4] h[n] = [1 1 1], n= [-1 0 1]
Answers:
Following is the MATLAB function code.
function y = conv_m_func(x, h)
y = zeros(1, length(x)+length(h)-1); %the length must be the sum of lenght of the
input sequence
for i = 1:length(x)
for j = 1:length(h)
y(i+j-1) = y(i+j-1) + x(i)*h(j); %); % Computes the convolution sum by
multiplying and summing the appropriately aligned elements of x and h
end
end
end

Following is the MATLAB code as well as answers.


% Define the sequences
% part 1
x = [1 2 1];
n_x = 0:2;

h = [1 1 1];
n_h = 0:2;

% Convolve using MATLAB built-in function


y_conv_builtin = conv(x, h);

% Define the user-defined convolution function


conv_m = @(x, h) conv_m_func(x, h); % @(x, h) defining an anonymous func

% Convolve using user-defined function


y_conv_user = conv_m(x, h);

% Plotting
figure;
subplot 221;
stem(n_x, x, 'filled');
title('Input Sequence x[n]');
xlabel('n');
ylabel('Amplitude');

subplot 222;
stem(n_h, h, 'filled');
title('Impulse Response h[n]');
xlabel('n');
ylabel('Amplitude');

n_y = n_x(1)+n_h(1):n_x(end)+n_h(end); % Define the range for output sequence


subplot 223;
stem(n_y, y_conv_builtin, 'filled', 'r');
xlabel('n');
ylabel('Amplitude');
legend('Using conv function', 'Location', 'Best');

subplot 224;
stem(n_y, y_conv_user, 'filled', 'b');
title('Output Sequence y[n]');
xlabel('n');
ylabel('Amplitude');
legend('Using conv\_m function', 'Location', 'Best');

Following is the MATLAB code as well as answers.


% Define the sequences
% part 2
x = [-1 4 -3 -2 1 0 2];
n_x = -2:4;

h = [1 1 1];
n_h = -1:1;

% Convolve using MATLAB built-in function


y_conv_builtin = conv(x, h);

% Define the user-defined convolution function


conv_m = @(x, h) conv_m_func(x, h); % @(x, h) defining an anonymous func

% Convolve using user-defined function


y_conv_user = conv_m(x, h);

% Plotting
figure;
subplot 221;
stem(n_x, x, 'filled');
title('Input Sequence x[n]');
xlabel('n');
ylabel('Amplitude');

subplot 222;
stem(n_h, h, 'filled');
title('Impulse Response h[n]');
xlabel('n');
ylabel('Amplitude');

n_y = n_x(1)+n_h(1):n_x(end)+n_h(end); % Define the range for output sequence


subplot 223;
stem(n_y, y_conv_builtin, 'filled', 'r');
xlabel('n');
ylabel('Amplitude');
legend('Using conv function', 'Location', 'Best');

subplot 224;
stem(n_y, y_conv_user, 'filled', 'b');
title('Output Sequence y[n]');
xlabel('n');
ylabel('Amplitude');
legend('Using conv\_m function', 'Location', 'Best');

…the end !

You might also like