%
% getFilteredLightsSPD.m
%
% get the filtered light source SPD from the white patch
%

load spd/reflected_light_spd;
wavelength_spd = wavelength;
reflected_light_spd = spd;

load macbethChart;

% interpolate the spd data so that they are in the range of wavelength

interpolated_reflected_light_spd = interp1(wavelength_spd, reflected_light_spd', wavelength)';

% get the surface reflectance of the white patch
white_patch_reflectance = macbethChart(:,19)';

invalid = (white_patch_reflectance <= 0.2);
white_patch_reflectance(invalid) = 1;

for i=1:18
 interpolated_reflected_light_spd(i,invalid) = 0;
end

filtered_light_spd = interpolated_reflected_light_spd;

for i=1:18
 filtered_light_spd(i,:) = filtered_light_spd(i,:)./white_patch_reflectance;
end

%plot
figure;
hold on;

% control
for i=1:1
 plot(wavelength, filtered_light_spd(i,:), 'k');
end

% Blue
for i=2:6
 plot(wavelength, filtered_light_spd(i,:), 'b');
end

% Amber
for i=7:11
 plot(wavelength, filtered_light_spd(i,:), 'r');
end

% Green
for i=12:15
 plot(wavelength, filtered_light_spd(i,:), 'g');
end

% Magenta
for i=16:18
 plot(wavelength, filtered_light_spd(i,:), 'm');
end

title('Filtered Lights SPD');
xlabel('Wavelength nm');
ylabel('Power');


%save 
save filtered_light_spd wavelength filtered_light_spd;
