Line 1: | Line 1: | ||
− | |||
− | |||
% Adam Siembida | % Adam Siembida | ||
+ | |||
% ECE 301 - HW2 - P7 | % ECE 301 - HW2 - P7 | ||
% initialize workspace | % initialize workspace | ||
+ | |||
clear all; | clear all; | ||
+ | |||
format compact; | format compact; | ||
+ | |||
clc; | clc; | ||
% x and h are vectors to be convolved. y is x*h | % x and h are vectors to be convolved. y is x*h | ||
+ | |||
x = input('Input vector x in the format [a b c ...]: '); | x = input('Input vector x in the format [a b c ...]: '); | ||
+ | |||
h = input('Input vector h in the format [a b c ...]: '); | h = input('Input vector h in the format [a b c ...]: '); | ||
+ | |||
ylen = length(x)+length(h)-1; | ylen = length(x)+length(h)-1; | ||
+ | |||
y = zeros(1,ylen); | y = zeros(1,ylen); | ||
%hr is h reversed in order | %hr is h reversed in order | ||
+ | |||
hr = h(end:-1:1); | hr = h(end:-1:1); | ||
% pad x and hr with zeros to make my algorithm work | % pad x and hr with zeros to make my algorithm work | ||
+ | |||
x=[zeros(1,ylen-length(x)) x]; | x=[zeros(1,ylen-length(x)) x]; | ||
+ | |||
hr=[hr zeros(1,ylen-length(hr))]; | hr=[hr zeros(1,ylen-length(hr))]; | ||
for i=1:ylen | for i=1:ylen | ||
+ | |||
y(i) = dot(x, hr); | y(i) = dot(x, hr); | ||
+ | |||
hr = hr([end 1:end-1]); | hr = hr([end 1:end-1]); | ||
+ | |||
end | end | ||
fprintf('\n'); | fprintf('\n'); | ||
+ | |||
fprintf('x*h = [ '); | fprintf('x*h = [ '); | ||
+ | |||
fprintf('%i ',y); | fprintf('%i ',y); | ||
− | |||
− | + | fprintf(']\n'); |
Latest revision as of 18:56, 1 July 2009
% Adam Siembida
% ECE 301 - HW2 - P7
% initialize workspace
clear all;
format compact;
clc;
% x and h are vectors to be convolved. y is x*h
x = input('Input vector x in the format [a b c ...]: ');
h = input('Input vector h in the format [a b c ...]: ');
ylen = length(x)+length(h)-1;
y = zeros(1,ylen);
%hr is h reversed in order
hr = h(end:-1:1);
% pad x and hr with zeros to make my algorithm work
x=[zeros(1,ylen-length(x)) x];
hr=[hr zeros(1,ylen-length(hr))];
for i=1:ylen
y(i) = dot(x, hr);
hr = hr([end 1:end-1]);
end
fprintf('\n');
fprintf('x*h = [ ');
fprintf('%i ',y);
fprintf(']\n');