%% 802.11ax Downlink Throughput Comparison of OFDM and OFDMA Through System-level Simulation
% This example demonstrates how to model a multinode downlink orthogonal
% frequency-division multiple access (OFDMA) IEEE(R) 802.11ax(TM) [ <#9 1>
% ] scenario using SimEvents(R), Stateflow(R), and WLAN Toolbox(TM). In
% OFDMA, independent groups of subcarriers are allocated to distinct users
% to realize simultaneous transmission. This multiplexing technique
% provides significant advantages in terms of spectrum efficiency,
% contention, latency, and network jitter. This example provides a model to
% enable downlink (DL) OFDMA communication in a 802.11ax network. The
% network topology consists of an access point (AP) and four linearly
% placed stations. The model enables you to configure multiple applications
% with same access category (AC) for different destination stations. In
% this example, the allocation of resource units (RUs) to the stations is
% fixed based on the number of users. The AP schedules transmissions to the
% stations in a round-robin fashion. The derived plot shows that the DL
% throughput at the AP is higher with OFDMA as compared to OFDM.

% Copyright 2019-2020 The MathWorks, Inc.

%% 802.11ax OFDMA
% The IEEE 802.11ax introduced significant enhancements over the existing
% 802.11ac standard [ <#9 2> ]. One of the key improvements is the OFDMA,
% which is an extension of orthogonal frequency division multiplexing
% (OFDM) digital modulation technology into a multiuser environment. The
% principle of OFDMA is to efficiently use the available frequency space.
% OFDMA partitions the channel bandwidth into multiple mutually exclusive
% subbands, called RUs. By partitioning the channel bandwidth, multiple
% users can access the air interface simultaneously. As a result,
% concurrent transmissions of small frames to multiple users is possible at
% the same time. For example, a conventional 20 MHz channel can be
% partitioned into a maximum of nine subchannels. Subsequently, using
% OFDMA, an 802.11ax AP can simultaneously transmit small frames to nine
% 802.11ax stations. The simultaneous transmission of frames not only cuts
% down on excessive overhead at the MAC but also minimizes the contention
% overhead. In OFDMA, the allocation of RUs is completely controlled by the
% AP. The 802.11ax standard specifies two types of OFDMA transmissions,
% namely downlink (DL) and uplink (UL) OFDMA.
%
% * *DL OFDMA:* AP transmits data to multiple stations
% simultaneously using a different RU for each station.
% * *UL OFDMA:* Multiple stations transmit data to AP at the same
% time with each station using a different RU.

%% Comparison of OFDM and OFDMA
% This section illustrates the difference between OFDM and OFDMA. In this
% figure, the 802.11n/ac/ax AP transmits DL to 4 OFDM stations
% independently over time. The entire channel bandwidth is used for the
% DL communication between the AP and a single OFDMA station. The same
% holds true for any uplink transmission from a single 802.11n/ac/ax client
% to the 802.11n/ac/ax AP.
%
% <<../OFDM.png>>
%
% This figure shows that when OFDMA is used, the 802.11ax AP partitions
% the channel bandwidth into RUs for multiple OFDMA stations on a
% continuous basis for simultaneous DL transmissions. By partitioning the
% channel bandwidth, OFDMA makes efficient use of the available frequency
% spectrum, resulting in reduced MAC contention and PHY preamble overhead.
%
% <<../OFDMA.png>>
%
% An 802.11ax AP can also coordinate with 802.11ax OFDMA
% stations for simultaneous uplink transmissions.

%% Model 802.11ax Network with OFDMA
% This example demonstrates DL OFDMA communication in an 802.11ax network
% with one AP and four stations. These stations are linearly placed with AP
% located at the start. These stations implement carrier-sense multiple
% access with collision avoidance (CSMA/CA) with physical carrier sense.
% The physical carrier sensing uses the clear channel assessment (CCA)
% mechanism to determine whether the medium is busy before transmitting.
% This example is an enhancement over the
% <docid:wlan_examples#example-WLANMACThroughputMeasurementExample 802.11
% MAC and Application Throughput Measurement> example. The enhancements are
% related to adding 802.11ax DL OFDMA support in the MAC and PHY library
% blocks. However, the |Node Position Allocator (NPA)|, |Visualizer| and
% the |Application Traffic Generator| blocks are the same as in the
% <docid:wlan_examples#example-WLANMACThroughputMeasurementExample 802.11
% MAC and Application Throughput Measurement> example.
%
% <<../downlinkOFDMANetwork.png>>
%
% The components of a WLAN node are shown in this figure. The information
% is retrieved by pressing the arrow button for each node in the above
% figure.
%
% <<../ofdmaNode.png>>
%
% *Application Configuration*
% 
% This example uses the same application layer blocks (traffic generator
% and traffic receiver) as used in the
% <docid:wlan_examples#example-WLANMACThroughputMeasurementExample 802.11
% MAC and Application Throughput Measurement> example. You can add or
% remove any number of application blocks with different configuration
% options like |Packet Size|, |Packet Interval|, |Destination Name|, and
% |Access Category (AC)|. Multiple applications with same AC can be enabled
% simultaneously with different |Destination Names|. To add or remove
% applications, enter the |Applications| subsystem block of a node by
% double clicking on it. This figure shows the applications inside the
% |Applications| subsystem block.
%
% <<../applications.png>>
%
% This figure shows the configuration options for an application. Double
% click on the |Application Traffic Generator| block to retrieve these
% options.
%
% <<../appConf.png>>
%
% *MAC Configuration*
% 
% The AP can be configured to transmit DL OFDMA multiuser format frames by
% setting |PHY Tx Format| to |HE-MU-OFDMA| in MAC configuration
% parameters. You can also limit the number of users in DL OFDMA
% transmission using the |Max Downlink Stations| property.
%
% <<../macConfOFDMA.png>>
% 
% *Model Limitations with HE-MU-OFDMA Format:*
%
% * This example supports only DL OFDMA communication. Any node can act as
% an AP or a station. Nodes with one or more applications enabled are considered
% APs. This model assumes that all the remaining stations are associated
% with APs.
% * Uplink acknowledgments are not supported.
% * Channel reservation using RTS & CTS is not supported.
% * OFDMA in combination with MIMO is not supported.
% * OFDMA in combination with rate adaptation is not supported.
%
% When the |PHY Tx Format| is |HE-MU-OFDMA|, the |Ack Policy|, |RTS
% Threshold|, and |Number of Transmit Chains| options are disabled.

%% Simulation Results
% To measure the network throughput, simulations are carried out for
% these two scenarios:
% 
% # *AP as transmitter*: Simulating the model for the AP serving 1,
% 2, 4, 6, 8, and 9 stations using the OFDM and OFDMA transmissions.
% # *AP and stations as transmitters*: The same scenario as above but
% with stations also communicating with an AP using OFDM transmissions.
%
% The throughput results obtained through these simulations
% are plotted as a function of the number of DL stations for both the OFDM
% (HE-SU) and the OFDMA (HE-MU-OFDMA) configurations.
%
% *Simulation Configuration*
%  
% Create an 802.11ax network with nine stations and one AP. At the AP, add
% nine |Application Traffic Generator| blocks generating data to the nine
% stations. To enable the stations as transmitters, configure |AppState| of
% an |Application Traffic Generator| block to On. Configure the
% AP/station(s) with the values shown in this table.
%
% <html>
% <table border="1"><tr><th>Parameter</th><th>Value</th></tr>
% <tr><td> Packet size </td><td> 1000 bytes </td></tr>
% <tr><td> Packet Interval </td><td> 0.00001 seconds </td></tr>
% <tr><td> Access Category </td><td> Best Effort </td></tr>
% <tr><td> Max A-MPDU Subframes </td><td> 1 </td></tr>
% <tr><td> MCS </td><td> 11 </td></tr>
% <tr><td> Ack Policy </td><td> No Ack</td></tr>
% </table>
% </html>
%
% Simulate the network for |HE-SU| and |HE-MU-OFDMA| formats by varying the
% number of applications enabled as 1, 2, 4, 6, 8, and 9 for each
% simulation. At the end of each simulation run, the throughput values for
% each node are retrieved from the |statistics.mat| file and summed to
% obtain the total throughput.
%
% Plot throughput results for OFDM and OFDMA configurations.
%
% *AP as transmitter*
%
% This code plots OFDM and OFDMA configurations using only AP as
% transmitters:

figure;

% Number of DL stations
numStations = [1 2 4 6 8 9];

% Throughput results for OFDMA configuration (Mbps)
throughputOFDMA = [32.64 46.08 59.52 55.68 72.96 82.08];

% Throughput results for OFDM configuration (Mbps)
throughputOFDM = [33.76 33.76 33.76 33.76 33.76 33.76];

% Plot throughput obtained from OFDM simulations
plot(numStations, throughputOFDM,'-o');
% Retain OFDM throughput plot
hold on;
% Plot throughput obtained from OFDMA simulations
plot(numStations, throughputOFDMA,'-x');
grid on;
xlabel('Number of DL Stations');
ylabel('Throughput (Mbps)');
legend('OFDM', 'OFDMA', 'Location', 'northeastoutside');
title('Downlink Throughput at AP');

%%
% *AP and stations as transmitters*
%
% This code plots OFDM and OFDMA configurations using both APs and stations
% as transmitters:

figure;

% Number of DL stations
numStations = [1 2 4 6 8 9];

% Throughput results for OFDMA configuration (Mbps)
throughputOFDMA = [20.8 27.52 32.64 29.76 40.96 38.88];
% Throughput results for OFDM configuration (Mbps)
throughputOFDM = [21.44 16.64 10.72 8.16 8 7.2];

% Plot throughput obtained from OFDM simulations
plot(numStations, throughputOFDM,'-o');
% Retain OFDM throughput plot
hold on;
% Plot throughput obtained from OFDMA simulations
plot(numStations, throughputOFDMA,'-x');
grid on;
xlabel('Number of DL Stations');
ylabel('Throughput (Mbps)');
legend('OFDM', 'OFDMA', 'Location', 'northeastoutside');
title('Downlink Throughput at AP');

%%
% The above plots show the 802.11ax throughput comparison of OFDM and
% OFDMA. As OFDMA reduces the MAC contention and the PHY preamble overhead,
% the throughput obtained with OFDMA is greater than that obtained using
% OFDM. When only AP is configured as the transmitter, the DL throughput
% shows no change for OFDM. However, when both the AP and the stations are
% configured as the transmitters, the DL throughput shows a steady
% decline. This is because the AP gets fewer opportunities to
% transmit as the number of transmitting stations increase in the network.
%
% This example enables you to model a DL OFDMA communication in a
% multinode IEEE 802.11ax network. The DL OFDMA support is added to the
% PHY and MAC library blocks. The application layer configuration allows
% you to enable multiple applications with same AC using different
% destination stations. Round-robin scheduling strategy is used to select
% stations for next transmission. The assignment of RUs is fixed based on
% the number of users. The plots confirm that the throughput at
% AP with OFDMA is greater than that obtained using OFDM.

%% Further Exploration
%
% *Scheduling Stations for Transmission*
%
% This example uses round-robin scheduling algorithm to select stations for
% next transmission.
% 
% *RU allocation*
%
% In a transmission, the allocation index defines the assignment of RUs.
% The allocation index is defined in Table 27-24 of [ <#9 1> ]. For each 20
% MHz subchannel, an 8-bit index describes the number of RUs, the size of
% RUs, and the number of users transmitted on each RU. In this example, the
% allocation of RUs to the DL stations is fixed based on the number of
% users as shown in this table. In both the above plots, the throughput
% drop for OFDMA from four stations to six stations can be affiliated to
% the RU size. Transmission time is shorter in a 52-tone RU as compared to
% 26-tone RU. When an MU frame is transmitted, all the PSDUs in the MU
% frame are padded to align at the same transmission time. The presence of
% 26-tone RU for six users results in a longer transmission time, thereby
% causing the throughput drop between four and six users.
%
% <html>
% <table border="1"><tr><th>Number of stations</th><th>Allocation index</th><th>Sizes of RU (tones)</th></tr>
% <tr><td>1</td><td>192</td><td><table border="1"><tr><td>242</td></tr></table></td></tr>
% <tr><td>2</td><td>96</td><td><table border="1"><tr><td>106</td><td>106</td></tr></table></td></tr>
% <tr><td>3</td><td>128</td><td><table border="1"><tr><td>106</td><td>26</td><td>106</td></tr></table></td></tr>
% <tr><td>4</td><td>112</td><td><table border="1"><tr><td>52</td><td>52</td><td>52</td><td>52</td></tr></table></td></tr>
% <tr><td>5</td><td>15</td><td><table border="1"><tr><td>52</td><td>52</td><td>26</td><td>52</td><td>52</td></tr></table></td></tr>
% <tr><td>6</td><td>7</td><td><table border="1"><tr><td>26</td><td>26</td><td>52</td><td>26</td><td>52</td><td>52</td></tr></table></td></tr>
% <tr><td>7</td><td>3</td><td><table border="1"><tr><td>26</td><td>26</td><td>26</td><td>26</td><td>26</td><td>52</td><td>52</td></tr></table></td></tr>
% <tr><td>8</td><td>1</td><td><table border="1"><tr><td>26</td><td>26</td><td>26</td><td>26</td><td>26</td><td>26</td><td>26</td><td>52</td></tr></table></td></tr>
% <tr><td>9</td><td>0</td><td><table border="1"><tr><td>26</td><td>26</td><td>26</td><td>26</td><td>26</td><td>26</td><td>26</td><td>26</td><td>26</td></tr></table></td></tr>
% </table>
% </html>
%
% The model used in this example implements four stations (allocation index
% 112). You can modify the station scheduling algorithm and RU allocation
% by updating the helper function <matlab:edit('roundRobinScheduler.m')
% roundRobinScheduler> used by the |EDCA MAC| block.

%% Appendix
% The example uses these helpers:
%
% # <matlab:edit('edcaFrameFormats.m') edcaFrameFormats.m>: Create an
% enumeration for PHY frame formats.
% # <matlab:edit('edcaNodeInfo.m') edcaNodeInfo.m>: Return MAC address of
% a node.
% # <matlab:edit('edcaPlotQueueLengths.m') edcaPlotQueueLengths.m>: Plot
% MAC queue lengths in the simulation.
% # <matlab:edit('edcaPlotStats.m') edcaPlotStats.m>: Plot MAC state
% transitions with respect to simulation times.
% # <matlab:edit('edcaStats.m') edcaStats.m>: Create an enumeration for
% simulation statistics.
% # <matlab:edit('edcaUpdateStats.m') edcaUpdateStats.m>: Update statistics
% of the simulation.
% # <matlab:edit('helperAggregateMPDUs.m') helperAggregateMPDUs.m>:
% Aggregate MPDUs to form an A-MPDU.
% # <matlab:edit('helperSubframeBoundaries.m') helperSubframeBoundaries.m>
% : Return subframe  boundaries of an A-MPDU.
% # <matlab:edit('phyRx.m') phyRx.m>: Model PHY operations related to
% packet reception.
% # <matlab:edit('phyTx.m') phyTx.m> Model PHY operations related to
% packet transmission.
% # <matlab:edit('edcaApplyFading.m') edcaApplyFading.m>: Apply Rayleigh
% fading effect on the waveform.
% # <matlab:edit('heSIGBUserFieldDecode.m') heSIGBUserFieldDecode.m>:
% Decode HE-SIG-B user field.
% # <matlab:edit('heCPECorrection.m') heCPECorrection.m>: Estimate and
% correct common phase error.
% # <matlab:edit('heSIGBCommonFieldDecode.m') heSIGBCommonFieldDecode.m>:
% Decode HE-SIG-B common field.
% # <matlab:edit('heSIGBMergeSubchannels.m') heSIGBMergeSubchannels.m>:
% Merge 20MHz HE-SIG-B subchannels.
% # <matlab:edit('addMUPadding.m') addMUPadding.m>: Add multiuser PSDU
% padding.
% # <matlab:edit('macQueueManagement.m') macQueueManagement.m>: Create a
% WLAN MAC queue management object.
% # <matlab:edit('roundRobinScheduler.m') roundRobinScheduler.m>: Create a
% round-robin scheduler object.
% # <matlab:edit('calculateSubframesCount.m') calculateSubframesCount.m>:
% Return number of subframes to be aggregated.
% # <matlab:edit('interpretHESIGABitsFailCheck.m')
% interpretHESIGABitsFailCheck.m>: Interpret HE-SIG-A bits with failure
% check.
% # <matlab:edit('interpretVHTSIGABitsFailCheck.m')
% interpretVHTSIGABitsFailCheck.m>: Interprets the bits in VHT-SIG-A field
% # <matlab:edit('rateAdaptationARF.m') rateAdaptationARF.m>: Create an
% auto rate fallback (ARF) algorithm object.
% # <matlab:edit('rateAdaptationMinstrelNonHT.m')
% rateAdaptationMinstrelNonHT.m>: Create a minstrel algorithm object.

%% References
% # IEEE P802.11ax(TM)/D4.1. "Amendment 6: Enhancements for High Efficiency
% WLAN" 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.
% # IEEE Std 802.11ac(TM) -2016. "Wireless LAN Medium Access Control (MAC) and
% Physical Layer (PHY) Specifications." IEEE Standard for Information
% technology-Telecommunications and information exchange between systems,
% Local and metropolitan area networks-Specific requirements.
% # Jiang, Tao, et al., editors. _Orthogonal Frequency Division Multiple
% Access Fundamentals and Applications_. Auerbach, 2010.
