%% Recover HE-Data Field for HE-SU-Format Packet
% Recover the HE-Data field for an HE-SU-format packet by decoding the HE signaling fields, updating the unknown properties in the recovery configuration object, and passing the updated object into the HE-Data recovery function.
% Create an HE-SU-format configuration object, specifying the MCS, and extract the channel bandwidth.

cfgHESU = wlanHESUConfig('MCS',0);
cbw = cfgHESU.ChannelBandwidth;

% Generate a waveform for the specified configuration object.

bits = randi([0 1],8*getPSDULength(cfgHESU),1,'int8');
waveform = wlanWaveformGenerator(bits,cfgHESU);

% Create a WLAN recovery configuration object, specifying the known channel bandwidth and an HE-SU-format packet.

cfgRx = wlanHERecoveryConfig('ChannelBandwidth',cbw,'PacketFormat','HE-SU');

% Recover the HE signaling fields by retrieving the field indices and performing the relevant demodulation operations.

ind = wlanFieldIndices(cfgRx);
heLSIGandRLSIG = waveform(ind.LSIG(1):ind.RLSIG(2),:);
symLSIG = wlanHEDemodulate(heLSIGandRLSIG,'L-SIG',cbw);
info = wlanHEOFDMInfo('L-SIG',cbw);

% Merge the L-SIG and RL-SIG fields for diversity and obtain the data subcarriers.

symLSIG = mean(symLSIG,2);
lsig = symLSIG(info.DataIndices,:);

% Decode the L-SIG field, assuming a noiseless channel, and use the length field to update the recovery object.

[~,~,lsigInfo] = wlanLSIGBitRecover(lsig,0);
cfgRx.LSIGLength = lsigInfo.Length;

% Recover and demodulate the HE-SIG-A field, obtain the data subcarriers and recover the HE-SIG-A bits.

heSIGA = waveform(ind.HESIGA(1):ind.HESIGA(2),:);
symSIGA = wlanHEDemodulate(heSIGA,'HE-SIG-A',cbw);
siga = symSIGA(info.DataIndices,:);
[sigaBits,failCRC] = wlanHESIGABitRecover(siga,0);

% Update the recovery configuration object with the recovered HE-SIG-A bits and obtain the updated field indices.

cfgRx = interpretHESIGABits(cfgRx,sigaBits);
ind = wlanFieldIndices(cfgRx);

% Retrieve and decode the HE-Data field.

heData = waveform(ind.HEData(1):ind.HEData(2),:);
symData = wlanHEDemodulate(heData,'HE-Data', ... 
    cbw,cfgRx.GuardInterval,[cfgRx.RUSize cfgRx.RUIndex]);
infoData = wlanHEOFDMInfo('HE-Data',cbw,cfgRx.GuardInterval,[cfgRx.RUSize cfgRx.RUIndex]);
data = symData(infoData.DataIndices,:,:);
dataBits = wlanHEDataBitRecover(data,0,cfgRx);

% Check that the returned data bits are the same as the transmitted data bits.

isequal(bits,dataBits)

% Copyright 2018 The MathWorks, Inc.