You are on page 1of 8

ĐẠI HỌC ĐÀ NẴNG

TRƯỜNG ĐẠI HỌC BÁCH KHOA

KHOA ĐIỆN TỬ- VIỄN THÔNG

---- d & c ----

BÁO CÁO LAB2:

Functions in
MATLAB and the
Groove Station

Giáo viên hướng dẫn: Nguyễn Hải Triều Anh

Sinh viên thực hiện: Phan Quốc Thắng

Lớp : 08DT3

Nhóm: 14B
1. Exercise 1: Fader

a. Hàm fade:
Matlab Code:

function [ y ] = fade(x, level)


% fade(x): this function fades the audio vector x.
% 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

b. Hàm fade với sóng sin:


Matlab Code:

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

0.5

-0.5

-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1

0.5

-0.5

-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Nhận xét: hàm fade có tác dụng làm tín hiệu sóng sin suy giảm theo thời gian.

2. Exercise 2: Repeater
a. Hàm repeat:
Mat lab code:

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

b. Hàm repeater đối với sóng sin:

time=0:0.01:1;
y=cos(pi*25*time);
plot(repeat (fade(y,1),5))

0.8

0.6

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1
0 100 200 300 400 500 600 700
Nhận xét: Hàm repeat có tác dụng lặp lại tín hiệu N lần
c. Hàm tạo “khoảng lă ̣ng” repeater :
Mat lab code:

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
else
error('N and M must be greater than 0');
end
end

0.8

0.6

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1
0 200 400 600 800 1000 1200 1400

Nhận xét: Khi sử dụng hàm repeater thì có thêm khoảng lặng giữa các lần lặp lại
3. Exercise 3: Delay
a. Hàm delay:
Code matlab:

function [ output ] = delay( signal, delaytime, fs )


numberofzeros = delaytime .* fs
r = zeros(1,numberofzeros);
output = [r signal];

end

b. Delay 2s đối với tín hiệu âm thanh ‘hatclosed’:


Code matlab:

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

0.5

-0.5

-1
0 500 1000 1500 2000 2500 3000 3500 4000

0.5

-0.5

-1
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
4
x 10

Nhận xét: Hàm delay có tác dụng làm trễ tín hiệu số một khoảng thời gian.
4. Exercise 4: Mixer

a. Hàm mixer.
Code matlab:

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)
x22 = [x2, zero];
output = x1 + x22;
else
x11 = [x1, zero];
output = x2 + x11;
end
max1 = max(output);
output = output/max1;

end

b. Cộng 2 tín hiệu âm thanh 'bleeep' và 'bassdrum'


Code matlab:

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.5
0 500 1000 1500 2000 2500 3000 3500 4000
1

-1
0 500 1000 1500 2000 2500 3000 3500 4000
1

-1
0 500 1000 1500 2000 2500 3000 3500 4000

Nhận xét: Hàm mixer là hàm để cộng 2 tín hiệu âm thanh với nhau

5. Exercise 5:
Code matlab:

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');
1

0.5

-0.5

-1

-1.5
0 0.5 1 1.5 2 2.5
5
x 10

Nhận xét: Dùng hàm repeat để lặp lại các đoạn âm thanh. Dùng hàm mixer để ghép các âm
thanh lại. Để tránh các âm thanh chồng lên nhau hỗn độn ta dùng hàm delay

You might also like