%% VHT Packet Recovery
% This example shows how to recover contents from a VHT format waveform.
%% Generate 80 MHz VHT Waveform
% Create a VHT configuration object. Set APEPLength to 3200 and MCS to 5. Create a transmission bit stream for the data field. For a VHT waveform, the data field contains PSDULength*8 bits.

cfgVHT = wlanVHTConfig('APEPLength',3200,'MCS',5);
txBits = randi([0 1],cfgVHT.PSDULength*8,1);

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

lstf = wlanLSTF(cfgVHT);
lltf = wlanLLTF(cfgVHT);
lsig = wlanLSIG(cfgVHT);
vhtSigA = wlanVHTSIGA(cfgVHT);
vhtstf = wlanVHTSTF(cfgVHT);
vhtltf = wlanVHTLTF(cfgVHT);
vhtSigB = wlanVHTSIGB(cfgVHT);
vhtData = wlanVHTData(txBits,cfgVHT);

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

txPPDU = [lstf; lltf; lsig; vhtSigA; vhtstf; vhtltf; vhtSigB; vhtData];

%
%% Pass VHT Waveform Through TGac SISO Channel
% Create TGac SISO and AWGN channel objects.

chBW = cfgVHT.ChannelBandwidth;
fs = 80e6;
tgac = wlanTGacChannel('SampleRate',fs,'ChannelBandwidth',chBW,...
    '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 TGac channel.

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

%
%% Recover VHT 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(cfgVHT)

%
% The stop index of VHT-SIG-B indicates the preamble length in samples.

numSamples = fieldInd.VHTSIGB(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.VHTSIGA(2)-1,1) = peak;
fieldMarkers(fieldInd.VHTSTF(2)-1,1) = peak;
fieldMarkers(fieldInd.VHTLTF(2)-1,1) = peak;
fieldMarkers(fieldInd.VHTSIGB(2)-1,1) = peak;
plot(time,abs(rxPPDU(1:numSamples)),time,fieldMarkers)
xlabel ('Time (microseconds)')
ylabel('Magnitude')
title('VHT Preamble')

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

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

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

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

% failCRC = 0 indicates that CRC passed.
%
% For the VHT 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 VHT format, the MCS setting in VHT-SIG-A2 determines the actual data rate.

rate = recLSIG(1:4)'

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

rxVHTSIGA = rxPPDU(fieldInd.VHTSIGA(1):fieldInd.VHTSIGA(2),:);
[recVHTSIGA,failCRC] = wlanVHTSIGARecover(rxVHTSIGA, ...
    chEstLLTF,noiseVar,chBW);
failCRC

%
% Extract the MCS setting from the VHT-SIG-A. For single user VHT, the MCS is located in VHT-SIG-A2 bits 4 through 7.

recMCSbits = (recVHTSIGA(29:32))';
recMCS = bi2de(double(recMCSbits))
isequal(recMCS,cfgVHT.MCS)

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

rxVHTLTF = rxPPDU(fieldInd.VHTLTF(1):fieldInd.VHTLTF(2),:);
demodVHTLTF = wlanVHTLTFDemodulate(rxVHTLTF,cfgVHT);
chEstVHTLTF = wlanVHTLTFChannelEstimate(demodVHTLTF,cfgVHT);

%
% Extract and recover the VHT-SIG-B.

rxVHTSIGB = rxPPDU(fieldInd.VHTSIGB(1):fieldInd.VHTSIGB(2),:);
recVHTSIGB = wlanVHTSIGBRecover(rxVHTSIGB,chEstVHTLTF,noiseVar,chBW);

%
% As described in IEEE Std 802.11ac-2013, Table 22-1, the value in the VHT-SIG-B Length field multiplied by 4 is the recovered APEP length for packets carrying data. Verify that the APEP length, contained in the first 19 bits of the VHT-SIG-B, corresponds to the specified APEP length.

sigbAPEPbits = recVHTSIGB(1:19)';
sigbAPEPlength = bi2de(double(sigbAPEPbits))*4
isequal(sigbAPEPlength,cfgVHT.APEPLength)

% The recovered value matches the configured APEP Length.
%
% Recover equalized symbols using channel estimates from the VHT-LTF.

recPSDU = wlanVHTDataRecover(rxPPDU(fieldInd.VHTData(1):fieldInd.VHTData(2),:),...
    chEstVHTLTF,noiseVar,cfgVHT);

%
% Compare transmission and receive PSDU bits.

numErr = biterr(txBits,recPSDU)

% The number of bit errors is zero.
% Copyright 2012 The MathWorks, Inc.