%Correction of spectra using NIST 2242 standard

clear all;  %clear any variables in the workspace
close all;  %close all figure windows

% load spectral file of the glass which has been imported from a text file into a *.mat file format
sp=input('Enter glass spectral filename:','s');     %takes keyboard input string
load(sp);       %loads .mat file

%create a wave number vector between 150-4000cm-1
W=linspace(150,4000,10000)';

% operator can input temp during which the spectra was taken
temp=input('What was the temperature for this file in degrees Celcius?');

%calculate SRM2242 intensity function
%constants from SRM2242 certificate
A0 = 0.037014;
A1 = 1.22531 * 10^(-4);
A2 = -4.21311 * 10^(-8);
A3 = 2.19705  * 10^(-10);
A4 = -9.04836 * 10^(-14);
A5 = 9.76795 * 10^(-18);

%polynomial equation
I=A0 + A1*W + A2*(W.^2) + A3*(W.^3)+ A4*(W.^4)+ A5*(W.^5);

% Temperature adjust X-axis
Wt=(temp-21)*(-1.9) + W;

% plot the polynomial with and without the adjustment
figure;plot(Wt,I);hold on; plot(W,I,'r--');

% the function truncSpectra, just orders the rows in ascending order
% and cuts off all points below 150 and above 4000 cm-1 
[GlassX,GlassY]=truncSpectra(X,Y);

ii=interp1(Wt,I,GlassX);
figure;plot(W,I,'b',Wt,I,'r',GlassX,ii,'g');

ICorr=ii./GlassY;
figure;plot(GlassX,ICorr);

s=input('Enter spectral file to be corrected:','s');
load(s);
[WaterXt,WaterYt]=truncSpectra(WaterX,WaterY);
WaterYCorr=WaterYt.*ICorr;

figure;plot(WaterXt,WaterYt,'b');
figure;plot(WaterXt,WaterYCorr)
