%project 5
clear all;

Im400 = imread('im400.tiff');
Im400 = imresize(Im400,[512 512], 'bicubic');

Im425 = imread('im425.tiff');
Im425 = imresize(Im425,[512 512], 'bicubic');

Im450 = imread('im450.tiff');
Im450 = imresize(Im450,[512 512], 'bicubic');

Im475 = imread('im475.tiff');
Im475 = imresize(Im475,[512 512], 'bicubic');

Im500 = imread('im500.tiff');
Im500 = imresize(Im500,[512 512], 'bicubic');

Im525 = imread('im525.tiff');
Im525 = imresize(Im525,[512 512], 'bicubic');

Im550 = imread('im550.tiff');
Im550 = imresize(Im550,[512 512], 'bicubic');

Im575 = imread('im575.tiff');
Im575 = imresize(Im575,[512 512], 'bicubic');

Im600 = imread('im600.tiff');
Im600 = imresize(Im600,[512 512], 'bicubic');

Im625 = imread('im625.tiff');
Im625 = imresize(Im625,[512 512], 'bicubic');

Im650 = imread('im650.tiff');
Im650 = imresize(Im650,[512 512], 'bicubic');

Im675 = imread('im675.tiff');
Im675 = imresize(Im675,[512 512], 'bicubic');

Im700 = imread('im700.tiff');
Im700 = imresize(Im700,[512 512], 'bicubic');

LMD= [400:20:700];
Xdata= LMD;

RGBGreen= [0.0003 0.0004 0.0008 0.0129 0.1056 0.4805 0.9111 0.9673 0.5376 0.1313 0.0170 0.0008 0.0003 0.0002 0.0001 0.0001];
RGBBlue= [ 0.0751 0.3661 0.8320 0.9158 0.4556 0.1364 0.0228 0.0017 0.0004 0.0004 0.0004 0.0003 0.0004 0.0002 0.0002 0.0002];
RGBRed = [ 0.0004 0.0009 0.0009 0.0009 0.0007 0.0006 0.0008 0.0008 0.0023 0.0213 0.2283 0.9581 0.9165 0.3844 0.0658 0.0200];
%YdataG= RGBGreen;
%YdataB= RGBBlue;
%YdataR= RGBRed;


Fun = inline('C(1) *exp(-(Xdata-C(2)).^2/C(3).^2)','C','Xdata');
%Xdata= LMD;
YdataG= RGBGreen;
YdataB= RGBBlue;
YdataR= RGBRed;

%BLUE----------------------------------------------
C = lsqcurvefit(Fun, [ 1 200 100], Xdata, YdataB);
YBlue = Fun(C, Xdata);
plot(Xdata,YdataB, 'r*', Xdata,YBlue,'b');
ValBlue= C;

B1= C(1)*exp(-(400-C(2)).^2/(C(3))^2);
B2= C(1)*exp(-(425-C(2)).^2/(C(3))^2);
B3= C(1)*exp(-(450-C(2)).^2/(C(3))^2);
B4= C(1)*exp(-(475-C(2)).^2/(C(3))^2);
B5= C(1)*exp(-(500-C(2)).^2/(C(3))^2);
B6= C(1)*exp(-(525-C(2)).^2/(C(3))^2);
B7= C(1)*exp(-(550-C(2)).^2/(C(3))^2);
B8= C(1)*exp(-(575-C(2)).^2/(C(3))^2);
B9= C(1)*exp(-(600-C(2)).^2/(C(3))^2);
B10= C(1)*exp(-(625-C(2)).^2/(C(3))^2);
B11= C(1)*exp(-(650-C(2)).^2/(C(3))^2);
B12= C(1)*exp(-(675-C(2)).^2/(C(3))^2);
B13= C(1)*exp(-(700-C(2)).^2/(C(3))^2);

%Im400= double(Im400);
%multiplicating images with formula
BM(:,:,1)= B1*double(Im400);
BM(:,:,2)= B2*double(Im425);
BM(:,:,3)= B3*double(Im450);
BM(:,:,4)= B4*double(Im475);
BM(:,:,5)= B5*double(Im500);
BM(:,:,6)= B6*double(Im525);
BM(:,:,7)= B7*double(Im550);
BM(:,:,8)= B8*double(Im575);
BM(:,:,9)= B9*double(Im600);
BM(:,:,10)= B10*double(Im625);
BM(:,:,11)= B11*double(Im650);
BM(:,:,12)= B12*double(Im675);
BM(:,:,13)= B13*double(Im700);

BLUESUM = sum(double(BM(:,:,:)),3);
%--------------------------------------------------


