You are on page 1of 22

H -tn: Tin Thnh-lp 09DT2 Nhm 38A

Lab 2a: Fourier Series and Gibbs Phenomenon


Exercise 1 : To mt hm c tn la sigsynth.m. load file m thanh trumpet.mat v ch y bng lnh sound vi tn s ly mu Fs=11025 Hz. Sau v mt phn ca tn hiu , khong 3 hoc hn 3 chu kz. Code : load trumpet.mat
Fs=11025; soundsc(trumpet,Fs) figure, plot(trumpet(1:400)); axis tight wavwrite(trumpet,Fs,'trumpet.wav') % looks very periodic

y ta v 9 chu kz th :

- Xem ph tn s ca tn hiu m thanh trn, bng cch s dung bin i FFT Code
load trumpet.mat Fs=11025; %soundsc(ed1,Fs) %figure, plot(ed1(200:400)); axis tight wavwrite(ed1,Fs,'trumpet.wav') x=wavread('trumpet'); % looks very periodic

Y = fft(x, 512); % take the fft of trumpet Ymag = abs(Y); % take the mag of Y f = Fs * (0:256)/512; % get a meaningful axis f=Fs*(0:256)/512;%getameaningfulaxis plot(f, Ymag(1:257)); % plot Ymag (only half the points are needed) xlabel('Frequency (Hz)') ylabel('Magnitude') axis tight

th :

Nhn ph ta thy hng lot cc nh khc nhau, chnh l giai iu ca m thanh. S dng con tr d liu ca s figure c thong tin cc nh. Ghi li cc tn s v bin , khoang 5-10 nh Code :t=0:1/Fs:3;
freq=[258.4 538.3 796.7 1055 1335 1593 1852]; mag=[11.8 37.8 65 52.3 52.6 39 28.3];

To mt hm tn l addcosines .m, mang 3 vector t, freq, mag. S dng vng lp cng cc tn hiu cosin ng vi mi cp freq/mag. Code :
function [ y ] = addcosines( t,freq,mag ) Fs=11025; sigsynth=0; for i=1:length(freq) sigsynth = sigsynth + mag(i)*cos(2*pi*freq(i)*t); end sigsynth=sigsynth/max(abs(sigsynth)); soundsc(sigsynth,Fs) wavwrite(sigsynth,Fs,'sigsynth1.wav');

end

th :

Play m thanh trumpet v sigsynth th ta thy 2 m thanh ny khc nhau. V mt phn ca tn hiu trumpet v sigsynth trn cng mt th dng lnh subplot. Code : load('trumpet');
sigsyn=wavread('sigsynth1.wav'); figure,subplot(2,1,1), plot(trumpet(100:400)); axis tight subplot(2,1,2), plot(sigsyn(100:400)); axis tight

th :

Exercise 2 : - To file phasefun.m. - Chn hai hi tn s v to ra tn hiu sig1 t 2 cosin bng cch cng cc tn s cng nhau. Chn tn s ly mu Fs=8000 Hz. Ngha l tn s hi phi nh hn Fs/2. c th khi phc li tn hiu ban u khi ly mu. V d : f1=120; f2=500;
sig1=cos(2*pi*120*t)+2*cos(2*pi*500*t);

By gi to ra tn hiu th hai sig2 ging tn hiu th nht, nhng thi gian delay bng na chu kz n. V d : f1=120; f2=500;
sig2=cos(2*pi*120*t)+2*cos(2*pi*500*(t-1/1000));

S dng subplot v mt vi chu kz ca hai tn hiu trn. Code : %phasefun


Fs=8000; t=0:1/Fs:1; sig1=cos(2*pi*120*t)+2*cos(2*pi*500*t); sig2=cos(2*pi*120*t)+2*cos(2*pi*500*(t-1/1000)); figure, subplot(2,1,1),plot(sig1(100:400)); subplot(2,1,2),plot(sig2(100:400)); soundsc(sig1,Fs); soundsc(sig2,Fs);

th :

