David Webb
David Webb

Reputation: 128

Why can't I get convolution to work properly in MATLAB?

Convolving signals in MATLAB produces unexpected results every time. Take the following code for example, in which I attempt to convolve a rect function with itself:

clc

clear all

x=-5:.01:5;

y=rectangularPulse(x);

C=conv(y,y);

plot(C)

Producing a triangle function is correct, however it should be centered at 0, not 1000, and the amplitude should be 1, not 100. I'm sure this is just a simple misunderstanding of how the conv() function works in MATLAB; if there's a way to do it that would produce a triangle function that gos from -1 to 1 with an amplitude of 1 please let me know how to do it.

Upvotes: 3

Views: 2361

Answers (1)

gnovice
gnovice

Reputation: 125854

Part of the confusion here is that the signal y that you are dealing with is discrete with its samples spaced out by 0.01 in x. Additionally CONV appears to pull double-duty for polynomial multiplication. From the help docs:

If u and v are vectors of polynomial coefficients, convolving them is equivalent to multiplying the two polynomials.

Convolution involves computing the area under the intersecting curves as you slide one across the other. CONV does the discrete version of this by simply multiplying overlapping sample points and essentially assuming a value of 1 for the distance between samples (i.e. the width of the rectangular strips approximating the area under the curve). To get a true convolution, you have to scale the resulting approximate area by the sample spacing of 0.01. Additionally, you will want to extract the central part of the convolution using the 'same' argument so you can plot the results versus x, like so:

C = 0.01.*conv(y, y, 'same');
plot(x, C);

enter image description here

Upvotes: 2

Related Questions