function res=min_diff_coupled(P,I_0,R_0,d,x_data,y_data,f)

global loop

% fit of the SR signal and phase rotation
% P parameter 
% P(1)=atomic width
% P(2)=amplitude of mixing Z
% P(3)=phase of mixing Z
% P(4)=detuning
loop=loop+1;
width=P(1);
F=P(2)*exp(1i*P(3));
x0=P(4);
Amp_0=I_0.*R_0;
F_angle=angle(F);
F_amp=abs(F);
[a,b]=coupledFM(Amp_0,width,F_amp,x0,F_angle,f);
x_theory=filtfilt(d.sosMatrix,d.ScaleValues,a);
y_theory=filtfilt(d.sosMatrix,d.ScaleValues,b);
% size(x_data)
% size(x_theory)
% size(f)
res=[x_data,y_data]-[x_theory,y_theory];


