issues with indexing in a convolution calculation
3 次查看(过去 30 天)
显示 更早的评论
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,
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/170823/image.jpeg)
y[n]= -2delta[n+1] + 4.5 delta [n+3] + 2 delta[n+4] where is should be getting
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/170824/image.jpeg)
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!!!
0 个评论
回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!