You are on page 1of 7

IMDEA Master 1

2014-2015

Signal Processing for EA - 167EN052


Practical #1

An introduction to Matlab:
sound manipulation
I

Introduction

MATLAB (matrix laboratory) is a numerical computing environment developed by Mathworks. It


allows matrix calculations, plotting of functions and data. A large number of toolboxes have been
developed for various backgrounds applications: engineering, science, and economics, etc.
There also exists free open source alternatives to MATLAB, in particular GNU Octave or Scilab that
you can use at home.

II

Matlabs basis

II .1

Matlabs use

There are two ways to use matlab:


1. The command window where you enter a statement and press return to run it:
>> 8/2
ans =
4

2. The script editor. A script is a file with a .m extension that contains multiple sequential lines
of MATLAB commands and function calls. For instance, the script given below plots a series
of 50 random numbers.
n = 50;
r = rand(n,1);
plot(r)

II .2

Need help?

To get help on a particular function, type help followed by the function name. For instance, to get
help on the sine function:
>>help sin
SIN
Sine of argument in radians.
SIN(X) is the sine of the elements of X.

You can also type doc sin to get more information.

B. Gazengel & M. Melon

Page 1 / 7

IMDEA Master 1
2014-2015

III

Signal Processing for EA - 167EN052


Practical #1

Mono audio signals

Your work is to create, plot, play and reverse mono audio signals.
1. Vector definition: create a discrete time vector t sampled at Fs = 44110 Hz that lasts 2
seconds.
Solution:
Fs=44100;
t=0:1/Fs:2;

2. Create the following synthetic sound S1 = cos(1 t) + cos(2 t) + sin(3 t), where the pulsations
i corresponds to the frequencies: f1 = 250, f2 = 600, f3 = 1000.
Solution:
f1=250; f2=600; f3=1000;
S1=cos(2*pi*f1*t)+cos(2*pi*f2*t)+sin(2*pi*f3*t);

3. Plot signal S1 and provide the following features:


Title: Audio signal
Labels for x (Time (s)) and y (Amplitude) axis
A grid
Time scaling ranges from 0 to 0.1 second while amplitude scaling ranges from -3 to +3
The curve should be plotted in red
Solution:
figure (1) ; plot(t,S1,'r') ; axis([0 .1 -3 3])
xlabel('Time (s)'); ylabel('Amplitude'); title('Audio signal'), grid on

Audio signal
3

Amplitude

B. Gazengel & M. Melon

0.01

0.02

0.03

0.04

0.05
Time (s)

0.06

0.07

0.08

0.09

0.1

Page 2 / 7

IMDEA Master 1
2014-2015

Signal Processing for EA - 167EN052


Practical #1

4. Play the signal S1 by using the sound command, make sure to avoid clipping.
Solution:
Snc=S1/max(S1);
sound(Snc,Fs);

If you have not normalized S1, you can use soundsc(S1,Fs).


5. Assign the letter S to the mono sound in the file flamenco2.wav. Time reverse the sound before
and play it.
Solution:
[S,Fs]=wavread('flamenco2');
Sf=flipud(S); % as the size of S is N by 1 use flipud, for line vector use ...
fliplr
soundsc(Sf,Fs)

IV

Stereo signal

Your work consists in creating a stereo signal with adjustable balance and delay.
1. Create and plot a 2 second stereo signal S for which the right SR and left SL channels are given
by SR = SL = cos(2f1 ) with f1 = 250 Hz. The stereo signal is sampled at 44.1 kHz. Your plot
should look like the following figure:
Left Channel
1

Amplitude

0.5
0
0.5
1
0

0.01

0.02

0.03

0.04

0.05
Time (s)

0.06

0.07

0.08

0.09

0.1

0.06

0.07

0.08

0.09

0.1

Right Channel
1

Amplitude

0.5
0
0.5
1
0

0.01

0.02

0.03

0.04

0.05
Time (s)

Solution:
Fs=44100;
f1=250;
t=0:1/Fs:2;

B. Gazengel & M. Melon

Page 3 / 7

IMDEA Master 1
2014-2015

Signal Processing for EA - 167EN052


Practical #1

SR=1*cos(2*pi*f1*t);
SL=SR;
S=[SL ;SR];
figure(1)
subplot(2,1,1)
plot(t,SL,'b'); axis([0 .1 -1.2 1.2])
title('Left Channel')
xlabel('Time (s)'); ylabel('Amplitude')
subplot(2,1,2);
plot(t,SR,'r'); axis([0 .1 -1.2 1.2])
title('Right Channel')
xlabel('Time (s)'); ylabel('Amplitude')

2. Modify the stereo signal S to provide the following features:


Adjustable delay t in ms applied to the left channel.
Adjustable gain: GdB = 20 log (AL /AR ), where AL and AR respectively are the maximum
left and right channel amplitudes. As GdB can positive or negative, adjust levels so that
the loudest one has a unity amplitude.

Solution:
Fs=44100;
t=0:1/Fs:2;
f1=250;
delt=0;
GdB=10;
if GdB<0;
SR=cos(2*pi*f1*t);
SL=10^(-GdB/20)*cos(2*pi*f1*(t-delt*1e-3));
else
SR=10^(-GdB/20)*cos(2*pi*f1*t);
SL=cos(2*pi*f1*(t-delt*1e-3));
end
S=[SL;SR];

