root/trunk/tests/farina.m @ 191

Revision 191, 5.3 KB (checked in by yomguy, 2 years ago)

Fix bad english...

RevLine 
[163]1% Cette routine permet de comparer trois méthodes de traitement
2% des sinus glissants (glissement logarithmique)
3% Le signal d'excitation est généré selon la méthode proposée par Farina
4% les trois méthodes de traitement sont :
5% - Fonction de Transfert
6% - Déconvolution par filtre inverse
7% - Déconvolution par filtre inverse avec passage dans le domaine
8% fréquentiel
9% Le système de test est un filtre passe-bas (type RC)
10% les paramètres à faire varier sont :
11% f1 et f2, les fréquences encadrant le signal généré
12% nwin le nombre de points sur lequel on fenêtre le signal (en début et en fin)
13% tmax : la durée du sinus glissant
14
15clear all
16close all
17%input parameters
18f1=1; % start frequency (Hz)
[190]19f2=5000; %end frequency(Hz)
[163]20tmax=.5; % excitation signal duration (s)
21nwin=100; % windowing point number (half at the begining - half at the end)
22
23%chirp generation (Farina)
24fs=44100; % sampling frequency (Hz)
25dt=1/fs; % sampling period
26t=0:dt:tmax; % time vector
27om1=2*pi*f1; % begining pulsation
28om2=2*pi*f2; % ending pulsation
29K=tmax*om1/log(om2/om1);
30L=tmax/log(om2/om1);
31yc=sin(K*(exp(t./L)-1)); % excitation signal (logarithmic swept signal)
32
33%chirp windowing
34win=hanning(nwin);
35siz=length(yc);
36wint=ones(1,siz);
37wint(1:nwin/2)=win(1:nwin/2);
38wint(siz-nwin/2+1:siz)=win(nwin/2+1:nwin);
39y=yc.*wint; %windowed swept sine
40
41%Test on a low pass filter: processing of the filtered signal (iterative
42% method)
43fc=400; % cut-off frequency
44RC=1/2/pi/fc;
45alf=dt/(dt+RC);
46yf(1)=y(1);
47for n=2:siz
48   yf(n)=alf*y(n)+(1-alf)*yf(n-1); % filtered signal
49end
50
51%plot of the input and filtered signals
52figure (1)
53plot(t,y,'b');
54xlabel('Time (s)');
55hold on
56plot(t,yf,'r')
57title('Input signals')
58grid on
59h = legend('Input signal','filtered signal',2);
60set(h,'location','SouthWest')
61
62% Impulse response: Inverse filter deconvolution (time domain)
63yinv=fliplr(y);
64fl=logspace(log10(f1),log10(f2),siz);
65yinv=yinv./fl; % inverse filter
66tic
67IR=conv(yf,yinv); % processed impulse response
68toc
69sizi=length(IR);
70is=fft(y).*conj(fft(yinv));
71norm=sqrt(sum(abs(is.^2))/siz);
72yeff=norm*sqrt(fs/2/(f2-f1));
73IR=IR*fs/yeff;
74t2=(-siz+1:1:siz-1).*dt;
75figure(2)
76subplot(3,1,1)
77plot(t2,IR,'b')
78grid on
79xlabel('samples')
80title('Impulse response')
81ylabel('Deconvolution')
82IRT=exp(-t2/RC)/RC;
83IRT(1:siz-1)=0;
84hold on
85plot(t2,IRT,'r')
[173]86legend('Processed','Theory',3)
[163]87
88% frequency response : Inverse filter deconvolution (time domain)
89FR=fft(IR)/fs;
90FR=FR(1:sizi/2);
91fconv=0:fs/sizi:fs/2;
92fconv=fconv(1:sizi/2);
93LFR=20*log10(abs(FR));
94figure(3)
95subplot(3,1,1)
96semilogx(fconv,LFR)
[191]97ylabel('Transfer function (deconvolution)')
[163]98title('Frequency response (dB)')
99axis([10 20000 -40 +5])
100grid on
101hold on
102FRT=abs(1./(1+j*2*pi*fconv*RC));
103LFRT=20*log10(FRT);
104semilogx(fconv,LFRT,'r')
105xlabel('Frequency (Hz)')
106legend('Processed','Theory',3)
[173]107imin=round(f1/fs*sizi)+1;
108imax=round(f2/fs*sizi)+1;
109E1=sqrt(sum(abs(FRT(imin:imax)-FR(imin:imax)).^2./(abs(FRT(imin:imax)).^2)))*100/(imax-imin+1);
[191]110E1t = strcat('Quadratic error :',num2str(E1,2),' %');
[173]111text(3000,-5,E1t);
[163]112
113%Transfer function method
114specy=fft(y);
115specyf=fft(yf);
116TF=specyf./specy;
117IR2=ifft(TF)*fs;
118figure(2)
119subplot(3,1,2)
[173]120% pad=zeros(1,siz-1);
121% IR2=[pad IR2];
122plot(t,IR2,'b')
[163]123grid on
124hold on
125plot(t2,IRT,'r')
126xlabel('samples')
127ylabel('Inverse Transfer Function')
[173]128legend('Processed','Theory',3)
[163]129TF2=TF(1:siz/2);
[173]130f=0:fs/siz:fs/2-fs/siz;
[163]131LFR2=20*log10(abs(TF2));
132figure(3)
133subplot(3,1,2)
134semilogx(f,LFR2)
135axis([10 20000 -40 +5])
136grid on
137hold on
138FRT2=abs(1./(1+j*2*pi*f*RC));
139LFRT2=20*log10(FRT2);
140semilogx(f,LFRT2,'r')
141xlabel('Frequency (Hz)')
142ylabel('Transfer Function')
143legend('Processed','Theory',3)
[173]144% imin=round(f1/fs*sizi)+1;
145% imax=round(f2/fs*sizi)+1;
146% E2=sqrt(sum(abs(FRT(imin:imax)-TF2(imin:imax)).^2./(abs(FRT(imin:imax)).^2)))*100/(imax-imin+1);
147% E2t = strcat('Erreur quadratique :',num2str(E2,2),' %');
148% text(3000,-5,E2t);
[163]149
150% Impulse response: Inverse filter deconvolution (freq domain)
151% yinv=fliplr(yc);
152% fl=logspace(log10(f1),log10(f2),siz);
153% yinv=yinv./fl; % inverse filter
154% yinvf=yinv.*wint; %windowed inverse filter
155% IR=conv(yf,yinvf); % processed impulse response
156tic
157iRF=fft([yf, zeros(1,siz-1)]).*fft([yinv, zeros(1,siz-1)])/yeff;
158IR3=real(ifft(iRF))*fs;
159toc
160% norm=sqrt(sum(abs(is.^2))/siz);
161% yeff=norm*sqrt(fs/2/(f2-f1));
162% scal=fs/yeff;
163t2=(-siz+1:1:siz-1).*dt;
164figure(2)
165subplot(3,1,3)
166plot(t2,IR3,'b')
167grid on
168xlabel('samples')
169title('Impulse response')
170ylabel('Deconvolution freq')
171IRT=exp(-t2/RC)/RC;
172IRT(1:siz-1)=0;
173hold on
174plot(t2,IRT,'r')
[173]175legend('Processed','Theory',3)
[163]176
177% frequency response : Inverse filter deconvolution (freq domain)
178FR3=iRF(1:sizi/2);
179fconv=0:fs/sizi:fs/2;
180fconv=fconv(1:sizi/2);
181LFR3=20*log10(abs(FR3));
182figure(3)
183subplot(3,1,3)
184semilogx(fconv,LFR3)
185ylabel('Transfert function (deconv freq)')
186title('Frequency response (dB)')
187axis([10 20000 -40 +5])
188grid on
189hold on
190FRT=abs(1./(1+j*2*pi*fconv*RC));
191LFRT=20*log10(FRT);
192imin=round(f1/fs*sizi)+1;
193imax=round(f2/fs*sizi)+1;
194E3=sqrt(sum(abs(FRT(imin:imax)-FR3(imin:imax)).^2./(abs(FRT(imin:imax)).^2)))*100/(imax-imin+1);
195E3t = strcat('Erreur quadratique :',num2str(E3,2),' %');
196text(3000,-5,E3t);
197semilogx(fconv,LFRT,'r')
198xlabel('Frequency (Hz)')
199legend('Processed','Theory',3)
Note: See TracBrowser for help on using the browser.