function Pd = pdSwerling3(Pfa,SNR,N)
%This function is for internal use only. It may be removed in the future.

%   Copyright 2020 The MathWorks, Inc.

%#codegen

SNRlen = numel(SNR);
T = real(gammaincinv(1 - Pfa, N));
T = T(:);

c = 1 ./ (1 + N * SNR / 2);
c = c(:)';

K0 = 1 - (N - 2) * c ./ (1 - c) + T * c;

if N < 3
    Pd = K0 .* exp(-T * c - (N - 2) * log(1 - c));
else
    A = repmat((N - 1) * log(T) - T, 1, SNRlen);
    Pd = exp(A + log(1 - c) - gammaln(N)) .* real(gammainc(T * (1 - c), N - 1, 'scaledlower'));
    Pd = exp(A + log(c) - gammaln(N-1)) + 1 - real(gammainc(T, N - 1)) + K0 .* Pd;
end

Pd(Pd > 1) = 1;