function muPaddedPSDU = addMUPadding(macFrame, muPSDULength)
%addMUPadding Adds or removes the padding difference between an HE-SU and
%HE-MU PSDU
%
%   This is an example helper function.
%
% MUPADDEDPSDU = addMUPadding(MACFRAME, MUPSDULENGTH) Adds or removes the
% padding to align MAC frame MACFRAME with PSDU length MUPSDULENGTH and
% returns the padded aligned MU PSDU MUPADDEDPSDU.

%   Copyright 2019 The MathWorks, Inc.

%#codegen

% EOF zero delimiter (4 octets)
eofDelimiter = [1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 1 1 1 0 0 1 1 1 0 0 1 0]';
delimiterLength = 4;

% Valid data in a MAC frame is always word aligned
psdu = macFrame(1 : end-rem(numel(macFrame)/8,4)*8);
psduLength = numel(psdu)/8;
% Difference between the required padding for SU and MU formats
muPadding = muPSDULength - psduLength;

% Calculate the number of EOF delimiters and zero padding needed
% for the MU padding
numEOFDelimiters = floor(abs(muPadding)/4);
wordPadding = rem(abs(muPadding),4);

if muPadding < 0
    % Delete the extra padding created with wlanHESUConfig object
    muPaddedPSDU = psdu(1 : end - (numEOFDelimiters*delimiterLength*8 + wordPadding*8));
else
    % Add MU padding to the PSDU
    muPaddedPSDU = [psdu; repmat(eofDelimiter, numEOFDelimiters,1); zeros(wordPadding*8,1)];
end
end