Nhn th ta thy hai tn hiu ny khc nhau, v thi delay ca tn hiu th hai lm cho pha cua hai tin hiu ny khc nhau. S dng soundsc chy hai tn hiu trn ta thy hai tn hiu trn c m ging nhau, tuy hai tn hiu sig1 v sig2 c bin ging nhau nhng pha ca chng khc nhau nn lam cho hai tn hiu ny khc nhau. Nhng do s khc bit v pha l nh nn s thay i m thanh cng rt nh, tai ngi khng th phn bit c. Lm li sig2 vi cc khong delay khc nhau. Code : %phasefun
Fs=8000; t=0:1/Fs:1; sig1=cos(2*pi*120*t)+2*cos(2*pi*500*t); soundsc(sig1,Fs); sig2_1=cos(2*pi*120*t)+2*cos(2*pi*500*(t-1/1500)); soundsc(sig2_1,Fs) figure, subplot(2,1,1),plot(sig1(100:400)); subplot(2,1,2),plot(sig2_1(100:400));

th :

V d khc : Code : sig2_1=cos(2*pi*120*t)+2*cos(2*pi*500*(t-1/500));


soundsc(sig2_1,Fs) figure, subplot(2,1,1),plot(sig1(100:400)); subplot(2,1,2),plot(sig2_1(100:400));

th :

Da vo th ta thy tn hiu th hai vn khc tn hiu ban u. Nh vy vi pha thay i th tn hiu cng b thay i theo, nhng khng ng k. - To ra tn hiu sig3 l hm cosin t mt tn s no , sau ta cng tn hiu ny vi version khc ca sig3 c delay chu kz to ra tn hiu sig4 Code : sig3=cos(2*pi*250*t);
sig4=sig3+cos(2*pi*250*(t-1/(250*4)));

S dng subplot v 2 tn hiu sig3 v sig4 trn cng mt th, v dng hm soundsc chy chng. Code : sig3=cos(2*pi*250*t);
sig4=sig3+cos(2*pi*250*(t-1/(250*4))); figure, subplot(2,1,1),plot(sig3(100:400)); subplot(2,1,2),plot(sig4(100:400)); soundsc(sig3,Fs); soundsc(sig4,Fs);

th :

