1-1
Department of Electrical & Computer Engineering
McGill University
Experiment 1
Audio Eects in MATLAB
1.1 Purpose
In this experiment, you will implement a few basic audio eect algorithms in order to be-
come familiar with the programming tools you will use during the class, namely MATLAB and
SIMULINK.
1.2 Reverberation
In any natural environment, the sounds we perceive depend on the listening conditions,
and in particular on the acoustic environment. The same signal played in a cathedral or in a
small room will not sound the same. The room in which the listening experience takes place
shapes the way any sound is perceived. Reverberation occurs when copies of an audio signal
reach the ear with dierent delays and dierent amplitudes, after taking dierent paths and
having bounced against walls and surrounding objects. Each room (with a xed conguration
of furniture and occupants) can be measured in terms of how it shapes the perception of
sound. For a xed position of the sound source and of the microphone, the dierent delayed and
attenuated versions of the original sound can be thought of as being produced by a linear time
invariant system. The amount of signal reected o a wall or object depends on the absorption
of the object. Walls treated with curtains or other sound-absorbing material will lead to less
reverberation. The perceived signal at the location of the microphone1 is
y[n] = i x[n Di ] = h[n] x[n], (1.1)
i
where h[n] = i i [n Di ]. One can measure the impulse response of a room by simply
producing an impulsive audio signal and recording the signal captured by the microphone. For
a large room, the impulse response can extend to a good fraction of a second and thus extend
a large number of samples, particularly at high sampling rates.
In practice, simulated reverberations are not usually produced using impulse responses mea-
sured from actual rooms. The following sections will introduce basic building blocks to construct
a synthetic reverberation tool.
1
We assume sampled signals in this experiment.
2006/01/10
1.2. Reverberation 1-2
1.2.1 FIR comb lter
An FIR comb lter with delay M is simply represented by an impulse response
h[n] = [n] + G[n M ],
where G is a scalar gain. Such a comb lter can generate one echo with a time delay
corresponding to M sampling periods, and an attenuation/gain dened by G.
Explain why such a lter is called a comb lter.
1.2.2 IIR reverberator model
Fig. 1.1 shows the system block diagram of a basic IIR reverberator, with two parameters,
a feedback gain G and a delay M . For reasonably sized rooms, M is usually chosen so that it
corresponds to a time delay between 25 and 100 ms.
x[n] y[n]
Gz M
Fig. 1.1 Block diagram of a simple IIR reverberator building block.
Compute the impulse response of a reverberator like the one in Fig. 1.1. Explain what kind
of room geometry would produce such an impulse response.
Compute the frequency response of the IIR reverberator. Use MATLAB to plot it for sample
values of M and G. What are the conditions for stability?
Implement an IIR reverberator building block both in MATLAB and SIMULINK. Replicate
the block to create a number of reverberators in parallel, with dierent parameters for each
reverberator.
Especially in the case of speech signals, the simple IIR reverberator can produce sounds with
a metallic quality. Try putting a simple FIR lter into the feedback loop to improve the
quality. Consider the case that high frequencies are absorbed (at a reection point) more
than low frequencies. What type of FIR lter should you use (lowpass, highpass, bandpass)?
1.2.3 Allpass reverberator model
An all-pass lter can give more substance to the reverberant signal and better simulates the
sound in a reverberant room than the system considered above. An all-pass lter modies the
phase response, while leaving the amplitude response unchanged. A very easy parametrization
of all-pass systems consists in using the same coecients for the numerator and denominator,
but in reversed order, such as e.g.
a + bz 1
A(z) = ,
b + az 1
1.3. Ring Modulation 1-3
for any real a and b. This is a rst-order lter. For use in a reverberator, the delay is changed
to D samples, as shown in Fig. 1.2.
x[n] zD y[n]
a
Fig. 1.2 Implementation of an all-pass reverberator transfer function.
Comment on the eect of this impulse response on the signal and how it can approximate a
reverberator. Check that the implementation in Fig. 1.2 yields an all-pass lter. To do so
plot its frequency response. Also plot its impulse response. What are the conditions on a to
ensure stability?
Cascade a few allpass reverberators with the IIR-based system above and try to adjust the
parameters so that you can simulate dierent types of rooms.
Use an FIR lter to model the so-called early-reections. This lter can be placed in
parallel with the above reverberator. In this case, the IIR/Allpass reverberator output should
be delayed so that the early reections reach the output before the IIR/allpass reverberated
signals.
1.3 Ring Modulation
Ring modulation consists in modulating a signal so that the resulting signal is still in the
audio range, but appears to have modied frequency content. The modulation consists in a
multiplication by a sine wave of frequency f0 , so that in the output, the frequencies appear as
shifts of the original frequencies by f0 . Ring modulation is implemented by a multiplication
in the time domain by a sinusoidal wave of frequency f0 .
In the extreme case, setting f0 to half of the sampling frequency has the eect of inverting
every second sample. The result is a frequency inversion, i.e. low frequencies become high
frequencies and vice-versa.
Let the sinusoid be s[n] = cos(2f0 /fs + ), where f0 is set to half of the sampling frequency
(fs ). Show that frequency inversion happens for this case. What are the requirements on the
phase for this to occur?
Implement a ring modulator. Test it on a speech signal. By adjusting the value of f0 , you
will be able to make speech sound robotic.
1.4 Vibrato, Flanging and Chorus
The eects of vibrato, anging and chorus are closely related in that they depend on the
implementation of fractional delays.
1.4. Vibrato, Flanging and Chorus 1-4
1.4.1 Varying Fractional delay implementation
The three eects studied in this section rely on a time-varying delay element. The input-
output relationship dening this element is given in time by:
y[n] = x[n D[n]],
where the dependence of the delay D on the time index has been made explicit. In order to enable
smooth variations of the delay D[n], one often has to consider non-integer values of D. The
values of a discrete-time signal between the sampling instants are obtained by interpolation.
More precisely, the value of x[n] at a non-integer time t is obtained by using ideal bandpass
reconstruction of the underlying analog signal, and by taking the value x(t) at the corresponding
time instant:
sin (t k)
x(t) = x[k] .
(t k)
k=
In practice, the innite sum is replaced by a simpler interpolation. For instance, linear interpo-
lation between sample points can be used. Fig. 1.3 shows a block diagram representation of a
variable delay.
Underlying
analog signal
x[n]
x[n] z D[n ] y[n]
0 0 . 75 1 2 3 n
(1) (b)
Fig. 1.3 (a) Block-diagram representation of a variable delay; (b) illustration of
interpolation in the time domain, where one wants to acquire the signal value at
time 0.75
1.4.2 Implementing the eects
Vibrato is an eect implemented directly by the structure in Fig. 1.3, where the delay D[n] is
periodically varied around an average value by a low-frequency oscillator (typically of frequency
515 Hz). This results in a periodic variation of the pitch (perceived frequency) of the signal.
Typical average delay values are between 5 and 10 ms.
Flanging is obtained by an FIR comb lter where the delay is made variable, as illustrated in
Fig. 1.4. The delay is again varied in a periodic way around some small value (less than 15 ms),
with a low frequency oscillator (around 1 Hz). The resulting signal will be aected by a very
distinctive periodic phase distortion.
1.5. Shelving Filter 1-5
x[n] y[n]
G1
G z D[n ]
x[n] z D[n ] y[n]
G2
z D[n ]
(a) (b)
Fig. 1.4 Block diagram of a (a) anging and (b) chorus eect implementation.
Chorus is an eect that simulates the presence of several sources playing in imperfect unison.
It is implemented by combining the original signal with several copies delayed by a varied number
of samples. Typical delays in the variable delay lines are 10 to 25 ms, and they are aected by
small, slowly changing random variations. Fig. 1.4 shows a possible implementation.
Implement all three eects in MATLAB.
1.5 Shelving Filter
Shelving lters are used to change the tonal characteristics of an audio signal. You will
examine a lowpass boost lter. The lter is shown in Fig. 1.5. The lter A(z) is an allpass lter
of the form
z 1
A(z) = ,
1 z 1
where the parameter is determined by the normalized cuto frequency,
1 tan(c /2)
= .
1 + tan(c /2)
The amount of boost will be determined by the value of K, where K varies between 0 and 1.
A( z )
1/ 2
K
Fig. 1.5 Shelving lter
1.6. A Few Hints 1-6
Show that the frequency response of the shelving lter is of the form
K 1
H(z) = [1 A(z)] + [1 + A(z)].
2 2
Choose c to give a cuto frequency of 300 Hz and nd the corresponding value of . This
value will depend on the sampling frequency of the audio signal. Note that not all of the
audio les have the same sampling frequency.
What is the amount of boost in dB for K = 10.
Plot the magnitude of the frequency response for value of found above and K = 10.
Implement the lter and try it out on an audio le.
1.6 A Few Hints
You will be processing a lot of data. Carefully write your MATLAB code so as to vectorize
as much as possible most of your operations. Consider using functions such as ones, zeros
and the operations ./, .*, to avoid for loops. Moreover, try to use conditions on vectors: for
example, a(a<0)=0 will clip all negative values in vector a to 0.
In order to load the audio les, use the MATLAB commands auread or wavread, depending
on the le type. Note that some of your les have a stereo recording. They will give you a
matrix with two rows or two columns. Select one of the channels for your processing, leaving
out the other.
In order to play a sound from MATLAB, use the command sound. Bring headphones to the
lab in order to listen to samples. SIMULINKs DSP Blockset contains an output to WAV
format block, that can also be directly output to the computers line out.
Write your code in reusable modules. For instance, the variable delay module appears several
times; implement it as a reusable module.
It is possible in MATLAB to program a anging script (linear interpolation) without any for
loop.