Professional Documents
Culture Documents
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));
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 :
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 :
{-10:10}
th :
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
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 = 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
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
Trumpet th:
th:
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
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(Y)));