%% Recover and Analyze Packets in 802.11 Waveform
% This example blindly detects, decodes, and analyzes multiple IEEE 802.11a™, IEEE 802.11n™, IEEE 802.11ac™, and IEEE 802.11ax™ packets in a waveform. The example provides a summary of the detected packets and displays the MAC contents, error vector magnitude (EVM), power, and signaling information for a selected packet.
% Table of Contents
% Introduction
% Setup Waveform Recovery Parameters
%     Reception with an SDR Device
%     Import a Captured Waveform from a File
% Signal Recovery and Analysis
% Further Exploration
% Selected Bibliography
%% Introduction
% In this example we detect, decode, and analyze multiple packets within a waveform. This example can decode OFDM non-HT, non-HT duplicate, HT, VHT [ 1 ], HE MU, HE SU, and HE ER SU [ 2 ]  packet formats. The receiver does not know any transmission parameters, except for the channel bandwidth, and retrieves these parameters by decoding the preamble fields of the packet. These measurements are displayed for a detected packet:
% The spectrum and time domain samples.
% The signaling field contents.
% The resource unit and user information for an HE waveform.
% The constellation of the equalized data symbols.
% The EVM of the signaling fields.
% The EVM per data subcarrier averaged over spatial streams and symbols.
% The EVM per data symbol averaged over spatial streams and subcarriers.
% The MAC frame contents: A-MPDU deaggregation status, Address1, Address2, Frame Check Sequence (FCS) and Frame Type.
%% Setup Waveform Recovery Parameters
% This example analyzes I/Q data containing non-HT, HT-MF, VHT, and HE packets. The useSDR variable controls the data source for this example:
% When set to false, comm.BasebandFileReader reads a synthetic waveform stored in a binary file format.
% When set to true, an SDR platform captures an off-the-air-waveform from a commercial device.

useSDR = false;

%
%% Reception with an SDR Device
% This section demonstrates how to capture an off-the-air-waveform using an SDR device. Communications Toolbox supports radio communication with several SDR devices. For a list of supported devices and for an overview of how to install and setup a support package for your device, refer to the documentation of these support packages.
% Communications Toolbox Support Package for Analog Devices ADALM-Pluto Radio
% Installation and Setup and Hardware Support
% Use Pluto as deviceName
% Communications Toolbox Support Package for USRP Embedded Series Radio
% Installation and Setup and Hardware Support
% Use E3xx as deviceName
% Communications Toolbox Support Package for Xilinx Zynq-Based Radio
% Installation and Setup and Hardware Support
% Use AD936x or FMCOMMS5 as deviceName

% Configure the analysis source
if useSDR %#ok<*UNRCH>
    deviceName    = "Pluto"; % Device name
    frequencyBand = 5; % Frequency band
    channelNumber = 102; % WLAN channel number
    captureTime   = 0.1; % Signal capture duration in seconds
    sr            = 20000000; % Captured sampling rate
    chanBW        = 'CBW20'; % Channel bandwidth of all packets within the waveform
    
    % Set the center frequency to the corresponding channel number. The
    % center frequency should be centered on the channel bandwidth.
    fc = helperWLANChannelFrequency(channelNumber,frequencyBand); 
    rx = sdrrx(deviceName,'BasebandSampleRate',sr,'CenterFrequency',fc,'OutputDataType','double');
    rxWaveform = capture(rx,captureTime,'Seconds');
    release(rx);
end

% To view an example on how to recover 802.11a™ OFDM non-HT beacon frames transmitted over the air from the commercial 802.11 hardware see the 802.11 OFDM Beacon Receiver with USRP® Hardware example.
%
%% Import a Captured Waveform from a File
% This section loads I/Q data from an existing binary file using comm.BasebandFileReader. The baseband file format includes the sample rate and the number of channels in the captured waveform. Alternately you can load the waveform in a MAT-file format.

