[583] | 1 | %%------------------------------------------------------- |
---|
| 2 | % --- Executes on button press in peaklocking. TODO: UPDATE |
---|
| 3 | %------------------------------------------------- |
---|
| 4 | function 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 |
---|
| 9 | global hfig1 hfig2 hfig3 |
---|
| 10 | global nbb Uval Vval Uhist Vhist % nbb resolution of the histogram nbb=10: 10 values in unity interval |
---|
| 11 | global xval xerror yval yerror |
---|
| 12 | |
---|
| 13 | set(handles.vector_y,'Value',1)% trigger the option Uhist on the interface |
---|
| 14 | set(handles.Vhist_input,'Value',1) |
---|
| 15 | set(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 |
---|
| 19 | minimU=round(min(Uval)-0.5)+0.5; %first value of the histogram with integer bins |
---|
| 20 | maximU=round(max(Uval)-0.5)+0.5; |
---|
| 21 | minim_fin=(minimU-0.5+1/(2*nbb)); % first bin valueat the beginning of an integer interval |
---|
| 22 | maxim_fin=(maximU+0.5-1/(2*nbb)); % last integer value |
---|
| 23 | nb_bin_min= round(-(minim_fin - min(Uval))*nbb); % nbre of bins added below |
---|
| 24 | nb_bin_max=round((maxim_fin -max(Uval))*nbb); %nbre of bins added above |
---|
| 25 | Uval=[minim_fin:(1/nbb):maxim_fin]; |
---|
| 26 | histu_min=zeros(nb_bin_min,1); |
---|
| 27 | histu_max=zeros(nb_bin_max,1); |
---|
| 28 | Uhist=[histu_min; Uhist ;histu_max]; % column vector |
---|
| 29 | |
---|
| 30 | %adjust the extremal values of the histogram in V |
---|
| 31 | minimV=round(min(Vval-0.5)+0.5); |
---|
| 32 | maximV=round(max(Vval-0.5)+0.5); |
---|
| 33 | minim_fin=minimV-0.5+1/(2*nbb); % first bin valueat the beginning of an integer interval |
---|
| 34 | maxim_fin=maximV+0.5-1/(2*nbb); % last integer value |
---|
| 35 | nb_bin_min=round((min(Vval) - minim_fin)*nbb); % nbre of bins added below |
---|
| 36 | nb_bin_max=round((maxim_fin -max(Vval))*nbb); |
---|
| 37 | Vval=[minim_fin:(1/nbb):maxim_fin]; |
---|
| 38 | histu_min=zeros(nb_bin_min,1); |
---|
| 39 | histu_max=zeros(nb_bin_max,1); |
---|
| 40 | Vhist=[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) |
---|
| 50 | Uval_ind=find(Uhist>=10); |
---|
| 51 | ind_min=min(Uval_ind); |
---|
| 52 | ind_max=max(Uval_ind); |
---|
| 53 | U_min=Uval(ind_min);% minimum allowed value |
---|
| 54 | U_max=Uval(ind_max);%maximum allowed value |
---|
| 55 | |
---|
| 56 | % selection of value ranges such that histo>=10 (enough statistics) |
---|
| 57 | Vval_ind=find(Vhist>=10); |
---|
| 58 | ind_min=min(Vval_ind); |
---|
| 59 | ind_max=max(Vval_ind); |
---|
| 60 | V_min=Vval(ind_min);% minimum allowed value |
---|
| 61 | V_max=Vval(ind_max);%maximum allowed value |
---|
| 62 | |
---|
| 63 | figure(4)% plot U histogram with smoothed one |
---|
| 64 | plot(Uval,Uhist,'b') |
---|
| 65 | grid on |
---|
| 66 | hold on |
---|
| 67 | plot(Uval,Uhistinter,'r'); |
---|
| 68 | hold off |
---|
| 69 | |
---|
| 70 | figure(5)% plot V histogram with smoothed one |
---|
| 71 | plot(Vval,Vhist,'b') |
---|
| 72 | grid on |
---|
| 73 | hold on |
---|
| 74 | plot(Vval,Vhistinter,'r'); |
---|
| 75 | hold off |
---|
| 76 | |
---|
| 77 | figure(6)% plot pixel error in two subplots |
---|
| 78 | hfig4=subplot(2,1,1); |
---|
| 79 | hfig5=subplot(2,1,2); |
---|
| 80 | axes(hfig4) |
---|
| 81 | plot(xval,xerror) |
---|
| 82 | axis([U_min U_max -0.4 0.4]) |
---|
| 83 | xlabel('velocity u (pix)') |
---|
| 84 | ylabel('peaklocking error (pix)') |
---|
| 85 | grid on |
---|
| 86 | axes(hfig5) |
---|
| 87 | plot(yval,yerror) |
---|
| 88 | axis([V_min V_max -0.4 0.4]); |
---|
| 89 | xlabel('velocity v (pix)') |
---|
| 90 | ylabel('peaklocking error (pix)') |
---|
| 91 | grid 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 |
---|
| 120 | function [histinter,x,error]=peaklock(nbb,minim,maxim,histu) |
---|
| 121 | |
---|
| 122 | nint=maxim-minim+1 |
---|
| 123 | xfin=[minim-0.5+1/(2*nbb):(1/nbb):maxim+0.5-(1/(2*nbb))]; |
---|
| 124 | histo=(reshape(histu,nbb,nint));%extract values with x between integer -1/2 integer +1/2 |
---|
| 125 | Integ=sum(histo)/nbb; %integral of the pdf on each integer bin |
---|
| 126 | [histinter,cc]=splinhist(Integ,minim,nbb); |
---|
| 127 | histx=reshape(histinter,nbb,nint); |
---|
| 128 | xint=[minim:1:maxim]; |
---|
| 129 | x=zeros(nbb,nint); |
---|
| 130 | %determination of the displacement x(j,:) |
---|
| 131 | %j=1 |
---|
| 132 | delx=histo(1,:)./histsmooth(-0.5*ones(1,nint),cc)/nbb; |
---|
| 133 | %del(1,:)=delx; |
---|
| 134 | x(1,:)=-0.5+delx-(delx.*delx/2).*histder(-0.5*ones(1,nint),cc); |
---|
| 135 | %histx(1,:)=histsmooth(x(j-1,:),cc); |
---|
| 136 | for 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); |
---|
| 140 | end |
---|
| 141 | %reshape |
---|
| 142 | xint=ones(nbb,1)*xint; |
---|
| 143 | x=x+xint; |
---|
| 144 | x=reshape(x,1,nbb*nint); |
---|
| 145 | error=xfin+1/(2*nbb)-x; |
---|
| 146 | |
---|
| 147 | %------------------------------------------------------- |
---|
| 148 | % --- determine the spline coefficients cc for the interpolated histogram. |
---|
| 149 | %------------------------------------------------- |
---|
| 150 | function [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 |
---|
| 161 | siz=size(Integ); |
---|
| 162 | nint=siz(2); |
---|
| 163 | izero=find(Integ==0); %indices of zero elements |
---|
| 164 | inonzero=find(Integ); |
---|
| 165 | Integ(izero)=min(Integ(inonzero)); |
---|
| 166 | aa=log(Integ);%initial guess for a coeff |
---|
| 167 | spli=spline4(aa,mini,nbb); %appel à la fonction spline4 |
---|
| 168 | histsmooth=exp(spli); |
---|
| 169 | |
---|
| 170 | S=(sum(reshape(histsmooth,nbb,nint)))/nbb;% integral of the fit histsmooth on ]i-1/2 i+1/2[ |
---|
| 171 | epsilon=max(abs(Integ-S)); |
---|
| 172 | iter=0; |
---|
| 173 | while epsilon > 0.000001 & iter<10 |
---|
| 174 | ident=eye(nint); |
---|
| 175 | dSda=ones(nint); |
---|
| 176 | for j=1:nint% determination of the jacobian matrix dSda |
---|
| 177 | dhistda=spline4(ident(j,:),mini,nbb); |
---|
| 178 | expdhistda=dhistda.*histsmooth; |
---|
| 179 | dSda(j,:)=(sum(reshape(expdhistda,nbb,nint)))/nbb; |
---|
| 180 | end |
---|
| 181 | aa=aa+(Integ-S)*inv(dSda);%new estimate of coefficients aa by linear interpolation |
---|
| 182 | [spli,bb]=spline4(aa,mini,nbb);% new fit histsmooth |
---|
| 183 | histsmooth=exp(spli); |
---|
| 184 | S=(sum(reshape(histsmooth,nbb,nint)))/nbb;% integral of the fit histsmooth on ]i-1/2 i+1/2[ |
---|
| 185 | epsilon=max(abs(Integ-S)); |
---|
| 186 | iter=iter+1; |
---|
| 187 | end |
---|
| 188 | if iter==10, errordlg('splinhist did not converge after 10 iterations'),end |
---|
| 189 | cc(1,:)=aa; |
---|
| 190 | cc(2,:)=bb(1,:); |
---|
| 191 | cc(3,:)=bb(2,:); |
---|
| 192 | cc(4,:)=bb(3,:); |
---|
| 193 | cc(5,:)=bb(4,:); |
---|
| 194 | |
---|
| 195 | %------------------------------------------------------- |
---|
| 196 | % --- determine the 4th order spline coefficients from the function values aa. |
---|
| 197 | %------------------------------------------------- |
---|
| 198 | function [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 |
---|
| 205 | L1=[1/2 1/4 1/8 1/16;1 1 3/4 1/2;0 2 3 3;0 0 6 12]; |
---|
| 206 | L2=[-1/2 1/4 -1/8 1/16;1 -1 3/4 -1/2;0 2 -3 3;0 0 6 -12]; |
---|
| 207 | M=inv(L2)*L1; |
---|
| 208 | [V,D]=eig(M); |
---|
| 209 | F=-inv(V)*inv(L2)*[1 ;0 ;0;0]; |
---|
| 210 | a1rev=[1 -1/D(1,1)]; |
---|
| 211 | b1rev=[F(1)/D(1,1)]; |
---|
| 212 | a2rev=[1 -1/D(2,2)]; |
---|
| 213 | b2rev=[F(2)/D(2,2)]; |
---|
| 214 | a3=[1 -D(3,3)]; |
---|
| 215 | b3=[F(3)]; |
---|
| 216 | a4=[1 -D(4,4)]; |
---|
| 217 | b4=[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! |
---|
| 224 | siz=size(aa); |
---|
| 225 | nint=siz(2); |
---|
| 226 | maxi=mini+nint-1; |
---|
| 227 | npdf=nint*n;% nbre total d'intervals à introduire dans la pdf: hist(u,npdf) |
---|
| 228 | %simulation de pdf |
---|
| 229 | xfin=[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 |
---|
| 235 | IP=[0 diff(aa)]; |
---|
| 236 | Irev=zeros(size(aa)); |
---|
| 237 | for i=1:nint |
---|
| 238 | Irev(i)=aa(end-i+1); |
---|
| 239 | end |
---|
| 240 | IPrev=[0 diff(Irev)]; |
---|
| 241 | |
---|
| 242 | %get the spline coelfficients a_d, using filter on the eigen vectors A,B,C |
---|
| 243 | Arev=filter(b1rev,a1rev,IPrev); |
---|
| 244 | Brev=filter(b2rev,a2rev,IPrev); |
---|
| 245 | C=filter(b3,a3,IP); |
---|
| 246 | D=filter(b4,a4,IP); |
---|
| 247 | A=zeros(size(Arev)); |
---|
| 248 | B=zeros(size(Brev)); |
---|
| 249 | for i=1:nint |
---|
| 250 | A(i)=Arev(end-i+1); |
---|
| 251 | B(i)=Brev(end-i+1); |
---|
| 252 | end |
---|
| 253 | %Matr=V*[A;B;C;D]; |
---|
| 254 | bb=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));% |
---|
| 263 | chi=xfin+1/(2*n)-min(xfin)-double(int16(xfin+(1/(2*n))-min(xfin)))-0.5;% decimal part |
---|
| 264 | chi2=chi.*chi; |
---|
| 265 | chi3=chi2.*chi; |
---|
| 266 | chi4=chi3.*chi; |
---|
| 267 | avec=reshape(ones(n,1)*aa,1,n*nint); |
---|
| 268 | bvec=reshape(ones(n,1)*bb(1,:),1,n*nint); |
---|
| 269 | cvec=reshape(ones(n,1)*bb(2,:),1,n*nint); |
---|
| 270 | dvec=reshape(ones(n,1)*bb(3,:),1,n*nint); |
---|
| 271 | evec=reshape(ones(n,1)*bb(4,:),1,n*nint); |
---|
| 272 | histsmooth=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 | %------------------------------------------------- |
---|
| 277 | function 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 |
---|
| 281 | chi2=chi.*chi; |
---|
| 282 | chi3=chi2.*chi; |
---|
| 283 | chi4=chi3.*chi; |
---|
| 284 | histx=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 | %------------------------------------------------- |
---|
| 289 | function 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 |
---|
| 294 | chi2=chi.*chi; |
---|
| 295 | chi3=chi2.*chi; |
---|
| 296 | chi4=chi3.*chi; |
---|
| 297 | histder=cc(2,:)+2*cc(3,:).*chi+3*cc(4,:).*chi2+4*cc(5,:).*chi3; |
---|