%RED ----------------------------------------------
C = lsqcurvefit(Fun, [ 1 200 100], Xdata, YdataR);
YRed = Fun(C, Xdata);
plot(Xdata,YdataR, 'r*', Xdata,YRed,'b');
ValRed= C;

R1= C(1)*exp(-(400-C(2)).^2/(C(3))^2);
R2= C(1)*exp(-(425-C(2)).^2/(C(3))^2);
R3= C(1)*exp(-(450-C(2)).^2/(C(3))^2);
R4= C(1)*exp(-(475-C(2)).^2/(C(3))^2);
R5= C(1)*exp(-(500-C(2)).^2/(C(3))^2);
R6= C(1)*exp(-(525-C(2)).^2/(C(3))^2);
R7= C(1)*exp(-(550-C(2)).^2/(C(3))^2);
R8= C(1)*exp(-(575-C(2)).^2/(C(3))^2);
R9= C(1)*exp(-(600-C(2)).^2/(C(3))^2);
R10= C(1)*exp(-(625-C(2)).^2/(C(3))^2);
R11= C(1)*exp(-(650-C(2)).^2/(C(3))^2);
R12= C(1)*exp(-(675-C(2)).^2/(C(3))^2);
R13= C(1)*exp(-(700-C(2)).^2/(C(3))^2);


%multiplicating images with formula
RM(:,:,1)= R1*double(Im400);
RM(:,:,2)= R2*double(Im425);
RM(:,:,3)= R3*double(Im450);
RM(:,:,4)= R4*double(Im475);
RM(:,:,5)= R5*double(Im500);
RM(:,:,6)= R6*double(Im525);
RM(:,:,7)= R7*double(Im550);
RM(:,:,8)= R8*double(Im575);
RM(:,:,9)= R9*double(Im600);
RM(:,:,10)= R10*double(Im625);
RM(:,:,11)= R11*double(Im650);
RM(:,:,12)= R12*double(Im675);
RM(:,:,13)= R13*double(Im700);

REDSUM = sum(double(RM(:,:,:)),3);

%---------------------------------------------


%GREEN ---------------------------------------
C = lsqcurvefit(Fun, [ 1 200 100], Xdata, YdataG);
YGreen = Fun(C, Xdata);
plot(Xdata,YdataG, 'r*', Xdata,YGreen,'b');
ValGreen= C;

G1= C(1)*exp(-(400-C(2)).^2/(C(3))^2);
G2= C(1)*exp(-(425-C(2)).^2/(C(3))^2);
G3= C(1)*exp(-(450-C(2)).^2/(C(3))^2);
G4= C(1)*exp(-(475-C(2)).^2/(C(3))^2);
G5= C(1)*exp(-(500-C(2)).^2/(C(3))^2);
G6= C(1)*exp(-(525-C(2)).^2/(C(3))^2);
G7= C(1)*exp(-(550-C(2)).^2/(C(3))^2);
G8= C(1)*exp(-(575-C(2)).^2/(C(3))^2);
G9= C(1)*exp(-(600-C(2)).^2/(C(3))^2);
G10= C(1)*exp(-(625-C(2)).^2/(C(3))^2);
G11= C(1)*exp(-(650-C(2)).^2/(C(3))^2);
G12= C(1)*exp(-(675-C(2)).^2/(C(3))^2);
G13= C(1)*exp(-(700-C(2)).^2/(C(3))^2);


%multiplicating images with formula
GM(:,:,1)= G1*double(Im400);
GM(:,:,2)= G2*double(Im425);
GM(:,:,3)= G3*double(Im450);
GM(:,:,4)= G4*double(Im475);
GM(:,:,5)= G5*double(Im500);
GM(:,:,6)= G6*double(Im525);
GM(:,:,7)= G7*double(Im550);
GM(:,:,8)= G8*double(Im575);
GM(:,:,9)= G9*double(Im600);
GM(:,:,10)= G10*double(Im625);
GM(:,:,11)= G11*double(Im650);
GM(:,:,12)= G12*double(Im675);
GM(:,:,13)= G13*double(Im700);

GREENSUM = sum(double(GM(:,:,:)),3);

%----------------------------------

%max value of images
BlueChan = uint8(BLUESUM/max(BLUESUM(:))*255);
RedChan = uint8(REDSUM/max(REDSUM(:))*255);
GreenChan = uint8(GREENSUM/max(GREENSUM(:))*255);

%assigning channels to new image
SUPERIMG(:,:,1)= BlueChan;
SUPERIMG(:,:,2)= RedChan;
SUPERIMG(:,:,3)= GreenChan;

imshow(SUPERIMG)