You are on page 1of 19

<LAB1.

a>
Exercise 1
a) Thay tn cua file dampedCosine.m thanh ThaoNguyen_dampedCosine.m
diary your_nameex1
%INSERT your name and section into these display commands
disp('NAME: Thao_Nguyen')
disp('SECTION: Thao-_section')
datestr(cputime)
%dampedCosine.m produces a plot of a cosine with frequency
0,5Hz and 1 Hz, with amplitude
% scaled by a decaying exponential
figure
x = -5:0.01:5;
y2=exp(-abs(x)).*cos(pi*x);
subplot(2,1,1);
plot(x,y2);
xlabel('thoi gian');
ylabel('bien do');
title('f(x)=exp(-|x|)cos(pi*x)');
subplot(2,1,2);
y1=exp(-abs(x)).*cos(2*pi*x);
plot(x,y1);
xlabel('thoi gian');
ylabel('bien do');
title('f(x)=exp(-|x|)cos(2*pi*x)');
diary off

RESULT:

b) Tao hai tin hi u hinh sin vi chu ky gp i ( tn s giam i m t na) so vi


ngun ban u.
diary your_nameex1
%INSERT your name and section into these display commands
disp('NAME: Thao_Nguyen')
disp('SECTION: Thao-_section')
datestr(cputime)
%dampedCosine.m produces a plot of a cosine with frequency
is reduced 2 times, with amplitude
% scaled by a decaying exponential
figure
x = -5:0.01:5;

y2=exp(-abs(x)).*cos(0.5*pi*x);
subplot(2,1,1);
plot(x,y2);
xlabel('thoi gian');
ylabel('bien do');
title('f(x)=exp(-|x|)cos(0.5*pi*x)');
subplot(2,1,2);
y1=exp(-abs(x)).*cos(2*pi*x);
plot(x,y1);
xlabel('thoi gian');
ylabel('bien do');
title('f(x)=exp(-|x|)cos(pi*x)');
diary off

RESULT:

Exercise 2:

a Tao tin hiu co tn s F = 4Hz va d = 4 bng lnh compexp(4,4).


RESULT:

Tao tin hiu co gia tri tn s giam i mt na va nhn xet thi


compexp(2,4).

Nhn xet:

Ta co th thy khi khng co mc level tin hiu se suy giam v


mc khng con nu s dung mc level thi tin hiu se c ghim gi
lai mc level.

Nu mc level bng 1 thi ta se thu c tin hiu tun hoan.

Nu khng s dung mc level thi thc cht la mt trng hp


c bit vi level bng 0.

<LAB1.b>

Exercise 1<Fader>.
a) Yu cu thc hin

Chnh sa lai function fade.m km theo ngun thc hin vic iu

chnh dc ca tin hiu da vao bc ca ham function.


S dung gia tri bin level ( trong ham fade.m) chn mc suy

giam dn ca tin hiu. Va gia tri level nm trong khoang [0..1]


