function Pjoint = itur_p0618_11_s2241(A1,A2,prob,Att_val1,Att_val2,PR1,PR2,Distance)
%  Pjoint = itur_p0618_11_s2241(A1,A2,prob,Att_val1,Att_val2,PR1,PR2,Distance)
%
% The function itur_p0618_11_s2241 provides an estimation of the joint
% probability of P(a1>A1,a2>A2) for two stations in a diversity
% configuration. The complete methodology is given in ITU-R Rec. P.618-11 section
% 2.2.4.1
%
% Input parameters:
%    A1 = Attenuation thresholds for the first site (dB)
%    A2 = Attenuation thresholds for the second site (dB)
%    prob = probability levels for the Attenuation CCDF for
%    each site (%)  (should be a vector)
%    Att_val1 = Attenuation exceeded or the percentage of time prob for the
%    site 1 (dB) (should be a vector of length length(prob) can be computed
%    according to ITU-R Rec. P.618-11 section 2.2.1.1
%    Att_val2 = Attenuation exceeded or the percentage of time prob for the
%    site 2 (dB) (should be a vector of length length(prob) can be computed
%    according to ITU-R Rec. P.618-11 section 2.2.1.1
%    PR1 = probability of rain for site 1 (%) can be filled with outputs
%    of ITU-R Rec P.837-6
%    PR2 = probability of rain for site 2 (%) can be filled with outputs
%    of ITU-R Rec P.837-6
%    Distance = distance in km between the two sites
%
%
% Output parameters:
%    Pjoint = joint probability of attenuation exceeded on site 1 and site 2
%
% Note:
%
% Ref: Paraboni and Barbaliscia, Multiple site attenuation prediction models based on the
% rainfall structures (meso or synoptic scales) for advanced TLC or broadcasting systems.
% URSI GA, August 2002
%
% Called functions :
%   bivnor.m (and related files bivnor.c, bivnor.dll, bivnor.mexhpux,
%   bivnor.mexglx, bivnor.mexa64 depending on hardware architecture)
%   Bivnor.c requires compilation:">>mex bivnor.c"
% Necessary toolboxes :
%   None
%
%
% DISCLAIMER: This material is made available for information purposes only and cannot be used and/or relied upon for any other purpose.
% The ITU-R SG-3 makes no representations or warranties of any kind, either express or implied, as to the completeness, accuracy,
% reliability, suitability or availability of sample implementations made available via its Software website.
% Any reliance you place on such information is therefore entirely at your own risk.
%--------------------------------------------------------------------------

% Rain correlation function
roR=0.7.*exp(-Distance./60)+0.3.*exp( -(Distance/700).^2 );

% Attenuation correlation function
roA=0.94.*exp(-Distance./30)+0.06.*exp( -(Distance/500).^2 );

R1=sqrt(2) .* erfcinv(2.*PR1./100);
R2=sqrt(2) .* erfcinv(2.*PR2./100);
% Joint probability of rain (R>0) in sites A and B
Pr=bivnor(R1,R2,roR);
Pr

coeff1=polyfit(log(Att_val1),erfcinv(2*prob/PR1),1);
coeff2=polyfit(log(Att_val2),erfcinv(2*prob/PR2),1);

coeff1
coeff2

sig1=1/(sqrt(2)*coeff1(1));
sig2=1/(sqrt(2)*coeff2(1));
m1=-coeff1(2)*sqrt(2)*sig1;
m2=-coeff2(2)*sqrt(2)*sig2;

log(A1)
A1

Pa=bivnor((log(A1)-m1)/sig1,(log(A2)-m2)/sig2,roA);


Pjoint=100.*Pr.*Pa;