Stem function in Matlab

I am trying to plot a discrete convolution:

n = 0:25;
x = 2 * (heaviside(n+2)-heaviside(n-12));
h = 0.9.^n .* (heaviside(n-2)-heaviside(n-13));
y = conv(x, h);
stem(n, y)

But I get a problem because the length of n is 26 and the length of y is 51, but I want to show the plot the convolution from n = 0 to n = 25. How can I modify this code to obtain such result?

Upvotes: 0

Views: 288

Answers (2)

Cris Luengo
Cris Luengo

Reputation: 60444

If you read the documentation to the conv function, you'll learn that it has a 3rd argument: a string that tells you how large the output should be.

y = conv(x, h, 'same');

will create a y that is the same size as x (and hence n).

Upvotes: 4

Felix
Felix

Reputation: 2668

Without deeper knowledge of convolutions or such:

If you are just trying to get the vectors to be similar in size, skip elements of the vector like so:

stem(n, y(1:2:end)) % Take every other element of y

This at least produces some curve with discrete points.

Alternatively, make the n twice as long:

stem(0:.5:25, y) % Double the amount of elements previously in n

Upvotes: 2

Related Questions