How can i convolve a unit ramp and unit step signal without using the 'conv' code ?
Ältere Kommentare anzeigen
function [a,b,m,sum] =convsamp( h )
m=0:h;
a=[0,(0:h)]; %unit ramp sequence
b=[0,ones(1,h)]; %unit step sequence
sum =(h+h)-1;
for i=0:(h+h)-1
y(i)=0;
for j=0:h
if(i-j+1>0)
y(i)=y(i)+a(j)*h(i-j+1);
end
end
end
subplot(3,1,1), stem (m,b), %plots the step signal
title('Unit Step Sequence');
subplot(3,1,2),stem(m,a), %plots the ramp signal
title('Unit Ramp Sequence');
---i used this code but there seems to be a problem in line 6 y(i)=0; can you please help me
Akzeptierte Antwort
Weitere Antworten (1)
It is not exactly "there seems to be a problem", but you get a real error message. Then please post it completely instead of letting the readers guess the problem.
sum =(h+h)-1;
This variable is not used anywhere. In addition shadowing the builtin function "sum" is a bad idea, because this causes unexpected behavior frequently, when you want to access the function later.
for i=0:(h+h)-1
y(i)=0;
In the first iteration you try to access y(0), but indices must be greater than 0. The error message explains this clearly. Use this instead:
y = zeros(1, 2 * h); % Pre-allocation
for i = 1:(h+h)
% y(i)=0; % Can be omitted
A pre-allocation is essential, because the iterative growing of arrays wastes a lot of resources. Search in the forum for details.
Kategorien
Mehr zu Correlation and Convolution finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!