function [ph1, ph2, Q1, Q2, w1, w2, q1, q2] = PhaseTrans2(th1, th2, Nopt, N, Nf, dt);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%        INSERT         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%   th1:   protophase of the 1st system
%%   th2:   protophase of the 2nd system
%%   Nopt:  order of Fourier Terms for Preprocessing with PhaseTrans1. For Example Nopt = 48.
%%   N:     order of Fourier Terms for Phase Transformation for Coupled Oscialltors. For example N = 10.
%%   Nf:    order of Fourier Terms of the Model of the Phase Dynamics - should be smaller than N. For example Nf = 9, if N = 10.
%%   dt:    time step of the data. For example dt = 1/1000 for data sampled  with 1 kHz
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%   OUTPUT     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%   ph1:  phase of the 1st system
%%   ph2:  phase of the 2nd system
%%   Q1:  coefficient matrix of the phase dynamics model of the 1st system
%%   Q2:  coefficient matrix of the phase dynamics model of the 2nd system
%%   w1:  estimate of the autonomous frequency of the 1st system (constant term of the phase model)
%%   w2:  estimate of the autonmous frequency of the 2nd system
%%   q1:  coupling function of the 1st system on a 50 x 50 grid
%%   q2:  coupling function of the 2nd system on a 50 x 50 grid
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

S = size(th1); if S(2) > S(1) th1 = th1'; end; % Unifying structure of data
S = size(th2); if S(2) > S(1) th2 = th2'; end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%          PREPROCESSING WITH AUTONOMOUS OSCILLATOR TRANSFORMATION
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[P1] = PhaseTrans1(th1, Nopt);
[P2] = PhaseTrans1(th2, Nopt);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%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%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%              SUBROUTINES
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%           PhaseTrans1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function[PH, S]=PhaseTrans1(TH, N); 
%%     INSERT
%%     TH:    protophase
%%     N:     order of Fourier terms N. For example N=48.
%%     OUTPUT
%%     PH:    phase
%%     S:     coefficients of the transformation
S = []; PH = []; % Define Variables
%%      COMPUTING TRANSFORMATION COEFFS Sn 
for n = 1 : (2*N)+1; % Begin of the iteration to compute coefficients S of the transformation
    S(n) = (1 / ( length(TH)*2*pi )) * sum( exp(-i*(n-(N+1))*TH));
end; % End of the iteration
%%     COMPUTING PHASE FROM COEFFS Sn BY ITERATION OVER N
PH = TH; % Sn for n = 0
for n = 1 : (2*N)+1;
if (abs((n-(N+1)))>0) PH = PH + 2*pi*S(n) * (exp(i*(n-(N+1))*TH)-1) / (i*(n-(N+1))); end; % Sn for n different from 0
end;
PH = real(PH); % Eliminating small imaginary parts, which should theoreticall be zero
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                     OptOmega
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ph1, ph2, w1, w2, s, r] = OptOmega(th1, th2, N, dt); 
%%      INPUT
%%      th1:   protophase of the 1st system
%%      th2:   protophase of the 2nd system       
%%      N:     order of Fourier terms
%%      dt:    time step of data  
%%      OUTPUT
%%      ph1: phase of the 1st system
%%      ph2: phase of the 2nd system
%%      w1: estimate of the autonomous frequency of the 1st system 
%%      w2: estimate of the autonomous frequency of the 2nd system 
%% COMPTING COEFFS Fnm (1st system), Gnm (2nd system) OF THE MODEL OF THE PROTOPHASE DYNAMICS
[F] = FLinReg2D(th1, th2, N, dt);
[G] = FLinReg2D(th2, th1 ,N , dt);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                 COMPUTING COEFFS s, r OF THE PHASE TRANSFORMATION
[S] = fsolve(@NLS, zeros(4*N+2,1), [], F, G);  % Solving non-linear system for coeffs
s = S(1:2*N);
s = [s(1:N); 1; s(N+1:end)];
r = S(2*N+1:4*N);
r = [r(1:N); 1; r(N+1:end)];
w1 = real(S(end-1));
w2 = real(S(end));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%           COMPUTING PHASE TRANSFORMATION
Su=th1;
for n= 1 : (2*N)+1
if (abs((n-(N+1)))>0) Su = Su + s(n) *exp(i*(n-(N+1))*th1) / (i*(n-(N+1))); end;
end;
ph1 = real(Su);

