%% Recover Feedback Status from HE TB Feedback NDP
% Configure an uplink HE TB feedback NDP transmission with four stations (STAs), a channel bandwidth of 20 MHz, and a signal-to-noise ratio (SNR) of 20 dB.

numSTA = 4;
cbw = 'CBW20';
snr = 20;
cfgSTA = cell(1,numSTA);

% Specify the resource unit (RU) tone set index, starting space-time stream, and feedback status for all STAs.

ruToneSetIndex = repmat([1 2],1,round(numSTA/2));
startingSTS = repmat([1 2],1,round(numSTA/2));
feedbackStatus = repmat([1 0],1,round(numSTA/2));

% Create a valid HE TB feedback NDP configuration.

cfg = wlanHETBConfig;
cfg = getNDPFeedbackConfiguration(cfg);

% Configure the channel for transmission, assuming no variation across STAs.

tgax = wlanTGaxChannel('ChannelBandwidth',cbw, ...
    'TransmissionDirection','Uplink', ...
    'SampleRate',wlanSampleRate(cfg));
chanInfo = info(tgax);
awgn = comm.AWGNChannel('NoiseMethod','Signal to noise ratio (SNR)', ...
    'SignalPower',1/tgax.NumReceiveAntennas);

% Configure STAs and generate an HE TB feedback NDP waveform.

rx = 0;
for idx = 1:numSTA
    
    % Configure STAs
    
    cfg.RUToneSetIndex = ruToneSetIndex(idx);
    cfg.StartingSpaceTimeStream = startingSTS(idx);
    cfg.FeedbackStatus = feedbackStatus(idx);
    cfgSTA{idx} = cfg;
    
    % Generate transmit waveform
    
    waveform = wlanWaveformGenerator([],cfg);
    
    % Pass waveform through TGax channel
    
    rx = rx + tgax([waveform; zeros(15,size(waveform,2))]);
end

% Pass the waveform through the AWGN channel, accounting for the noise energy in nulls to ensure the SNR is defined per active and complementary subcarrier.

field = 'HE-LTF';
ofdmInfo = wlanHEOFDMInfo(field,cbw,cfg.GuardInterval);
awgn.SNR = snr - 10*log10(ofdmInfo.FFTLength/12);
rx = awgn(rx);

% Get the field indices and extract the HE-LTF.

ind = wlanFieldIndices(cfgSTA{1});
offset = chanInfo.ChannelFilterDelay;
heltf = rx(offset+(ind.HELTF(1):ind.HELTF(2)),:);

% Demodulate the HE-LTF.

rxSym = wlanHEDemodulate(heltf,field,cbw,cfg.GuardInterval,cfg.HELTFType);

% Recover the feedback status for the STAs.

status = zeros(1,numSTA);
for n = 1:numSTA
    status(n) = wlanHETBNDPFeedbackStatus(rxSym,cfgSTA{n});
end

% Compare the transmitted and received feedback status for the STAs.

disp(isequal(feedbackStatus(1:numSTA),status))

% Copyright 2020 The MathWorks, Inc.