% This gives demod. phase for the five DOF that
% give maximum signals. Run dp.kat

! perl mkat dp

load dp_run1.out % l+
load dp_run2.out % lm
load dp_run3.out % ls
load dp_run4.out % L+
load dp_run5.out % L-

 Lpx= dp_run4(:,1);
 Lpy= dp_run4(:,2);
 Lmx= dp_run5(:,1);
 Lmy= dp_run5(:,2);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
N = 181; % from 0 to 180 degrees
L = length(dp_run1)/N; 

%for data X
for i = 1:L
n = 1;
m = L - n ;
j = (i - 1)*(m + 1) + n;
k = j + m;
datx1(:,i)=dp_run1(j:k ,1);
datx2(:,i)=dp_run2(j:k ,1);
datx3(:,i)=dp_run3(j:k ,1);
end

%for data Y
for i = 1:L
n = 1;
m = L - n ;
j = (i - 1)*(m + 1) + n;
k = j + m;
daty1(:,i)=dp_run1(j:k ,2);
daty2(:,i)=dp_run2(j:k ,2);
daty3(:,i)=dp_run3(j:k ,2);
end

%for data Z
for i = 1:L
n = 1;
m = L - n ;
j = (i - 1)*(m + 1) + n;
k = j + m;
datz1(:,i)=dp_run1(j:k ,3);
datz2(:,i)=dp_run2(j:k ,3);
datz3(:,i)=dp_run3(j:k ,3);
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %for L+

[CLp,ILp]= max(abs(Lpy));   % ILp= zmax
fLp=180*(ILp-1)/(N-1);%phase1

%for L-

[CLm,ILm]= max(abs(Lmy));   % ILp= zmax
fLm=180*(ILm-1)/(N-1);%phase1

%for l+

Ap=max(abs(datz1));   %= (zmax1, Zmax2, ..., zmaxn)
[Cp,Ip]=max(Ap);      % zmaxIm
Bp=datz1(:,Ip);       
[Dp,Ep]=max(abs(Bp)); %max
f1p=180*(Ip-1)/(N-1);%phase1
f2p=180*(Ep-1)/(N-1);%phase2
Fp=[f1p f2p];

%for l-

Am=max(abs(datz2));   %= (zmax1, Zmax2, ..., zmaxn)
[Cm,Im]=max(Am);      % zmaxIm
Bm=datz2(:,Im);       
[Dm,Em]=max(abs(Bm)); %max
f1m=180*(Im-1)/(N-1);%phase1
f2m=180*(Em-1)/(N-1);%phase2
Fm=[f1m f2m];

%for ls

As=max(abs(datz3));   %= (zmax1, Zmax2, ..., zmaxn)
[Cs,Is]=max(As);      % zmaxIm
Bs=datz3(:,Is);       
[Ds,Es]=max(abs(Bs)); %max
f1s=180*(Is-1)/(N-1);%phase1
f2s=180*(Es-1)/(N-1);%phase2
Fs=[f1s f2s];

% phase1  phase2
% L+
% L-
% l+
% lm
% ls
%%%%%%%
Fall(1,1)=fLp;
Fall(2,1)=fLm;
Fall(3,1)=Fp(1,1);
Fall(3,2)=Fp(1,2);
Fall(4,1)=Fm(1,1);
Fall(4,2)=Fm(1,2);
Fall(5,1)=Fs(1,1);
Fall(5,2)=Fs(1,2);
dlmwrite('phase.txt',Fall)
