(New page: %% Code by Ryne Rayburn %% Performs convolution on inputted vectors x & y. Then displays the %% answer (y). clc; clear all; %%Input by user in vector format, i.e. [1,2,3,4,5,etc] x=input...) |
|||
Line 1: | Line 1: | ||
%% Code by Ryne Rayburn | %% Code by Ryne Rayburn | ||
+ | |||
%% Performs convolution on inputted vectors x & y. Then displays the | %% Performs convolution on inputted vectors x & y. Then displays the | ||
+ | |||
%% answer (y). | %% answer (y). | ||
+ | |||
clc; | clc; | ||
+ | |||
clear all; | clear all; | ||
%%Input by user in vector format, i.e. [1,2,3,4,5,etc] | %%Input by user in vector format, i.e. [1,2,3,4,5,etc] | ||
+ | |||
x=input('Enter vector ''x'' in [a,b,c] format: '); | x=input('Enter vector ''x'' in [a,b,c] format: '); | ||
+ | |||
h=input('Enter vector ''h'' in [a,b,c] format: '); | h=input('Enter vector ''h'' in [a,b,c] format: '); | ||
Lx=length(x); %%Length of vector x | Lx=length(x); %%Length of vector x | ||
+ | |||
Lh=length(h); %%Length of vector h | Lh=length(h); %%Length of vector h | ||
X=[x,zeros(1,Lh)]; %%Pads vector x with zeros | X=[x,zeros(1,Lh)]; %%Pads vector x with zeros | ||
+ | |||
H=[h,zeros(1,Lx)]; %%Pads vector h with zeros | H=[h,zeros(1,Lx)]; %%Pads vector h with zeros | ||
+ | |||
%%Padding with zeroes allows the vector arithmetic to be carried out | %%Padding with zeroes allows the vector arithmetic to be carried out | ||
+ | |||
%%successfully, without errors | %%successfully, without errors | ||
for(i=1:Lx+Lh-1) | for(i=1:Lx+Lh-1) | ||
+ | |||
y(i)=0; | y(i)=0; | ||
+ | |||
for(j=1:Lx) | for(j=1:Lx) | ||
+ | |||
if(i-j+1>0) | if(i-j+1>0) | ||
+ | |||
y(i)=y(i)+X(j)*H(i-j+1); | y(i)=y(i)+X(j)*H(i-j+1); | ||
+ | |||
end | end | ||
+ | |||
end | end | ||
+ | |||
end | end | ||
y | y | ||
+ | |||
+ | [https://kiwi.ecn.purdue.edu/rhea/index.php/Homework_2 Return to Homework 2] |
Latest revision as of 08:56, 1 July 2009
%% Code by Ryne Rayburn
%% Performs convolution on inputted vectors x & y. Then displays the
%% answer (y).
clc;
clear all;
%%Input by user in vector format, i.e. [1,2,3,4,5,etc]
x=input('Enter vector x in [a,b,c] format: ');
h=input('Enter vector h in [a,b,c] format: ');
Lx=length(x); %%Length of vector x
Lh=length(h); %%Length of vector h
X=[x,zeros(1,Lh)]; %%Pads vector x with zeros
H=[h,zeros(1,Lx)]; %%Pads vector h with zeros
%%Padding with zeroes allows the vector arithmetic to be carried out
%%successfully, without errors
for(i=1:Lx+Lh-1)
y(i)=0;
for(j=1:Lx)
if(i-j+1>0)
y(i)=y(i)+X(j)*H(i-j+1);
end
end
end
y