function [PhaseC] = calculatePhaseCoherence(~,psi,Transf)

    norm = sum(sum(sum(abs(psi).^2,1),2),3)*Transf.dx*Transf.dy*Transf.dz;
    psi = psi/sqrt(norm);
    
    NumGlobalShifts = 800;
    betas = []; phishift = [];
    for jj = 1:NumGlobalShifts
        phishift(jj) = -pi + 2*pi*(jj-1)/NumGlobalShifts;
        betas(jj) = sum(sum(sum(abs(angle(psi*exp(-1i*phishift(jj)))).*abs(psi).^2)));
    end
    [~,minidx] = min(betas);
    
    psi = psi*exp(-1i*phishift(minidx));
    phi = angle(psi);
    
    avgphi = sum(sum(sum(phi.*abs(psi).^2,1),2),3)*Transf.dx*Transf.dy*Transf.dz;
    PhaseC = sum(sum(sum(abs(angle(psi)-avgphi).*abs(psi).^2)))*Transf.dx*Transf.dy*Transf.dz;
end