0% found this document useful (0 votes)
11 views9 pages

Lab No. 5

The document provides a series of tasks related to the convolution of discrete-time sequences using MATLAB. It includes code for decomposing a sequence into even and odd components, implementing a custom convolution function, and performing convolution on given sequences while plotting the results. Additionally, it describes the creation of a simple 1D convolution function without using the built-in 'conv' command.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views9 pages

Lab No. 5

The document provides a series of tasks related to the convolution of discrete-time sequences using MATLAB. It includes code for decomposing a sequence into even and odd components, implementing a custom convolution function, and performing convolution on given sequences while plotting the results. Additionally, it describes the creation of a simple 1D convolution function without using the built-in 'conv' command.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

LAB NO.

3 Convolution of Discrete-Time Sequences


Task No.1:
Write a Matlab code to decompose a sequence into its even and odd components.
Take help from Pre-Lab work.

S O U R C E C O D E

clc;

clear;

close all;

% Define the sequence

n = -5:5; % Range of n values

x = [2, 3, 1, -1, 4, 0, -2, -3, 1, 2, -1]; % Example sequence

% Compute the even and odd components

x_flipped = fliplr(x); % Flip the sequence

x_even = (x + x_flipped) / 2; % Even component

x_odd = (x - x_flipped) / 2; % Odd component

% Plot the original sequence

subplot(3,1,1);

stem(n, x, 'filled');

title('Original Sequence');

xlabel('n');

ylabel('x[n]');

grid on;
% Plot the even component

subplot(3,1,2);

stem(n, x_even, 'filled');

title('Even Component');

xlabel('n');

ylabel('x_{even}[n]');

grid on;

% Plot the odd component

subplot(3,1,3);

stem(n, x_odd, 'filled');

title('Odd Component');

xlabel('n');

ylabel('x_{odd}[n]');

grid on;
Task No.2:
You should have noticed that ‘conv’ command calculates convolution assuming
both input sequences are starting from origin (i-e no values on –ve t-axis). This is not always the
case, we do have sequences which have values for t.
Code:
function [y, ny] = conv_m(x1, n1, x2, n2)

y = conv(x1, x2);

ny_start = n1(1) + n2(1); % Start index of the result

ny_end = n1(end) + n2(end); % End index of the result

ny = ny_start:ny_end; % Define the new index range

End.

Example Code:
clc; clear; close all;

% Define first sequence with indices

x1 = [1, 2, 3]; % Example sequence

n1 = -1:1; % Corresponding indices (-1, 0, 1)

% Define second sequence with indices

x2 = [4, 5]; % Example sequence

n2 = 0:1; % Corresponding indices (0, 1)

% Call the custom convolution function

[y, ny] = conv_m(x1, n1, x2, n2);

% Display results

disp('Convolution Result:');

disp(y);

disp('Resulting Indices:');

disp(ny);

% Plot results

stem(ny, y, 'filled');

xlabel('n');

ylabel('y[n]');

title('Convolution Result with Correct Indexing');

grid on;

Plot:
Task No.3:
Convolve following sequences using MATLAB Function “conv” and “conv_m”
and plot the input, impulse response and output in one figure using “subplot”:  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].
Code:
clc; clear; close all;

x1 = [1 2 1]; % Input sequence


n1 = [0 1 2]; % Time indices

h1 = [1 1 1]; % Impulse response

n_h1 = [0 1 2]; % Time indices for h[n]

y1 = conv(x1, h1);

n_y1 = n1(1) + n_h1(1) : n1(end) + n_h1(end); % Compute correct indices

% Define second input and impulse response

x2 = [-1 4 -3 -2 1 0 2]; % Input sequence

n2 = -2:4; % Time indices

h2 = [1 1 1]; % Impulse response

n_h2 = [-1 0 1]; % Time indices for h[n]

% Compute convolution using conv

y2 = conv(x2, h2);

n_y2 = n2(1) + n_h2(1) : n2(end) + n_h2(end); % Compute correct indices

% Plot results using subplot

figure;

% First input sequence

subplot(3,2,1);

stem(n1, x1, 'filled');

xlabel('n'); ylabel('x_1[n]');

title('Input Sequence x_1[n]');

grid on;

% First impulse response

subplot(3,2,3);
stem(n_h1, h1, 'filled');

xlabel('n'); ylabel('h_1[n]');

title('Impulse Response h_1[n]');

grid on;

% First output sequence

subplot(3,2,5);

stem(n_y1, y1, 'filled');

xlabel('n'); ylabel('y_1[n]');

title('Output y_1[n] = x_1[n] * h_1[n]');

grid on;

% Second input sequence

subplot(3,2,2);

stem(n2, x2, 'filled');

xlabel('n'); ylabel('x_2[n]');

title('Input Sequence x_2[n]');

grid on;

% Second impulse response

subplot(3,2,4);

stem(n_h2, h2, 'filled');

xlabel('n'); ylabel('h_2[n]');

title('Impulse Response h_2[n]');

grid on;

% Second output sequence

subplot(3,2,6);

stem(n_y2, y2, 'filled');

xlabel('n'); ylabel('y_2[n]');

title('Output y_2[n] = x_2[n] * h_2[n]');


grid on;

Plot:

Task No.4:
Write a function convolution in MATLAB that performs 1D linear convolution by
the steps mentioned above. You can use any method.
Code:
function y = my_conv(x, h)

% Simple 1D convolution function without using conv()

L = length(x);

M = length(h);

N = L + M - 1; % Output length

y = zeros(1, N); % Initialize output

% Compute convolution
for n = 1:N

for k = max(1, n-M+1):min(n, L)

y(n) = y(n) + x(k) * h(n-k+1);

end

end

End

You might also like