% Routine to compare predictions against actual data 
% Written by Joan Moh
% --------------------------------------------------
% This routine calls up the predictions as well as actual raw
% data and plots them as well as calculates the mean square error.

clear; clc;

load prediction_tungsten2
load macbeth;

mean_all = mean_all';


% Finding linear scaling factor
meanstraight = reshape(mean_all, 72, 1);
predstraight = reshape(prediction_tungsten, 72, 1);


p = polyfit(meanstraight, predstraight, 1);
prediction_tungsten_scaled = prediction_tungsten/p(1);

plot(mean_all);
figure
plot(prediction_tungsten_scaled);

errorR = prediction_tungsten_scaled(:,1) - mean_all(:,1);
errorRsquared = errorR.*errorR;
errorRmeansquared = sum(errorRsquared)/24;

errorG = prediction_tungsten_scaled(:,2) - mean_all(:,2);
errorGsquared = errorG.*errorG;
errorGmeansquared = sum(errorGsquared)/24;

errorB = prediction_tungsten_scaled(:,3) - mean_all(:,3);
errorBsquared = errorB.*errorB;
errorBmeansquared = sum(errorBsquared)/24;


[errorRmeansquared errorGmeansquared errorBmeansquared]

errorVector = [prediction_tungsten_scaled(:,1)-mean_all(:,1) prediction_tungsten_scaled(:,2)-mean_all(:,2) prediction_tungsten_scaled(:,3)-mean_all(:,3)];
index = 1:24;
plot(index, errorVector(:,1), 'r', index, errorVector(:,2), 'g', index, errorVector(:,3), 'b');
xlabel('Index of Squares');
ylabel('Sensor Reading (V)');
title('Error between Predicted and Actual Sensor Readings');
legend('Red Sensor', 'Green Sensor', 'Blue Sensor');

figure;
title('Predicted versus Actual Sensor Readings');
subplot(1,3,1);
plot(index, mean_all(:,1), 'r-', index, prediction_tungsten_scaled(:,1), 'k--');
title('Red Sensor');
xlabel('Index of Squares');
ylabel('Sensor Reading (V)');
axis tight;
subplot(1,3,2);
plot(index, mean_all(:,2), 'g-', index, prediction_tungsten_scaled(:,2), 'k--');
title('Blue Sensor');
xlabel('Index of Squares');
ylabel('Sensor Reading (V)');
axis tight;
subplot(1,3,3);
plot(index, mean_all(:,3), 'b-', index, prediction_tungsten_scaled(:,3), 'k--');
title('Green Sensor');
xlabel('Index of Squares');
ylabel('Sensor Reading (V)');
axis tight;