Previous Up Next

5.27.7  Convolution of two signals or functions : convolution

convolution takes two arguments, a real vector v of length n and a real vector w of length m , and returns their convolution z=vw which is the vector of length N=n+m−1 defined as :

vi wki,   k=0,1,…,N−1, 

such that vj=0 for jn and wj=0 for jm . The two arguments may also be real functions f(x) and g(x) , with the variable x as an optional third argument, in which case the integral



is computed. It is assumed that f and g are causal functions, i.e.  f(x)=g(x)=0 for x<0 . Therefore both f and g are multiplied by Heaviside function prior to integration.

For example, input :


Output :


To compute the convolution of f(x)=25 ex u(x) and g(x)=x e−3 x u(x) , where u is the Heaviside function, input :


Output :


To compute the convolution of f(t)=ln(1+tu(t) and g(t)=1/√t , input :


Output :

2*Heaviside(t)*((t+1)*ln(abs(sqrt(t)-sqrt(t+1))/(sqrt(t)+sqrt(t+1)))+ 2*sqrt(t^2+t))/sqrt(t+1)

In the following example convolution is used for reverberation. Assume that the directory sounds contains two files, a dry, mono recording of a guitar stored in guitar.wav and a two-channel impulse response recorded in a French 18th century salon and stored in salon-ir.wav. Files are loaded with the following command lines :

clip:=readwav("/path/to/sounds/guitar.wav"):; ir:=readwav("/path/to/sounds/salon-ir.wav"):;

Input :


Output :

Input :


Output :

Convolving the data from clip with both channels in ir produces a reverberated variant of the recording, in stereo. Input :

data:=channel_data(clip):; L:=convolution(data,channel_data(ir,1)):; R:=convolution(data,channel_data(ir,2)):;

The convolved signals L and R now become the left and right channel of a new audio clip, respectively. The normalize option is used because convolution usually results in a huge increase of sample values (which is clear from the definition). Input :

spatial:=createwav([L,R],normalize=-3):; playsnd(spatial)

The result sounds as it was recorded in the same salon as the impulse response. Furthermore, it is a true stereo sound. To visualize it, input :


Output :

Note that the resulting audio is longer than the input (for the length of the impulse response).

Previous Up Next