3. Adjust t and GdB to move the stereo from the center to the left or right. Use the command
sound(S,Fs) to listen to the stereo signal. Try to find the maximum delay and gain from which
the stereo images does no move anymore.
Solution: Here are the curve obtained for with a stereo loudspeaker system:

B. Gazengel & M. Melon

Page 4 / 7

IMDEA Master 1
2014-2015

Signal Processing for EA - 167EN052


Practical #1

You should find values in these range. Headphone listening can provide issues as the stereo
image is usually located in the head. Do not use too large delays: with sinusoidal signal, a
phase delay can turn into a phase lead.
4. Open the mono file remurdered_intro.wav. Create a Matlab script that pan this sound left to
right and then right to left the stereo image.
Solution: This is a script example:
[L,Fs]=wavread('remurdered_intro');
Len=length(L);
R=L;
t=0:1:Len-1;
GdB=-16;
Lfade=ones(Len,1);
Rfade=Lfade;
Lfade(1:ceil(Len/2))=1+2*(10^(GdB/20)-1).*t(1:ceil(Len/2))/Len;
Lfade(ceil(Len/2)+1:Len)=2*10^(GdB/20)-1 ...
+2*(1-10^(GdB/20)).*t(ceil(Len/2)+1:Len)/Len;
Rfade(1:floor(Len/2))=Lfade(ceil(Len/2)+1:Len);
Rfade(floor(Len/2)+1:Len)=Lfade(1:ceil(Len/2));
R=R.*Rfade;
L=L.*Lfade;
Stereo=[L R];
player = audioplayer(Stereo,Fs); play(player)

Artificial reverberation

You will find in the UMTICE environment an excerpt of an anechoic recording of a blues song entitled
blues.wav [1] . In this exercise, you will test different ways to add reverberation to an audio signal
1. Build the following impulse response : h = et/ where is the time room constant. The rooms
reverberation time TR is linked to by the following equation: TR = 13.8 .
Use the following parameters: h has a duration of 4 s and is sampled at Fs = 44.1. TR = 1 s.

B. Gazengel & M. Melon

Page 5 / 7

IMDEA Master 1
2014-2015

Signal Processing for EA - 167EN052


Practical #1

Solution:
Fs=44100;
th=0:1/Fs:4';
TR=1;
tau=TR/13.8;
h=exp(-th/tau);

2. By using convolution, add reverberation to the blues signal with h. Listen to the result for
several TR values. Explain why the result is not very convincing.
Solution:
[S,Fs]=wavread('blues')
blues_rev=conv(h,S);
sound(blues_rev/max(blues_rev),Fs) %don't forget to normalize the signal ...
before playing it to avoid clipping.

The exponential decay curve model the time dependence of the acoustical intensity, which is
proportional to the squared pressure, and not to the pressure. Moreover, the et/ shape is
the impulse response of a low frequency filter. You can hear the high frequency attenuation
in the convolution result.
3. To obtain a more a convincing result, you can multiply the exponential decay by a random signal
to simulate the diffuse field. Use this approach to compute a new reverberated sound and listen
to it.
Solution:
[S,Fs]=wavread('blues');
th=0:1/Fs:4;
TR=1;
tau=TR/13.8;
h=exp(-th/tau)';
nois=rand(1,length(h))';
h=h.*nois;
blues_rev=conv(h,S);

Real room impulse responses generally exhibit two different domains:


A sparse one at the beginning including the direct sound and early reflections;
A dense one at the end that corresponds to the diffuse field.
4. Build a room impulse response h with a 100 ms spare part and a 2 s dense part.
B. Gazengel & M. Melon

Page 6 / 7

IMDEA Master 1
2014-2015

Signal Processing for EA - 167EN052


Practical #1

The sparse part is given by: h(t) =

Ai = [1, 0.8, 0.85, 0.7, 0.75, 0.72, 0.65].

Ai (t ti ) with ti [25, 40, 50, 70, 80, 85, 92] ms and

The diffuse field will be modeled by a random series multiplied (with unity maximum
amplitude) by the following exponential decay 0.6 et/ , with TR = 1 s.
Plot h.
Solution:
Fs=44100;
ti=[25 40 50 70 80 85 92];
Ai=[1 .8 .85 .7 .75 .72 .65];
h_i=zeros(floor( .1 * Fs ),1);
len_ref=length(ti);
for it=1:len_ref
h_i(floor(ti(it)*Fs/1000))=Ai(it);
end;
Tr=1;
tau=Tr/13.8;
th=0:1/Fs:2;
h=.6*exp(-th/tau)';
nois=rand(1,length(h))';
h=h.*nois/max(nois);
h=[h_i ; h];

1
0.9
0.8

0.7

Amplitude

0.6

0.5
0.4
0.3

0.2
0.1

5
Time (s)

10
4

x 10

Listen to the different reverberated audio signals and tell which one is your favorite.

References
[1] Cologne University of Applied Sciences - Anechoic Recordings, Michio Woirgardt, Philipp
Stade, Jeffrey Amankwor, Benjamin Bernschtz and Johannes Arend, 2012.

B. Gazengel & M. Melon

Page 7 / 7

You might also like