Saturday, November 30, 2013

DSP in MATLAB®: Echo and Reverberation Part 2

We observed in the last post how the delays can be set according to the amount of reverberation required. But, what if you wish to alter the characteristics? That would be a tedious job, you cannot go into the software every time to program the delays (totally impractical).

So, lets take a look at the Convolution reverberation algorithm, whose use is widespread in the audio industry.

Method 2: The Convolution Reverb 

The convolution reverb is a modification of the concept of transfer functions in control system theory.

To take a glance on the same,
A Transfer Function is the ratio of the output of a system to the input of a system, in the Laplace domain considering its initial conditions and equilibrium point to be zero. If we have an input function of X(s), and an output function Y(s), we define the transfer function H(s) to be:

Readers who have read the Circuit Theory book will recognize the transfer function as being the impedance, admittance, impedance ratio of a voltage divider or the admittance ratio of a current divider. 

So, now we will consider that our theater, which we were discussing last time, is H(s) (obviously it's larger than the box above ;) ). Now, out input will be sound signal, let's say music played on loudspeaker, and the output is the sound with the reverberation.
Essentially, to duplicate this effect, we can find out the transfer function. How? By using impulse response, i.e. output when the input is an impulse function.
The Impulse Function, denoted with δ(t) is a special function defined piece-wise as follows:

\delta(t) = \left\{
  0, & t < 0
  \mbox{undefined}, & t = 0
  0, & t > 0
The impulse function is also known as the delta function because it's denoted with the Greek lower-case letter δ. The delta function is typically graphed as an arrow towards infinity, as shown below:

Delta Function.svg

Thus, it is similar to unit identity among transfer functions. Thus, when an impulse is provided, the output is the system transfer function. 

You need not find out the impulse response yourself, the files are available at 

and you may use them for your experimentation.

Now, according to the equation of transfer function, after obtaining this impulse response, we will convolve it with the input. Thus, we will get the output which will sound as if the recording was done in the place whose impulse response we used for processing.

So, if you convolve your opera music with the response of the theater, you can enjoy the same at your convenience, no theater and no tickets!

The MATLAB code is discussed here for the same, which mainly depends on the conv function. The important conclusion drawn from above is that frequency domain multiplication is time domain convolution. Thus, we use convolution here.

 d=wavread('cathIR.wav'); %impulse response file, this one is of a cathedral  
 g=wavread('wilcut.wav'); %input music file, you may choose one of your liking  
 d=d(:,1); % select any one sound channel, here I chose left  
 g=g(:,1); % because conv function does not apply to matrix, only vectors  
 sound(d,44100); %play file to check  
 sound(g,44100); %the sampling frequency depends on the file you chose, take care both %should be same or the output will not be accurate due to under or over sampling  
 y=conv(d,g); % you may write conv(g,d) as it is a commutative function   
 sound(y,44100); % play output file  
 wavwrite('output.wav',44100); %save output file to your current directory  

Thus, you finally get what you want, the theater effect, or an discotheque effect on your music devices. Generally, Digital Signal Controllers (DSC) are used which perform this process in real time. Also, the impulse responses are stored in the memory, or implemented using gates on an FPGA.