Calculations/IRF/doNoiseCorrelation.m

87 lines
2.6 KiB
Matlab

function [result,avgpic]=doNoiseCorrelation(imgs,mask)
%imgs:cell arrays of the Nim images to treat together. each element of
%imgs should have the same size:(sy,sy)
Nim=numel(imgs);
%% initialize sizes
enlarge=1;%use so that it works well whatever the images sizes are (even/odd)
ROIsizey = size(imgs{1},1)-1;
if enlarge; maxsizey = ROIsizey*2; else maxsizey = ROIsizey+1; end%*2;
sy = ROIsizey+1;
ROIsizex = size(imgs{1},2)-1;
if enlarge; maxsizex = ROIsizex*2; else maxsizex = ROIsizex+1; end%*2
sx = ROIsizex+1;
xcors=(-ROIsizex/2:ROIsizex/2);%center=0
ycors=(-ROIsizey/2:ROIsizey/2);%center=0
avgpic = zeros(maxsizey,maxsizex);
avgpicFFT = zeros(maxsizey,maxsizex);
counter = 0;% not really need if all images of the table are used as then always equal to indim but can come useful is selection is done
m0=0.01;%for plot
for indim=1:Nim
disp(['>>>>>>>>>>>>>>>>>>>>>> Image ' num2str(indim) '/' num2str(numel(Nim)) ' <<<<<<<<<<<<<<<<<<<<<'])
counter=counter+1;
%% resized data for the NoiseCorrelations.
if nargin==1;ROI1=imgs{indim};else ROI1=imgs{indim}.*mask;end
text1='raw images';
pixToCalc = zeros(maxsizey,maxsizex);
pixToCalc(1:sy,1:sx) = ROI1;
pixToCalc = pixToCalc/sum(sum(pixToCalc));
%Calculate correlation function
picAuxFFT = ifftshift(ifft2((abs(fft2(pixToCalc)).^2)));
%for means
avgpic = avgpic+pixToCalc;
avgpicFFT = avgpicFFT+picAuxFFT;
%temporary means:
avgpictemp = avgpic/counter;
figure(1); clf;
subplot(2,2,1);imagesc(avgpictemp); colorbar;hold all
avgpicFFTtemp = avgpicFFT/counter;
subplot(2,2,2);imagesc(abs(avgpicFFTtemp)); colorbar;hold all
avgpictemp = ifftshift(ifft2((abs(fft2(avgpictemp)).^2)));
subplot(2,2,3);imagesc(abs(avgpictemp)); colorbar;hold all
%temporary results:
result = (avgpicFFTtemp./avgpictemp-1);
if enlarge result=result(ROIsizey/2+1:ROIsizey*3/2+1,ROIsizex/2+1:ROIsizex*3/2+1); end%/result(ROIsizey+1,ROIsizex+1);
subplot(2,2,4);imagesc(real(result),[-1 1]); colorbar;hold all
plot(ROIsizey/2+1,ROIsizex/2+1,'w+')
normr=result(ROIsizey/2+1,ROIsizex/2+1);
disp(['Normalization:' num2str(normr)])
%temporary plot
disp(['plot ' num2str(indim) '...'])
%plot correlation function
figure(100);clf
subplot(1,2,1); imagesc(ROI1);title(['Im' num2str(indim)])
subplot(1,2,2); imagesc(xcors,ycors,real(result),[-m0/2,m0]);hold all; plot(0,0,'w+');colorbar;
nametitle = [text1 '- Nb averages: ',num2str(counter) ', norm: ' num2str(normr)];
title(nametitle);
drawnow;
end
avgpic=avgpic(1:sy,1:sx);
end