How to calculate the convolution of a function with itself in MATLAB and WolframAlpha?

Bruno Calza picture Bruno Calza · Sep 14, 2011 · Viewed 25.5k times · Source

I am trying to calculate the convolution of

x(t) = 1, -1<=t<=1
x(t) = 0, outside

with itself using the definition.

http://en.wikipedia.org/wiki/Convolution

I know how to do using the Matlab function conv, but I want to use the integral definition. My knowledge of Matlab and WolframAlpha is very limited.

Answer

Amro picture Amro · Sep 14, 2011

I am still learning Mathematica myself, but here is what I came up with..

First we define the piecewise function (I am using the example from the Wikipedia page)

f[x_] := Piecewise[{{1, -0.5 <= x <= 0.5}}, 0]

piecewise_function

Lets plot the function:

Plot[f[x], {x, -2, 2}, PlotStyle -> Thick, Exclusions -> None]

function_plot

Then we write the function that defines the convolution of f with itself:

g[t_] = Integrate[f[x]*f[t - x], {x, -Infinity, Infinity}]

convolution_integral

and the plot:

Plot[g[t], {t, -2, 2}, PlotStyle -> Thick]

convolution_plot


EDIT

I tried to do the same in MATLAB/MuPad, I wasn't as successful:

f := x -> piecewise([x < -0.5 or x > 0.5, 0], [x >= -0.5 and x <= 0.5, 1])

func_def

plot(f, x = -2..2)

func_plot

However when I try to compute the integral, it took almost a minute to return this:

g := t -> int(f(x)*f(t-x), x = -infinity..infinity)

convolution

the plot (also took too long)

plot(g, t = -2..2)

convolution_plot

Note the same could have been done from inside MATLAB with the syntax:

evalin(symengine,'<MUPAD EXPRESSIONS HERE>')