% Jon Newman 
% 2012 03 23
% 
% I measured the optical conversion functions for both blue and yellow
% light on the optoclamp rig

%% Blue LED

% The blue power density at the culture
% The photodiode voltages (should be linearly proporational to the power
% density at the culture
vPD = [20.05 100:100:1400]'/1000; % volts

% The blue power density at the culture
pow = [0
    7
    16
    27.5
    37
    46.5
    57
    67
    78.6
    88.9
    98.5
    110
    119
    129.5
    139.5]/area;

% current through LED
iLED = [31
    67.7
    117
    172
    225
    288
    351
    414
    484
    555
    623
    699
    771
    860
    956]/1000; % amps


pow0i956 = 11.3200; % Previously measured
pow = pow0i956 * pow/max(pow);

iLED = iLED - iLED(1);
    
% Create conversion functions
blue.info = 'First order polynomail coefficients for the conversion from vPD (vREF) to power density at the cultture (mW/mm^2). Second order polynomail coefficients for the conversion from LED current (amps) to power density at the culture (mW/mm^2)'; 
blue.p_vpd2pow = polyfit(vPD,pow,1);
blue.p_i2pow = polyfit(iLED,pow,2);
blue.i = iLED;
blue.vpd = vPD;
blue.pow =  pow;

%% Yellow LED
area = 6.2; % Area of spot at culture (during this measurement)

% The blue power density at the culture
pow = [0
    0
    0
    6.81
    15.15
    22.7
    29.3
    37.6
    44.2
    50.8
    55.7
    62.1
    66.0]/area;

% Current through LED
iLED = ([0 0 0:100:1000])'/1000; % amps
    
% Create conversion functions
yellow.info = 'Second order polynomail coefficients for the conversion from LED current (amps) to power density at the cutlture (mW/mm^2)'; 
yellow.p_i2pow = polyfit(iLED,pow,2);
yellow.i = iLED;
yellow.pow =  pow;

%% Save the conversions as polynomial coeffs
save('combined_led_conversions','blue','yellow');

%% Figure

close all
figure('units','centimeter','position',[2 2 15 10])

aw = 3.5;
ah = 3.5;
as = 1.0;
tl_w = 2/(28.3464567*aw);
tl_h = 2/(28.3464567*ah);

% VPD (Vref) vs. blue light power 
axes('units','centimeter','position',[2+1*as+0*aw 2 aw ah])

vtest = 0:0.01:1.5;

hold on
plot(blue.vpd,blue.pow,'bo','MarkerSize',3)
% plot(blue.vpd,10*blue.i,'bo','MarkerSize',3)
plot(vtest,polyval(blue.p_vpd2pow,vtest),'r-');
ylim([0 13])
ylabel('Power Density @ cells (mW/mm^2)')
xlabel('V_{REF} prop to V_{PD}')
set(gca,'TickDir','out','TickLength',[tl_w tl_h])

% Current to power
axes('units','centimeter','position',[2+2*as+1*aw 2 aw ah])

itest = 0:0.01:1;

hold on
% plot(blue.i,blue.pow,'ro')
% plot(itest,polyval(blue.p_i2pow,itest),'b-');
plot(yellow.i,yellow.pow,'bo','MarkerSize',3)
plot(itest,polyval(iLED2pow.p_yel,itest),'r-');
ymax = get(gca,'YTick');
ylim([0 13])
xlabel('V_{REF} prop to I_{LED}')
set(gca,'TickDir','out','TickLength',[tl_w tl_h])

%% Save
% Figure
export_fig combined_led_conversions -pdf -transparent









