%% Recover Single-User HE Packet Data in AWGN Channel
% Recover HE data from a single-user HE packet waveform transmitted though an AWGN channel.

cfgHE = wlanHESUConfig('MCS',11);

% Generate a transmit waveform containing eight data packets.

msgLen = getPSDULength(cfgHE)*8;
txBits = randi([0 1],msgLen,1,'int8');
txWaveform = wlanWaveformGenerator(txBits,cfgHE);

% Add noise to the waveform.

snr = 30;
rxWaveform = awgn(txWaveform,snr);

% Extract the data field.

ind = wlanFieldIndices(cfgHE);
rxData = rxWaveform(ind.HEData(1):ind.HEData(2),:);

% Assuming 20-MHz channel bandwidth and 3.2-microsecond guard interval, OFDM demodulate the received waveform and extract the data-carrying subcarriers.

Nfft = 256; % FFT length (20 MHz)
Ncp = 64;   % Cyclic prefix length (3.2 us at 20 MHz)
pilotSC = [-116; -90; -48; -22; 22; 48; 90; 116]; % Pilot indices
ruSC = [-122:-2 2:122].'; % Active subcarrier indices
nullIdx = setdiff((-Nfft/2:(Nfft/2-1)).',ruSC)+Nfft/2+1;
pilotIdx = pilotSC+Nfft/2+1;
sf = (1/sqrt(numel(ruSC)))*Nfft; % Scaling factor
rxSym = ofdmdemod(rxData,Nfft,Ncp,Ncp,nullIdx,pilotIdx)/sf;

% Recover the data bits.

csi = ones(length(rxSym),1); % Assume CSI estimate of all ones
nVar = 10^(-snr/10); % Noise variance
rxBits = wlanHEDataBitRecover(rxSym,nVar,csi,cfgHE);

% Compare the recovered bits to the original information bits.

disp(isequal(txBits,rxBits));

% Copyright 2018 The MathWorks, Inc.