Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function [u dt cur energy] = match_pdfs_2d_haker( pdf1, pdf2)
- % normalize pdfs
- cumsum1 = cumIntegrate(pdf1(:));
- cumsum2 = cumIntegrate(pdf2(:));
- pdf1 = pdf1/cumsum1(end);
- pdf2 = pdf2/cumsum2(end);
- [M N] = size(pdf1);
- % find an initial map, by doing 1D matchings in x and then in y.
- proj1 = zeros(1,N);
- proj2 = zeros(1,N);
- for i=1:N
- cumsum1 = cumIntegrate(pdf1(:,i));
- cumsum2 = cumIntegrate(pdf2(:,i));
- proj1(i) = cumsum1(end);
- proj2(i) = cumsum2(end);
- end;
- a = find_warp(proj2, proj1);
- ga = gradientAccurate(a);
- [X,Y] = meshgrid(1:N, 1:M);
- interpPDF2 = interp2(X,Y,pdf2, a, 1:M, 'spline',0.);
- b=zeros(M,N);
- for i=1:N
- b(:,i) = find_warp( interpPDF2(:, i).*ga(i), pdf1(:,i));
- end;
- %u^0 = (a,b)
- u=reshape([repmat(a', [M 1]) b], M, N,2);
- %let's do a constant number of iterations
- for t=1:3000
- %%%%%%%% compute grad^orthog Laplace^{-1} div(u^orthog) %%%%%%%%%
- %%%%%%%% where orthog stands for a 90 degrees rotations,%%%%%%%%%
- %%%%%%%% and Laplace^-1(g) solves Laplacian f = g %%%%%%%%%
- [dudx dudy] = gradientAccurate(u);
- divorthog = -dudy(:,:,1)+dudx(:,:,2); %div(u^orthog)
- f=reshape(poicalc(divorthog(:),1,1,M,N), M, N); %Laplace^{-1} div(u^orthog)
- [dfdx dfdy] = gradientAccurate(f); %grad Laplace^{-1} div(u^orthog)
- % 1./pdf1 * grad^orthog Laplace^{-1} div(u^orthog)
- update1 = repmat(1./pdf1, [1 1 2]).*reshape([-dfdy dfdx], size(u));
- %%%%%%%% upwind for Du %%%%%%%%%%%%%%%%%
- [dudx dudy] = gradientAccurate(u);
- dudxm = dudx;
- dudym = dudy;
- dudxp = dudx;
- dudyp = dudy;
- dudxm(:, 3:end-2, :) = (3*u(:, 3:end-2, :) - 4*u(:, 2:end-3, :) + u(:, 1:end-4, :))*0.5;
- dudxp(:, 3:end-2, :) = (-3*u(:, 3:end-2, :) + 4*u(:, 4:end-1, :) - u(:, 5:end, :))*0.5;
- dudym(3:end-2, :, :) = (3*u(3:end-2, :, :) - 4*u(2:end-3, :, :) + u(1:end-4, :, :))*0.5;
- dudyp(3:end-2, :, :) = (-3*u(3:end-2, :, :) + 4*u(4:end-1, :, :) - u(5:end, :, :))*0.5;
- Dupp = abs(dudxp(:,:,1).*dudyp(:,:,2)-dudxp(:,:,2).*dudyp(:,:,1));
- Dupm = abs(dudxp(:,:,1).*dudym(:,:,2)-dudxp(:,:,2).*dudym(:,:,1));
- Dump = abs(dudxm(:,:,1).*dudyp(:,:,2)-dudxm(:,:,2).*dudyp(:,:,1));
- Dumm = abs(dudxm(:,:,1).*dudym(:,:,2)-dudxm(:,:,2).*dudym(:,:,1));
- Du = (update1(:, :,1)>0).*(update1(:, :,2)>0).*Dupp ...
- + (update1(:, :,1)>0).*(update1(:, :,2)<0).*Dupm ...
- + (update1(:, :,1)<0).*(update1(:, :,2)>0).*Dump ...
- + (update1(:, :,1)<0).*(update1(:, :,2)<0).*Dumm;
- dt(t) = 0.2*min(1./abs(update1(:))); %dt according to stability conditions
- % update : du/dt = 1./pdf1 * det(Jac(u)) * update1
- u = u+dt(t).*repmat(Du, [1 1 2]).*update1;
- end;
- function y = find_warp(pdf1, pdf2) %1D matching
- cdf1 = cumIntegrate(pdf1);
- cdf2 = cumIntegrate(pdf2);
- cdf1 = cdf1/cdf1(end);
- cdf2 = cdf2/cdf2(end);
- N = length(cdf1);
- [ucdf1, uxx] = unique(cdf1, 'first');
- y = transpose(interp1(ucdf1,uxx,cdf2,'spline',N));
- function y = cumIntegrate(f) % generic function to approximate the cumulative integral
- y= cumtrapz(f); % or y=cumsum, or y=intgrad1 with the external library
- function varargout=gradientAccurate(f)
- if (length(f)==length(f(:)))
- [varargout{1}] = gradient2(f); %or my 5th order gradient2(f) code
- else
- [varargout{1} varargout{2}] = gradient2(f); %or my 5th order gradient2(f) code
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement