Clear Filters
Clear Filters

issues with indexing in a convolution calculation

3 views (last 30 days)
so here is my issue. When i compute the convolution of x[n]= -delta[n+1] + 0.5 delta[n] + 2 delta[n-1] with h[n]= 2 delta[n] + delta[n-1] i am getting the correct amplitudes but at the wrong position. here is what i am getting,
y[n]= -2delta[n+1] + 4.5 delta [n+3] + 2 delta[n+4] where is should be getting
y[n]= -2 delta[n+1] + 4.5 delta[n-1] + 2 delta[n-2].
here is my code:
if true
% code
function [y] = myconv( x,h )
m=length(x);
n=length(h);
x=[x,zeros(1,m)];
h=[h,zeros(1,n)];
for i=1:(n+m-1)
y(i)=0;
for j=1:m
if(i-j+1>0) && (i-j+1<m)
y(i)=y(i)+x(j)*h(i-j+1);
else
end
end
en
stem(y,'filled');%grid
% plot the resulting sequence y[n]
title('convolution of two sequence') % add title to the plot
ylabel('y[n]=x[n]*h[n]')
% label the y-axis
xlabel('index,[n]')
end
any help would be greatly appreciated!!!

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!