% Jon Newman 
% 2012 05 18
% 
% I measured the optical conversion functions both the intracellular and
% extracellular rigs. The purpose of doing this was to fully characterize
% the relationship between the light power measured by the photodiode
% (PDA30) and the power density of the spot exciting the culture. This file
% contains the conversion for the:
% 
% 2. For the improved extraceullar rig configuration with photodiode
% mounted between the source bundle and the collector lens

%% Extracellular Rig (new)
area = 6.2; % Area of spot at 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
    
close all
figure

subplot(131)
hold on
plot(vPD,pow,'b-o','LineWidth',1.5);
xlabel('V_{PD} (V)')
ylabel('Power Density @ cells (mW/mm^2)')
set(gca,'TickDir','out','TickLength',[0.01 0.025])
legend('457 nm','Location','NorthWest')
axis square

subplot(132)
hold on
plot(vPD,iLED,'b-o','LineWidth',1.5);
xlabel('V_{PD} (V)')
ylabel('I_{LED} (A)')
set(gca,'TickDir','out','TickLength',[0.01 0.025])
legend('457 nm','Location','NorthWest')
axis square

subplot(133)
hold on
plot(iLED,pow,'b-o','LineWidth',1.5);
ylabel('Power Density @ cells (mW/mm^2)')
xlabel('I_{LED} (A)')
set(gca,'TickDir','out','TickLength',[0.01 0.025])
legend('457 nm','Location','NorthWest')
axis square

%% Create conversion functions

vPD2pow.p = polyfit(vPD,pow,1);
vPD2pow.info = 'First order polynomail coefficients for the conversion from photodiode voltage (volts) to power density at the cutlture (mW/mm^2)'; 
vPD2iLED.p = polyfit(vPD,iLED,2);
vPD2iLED.info = 'First order polynomail coefficients for the conversion from photodiode voltage (volts) to LED current (amps)'; 
iLED2pow.p = polyfit(iLED,pow,2);
iLED2pow.info = 'Second order polynomail coefficients for the conversion from LED current (amps) to power density at the cutlture (mW/mm^2)'; 

vtest = 0:0.01:1.5;
itest = 0:0.01:1;

subplot(131)
plot(vtest,polyval(vPD2pow.p,vtest),'r-','LineWidth',1.5);
ymax = get(gca,'YTick');
ylim([0 ymax(end)])

subplot(132)
plot(vtest,polyval(vPD2iLED.p,vtest),'r-','LineWidth',1.5);
ymax = get(gca,'YTick');
ylim([0 ymax(end)])

subplot(133)
plot(itest,polyval(iLED2pow.p,itest),'r-','LineWidth',1.5);
ymax = get(gca,'YTick');
ylim([0 ymax(end)])

% Save the conversions as polynomial coeffs
save('vPD2pow','vPD2pow');
save('vPD2iLED','vPD2iLED');
save('iLED2pow','iLED2pow');

%% Figure
export_fig 2012_05_18_trans-stimulator_measurment-conversion -pdf -transparent