Su=th2;
for n= 1 : (2*N)+1
if (abs((n-(N+1)))>0) Su = Su + r(n) *exp(i*(n-(N+1))*th2) / (i*(n-(N+1))); end;
end;
ph2 = real(Su);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                 FLinReg2D
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function[C]=FLinReg2D(P1,P2,or, c);
%%  INSERT
%%  P1: protphase or phase of the 1st system
%%  P2: protophase or phase of the 2nd system ('external')
%%  or: order of Fourier terms
%%  dt: time step of data
%%  OUTPUT
%%  F: coefficients of the model describing the derivative of P1 as function of P1 and P2
G=[]; B=[]; GP1 = gradient(P1)/c;
%%%%   computing coeffs of matrix G (later C2)
for n = -2*or : 2*or;
    for m = -2*or : 2*or;
        G(n +(2*or)+1, m+(2*or)+1) =  mean(exp( i*(n*P1 + m*P2) ));
    end;
end;
%%%%  computing coeffs of matrix C2
C1=[]; C2=[];
for n = -or : or;
    for m = -or : or;
    b = mean( GP1.* exp(-i*( n*P1 + m*P2) ));  %%Defining the matrix of components
    B(n + or +1, m+ or +1)=b;
    C1=[C1; b];
    for r = -or : or;
    for s = -or : or;
   C2( (n+or)*(2*or+1) + (m+or+1) , (r+or)*(2*or+1) + (s+or+1) ) = G( (n-r)+(2*or)+1, (m-s)+(2*or)+1);    
end;
end;
end;
end;
%% solving linear system to obtain coeffs c
c= conj(C2) \ C1;
%Writing Coeffs as Matrix
C=[];
for n = 0 : 2*or;
    for m = 0 : 2*or;
        C(n+1, m+1)=c(n*(2*or+1) + m+1);    
    end;
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%             NLS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [x] = NLS(X, F, G);  % Defining non-linear system to solve
N=(size(F)-1)/2;
N=N(1);

S=X(1:2*N);
S=[S(1:N);1;S(N+1:end)];

R=X(2*N+1:4*N);
R=[R(1:N);1;R(N+1:end)];
w1=X(end-1);
w2=X(end);

M=S*R.';

F1=conj(F); G1=G';
x(1)=sum(sum(M.*F1))-w1;
x(2)=sum(sum(M.*G1))-w2;

J=F1; K=G1;
for n= 1 : N;
J(1,:)=[]; K(:,1)=[];
J=[J;zeros(1,2*N+1)]; K=[K zeros(2*N+1,1)]; 
x=[x sum(sum(M.*J))];
x=[x sum(sum(M.*K))];
end;

J=F1; K=G1;
for n= 1: N;
J(end, :)=[]; K(:, end)=[];
J=[zeros(1,2*N+1); J]; K=[zeros(2*N+1,1) K]; 
x=[x sum(sum(M.*J))];
x=[x sum(sum(M.*K))];
end;
x = x*1000; % multiplying the entire system, which si sloved to be zero, with factor of 1000x to increase accuracy, since original coefficients are rather small
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%               CoupFunc
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function[q1] = CoupFunc(Q, PL);
%%            INSERT
%%   Q:    coefficients of the phase or protophase model
%%   PL:   set PL ==1 for plotting function / PL==0 for not plotting function
%%            OUTPUT
%%   q1: phase or protophase function on a 50 x 50 grid
%%
S=size(Q);
S=(S(1)-1)/2;
[X,Y]=meshgrid(2*pi*(0:49)/50,2*pi*(0:49)/50); % defining grid
q1=zeros(size(X));

for n = -S : S;
    for m = -S : S;
    q1 = q1 + Q(n+S+1, m+S+1) * exp(i*n*X + i*m*Y);       % computing function
end;
end;
q1 = real(q1);

if PL == 1;
figure; mesh(q1);
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

