% Program: DSNU_Computation
% Author: Greg Wientjes
% Class: EE 362, Applied Vision Systems
% Copyright 2005 Greg Wientjes
% ----------------------------------------------------------------------------------
% Description: This program computes the DNSU value for the Nikon D70 Digital Camera. 
% We will be using the DSNU value for the simulation of the camera using ISET.
% ----------------------------------------------------------------------------------
% Usage: For each time you compute the DSNU for each exposure time, you need to do the following:
%       1) Change the indexOfAppropriateExposureTime value by finding the appropriate row number of 'photo list.xls' that 
%           matches to the exposure time (in this case, the value is 40).
%       2) Change the saved workspace value (DSNU_1secExposureTime, in this case) to the appropriate exposure time name.
% ----------------------------------------------------------------------------------

clear;      % clear the workspace

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Constants %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
totalNumberImagesPerExposure = 100; 
indexOfAppropriateExposureTime = 52;                                %%%%%%%%%%%%% CHANGE THIS FOR EACH EXPOSURE TIME.
                                                % We find the value 40, based on the excel file, photo list.  We match the 
                                                % the appropriate exposure time and row.  This value will need to be modified
                                                % for each different exposure time --just look on the photo list file.
imageDimension1 = 1007;
imageDimension2 = 1520;
imageDimension3 = 4;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                                        

% Compute DSNU.
imageTotal = zeros(imageDimension1, imageDimension2, imageDimension3);

for i=1:totalNumberImagesPerExposure
    num = num2str(i);
    if ( i <= 9 )
        name = strcat('secExposure\DSC_000', num, '.nef');          %%%%%%%%%%%%% CHANGE THIS FOR EACH EXPOSURE TIME.
    elseif ( i >=10 & i <= 99 )
        name = strcat('secExposure\DSC_00', num, '.nef');           %%%%%%%%%%%%% CHANGE THIS FOR EACH EXPOSURE TIME.      
    else 
        name = strcat('secExposure\DSC_0', num, '.nef');            %%%%%%%%%%%%% CHANGE THIS FOR EACH EXPOSURE TIME.
    end;    
    
    image = nefread(name);                              % Read a single image.
    imageTotal = imageTotal + double(image);            % Sum up all of the images.
    
    if( mod(i, 3) == 0 )            % Used to just keep track of the program running.
        disp(i);
    end;
end;

                % Calculate the time average.
timeAverage = imageTotal / totalNumberImagesPerExposure;         


                % Normalize the timeAverage of the image with respect to dark current
load DarkCurrent_AverageAll;
darkCurrent = average_3cds(indexOfAppropriateExposureTime);               
normalizedDSNUimage = timeAverage / darkCurrent;


                % Reshape the normalizedDSNUimage into a single column from the the original n by m by 4 matrix.
reshapedNormalizedDSNU = reshape( normalizedDSNUimage, imageDimension1 * imageDimension2 * imageDimension3, 1);


                % Find the variance of the normalized DSNU.        
varianceOfDSNU = var( reshapedNormalizedDSNU );


                % Save our DSNU workspace variable.
save DSNU_15secExposureTime varianceOfDSNU;                             %%%%%%%%%%%%% CHANGE THIS FOR EACH EXPOSURE TIME.


% Display 'done' once the program finishes.
disp('done');