%% HT Packet Recovery
% This example shows how to recover content from an HT-format waveform.
%% Generate 20 MHz HT Waveform
% Create an HT configuration object and transmission PSDU. Set MCS to 2. For an HT waveform, the data field is PSDULength*8 bits.

cfgHT = wlanHTConfig('MCS',2);
txPSDU = randi([0 1],cfgHT.PSDULength*8,1);

%
% Create the PPDU fields individually. Create L-STF, L-LTF, L-SIG, HT-SIG, HT-STF, and HT-LTF preamble fields and the HT-Data field.

lstf = wlanLSTF(cfgHT);
lltf = wlanLLTF(cfgHT);
lsig = wlanLSIG(cfgHT);
htsig = wlanHTSIG(cfgHT);
htstf = wlanHTSTF(cfgHT);
htltf = wlanHTLTF(cfgHT);
htData = wlanHTData(txPSDU,cfgHT);

%
% Concatenate the individual fields to create a single PPDU waveform.

txPPDU = [lstf; lltf; lsig; htsig; htstf; htltf; htData];

%
%% Pass HT Waveform Through TGn SISO Channel
% Create TGn SISO channel and AWGN channel objects.

fs = 20e6;
tgnChan = wlanTGnChannel('SampleRate',fs,'LargeScaleFadingEffect','Pathloss and shadowing');
awgnChan = comm.AWGNChannel('NoiseMethod','Variance','VarianceSource','Input port');

%
% Calculate the noise variance for a receiver with a 9 dB noise figure. The noise variance, noiseVar, is equal to kTBF, where k is Boltzmann's constant, T is the ambient temperature of 290 K, B is the bandwidth (sample rate), and F is the receiver noise figure. Pass the transmitted waveform through the noisy TGn channel.

noiseVar = 10^((-228.6 + 10*log10(290) + 10*log10(fs) + 9)/10);
rxPPDU = awgnChan(tgnChan(txPPDU),noiseVar);

%
%% Recover HT Preamble Contents from PPDU
% In general, the L-STF and L-LTF are processed to perform frequency offset estimation and correction, and symbol timing. For this example, the carrier frequency is not offset and the packet timing is 'on-time'. Therefore, for accurate demodulation, determination of carrier frequency offset and symbol timing is not required.
% Find the start and stop indices for the PPDU fields.

fieldInd = wlanFieldIndices(cfgHT)

%
% The stop index of HT-LTF indicates the preamble length in samples.

numSamples = fieldInd.HTLTF(2);

%
% Plot the preamble and the beginning of the packet data. Add markers to and plot to delineate the packet field boundaries.

time = ([0:double(numSamples)-1]/fs)*1e6;
peak = 1.2*max(abs(rxPPDU(1:numSamples)));
fieldMarkers = zeros(numSamples,1);
fieldMarkers(fieldInd.LSTF(2)-1,1) = peak;
fieldMarkers(fieldInd.LLTF(2)-1,1) = peak;
fieldMarkers(fieldInd.LSIG(2)-1,1) = peak;
fieldMarkers(fieldInd.HTSIG(2)-1,1) = peak;
fieldMarkers(fieldInd.HTSTF(2)-1,1) = peak;
fieldMarkers(fieldInd.HTLTF(2)-1,1) = peak;
plot(time,abs(rxPPDU(1:numSamples)),time,fieldMarkers)
xlabel ('Time (microseconds)')
ylabel('Magnitude')
title('HT Format Preamble')

%
% Demodulate the L-LTF and estimate the channel.

rxLLTF = rxPPDU(fieldInd.LLTF(1):fieldInd.LLTF(2),:);
demodLLTF = wlanLLTFDemodulate(rxLLTF,cfgHT);
chEstLLTF = wlanLLTFChannelEstimate(demodLLTF,cfgHT);

%
% Extract the L-SIG field from the received PPDU and recover its information bits.

rxLSIG = rxPPDU(fieldInd.LSIG(1):fieldInd.LSIG(2),:);
[recLSIG,failCRC] = wlanLSIGRecover(rxLSIG,chEstLLTF,noiseVar,cfgHT.ChannelBandwidth);
failCRC

% failCRC = 0 indicates that CRC passed.
%
% For the HT format, the L-SIG rate bits are constant and set to [1 1 0 1]. Inspect the L-SIG rate information and confirm that this constant sequence is recovered. For the HT format, the MCS setting in HT-SIG determines the actual data rate.

rate = recLSIG(1:4)'

%
% Extract the HT-SIG and confirm that the CRC check passed.

recHTSIG = rxPPDU(fieldInd.HTSIG(1):fieldInd.HTSIG(2),:);
[recHTSIG,failCRC] = wlanHTSIGRecover(recHTSIG,chEstLLTF,noiseVar,cfgHT.ChannelBandwidth);
failCRC

%
% Extract the MCS setting from the HT-SIG. For HT, the MCS is located in HT-SIG bits 0 through 6.

recMCSbits = (recHTSIG(1:7))';
recMCS = bi2de(double(recMCSbits))
isequal(recMCS,cfgHT.MCS)

% The recovered MCS setting matches the MCS value in the configuration object.
%
% Extract and demodulate the HT-LTF. Use the demodulated signal to perform channel estimation. Use the channel estimate to recover the HT-Data field.

rxHTLTF = rxPPDU(fieldInd.HTLTF(1):fieldInd.HTLTF(2),:);
demodHTLTF = wlanHTLTFDemodulate(rxHTLTF,cfgHT);
chEstHTLTF = wlanHTLTFChannelEstimate(demodHTLTF,cfgHT);

%
%% Recover HT-Data Contents from PPDU
% Recover the received equalized symbols using channel estimates from the HT-LTF.

[recPSDU] = wlanHTDataRecover(rxPPDU(fieldInd.HTData(1):fieldInd.HTData(2),:),...
    chEstHTLTF,noiseVar,cfgHT);

% Compare the transmitted and received PSDU bits, and confirm that the number of bit errors is zero.

numErr = biterr(txPSDU,recPSDU)

% Copyright 2012 The MathWorks, Inc.