DSP Lab Report 3
DSP Lab Report 3
equences
3 / 13 / 24
03
EE-20-A
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;
% Verificaion
sum = even + odd;
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);
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;
h = [1 1 1];
n_h = 0:2;
% 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');
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');
h = [1 1 1];
n_h = -1:1;
% 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');
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 !