%% Recover HE-Data Field from HE SU Transmission
% Recover bits from the HE-Data field of an HE SU transmission.
% Configure an HE SU transmission by creating a configuration object with the specified modulation and coding scheme (MCS). Extract the channel bandwidth.

cfgHESU = wlanHESUConfig('MCS',0);
cbw = cfgHESU.ChannelBandwidth;       % Channel bandwidth of transmission

% Create a sequence of data bits and generate an HE SU waveform.

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 packet format.

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.

noiseVarEst = 0;
[~,~,lsigInfo] = wlanLSIGBitRecover(lsig,noiseVarEst);
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.

cfgHE = interpretHESIGABits(cfgRX,sigaBits);
ind = wlanFieldIndices(cfgHE);

% Retrieve and decode the HE-Data field.

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

% Confirm that the recovered bits match the transmitted bits.

isequal(bits,dataBits)

% Copyright 2018 The MathWorks, Inc.