Beruflich Dokumente
Kultur Dokumente
2014-2015
An introduction to Matlab:
sound manipulation
I
Introduction
II
Matlabs basis
II .1
Matlabs use
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.
Page 1 / 7
IMDEA Master 1
2014-2015
III
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);
Audio signal
3
Amplitude
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
4. Play the signal S1 by using the sound command, make sure to avoid clipping.
Solution:
Snc=S1/max(S1);
sound(Snc,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;
Page 3 / 7
IMDEA Master 1
2014-2015
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')
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:
Page 4 / 7
IMDEA Master 1
2014-2015
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.
Page 5 / 7
IMDEA Master 1
2014-2015
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);
Page 6 / 7
IMDEA Master 1
2014-2015
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.
Page 7 / 7