function pilots = ehtPilots(ruSize,numSTS,n,z)
%ehtPilots EHT pilot sequence
% 
%   PILOTS = ehtPilots(RUSIZE,NUMSTS,N,Z) returns the pilot sequence as per
%   IEEE P802.11be/D1.0, Section 36.3.13.11.
%
%   RUSIZE is the RU size in subcarriers and must be one of 26, 52, 106,
%   242, 484, 996, 1992, or 3984.
%
%   NUMSTS is the number of space-time streams.
%
%   N is the OFDM symbol index.
%
%   Z is the OFDM symbol offset index.

%   Copyright 2020-2021 The MathWorks, Inc.

% Pilot sequence - IEEE P802.11be/D1.0, Section 36.3.13.11
assert(isrow(n));
temp = [];
for i=1:numel(ruSize)
    switch ruSize(i)
        case 26
            Psi = [1 -1]; % IEEE P802.11ax/D4.1, Table 27-36
            pilotSeq = [Psi(mod(n,2)+1); Psi(mod(n+1,2)+1)]; % IEEE P802.11ax/D4.1, Equation 27-101
        case {52 106}
            Psi = [1 1 1 -1]; % IEEE P802.11ax/D4.1, Table 27-38
            pilotSeq = [Psi(mod(n,4)+1); Psi(mod(n+1,4)+1); Psi(mod(n+2,4)+1); Psi(mod(n+3,4)+1)]; % IEEE P802.11ax/D4.1, Equation 27-102 and 27-103
        case 242
            Psi = [1 1 1 -1 -1 1 1 1]; % IEEE P802.11ax/D4.1, Table 27-41
            pilotSeq = [Psi(mod(n,8)+1); Psi(mod(n+1,8)+1); Psi(mod(n+2,8)+1); Psi(mod(n+3,8)+1); ...
                Psi(mod(n+4,8)+1); Psi(mod(n+5,8)+1); Psi(mod(n+6,8)+1); Psi(mod(n+7,8)+1)]; % IEEE P802.11ax/D4.1, Equation 27-104
        case {484 996}
            Psi = [1 1 1 -1 -1 1 1 1]; % IEEE P802.11ax/D4.1, Table 27-41
            pilotSeq = ru484Seq(Psi,n);
        case 2*996
            assert(ruSize==2*996)
            Psi = [1 1 1 -1 -1 1 1 1]; % IEEE P802.11ax/D4.1, Table 27-41
            pilotSeq = [ru484Seq(Psi,n); ru484Seq(Psi,n)]; % IEEE P802.11ax/D4.1, Equation 27-107
        otherwise % 4*996
            assert(ruSize==4*996)
            Psi = [1 1 1 -1 -1 1 1 1]; % IEEE P802.11ax/D4.1, Table 27-41
            pilotSeq = [ru484Seq(Psi,n); ru484Seq(Psi,n); ru484Seq(Psi,n); ru484Seq(Psi,n)]; % IEEE P802.11be/D1.0, Equation 36-80
    end
    temp = [temp; pilotSeq]; %#ok<AGROW> 
end
pilotSeq = temp;

polaritySeq = wlan.internal.pilotPolaritySequence(n+z).';
pilots = repmat(bsxfun(@times,polaritySeq,pilotSeq),1,1,numSTS); % Same pilots for each STS

end

function pilotSeq = ru484Seq(Psi,n)
    pilotSeq = [Psi(mod(n,8)+1); Psi(mod(n+1,8)+1); Psi(mod(n+2,8)+1); Psi(mod(n+3,8)+1); ...
        Psi(mod(n+4,8)+1); Psi(mod(n+5,8)+1); Psi(mod(n+6,8)+1); Psi(mod(n+7,8)+1); ...
        Psi(mod(n+8,8)+1); Psi(mod(n+9,8)+1); Psi(mod(n+10,8)+1); Psi(mod(n+11,8)+1); ...
        Psi(mod(n+12,8)+1); Psi(mod(n+13,8)+1); Psi(mod(n+14,8)+1); Psi(mod(n+15,8)+1)]; % IEEE P802.11ax/D4.1, Equation 27-105 and 27-106
end