How can i convolve a unit ramp and unit step signal without using the 'conv' code ?

1 view (last 30 days)
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

Accepted Answer

Guillaume
Guillaume on 30 Apr 2017
An easy way to implement a convolution without using conv is to implement the convolution theorem:
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
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.
  2 Comments
Deborah Montojo
Deborah Montojo on 30 Apr 2017
This is the problem:
Create a function similar to convolution using if/while/for loops.
Plot a ramp signal and a unit step signal.
Using the convolution function you created, find the convolution of the two signal.
Plot the output convolution, unit step and ramp signal in a figure using subplot.

Sign in to comment.

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!