b) Kt qu thc hin.
- Tin hiu ph hp vi handout
- Function fade2.m
function [ y ] = fade2(x, level)
%
% x: vector am thanh dau vao
% level : slope of ramp
if((level<0) |(level>1))
fprintf('nhap lai level trong khoang [0,1]\n');
y=[];
%Thong bao loi neu level nam ngoai khoang [0,1]
end
% Thuc hien ramp vector khi gia tri level phu hop voi yeu cau.
if((level>=0) & (level<=1))
t = linspace(0,level, length(x));
% Nhan vector am thanh voi vector ramp de thuc hien suy giam
%y = (1 - (t')) .* x; % Truong h?p tin hieu am thanh
y = (1 - (t)) .* x; %Doi voi truong hop la vector test nhu handout
end
- s dng hm trn vi tn hiu sng sine xem kt qu
>> t=0:0.01:1;
>> x=cos(t.*pi.*25);
>> y=fade(x,0.5);
>> suptitle ('Khao sat ham fade');
>> subplot(2,1,1);
>> plot(x);
>> xlabel('Truoc khi fade')
>> subplot(2,1,2);
>> plot(t,y);
>> xlabel('Sau khi fade');

RESULT:

- Khao st fade funcition


t=0:0.01:1;
x=cos(t.*pi.*25);
y1=fade(x,1);
suptitle ('Khao sat ham fade');
subplot(2,1,1);
plot(t,y1);
xlabel('Muc level =1');
y2=fade(x,0.2);
subplot(2,1,2);
plot(t,y2);
xlabel('Muc level =0.2');

RESULT:

c) Nhn xt kt qu.
- Trn thc t tin hiu sau khi c fade se co nhng mu phia sau suy
giam hn nhiu so vi tin hiu gc. iu nay co th chng minh c
-

trn thi va kt qua ca ca x,y.


Gia tri level trong khoang t 0-1, mc level cang tng thi tin hiu suy
hao cang ln va nhanh!

Exercise 2<Repeater>.
a) Yu cu thc hin:

- Tao function thc hin lp tin hiu m thanh N ln. Dng vng lp thc
hin n. Bn trong vng lp for chng ta cn ghp tin hiu c tin hiu sau
cng.
- Vi d, nu mun ghp 2 vector x,y nh bn di c mt vector thi ta thc
hin nh sau:
>> x = [1 4 2 2 3];
>> y = [5 8 3 9 0];
>> x = [x y];

b) Kt qu thc hin:
- Function: repeat.m thc hin repeat.

function [y] = repeat(x,N)


%Function thuc hien chuc nang repear tin hieu len N lan
y=[];
% Khoi tao gia tri ban dau cho tin hieu output.
% Voi dieu kien N phai la so nguyen v lon >=1 moi thuc hien
repeat.
if (N>=1)
for i=1:1:N
y=[x; y] ;
end
else
fprintf('N phai lon hon hoac bang 1\n');

end

Vi file m thanh khao st la bassdrum.wav khi thc hin oan script repeat
la 5 times.
>>x=wavread('bassdrum');
>>[y]= repeat(x,5);
>>suptitle('Khao sat ham repeat');
>>subplot(2,1,1);
>>plot(x);
>>xlabel('Bassdrum');
>>subplot(2,1,2);
>>plot([y]);
>>xlabel('Repeat bassdrum 5 times');
RESULT:

a. Nhn xt kt qu
Kt qua tin hiu sau khi c qua ham repeat se c lp lai theo N ln
yu cu.
Bin ch lp lai theo s ln quy inh ch khng co hin tng bi bin
i dang tin hiu.
Exercise 3 <DELAY>.
a)
-

Yu cu thc hin:
Tao 1 function lam tr tin hiu. Vi chng ta ang lam vic vi tin
hiu s nn chng ta co th thc hin mt cach n gian o la them s

0 vao ng trc tin hiu mun lam tr.


u vao ca ham delay bao gm tin hiu va thi gian delay cn thc
hin.

- S lng s 0 cn them vao trc tin hiu chinh la s thi gian delay va s mu
tng ng. i vi cc bai chng ta khao st thi t l mu la 8000 mu/s.
b) Kt qu thc hin:
-

Function delay.m thc hin delay tin hiu:

function [y] = delay(x,t,Fs )


%function thuc hien chuc nang delay tin hieu
% Cac yeu cau dau vao gom: tin hieu x, thi gian can de lay:
t(s)
%Tan so Fs.
m=zeros(t*Fs,1);
% so so 0 can them vao truoc tin hieu
y =[m;x];
end
% Doi voi truong hop tan so Fs=8000 la co dinh chung ta thuc
%
%
%
%
%

hien function
lai nhu sau:
function [y] = delay(x,t)
m=zeros(t*8000,1);
y =[m;x];
end

Vi file m thanh khao st la bleeep.wav khi thc hin oan scriptd


