How can i convolve a unit ramp and unit step signal without using the 'conv' code ?
1 view (last 30 days)
Show older comments
Deborah Montojo
on 30 Apr 2017
Commented: Deborah Montojo
on 30 Apr 2017
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
0 Comments
Accepted Answer
Guillaume
on 30 Apr 2017
function c = myconv(a, b)
len = numel(a) + numel(b) - 1;
c = ifft(fft(a, len) .* fft(b, len));
end
Probably faster than the built-in conv as well.
More Answers (1)
Jan
on 30 Apr 2017
Edited: Jan
on 30 Apr 2017
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.
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!