%% 
%
%This is the file to make the plots of RFPD's TF from sokutei 0 & 1.
%Please put the serial number of RFPD into 'SerialNum'
%
%Written by Kohei Yamamoto
%Partially modified by Y. Enomoto
%
%%
clear all
close all

SerialNum = 'JGW-S1808567';
RFHIFreq = 45.02; %[MHz]
RFLOFreq = 16.87; %[MHz]
TxtOffset = 8;

V2A = 18e-3; % A/V, V to A factor of test input port

%import the data of sokutei0
filename1 = 'data/sokutei0_Channels.csv';
Soku0 = csvread(filename1, TxtOffset);
Soku0(:,2) = 20.^((Soku0(:,2)+6.0)./20);  %dB->ratio[V/V], +6.0 compensates 6 dB ATTN inserted in sokutei0
Soku0(:,3) = (pi/180)*Soku0(:,3);  %degree->rad

%import the data of sokutei1_HI
filename3 = 'data/sokutei1_HI_Channels.csv';
Soku1HI = csvread(filename3, TxtOffset);
Soku1HI(:,2) = 20.^(Soku1HI(:,2)./20);  %dB->ratio[V/V]
Soku1HI(:,3) = (pi/180)*Soku1HI(:,3);  %degree->rad

%import the data of sokutei1_LO
filename4 = 'data/sokutei1_LO_Channels.csv';
Soku1LO = csvread(filename4, TxtOffset);
Soku1LO(:,2) = 20.^(Soku1LO(:,2)./20);  %dB->ratio[V/V]
Soku1LO(:,3) = (pi/180)*Soku1LO(:,3);  %degree->rad

%% phase delay tuning with the sokutei0

%sokutei1_HI
RFHI(:,1) = Soku1HI(:,1);
RFHI(:,2) = Soku1HI(:,2)./Soku0(:,2);
RFHI(:,3) = Soku1HI(:,3)-Soku0(:,3);

%sokutei1_LO
RFLO(:,1) = Soku1LO(:,1);
RFLO(:,2) = Soku1LO(:,2)./Soku0(:,2);
RFLO(:,3) = Soku1LO(:,3)-Soku0(:,3);

%% plot
%Sokutei0
figure(1);
subplot(2,1,1);
semilogx(Soku0(:,1)*10^(-6),20*log10(Soku0(:,2)),'r-','LineWidth',1.5); %The unit of x-axis is [MHz]
ylabel('Magnitude[dB]')
xlabel('Frequency[MHz]')
title(['Sokutei0,  ', SerialNum], 'Fontsize', 16)
grid on;

subplot(2,1,2);
semilogx(Soku0(:,1)*10^(-6),(180/pi)*Soku0(:,3),'r-','LineWidth',1.5);
ylabel('Phase[degree]')
xlabel('Frequency[MHz]')
grid on;

%Sokutei1HI_RawData
[M, I] = min(20*log10(Soku1HI(:,2)));
HIrange_min = 20*log10(Soku1HI(I,2))-5;
[M, I] = max(20*log10(Soku1HI(:,2)));
HIrange_max = 20*log10(Soku1HI(I,2))+5;

figure(2);
subplot(2,1,1);
semilogx(Soku1HI(:,1)*10^(-6),20*log10(Soku1HI(:,2)),'r-','LineWidth',1.5); %The unit of x-axis is [MHz]
ylabel('Magnitude[dB]')
xlabel('Frequency[MHz]')
title(['Sokutei1 HI(45.02[MHz]) Raw Data,  ', SerialNum], 'Fontsize', 16)
ylim([HIrange_min HIrange_max]);
grid on;
hold on
plot([RFHIFreq RFHIFreq],[HIrange_min HIrange_max],'LineWidth',0.8,'color','m');

subplot(2,1,2);
semilogx(Soku1HI(:,1)*10^(-6),(180/pi)*Soku1HI(:,3),'r-','LineWidth',1.5);
ylabel('Phase[degree]')
xlabel('Frequency[MHz]')
grid on;

%Sokutei1LO_RawData
[M, I] = min(20*log10(Soku1LO(:,2)));
LOrange_min = 20*log10(Soku1LO(I,2))-5;
[M, I] = max(20*log10(Soku1LO(:,2)));
LOrange_max = 20*log10(Soku1LO(I,2))+5;