delay la 3 chu ky. Va tn s Fs da vao file cho.
x=wavread('bassdrum');
size(x,1)
ans = 4000;
Fs=2*4000;
y=delay(x,3,Fs);
subplot(2,1,1);
plot(x);
title('Do Thi Cua Tin Hieu Truoc Khi fade');
ylabel('Bien Do Tin HIeu');xlabel('So Mau Tin Hieu');
subplot(2,1,2);
plot(y)
ylabel('Bien Do Tin HIeu');xlabel('So Mau Tin Hieu');
title('Do Thi Cua Tin Hieu Sau Khi Delay 3 Chu Ky ');

c) Nhn xt kt qu.
- Ham delay ch lam tr tin hiu i mt khoang theo yu cu ch khng lam bin
dang tin hiu gc.
Exercise 4. Mixer.
a) Yu cu thc hin
- Tao 1 function thc hin chc nng cng 2 vector m thanh.
- Function cn thc hin s c th x l c cc yu t u vao khng cng kich c.
Va s phai re-scale lai tin hiu cho n khng nm ngoai pham vi [-1,1].
b) Kt qu thc hin
- Function thc hin vic add ng thi re-scale tin hiu tin hiu ra khng
vt ngoai khoang [-1,1]

function [out] = mixer(x1,x2)


%tao hai tin hieu co do dai bang nhau
a = length(x1) - length(x2);
if a>0
x2 = [x2;zeros(a,1)];
else
x1 = [x1;zeros(-a,1)];
end
x = x1 + x2;% tron hai tin hieu
xmin = min(x(:));
xmax = max(x(:));
slim = [xmin,xmax];
v = diff(slim);
if v==0
out = zeros(size(x));
else
out = (x-slim(1))/v*2-1;
end

x = wavread(bleeep);plot(x);

y = wavread('bassdrum');
plot(y)

v = mixer(x,y);
>> plot(v)

Nh n xt :
Chng ta c th thy khi mixer hai tin hi u m thanh vi nhau cc phn tin hi u
trng nhau s c tng cng ln nhau nhng khng vt qu gi tri [-1;1] do bi
gii han, phn nao vt qua s bi xn bt.
Exercise 5: Groove
Yu cu thc hin

Tap 1 script (khng phai function) xy dng oan m thanh t cc function c


hoc la tao cc function b sung nu mun. Ni cc phn lai vi nhau uc mt
oan m thanh hoan chnh. Sau khi hoan thanh vic ghp ni cc oan vi nhau ta s

dng command wavwrite lu oan nhac thc hin.


- Tao file m thanh c chiu dai t 10 cho n 30 giy.
- V thi tin hiu cua file tao ra.
a. Kt qu thc hin:
- Vi cc function c bao gm nh: addsound, fade, delay, repeat. Va mt s file m
-

thanh c sn hoc la tim kim thm.


ng thi c th tao file m thanh t cc file ngun sn c.

Tng hp lai cc function trn vi cc tin hi u c trong lab1b.


a=wavread('bassdrum');
b=wavread('bleeep');
c=wavread('snare');
d=wavread('hatclosed');
out=[];
x1=repeat(b,10);
out=[out;fade(x1',0.9)'];
x2=delay(d,1,8000);
out=[out;x2];
out=[out;repeat(a,10)];
Kt qua:
wavwrite(out,'result');
plot(out);

Cch tao tin hi u trn :

u tin ta l p lai tin hi u b 10 ln.


Sau gii han mc tin hi u mc level = 0,9.
Lam tr tin hi u d vi tn s fs = 8000Hz.
Cui cng la kt hp tin hi u mi thu c va tin hi u a c l p lai 10 ln
vao tin hi u ra out.
Nhn xt:

- File m thanh uc tao ra ban u mi ch la m n 1 chiu.


- Do mi s dng cc function n gian nn trong tit tu cua file cng cn n
gian.

You might also like