% demonstration of autofocus using fourier
% finds frame with highest contrast in given
% set of frames (SHARPEN.mat)

load SHARPEN.mat

% get a = diagonal
for i=1:16
    for j=1:240
        a(i,j)=m3(j,j,i);
    end
end

% compute contrast of each frame
mn=zeros(1,16);
for i=1:16
    subplot(2,2,1);
    imshow(m3(:,:,i));   % frame
    title('Frame');
    
    subplot(2,2,2);
    plot(1:240,a(i,:)); % diagonal
    title('Diagonal Grayvalues')
    
    f=fft(a(i,:),128);
    F=abs(f(1:63));
    subplot(2,2,3);
    plot(1:63,log(F)); % fourier spectrum
    title('Fourier Spectrum')
    
    mn(i)=mean(F(2:end));  % mean of spectrum
    subplot(2,2,4);
    bar(1:16,mn);
    title('Mean FS of frames');
    
    fprintf('%d %f\n',i,mn(i)) ;
    pause
end

% result
[v i] = max(mn);
figure;
imshow(m3(:,:,i(1)));
