offset=13;
max=[361,262,410,340,284,222,388, 352, 382,404, 408, 342,233]-offset;
min=[23,30,22,26,28,29,26, 20, 24, 24, 20, 22, 26]-offset;
temp=[32.85,32,34.02,34.81,35.33,35.83,34.5, 34.37,34.2, 33.93, 33.69, 33.32, 32.96];
unlocked_temp=[33,32,34,35,35.5,36,34.5, 34.5, 34.3, 34, 33.7, 33.4, 33];

x=(sqrt(max./min)-1)./(1+sqrt(max./min));

gain=1./(1-x).^2;

figure(1)
subplot(2,1,1)
plot(unlocked_temp(1:7), gain(1:7), 'o',...
    unlocked_temp(8:13), gain(8:13), 'x')
legend('First set of measurements', 'second set')
xlabel('Temperature setting for scanning OPO (C)')
ylabel('non linear gain')

subplot(2,1,2)
plot(unlocked_temp(1:7), max(1:7), 'o',...
    unlocked_temp(8:13), max(8:13), 'x')
legend('First set of measurements', 'second set')
xlabel('Temperature setting for scanning OPO (C)')
ylabel('Maximum amplified signal (mV)')

print('Phase_matching_in_air.png','-dpng')

figure(2)
subplot(2,1,1)
plot(temp(1:7), gain(1:7), 'o',...
    temp(8:13), gain(8:13), 'x')



subplot(2,1,2)
plot(temp(1:7), max(1:7), 'o',...
    temp(8:13), max(8:13), 'x')

%we got a nonlinear gain of something like 15-20 with about 6mW injected
%into OPO, so our threshold is about 10-11mW

Pthresh=6/(1-1/sqrt(20))^2  
Pthresh_mm=Pthresh*0.74  %correct for 66 % mode mismatch (alog 40594)