% RGB Spectral Sensitivities Calculation of Nikon D70 System
% Written by : Joan Moh
% ----------------------------------------------------------
% This routine loads the RAW data acquired from camera images of the
% narrowband light from the monochrometer and produces an average RGB triad.
% Using these RGB values and the spectral content of each narrowband light
% that was measured with the spectrometer, a simple SVD decomposition is done
% to estimate the RGB spectral sensitivity vectors of the Nikon D70 system that
% minimizes the least square error. The SVD estimation is done in routine
% estimateSensorSpectralResponse.m written by Feng Xiao.


numPhotos = 38;
rgb = [];

for i=4:numPhotos,
    if(i~= 6) % image 6 was wrongly acquired during procedure
       num = num2str(i);
       if(i<10) name = strcat('DSC_000', num, '.nef'); 
       elseif(i>=10) name = strcat('DSC_00', num, '.nef'); 
       end;
       data = double(nefread(name));
       r = mean(mean(data(:,:,1)));
       g = mean(mean(data(:,:,2)));
       b = mean(mean(data(:,:,3)));
       g2 = mean(mean(data(:,:,4)));
       g_avg = (g+g2)/2;    % Averaging green response from two green channels
       rgb_this = [r g_avg b];
       rgb = [rgb; rgb_this];
   end;
end;

load spectrum_SpectraScan1; % Loads in spectrometer readings for 37 narrowband lights

response_svd = estimateSensorSpectralResponse(spectrum(:,4:37), rgb, 'gsvd');
save rgb_dataSub response_svd;

