Thursday, 8 September 2016

DSP - Linear Convolution of Discrete Time Signals (Scilab Code with Plot)

Code:



clc; clear; close;
 
x = input("Enter the first sequence: ");
h = input("Enter the second sequence: ");
x_origin = input("Enter the position of origin in first sequence: ");
h_origin = input("Enter the position of origin in second sequence: ");
 
x_lower = -(x_origin - 1);     //lower range of x
h_lower = -(h_origin - 1);     //lower range of h
x_upper = length(x) - x_origin;     //upper range of x
h_upper = length(h) - h_origin;     //upper range of h
n_lower = x_lower + h_lower;
n_upper = x_upper + h_upper;
n = n_lower :1: n_upper;
 
disp(x_lower, "x_lower = ");
disp(h_lower, "h_lower = ");
disp(x_upper, "x_upper = ");
disp(h_upper, "h_upper = ");
disp(n, "n = ");
disp(conv(x, h), "Convolution = ");
 
y = conv(x, h);
 
subplot(3, 1, 1);
plot2d3(x_lower:1:x_upper, x, style=color("green"));
xtitle("First Sequence","n","x(n)");
a = gca();
a.x_location = "origin";
a.y_location = "origin";
a.children.children(1).thickness = 2;
 
subplot(3, 1, 2);
plot2d3(h_lower:1:h_upper, h, style=color("red"));
xtitle("Second Sequence","n","h(n)");
a = gca();
a.x_location = "origin";
a.y_location = "origin";
a.children.children(1).thickness = 2;
 
subplot(3, 1, 3);
plot2d3(n, y, style=color("blue"));
xtitle("Linear Convolution: x(n)*h(n)","n","y(n)");
a = gca();
a.x_location = "origin";
a.y_location = "origin";
a.children.children(1).thickness = 2;


Console I/O: 





Plot:





No comments:

Post a Comment