Exercise 3 : - To mt script tn l gibbs.m. - Download hm Ck.m, l hm to ra h s fourier Ck da vo : Ck={ * ( ) ( )+

V bin v pha ca h s Ck vi k Code : k=[-10:10];

{-10:10}

fct=Ck(k); mag=abs(fct); phase=angle(fct); figure, subplot(2,1,1), stem(mag) ,title('magnitude'); subplot(2,1,2),stem(phase) ,title('phase');

th :

To ra mt hm tnh tn hiu x(t) t chui fourier : X(t)= V i Code : t=-5:0.1:5;


k=[-kmax:kmax]; fct=Ck(k); mag=abs(fct); pha=angle(fct); sigsynth=zeros(1,length(t)); for i=kmax+1:length(k) sigsynth = sigsynth + 2*mag(i)*cos((i-kmax)*t+pha(i)); end

V x(t) trn don t(-5:5), vi kmax: 5,15,30. V v chng trn cng mt th. Code : k1=5;k2=15;k3=30;
t=-5:0.1:5; sig1=gibbs(k1); sig2=gibbs(k2); sig3=gibbs(k3); figure, subplot(3,1,1),plot(t,sig1),title('kmax=5'); subplot(3,1,2),plot(t,sig2),title('kmax=15'); subplot(3,1,3);plot(t,sig3),title('kmax=30');

th :

Nh ta thy th tn hiu khng phi l xung vung nh ta ngh m n s b mo dng, l hin tng gibbs, khi ta tng s lng cc h s fourrier th hin tng ny c ci thin. khc phc hin tng gibbs ta c th dng cc b lc to ra tn hiu vung, nhng thc t th khng th thc hin c m ch khc phc c mt phn no ca tn hiu thi

LAB 2b: Filtering Periodic Signals


Exercise 1: Code:
x=wavread('castanets44m'); X = fft(x); Fs=8000; N = length(x); freq = [-N/2+1:N/2]*Fs/N; figure; plot(freq,abs(fftshift(X))); % centered version of X

th:

a = 500*2*pi; w =[-N/2+1:(N/2)]; % centered frequency vector H = a ./ (a + 0.5*i*w); % centered version of H Hshift = fftshift(H); % uncentered version of H figure; plot(w*Fs/N,abs(H));

Y = X .* Hshift'; % filter the signal figure; plot(w*Fs/N,abs(fftshift(Y))); % centered version of Y

y = real(ifft(Y)); y = y * (max(abs(x))/max (abs(y))); sound(x, Fs); % original sound pause;sound(y, Fs); % low-pass-filtered sound

with a=1500;

dng tn hiu u ra

Exercise 2:
Code: x = repmat([zeros(1,99) 1], 1, 5); X = fft(x); N = length(x); a = 20*2*pi; w =[-N/2+1:(N/2)]; % centered frequency vector H = a ./ (a + 0.5*i*w); % centered version of H Hshift = fftshift(H); % uncentered version of H Y = X .* Hshift; y = real(ifft(Y)); t=[0:1/N:(1-1/N)]; figure; subplot(2,1,1); plot(t,x); xlabel('t'); ylabel('x'); subplot(2,1,2); plot(t,y); xlabel('t'); ylabel('x'); th:

Exercise 3:
Code: x=wavread('castanets44m'); X = fft(x); Fs=8000; N = length(x); freq = [-N/2+1:N/2]*Fs/N; figure; plot(freq,abs(fftshift(X))); % centered version of X

a = 2000*2*pi; w =[-N/2+1:(N/2)]; % centered frequency vector H = 1-(a ./ (a + 0.5*i*w)); % centered version of H Hshift = fftshift(H); % uncentered version of H figure; plot(w*Fs/N,abs(H));% centered version of H

Y = X .* Hshift'; % filter the signal figure;

plot(w*Fs/N,abs(fftshift(Y))); % centered version of Y

dng tn hiu u ra

y = real(ifft(Y)); y = y * (max(abs(x))/max (abs(y))); sound(x, Fs); % original sound pause; sound(y, Fs); % High-pass-filtered sound

with a= 1000;

dng tn hiu u ra

Exercise 4: Code:
b= wavread('bassdrum.wav'); B = fft(b); Fs=8000; N = length(b); freq = [-N/2+1:N/2]*Fs/N; figure; plot(freq,abs(fftshift(B)));

BASS

c= load('shake'); C = fft(c); Fs=8000;

N = length(c); freq = [-N/2+1:N/2]*Fs/N; figure; plot(freq,abs(fftshift(C)));

Trumpet th:

EX4 script: Code:


x = wavread('mixed.wav'); X = fft(x); N = length(x); Fs=8000; a = 200*2*pi; w =[-N/2+1:(N/2)]; % centered frequency vector H = 1-(a ./ (a + 0.5*i*w)); % centered version of H Hshift = fftshift(H); % uncentered version of H Y = X .* Hshift' .* Hshift' .* Hshift'; freq = [-N/2+1:N/2]*Fs/N; figure; plot(freq,abs(fftshift(X))); % centered version of X mixed.wav

th:

figure; plot(w*Fs/N,abs(fftshift(Y))); % centered version of Y

Bass drum filted

y = real(ifft(Y)); y = y * (max(abs(x))/max (abs(y)));

a2 = 200*2*pi; H2 = a2 ./ (a2 + 0.5*i*w); % centered version of H Hshift2 = fftshift(H2); % uncentered version of H Y2 = X .* Hshift2' .* Hshift2' .* Hshift2'; y2 = real(ifft(Y2)); y2 = y2 * (max(abs(x))/max (abs(y))); figure; plot(w*Fs/N,abs(fftshift(Y2))); % centered version of Y

Trumpet filted

sound(x); pause; soundsc(y); pause; soundsc(y2);

Exercise 5: Code:

input = wavread('mixedsig'); X = fft(input); Xshift = fftshift(X); N = length(input); Fs = 8000; w = (-N/2+1:(N/2)).*Fs/N; % centered frequency vector a = 1400*2*pi; a2=1650*2*pi; H1 = 1-(a ./ (a + 1i*w)); % high pass filter H2 = a2 ./ (a2 + 1i*w); % low pass filter H=H1 .* H2; H=H .* H .* H .* H.* H.* H; Hshift = fftshift(H); Y = Xshift .* Hshift'; % filter the signal y = real(ifft(Y)); y = y * (max(abs(input))/max (abs(y))); figure; plot(w,abs(H)); % centered version of H

figure; plot(w,abs(fftshift(X))); % centered version of X

figure; plot(w,abs(fftshift(Y)));

soundsc(input, 8000); pause;soundsc(y, 8000);

You might also like