You are on page 1of 7

Bo co lab2 th nghim

DSP
Exercise 1
1. Sa i ham fade
Matlab Code:
function [ y ] = fade(x, level)
%
% fade(x): this function fades the audio vector x.
%
% Example:
% y = fade(x);
if (level <= 1 && level >= 0)
% create the ramp vector
t = linspace(0, 1, length(x));
% multiply the audio vector with the ramp vector to fade
y = (1 - (t)*level) .* x;
else
error('input has to be between 0 and 1');
end

2. Thc hin ham fade vi song sin


time = 0:0.01:1;
y = cos(time * pi * 25);
subplot(2,1,1);
plot(time, fade(y,0.5));
subplot(2,1,2);
plot(time, fade(y,0.9));

BO CO LAB2 DSP
08DT3-NHM 15

PHAN VN NG -L P

1
0.5
0
-0.5
-1

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1
0.5
0
-0.5
-1

Nhn xt:
Hm fade c tc dng suy gim tn hiu theo thi gian
y yu cu bin vo level phi nm trong khong t 0 n 1 nn khi vit hm matlab
phi s dng lnh if else

Exercise 2:
1. Vit ham repeat
function [ out ] = repeat( in,N )
%
% repeat(in,N): this function repeat the audio vector x N times.
% N must be positive integers.
%
if N>0
out=in;
for index=1:N
out=[out in];
end
else
error('N must be greater than 0');
end
end

BO CO LAB2 DSP
08DT3-NHM 15

PHAN VN NG -L P

2. Thc hin ham repeater i vi song sin


>> time=0:0.01:1;
>> y=cos(pi*25*time);
>> plot(repeat (fade(y,0.9),5))
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

100

200

300

400

500

600

Nhn xt:
Hm repeat c tc dng lp li tn hiu N ln

y ta dng lnh vng lp for do matlab cung cp ni cc tn hiu lp li. y ta


thao tc vi tn hiu s nh cc vecto bi v tn hiu s c th coi l cc vecto cha cc
gi tr c to ra t qu trnh ly mu tn hiu lin tc
y l mt im mnh ca matlab khi x l tn hiu s
3. Ham tao khoang lng gia mi ln lp lai.
function [ out ] = repeater( in,N,M)
%
% repeat(in,N): this function repeat the audio vector x N times.
% insert silence between each repetition
% M: numer of zeros add between each repetition
% N and M must be positive integers.
%
if ((N>0)&&(M>0))
out=in;
for index=1:N
out=[out zeros(1,M) in];
end

BO CO LAB2 DSP
08DT3-NHM 15

PHAN VN NG -L P

else

error('N and M must be greater than 0');

end
end
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

200

400

600

800

1000

1200

1400

Nhn xt: Hm repeater tng t hm repeat ch khc l c thm khong lng gia

cc ln lp li

Exercise 3:
1. Thc hin hm delay
function [ output ] = delay( signal, delaytime, fs )
numberofzeros = delaytime .* fs
r = zeros(1,numberofzeros);
output = [r signal];
end

2. Thc hin delay 2s i vi tn hiu m thanh hatclosed

x1 = wavread('hatclosed')';
subplot(2,1,1);
plot(x1);
subplot(2,1,2);
plot(delay(x1,2,8000));

BO CO LAB2 DSP
08DT3-NHM 15

PHAN VN NG -L P

1
0.5
0
-0.5
-1

500

1000

1500

2000

2500

3000

3500

4000

1
0.5
0
-0.5
-1

0.2

0.4

0.6

0.8

1.2

1.4

1.6

1.8

2
4

x 10

Nhn xt:
Hm delay c tc dng lm tr tn hiu s mt khong thi gian. V tn hiu s l mt vecto cc
gi tr c ly mu nn iu ny tng ng vi vic chn thm mt vecto hng zeros trc
tn hiu gc. S lng cc phn t c thm vo ph thuc vo thi gian delay v tn s ly
mu theo cng thc numberofzeros = delaytime .* fs
Tn s ly mu ca cc tn hiu trong file source u l 8000 nhng tin thay i nu cn
thit ta vn cho vo hm bin Fs

Exercise 4:
1.thc hin hm mixer.
function [ output ] = mixer( x1, x2 )
% mixer( x1, x2): this function add two sound vector together
l1 = length(x1);
l2 = length(x2);
diff = abs(l1-l2);
zero = zeros(1,diff);
if (l1>=l2)

BO CO LAB2 DSP
08DT3-NHM 15

PHAN VN NG -L P

x22 = [x2, zero];


output = x1 + x22;
else
x11 = [x1, zero];
output = x2 + x11;
end
max1 = max(output);
output = output/max1;
end

3. Thc hin cng 2 tn hiu m thanh 'bleeep' v 'bassdrum'


x1 = wavread('bleeep')';
x2 = wavread('bassdrum')';
subplot(3,1,1);
plot(x1);
subplot(3,1,2);
plot(x2);
x3=mixer(x1,x2);
subplot(3,1,3);
plot(x3);

1
0.5
0
-0.5

500

1000

1500

2000

2500

3000

3500

4000

500

1000

1500

2000

2500

3000

3500

4000

500

1000

1500

2000

2500

3000

3500

4000

-1
1

-1

Nhn xt:
Hm mixer l hm cng 2 tn hiu m thanh vi nhau. V thao tc l trn vecto nn 2 tn
hiu phi cng di. lm iu ny ta thm vecto hng 0 pha sau vecto c chiu di nh hn

Exercise 5:
BO CO LAB2 DSP
08DT3-NHM 15

PHAN VN NG -L P

x1 = wavread('snare')';
x2 = wavread('bassdrum')';
x3 = repeater(x1, 2);
x4= delay(mixer(x1,x2),0.3,8000);
x5=mixer(x4,x3);
x6=repeater(x5, 15)
x7=mixer(x6,xn);
Fs=8000;
sound(x7,8000);
plot(x7)
wavwrite(x7, 'pro.wav');

0.5

-0.5

-1

-1.5

0.5

1.5

2.5
5

x 10

Nhn xt:

To mt bn nhc t cc hm pha trn v cc on nhc ngn c sn. ngi nghe kp


cm nhn ta dng hm repeat lp li cc on m thanh. ghp cc m thanh li ta
dng hm mixer nh khi mt ngi nhc s ho m. trnh cc m thanh chng ln
nhau hn n ta dng hm delay.

BO CO LAB2 DSP
08DT3-NHM 15

PHAN VN NG -L P