source: trunk/src/series/check_peaklock.m @ 583

Last change on this file since 583 was 583, checked in by sommeria, 11 years ago

various bugs corrected, check_peaklocking added (to upgrade)

File size: 10.4 KB
Line 
1%%-------------------------------------------------------
2% --- Executes on button press in peaklocking. TODO: UPDATE
3%-------------------------------------------------
4function peaklocking(handles)
5%evaluation of peacklocking errors
6%use splinhist: give spline coeff cc for a smooth histo (call spline4)
7%use histsmooth(x,cc): calculate the smooth histo for any value x
8%use histder(x,cc): calculate the derivative of the smooth histo
9global hfig1 hfig2 hfig3
10global nbb Uval Vval Uhist Vhist % nbb resolution of the histogram nbb=10: 10 values in unity interval
11global xval xerror yval yerror
12
13set(handles.vector_y,'Value',1)% trigger the option Uhist on the interface
14set(handles.Vhist_input,'Value',1)
15set(handles.cm_switch,'Value',0) % put the switch to 'pixel'
16
17%adjust the extremal values of the histogram in U with respect to integer
18%values
19minimU=round(min(Uval)-0.5)+0.5; %first value of the histogram with integer bins
20maximU=round(max(Uval)-0.5)+0.5;
21minim_fin=(minimU-0.5+1/(2*nbb)); % first bin valueat the beginning of an integer interval
22maxim_fin=(maximU+0.5-1/(2*nbb)); % last integer value
23nb_bin_min= round(-(minim_fin - min(Uval))*nbb); % nbre of bins added below
24nb_bin_max=round((maxim_fin -max(Uval))*nbb); %nbre of bins added above
25Uval=[minim_fin:(1/nbb):maxim_fin];
26histu_min=zeros(nb_bin_min,1);
27histu_max=zeros(nb_bin_max,1);
28Uhist=[histu_min; Uhist ;histu_max]; % column vector
29
30%adjust the extremal values of the histogram in V
31minimV=round(min(Vval-0.5)+0.5);
32maximV=round(max(Vval-0.5)+0.5);
33minim_fin=minimV-0.5+1/(2*nbb); % first bin valueat the beginning of an integer interval
34maxim_fin=maximV+0.5-1/(2*nbb); % last integer value
35nb_bin_min=round((min(Vval) - minim_fin)*nbb); % nbre of bins added below
36nb_bin_max=round((maxim_fin -max(Vval))*nbb);
37Vval=[minim_fin:(1/nbb):maxim_fin];
38histu_min=zeros(nb_bin_min,1);
39histu_max=zeros(nb_bin_max,1);
40Vhist=[histu_min; Vhist ;histu_max]; % column vector
41
42% RUN_histo_Callback(hObject, eventdata, handles)
43% %adjust the histogram to integer values:
44
45%histoU and V
46[Uhistinter,xval,xerror]=peaklock(nbb,minimU,maximU,Uhist);
47[Vhistinter,yval,yerror]=peaklock(nbb,minimV,maximV,Vhist);
48
49% selection of value ranges such that histo>=10 (enough statistics)
50Uval_ind=find(Uhist>=10);
51ind_min=min(Uval_ind);
52ind_max=max(Uval_ind);
53U_min=Uval(ind_min);% minimum allowed value
54U_max=Uval(ind_max);%maximum allowed value
55
56% selection of value ranges such that histo>=10 (enough statistics)
57Vval_ind=find(Vhist>=10);
58ind_min=min(Vval_ind);
59ind_max=max(Vval_ind);
60V_min=Vval(ind_min);% minimum allowed value
61V_max=Vval(ind_max);%maximum allowed value
62
63figure(4)% plot U histogram with smoothed one
64plot(Uval,Uhist,'b')
65grid on
66hold on
67plot(Uval,Uhistinter,'r');
68hold off
69
70figure(5)% plot V histogram with smoothed one
71plot(Vval,Vhist,'b')
72grid on
73hold on
74plot(Vval,Vhistinter,'r');
75hold off
76
77figure(6)% plot pixel error in two subplots
78hfig4=subplot(2,1,1);
79hfig5=subplot(2,1,2);
80axes(hfig4)
81plot(xval,xerror)
82axis([U_min U_max -0.4 0.4])
83xlabel('velocity u (pix)')
84ylabel('peaklocking error (pix)')
85grid on
86axes(hfig5)
87plot(yval,yerror)
88axis([V_min V_max -0.4 0.4]);
89xlabel('velocity v (pix)')
90ylabel('peaklocking error (pix)')
91grid on
92
93
94
95
96
97
98
99
100
101%'peaklock': determines peacklocking errors from velocity histograms.
102%-------------------------------------------------------
103%first smooth the input histogram 'histu' in such a way that the integral over
104%n-n+1 is preserved, then deduce the peaklocking 'error' function of the pixcel displacement 'x'.
105%
106% [histinter,x,error]=peaklock(nbb,minim,maxim,histu)
107%OUTPUT:
108%histinter: smoothed interpolated histogram
109% x: vector of displacement values.
110% error: vector of estimated errors corresponding to x
111%INPUT:
112%histu=vector representing the values of histogram  of measured velocity ;
113%minim, maxim: extremal values of the measured velocity (absica for histu)
114%nbb: number of bins inside each integer interval for the histograms
115%SUBROUTINES INCLUDED:
116%spline4.m% spline interpolation at 4th order
117%splinhist.m: give spline coeff cc for a smooth histo (call spline4)
118%histsmooth.m(x,cc): calculate the smooth histo for any value x
119%histder.m(x,cc): calculate the derivative of the smooth histo
120function [histinter,x,error]=peaklock(nbb,minim,maxim,histu)
121
122nint=maxim-minim+1
123xfin=[minim-0.5+1/(2*nbb):(1/nbb):maxim+0.5-(1/(2*nbb))];
124histo=(reshape(histu,nbb,nint));%extract values with x between integer -1/2 integer +1/2
125Integ=sum(histo)/nbb; %integral of the pdf on each integer bin
126[histinter,cc]=splinhist(Integ,minim,nbb);
127histx=reshape(histinter,nbb,nint);
128xint=[minim:1:maxim];
129x=zeros(nbb,nint);
130%determination of the displacement x(j,:)
131%j=1
132delx=histo(1,:)./histsmooth(-0.5*ones(1,nint),cc)/nbb;
133%del(1,:)=delx;
134x(1,:)=-0.5+delx-(delx.*delx/2).*histder(-0.5*ones(1,nint),cc);
135%histx(1,:)=histsmooth(x(j-1,:),cc);
136for j=2:nbb
137    delx=histo(j,:)./histsmooth(x(j-1,:),cc)/nbb;
138    %delx=delx.*(delx<3*ones(1,nint)/nbb)+3*ones(1,nint)/nbb.*~(delx <3*ones(1,nint)/nbb)
139    x(j,:)=x(j-1,:)+delx-(delx.*delx/2).*histder(x(j-1,:),cc);
140end
141%reshape
142xint=ones(nbb,1)*xint;
143x=x+xint;
144x=reshape(x,1,nbb*nint);
145error=xfin+1/(2*nbb)-x;
146
147%-------------------------------------------------------
148% --- determine the spline coefficients cc for the interpolated histogram.
149%-------------------------------------------------
150function [histsmooth,cc]= splinhist(Integ,mini,nbb)
151% provides a smooth histogramm histmooth, which remains always positive,
152% and is such that its sum over each integer bin [i-1/2 i+1/2] is equal to
153% Integ(i). The function determines histmooth as the exponential of a 4th
154% order spline function and adjust the cefficients by a Newton method to
155% fit the integral conditions Integ
156% histmooth is determined at the abscissa
157% xfin=[mini-0.5+1/(2*n):(1/n):maxi+0.5-(1/(2*n))] (maxi=mini+size(aa)-1)
158%cc(1-5,i) provides the spline coefficients
159
160% order 0
161siz=size(Integ);
162nint=siz(2);
163izero=find(Integ==0); %indices of zero elements
164inonzero=find(Integ);
165Integ(izero)=min(Integ(inonzero));
166aa=log(Integ);%initial guess for a coeff
167spli=spline4(aa,mini,nbb);  %appel à la fonction spline4
168histsmooth=exp(spli);
169
170S=(sum(reshape(histsmooth,nbb,nint)))/nbb;% integral of the fit histsmooth on ]i-1/2 i+1/2[
171epsilon=max(abs(Integ-S));
172iter=0;
173while epsilon > 0.000001 & iter<10
174ident=eye(nint);
175dSda=ones(nint);
176for j=1:nint% determination of the jacobian matrix dSda
177dhistda=spline4(ident(j,:),mini,nbb);
178expdhistda=dhistda.*histsmooth;
179dSda(j,:)=(sum(reshape(expdhistda,nbb,nint)))/nbb;
180end
181aa=aa+(Integ-S)*inv(dSda);%new estimate of coefficients aa by linear interpolation
182[spli,bb]=spline4(aa,mini,nbb);% new fit histsmooth
183histsmooth=exp(spli);
184S=(sum(reshape(histsmooth,nbb,nint)))/nbb;% integral of the fit histsmooth on ]i-1/2 i+1/2[
185epsilon=max(abs(Integ-S));
186iter=iter+1;
187end
188if iter==10, errordlg('splinhist did not converge after 10 iterations'),end
189cc(1,:)=aa;
190cc(2,:)=bb(1,:);
191cc(3,:)=bb(2,:);
192cc(4,:)=bb(3,:);
193cc(5,:)=bb(4,:);
194
195%-------------------------------------------------------
196% --- determine the 4th order spline coefficients from the function values aa.
197%-------------------------------------------------
198function [histsmooth,bb]= spline4(aa,mini,n)
199% spline interpolation at 4th order
200%aa=vector of values of a function at integer abscissa, starting at mini
201%n=number of subdivisions for the interpolated function
202% histmooth =interpolated values at absissa
203% xfin=[mini-0.5+1/(2*n):(1/n):maxi+0.5-(1/(2*n))] (maxi=mini+size(aa)-1)
204%bb=[b(i);c(i);d(i); e(i)] matrix of spline coeff
205L1=[1/2 1/4 1/8 1/16;1 1 3/4 1/2;0 2 3 3;0 0 6 12];
206L2=[-1/2 1/4 -1/8 1/16;1 -1 3/4 -1/2;0 2 -3 3;0 0 6 -12];
207M=inv(L2)*L1;
208[V,D]=eig(M);
209F=-inv(V)*inv(L2)*[1 ;0 ;0;0];
210a1rev=[1 -1/D(1,1)];
211b1rev=[F(1)/D(1,1)];
212a2rev=[1 -1/D(2,2)];
213b2rev=[F(2)/D(2,2)];
214a3=[1 -D(3,3)];
215b3=[F(3)];
216a4=[1 -D(4,4)];
217b4=[F(4)];
218
219%data
220% n=10;% résolution de la pdf: nbre de points par unite de u
221% mini=-10.0;%general mini=uint16(min(values)-1 CHOOSE maxi-mini+1 EVEN
222% maxi=9.0; % general maxi=uint16(max(values))+1
223%nint=double(maxi-mini+1); % nombre d'intervals entiers EVEN!
224siz=size(aa);
225nint=siz(2);
226maxi=mini+nint-1;
227npdf=nint*n;% nbre total d'intervals à introduire dans la pdf: hist(u,npdf)
228%simulation de pdf
229xfin=[mini-0.5+1/(2*n):(1/n):maxi+0.5-(1/(2*n))];% valeurs d'interpolation: we take n values in each integer interval
230%histolin=exp(-(xfin-1).*(xfin-1)).*(2+cos(10*(xfin-1)));% simulation d'une pdf
231%histo=log(histolin);
232%histo=sin(2*pi*xfin);
233%histextract=(reshape(histo,n,nint));
234%aa=sum(histextract)/n %integral of the pdf on each integer bin
235IP=[0 diff(aa)];
236Irev=zeros(size(aa));
237for i=1:nint
238    Irev(i)=aa(end-i+1);
239end
240IPrev=[0 diff(Irev)];
241
242%get the spline coelfficients a_d, using filter on the eigen vectors A,B,C
243Arev=filter(b1rev,a1rev,IPrev);
244Brev=filter(b2rev,a2rev,IPrev);
245C=filter(b3,a3,IP);
246D=filter(b4,a4,IP);
247A=zeros(size(Arev));
248B=zeros(size(Brev));
249for i=1:nint
250    A(i)=Arev(end-i+1);
251    B(i)=Brev(end-i+1);
252end
253%Matr=V*[A;B;C;D];
254bb=V*[A;B;C;D];
255%b=Matr(1,:);
256%c=Matr(2,:);
257%d=Matr(3,:);
258%e=Matr(4,:);
259%a=aa;
260
261%calculate the interpolation using the spline coefficients a-d
262%xextract=(reshape(xfin,n,nint));%
263chi=xfin+1/(2*n)-min(xfin)-double(int16(xfin+(1/(2*n))-min(xfin)))-0.5;% decimal part
264chi2=chi.*chi;
265chi3=chi2.*chi;
266chi4=chi3.*chi;
267avec=reshape(ones(n,1)*aa,1,n*nint);
268bvec=reshape(ones(n,1)*bb(1,:),1,n*nint);
269cvec=reshape(ones(n,1)*bb(2,:),1,n*nint);
270dvec=reshape(ones(n,1)*bb(3,:),1,n*nint);
271evec=reshape(ones(n,1)*bb(4,:),1,n*nint);
272histsmooth=avec+bvec.*chi+cvec.*chi2+dvec.*chi3+evec.*chi4;
273
274%-------------------------------------------------------
275% --- determine the interpolated histogram at points chi from the spline ceff cc.
276%-------------------------------------------------
277function histx= histsmooth(chi,cc)
278% provides the value of the interpolated histogram at values chi=x-i
279%(difference with the mnearest integer)
280% cc(5,size(chi)) is the set of spline coefficients obtained by splinhist
281chi2=chi.*chi;
282chi3=chi2.*chi;
283chi4=chi3.*chi;
284histx=exp(cc(1,:)+cc(2,:).*chi+cc(3,:).*chi2+cc(4,:).*chi3+cc(5,:).*chi4);
285
286%-------------------------------------------------------
287% --- determine the derivative p'/p of the interpolated histogram at points chi from the spline ceff cc.
288%-------------------------------------------------
289function histder= histder(chi,cc)
290% provides the logarithmique derivative p'/p of the interpolated histogram
291%at values chi=x-i
292%(difference with the nearest integer)
293% cc(5,size(chi)) is the set of spline coefficients obtained by splinhist
294chi2=chi.*chi;
295chi3=chi2.*chi;
296chi4=chi3.*chi;
297histder=cc(2,:)+2*cc(3,:).*chi+3*cc(4,:).*chi2+4*cc(5,:).*chi3;
Note: See TracBrowser for help on using the repository browser.