% Configure the analysis source
if ~useSDR
    BBR = comm.BasebandFileReader('wlanWaveform.bb'); % Create a baseband file reader object
    chanBW = 'CBW20'; % Channel bandwidth of all packets within the waveform
    bbrInfo = info(BBR);
    BBR.SamplesPerFrame = bbrInfo.NumSamplesInData; % Number of sample in the waveform
    rxWaveform = BBR();  % Load the I&Q sample from a binary file
    sr = BBR.SampleRate; % Sampling rate of the input signal
    release(BBR);
end

% To view an example that recovers beacon packets from a baseband file see the 802.11 OFDM Beacon Receiver with Captured Data example.
%
%% Signal Recovery and Analysis
% This section detects, analyzes, and displays a summary of the detected packets. All packets in the waveform must have the specified channel bandwidth, chanBW.

% Create a WaveformAnalyzer object to parse and analyze the packet within a waveform
analyzer = WaveformAnalyzer;
process(analyzer,rxWaveform,chanBW,sr);

% Display the summary of the detected packets
detectionSummary(analyzer);

%
% Use the pktNum variable to display the MAC and PHY analysis for a selected packet.

pktNum = 4 ;

% Display the MAC information of the selected packet
macSummary(analyzer,pktNum);
% Display the time samples and the spectrum of the detected packet
plotWaveform(analyzer,pktNum)
% Display the packet field information of the selected packet
fieldSummary(analyzer,pktNum);
% Display signaling field information of the selected packet
signalingSummary(analyzer,pktNum);
% Display the RU information
ruSummary(analyzer,pktNum);
% Display the user information
userSummary(analyzer,pktNum);
% Display EVM per spatial streams for all user
userEVM(analyzer,pktNum);
% Plot constellation for all users
plotConstellation(analyzer,pktNum);
% Plot EVM
plotEVM(analyzer,pktNum);

%% Further Exploration
% The WaveformAnalyzer provides properties to control the pilot tracking, equalization, DC blocking, and packet detection algorithms that can be tweaked to improve packet detection and analysis performance.
% False packet detections
% False packet detections are detected packets that you do not believe are actual packets. Evaluating the time domain waveform of the packet is one way to determine if the detected packet is legitimate. If there is a significant number of false detections present these techniques may help reduce them:
% Enable the EnergyDetection property and set the EnergyDetectionThreshold property to a suitable value given the noise floor of the capture device. When enabled, EnergyDetection only detects packets with a power exceeding EnergyDetectionThreshold during the preamble.
% Increase the LLTFSNRDetectionThreshold and PacketDetectionThreshold properties to discard packets with a low measured SNR during detection.
% Missed packet detections
% Missed packet detections are packets that you believe are in the waveform but have not been detected.
% One possible reason a packet detection may have been missed is if a false detection occurred earlier in the waveform, but the L-SIG check passed, causing samples to be skipped. To search within possible false detections, enable the SearchWithinUnsupportedPacket property.
% Alternatively try decreasing the PacketDetectionThreshold property to detect packet with low SNR during detection.
% For detail on 802.11ax and 802.11ac signal recovery and processing, see the Recovery Procedure for an 802.11ax Packet and Recovery Procedure for an 802.11ac Packet examples.
%% Selected Bibliography
% IEEE Std 802.11™ - 2020 IEEE Standard for Information technology - Telecommunications and information exchange between systems - Local and metropolitan area networks - Specific requirements - Part 11: Wireless LAN Medium Access Control (MAC) and Physical Layer (PHY) Specifications.
% IEEE P802.11ax™/D4.1 Draft Standard for Information technology - Telecommunications and information exchange between systems - Local and metropolitan area networks - Specific requirements - Part 11: Wireless LAN Medium Access Control (MAC) and Physical Layer (PHY) Specifications - Amendment 6: Enhancements for High Efficiency WLAN.
% Copyright 2019-2021 The MathWorks, Inc.