% HAM6 opo green scan data 

%% load data
%greenscan = load('final_green_scan.csv');
%greenscan = load('good_green_scan_April19.csv');
greenscan = load('final_final_greenscan.csv');

greenscan_x = greenscan(:,1)+99.64;
DN = ones(size(greenscan_x))*.03; %darknoise

greenscan_refl = greenscan(:,2);
greenscan_trans = greenscan(:,3)-DN;
%greenscan_trans = greenscan(:,3);
greenscan_ramp = greenscan(:,4);



%freq = green_ramp_x.*2.2718e5;

%format long

%% constant
FSR = 851.934e6; %Hz
fsr = [0, 1, 2];
fsr1 = [0*FSR, 1*FSR, 2*FSR];

%% Find transmission peaks
%pks = PD volt, locs = sec at peak

%format long
%[pks,locs] = findpeaks(greenscan_trans, greenscan_x,'MinPeakDistance', 0.5);
[pks,locs] = findpeaks(greenscan_trans, greenscan_x,'MinPeakDistance', 55); %For April19 scan


%Return ramp voltage at a given x gotten from findpeaks
for i=1:length(locs)
    index = find(greenscan_x == locs(i));   % May be multiple indexes, possibly
    VoltatPeak = greenscan_ramp(index);
    sec(i) = locs(i);
    volt(i) = VoltatPeak;

end

sec
volt



Ft = (1.479e4.*greenscan_x.^2)+1.022e7.*greenscan_x-6.509e8; 
%got from sec vs. fsr1 plot in curve fitting

%Now find time that corresponds to new peak
[pks1,locs1] = findpeaks(greenscan_trans,Ft,'MinPeakDistance',852400000/4);

for i=1:length(locs1)
    index = find(Ft == locs1(i));   % May be multiple indexes, possibly
    VoltatPeak1 = greenscan_trans(index);
    sec1(i) = locs1(i);
    volt1(i) = VoltatPeak1;
end

sec1
volt1

%Find local min for dips
[min,t] = findpeaks(-greenscan_refl,Ft,'MinPeakDistance',852400000/4)
for i=1:length(t)
    index = find(Ft == t(i));   % May be multiple indexes, possibly
    Voltatdip2 = greenscan_refl(index);
    sec_min(i) = t(i);
    volt_min(i) = Voltatdip2;
end

sec_min
volt_min

figure(1);
subplot(3,1,1);

plot(greenscan_x, greenscan_ramp, '-r') %plotting ramp voltage. 
title('VOPO scan @ 532 raw, April 19','FontSize', 30,'FontWeight','normal') 
legend('PZT ramp voltage')
subplot(3,1,2);
plot(greenscan_x, greenscan_trans, '-b', locs, pks, '*r') %ploting transmitted green23.66
legend('532 Transmitted Power')
subplot(3,1,3);
plot(greenscan_x, greenscan_refl, '-g') %plotting reflected green
legend('532 Reflected Power')

figure(2)
subplot(3,1,1);
plot(Ft*1e-6, greenscan_ramp, '-r')
%legend('PZT drive voltage (corrected for the non-linearity)', 'Location', 'northwest')
title('VOPO scan data @532nm, corrected for the non-linearity','FontSize', 30,'FontWeight','normal');
legend('April 23, 2018')
ylabel('Ramp Voltage (V)','FontSize', 18)
subplot(3,1,2)
plot(Ft*1e-6, greenscan_trans, '-b', locs1*1e-6, pks1, '*r')
ylabel('Transmitted PD (V)','FontSize', 18)
subplot(3,1,3)
plot(Ft*1e-6, greenscan_refl, '-r', sec_min*1e-6, volt_min, '*k')
xlabel('Frequency (MHz)', 'FontSize', 30)
ylabel('Reflected PD (V)','FontSize', 18)

