%load pbData;

macBethSpectrum='TakeTwo/cam1/t2mbSpecIndiv.mat';
load(macBethSpectrum);

pbResp = zeros(3,31);

pbResp(1,:)=[0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.009 ...
	     0.024 0.078 0.114 0.083 0.053 0.052 0.050 0.042 0.035 ...
	     0.063 0.146 0.452 0.619 0.713 0.790 0.849 0.885 0.908 ...
	     0.898 0.876 0.855 0.860];

pbResp(2,:)=[0.035 0.017 0.006 0.000 0.000 0.000 0.000 0.000 0.009 ...
	     0.062 0.244 0.476 0.583 0.583 0.541 0.485 0.432 0.366 ...
	     0.308 0.254 0.163 0.076 0.067 0.059 0.049 0.030 0.020 ...
	     0.000 0.000 0.000 0.009];

pbResp(3,:)=[0.163 0.245 0.320 0.387 0.454 0.500 0.504 0.490 0.444 ...
	     0.362 0.264 0.162 0.092 0.062 0.052 0.042 0.034 0.026 ...
	     0.036 0.066 0.096 0.067 0.058 0.059 0.049 0.030 0.020 ...
	     0.000 0.000 0.000 0.009];

pbWaves = [400:10:700];


% mbWave is 380:4:780
% pbWaves is 400:10:700
% so interpolate pbData stuff

fivePbResp=zeros(3,31*5);
fivePbResp(1,:)=interp(pbResp(1,:),5);
fivePbResp(2,:)=interp(pbResp(2,:),5);
fivePbResp(3,:)=interp(pbResp(3,:),5);
% Chop the last four values, they're past the end
newResp=fivePbResp(:,1:31*5-4);
% Get rid of negative responses
[i,j]=find(newResp<0);
%newResp(i,j)=0;
newWaves=[400:2:700];

% Now, finally, grab the ones that we have data for from the 
% photospectrometer.

waves=[400:4:700];
resp =newResp(:,[1:2:151]);
figure;
hold on
plot(waves,resp(1,:),'r');
plot(waves,resp(2,:),'g');
plot(waves,resp(3,:),'b');
title('Photobit Image Sensor Relative Quantum Efficiency');
xlabel('Wavelength (nm)');
ylabel('Relative Response');
hold off;

% Get rid of extra variables

clear fivePbResp;
clear interpPbResp;
clear i;
clear j;
clear newWaves;
clear pbResp;
clear pbWaves;

% All righty, now let's multiply these values by the MacBeth
% spectral data.

macSpec=mbSpec([6:81],:);

% This is Photobit response before accounting for gain settings
% Notice how the red image sensor is 3x green and 6x blue for 
% white color patch! 
pbRgb=resp*macSpec;

% Now we need to try to figure out what the gain settings were.
% Use the gray, non-saturated patches = 3:5
% This part could be tough...
% Gain was nominally around 8, looking for some integer combination
% that would match patch responses. Try them all?
% red = 8, g,b=9:15 see if any fit?
% Let's try to get a rough idea first.
% Divide pbRgb for patches by blue value--gives ratios.
% Do the same for validRgb.
% Divide computed by measured ratios. 

measRgb=pbRgb;
pbRatios = zeros(2,size(measRgb,2));
measRatios = zeros(2,size(measRgb,2));
pbRatios(1,:)=pbRgb(1,:)./pbRgb(3,:);
pbRatios(2,:)=pbRgb(2,:)./pbRgb(3,:);
measRatios(1,:)=measRgb(1,:)./measRgb(3,:);
measRatios(2,:)=measRgb(2,:)./measRgb(3,:);

% now div one by the other