figure(3);
subplot(2,1,1);
semilogx(Soku1LO(:,1)*10^(-6),20*log10(Soku1LO(:,2)),'b-','LineWidth',1.5); %The unit of x-axis is [MHz]
ylabel('Magnitude[dB]')
xlabel('Frequency[MHz]')
title(['Sokutei2 LO(16.87[MHz]) Raw Data,  ', SerialNum], 'Fontsize', 16)
ylim([LOrange_min LOrange_max]);
grid on;
hold on
plot([RFLOFreq RFLOFreq],[LOrange_min LOrange_max],'LineWidth',0.8,'color','m');

subplot(2,1,2);
semilogx(Soku1LO(:,1)*10^(-6),(180/pi)*Soku1LO(:,3),'b-','LineWidth',1.5);
ylabel('Phase[degree]')
xlabel('Frequency[MHz]')
grid on;

%RF_HI
[M, I] = min(20*log10(RFHI(:,2)));
HIrange_min = 20*log10(RFHI(I,2))-5;
[M, I] = max(20*log10(RFHI(:,2)));
HIrange_max = 20*log10(RFHI(I,2))+5;

figure(4);
subplot(2,1,1);
loglog(RFHI(:,1)*10^(-6),RFHI(:,2)/V2A,'r-','LineWidth',1.5); %The unit of x-axis is [MHz]
ylabel('Transimpedance[Ohm]')
xlabel('Frequency[MHz]')
title(['RF HI(45.02[MHz]) Transfer Function,  ', SerialNum], 'Fontsize', 16)
ylim([10^(HIrange_min/20)/V2A 10^(HIrange_max/20)/V2A]);
grid on;
hold on
plot([RFHIFreq RFHIFreq],[10^(HIrange_min/20)/V2A 10^(HIrange_max/20)/V2A],'LineWidth',0.8,'color','m');

subplot(2,1,2);
semilogx(RFHI(:,1)*10^(-6),(180/pi)*RFHI(:,3),'r-','LineWidth',1.5);
ylabel('Phase[degree]')
xlabel('Frequency[MHz]')
grid on;

saveas(gcf,[strcat(SerialNum,'_Transimpedance_HI.png')])

% save data
data1(:,1)=RFHI(:,1);
data1(:,2)=RFHI(:,2)/V2A;
data1(:,3)=(180/pi)*RFHI(:,3);
filename=strcat(SerialNum,'_Transimpedance_HI.txt');
fileID = fopen(filename,'w');
fprintf(fileID,'%6.10f %12.10f %12.10f\r\n',data1');
fclose(fileID);
clear data1

%RF_LO
[M, I] = min(20*log10(RFLO(:,2)));
LOrange_min = 20*log10(RFLO(I,2))-5;
[M, I] = max(20*log10(RFLO(:,2)));
LOrange_max = 20*log10(RFLO(I,2))+5;

figure(5);
subplot(2,1,1);
loglog(RFLO(:,1)*10^(-6),RFLO(:,2)/V2A,'b-','LineWidth',1.5); %The unit of x-axis is [MHz]
ylabel('Transimpedance[Ohm]')
xlabel('Frequency[MHz]')
title(['RF LO(16.87[MHz]) Transfer Function,  ', SerialNum], 'Fontsize', 16)
ylim([10^(LOrange_min/20)/V2A 10^(LOrange_max/20)/V2A]);
grid on;
hold on;
plot([RFLOFreq RFLOFreq],[10^(LOrange_min/20)/V2A 10^(LOrange_max/20)/V2A],'LineWidth',0.8,'color','m');

subplot(2,1,2);
semilogx(RFLO(:,1)*10^(-6),(180/pi)*RFLO(:,3),'b-','LineWidth',1.5);
ylabel('Phase[degree]')
xlabel('Frequency[MHz]')
grid on;

saveas(gcf,[strcat(SerialNum,'_Transimpedance_LO.png')])

% save data
data1(:,1)=RFLO(:,1);
data1(:,2)=RFLO(:,2)/V2A;
data1(:,3)=(180/pi)*RFLO(:,3);
filename=strcat(SerialNum,'_Transimpedance_LO.txt');
fileID = fopen(filename,'w');
fprintf(fileID,'%6.10f %12.10f %12.10f\r\n',data1');
fclose(fileID);
clear data1
