Hey there, I've been having difficulty writing the matlab equivalent of the conv(x,y)
function. I cant figure out why this gives the incorrect output. For the arrays
x1 = [1 2 1]
and x2 = [3 1 1]
.
Here's what I have
x1 = [1 2 1];
x2 = [3 1 1];
x1len = leng(x1);
x2len = leng(x2);
len = x1len + x2len - 1;
x1 = zeros(1,len);
x2 = zeros(1,len);
buffer = zeros(1,len);
answer = zeros(1,len);
for n = 1:len
buffer(n) = x(n);
answer(n) = 0;
for i = 1:len
answer(n) = answer(n) + x(i) * buffer(i);
end
end
The matlab conv(x1,x2)
gives 3 7 6 3 1
as the output but this is giving me 3 5 6 6 6
for answer.
Where have I gone wrong?
Also, sorry for the formatting I am on opera mini.