function Pd = pdSwerling4(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);
Pd = zeros(numel(Pfa),SNRlen);  % preallocate
T = real(gammaincinv(1-Pfa,N));
T = T(:);

for k = 1:SNRlen
    tmp1 = T/(1+SNR(k)/2);
    pdtmp = zeros( numel( Pfa ), 1 );
    for gammaI = 0:N
        c = gammaI*log(SNR(k)/2)+sum(log(N-gammaI+1:N))-sum(log(1:gammaI));
        pdtmp = pdtmp + exp(c-N*log(1+SNR(k)/2))*real(gammainc(tmp1,N+gammaI));
    end
    Pd(:,k) = 1 - pdtmp;
end

Pd(Pd > 1) = 1;