Nick
Nick

Reputation: 177

Plotting Convolution in Matlab

I am trying to use matlab to plot the convolution of two functions, on a set interval of time, and cannot get the plot function to work.

My code so far is this:

>> t = -10:.1:10
>> f = heaviside(t)-heaviside(t-3)
>> g = heaviside(t)-heaviside(t-1)
>> y = conv(f,g)

which produces 3 variables of "value" 1x201 double, (t, f, g) and the convolution which is of value 1x401 double, which I believe means it is double the amount of points of the previous variables

When I go to plot this using the plot command I try to plot the convolution with respect to t, using this command:

>> plot(t,y)

which throws and error saying "vectors must be the same length". How do I make it so that I can plot y with respect to t?

Upvotes: 1

Views: 4493

Answers (1)

Robert Dodier
Robert Dodier

Reputation: 17576

The range of t for the convolution should be from two times the minimum value of the original range to two time the maximum value of the original range, at the same interval. Therefore the number of values of t for the convolution is 2*n - 1 where n is the original number of values of t. So in summary I think you can say t = -20 : 0.1 : 20 and then you should have the correct range and the correct number of values of t. EDIT: corrected step size; should be the same (namely 0.1) as the original range.

More generally, if you are convolving two series which have different ranges, the minimum value of the range of the result is the sum of the minimum values of the original ranges, and the maximum is the sum of the original ranges' maximum values.

Upvotes: 2

Related Questions