%% Configure Uplink and Downlink Traffic at 802.11ax  Access Point and Visualize Statistics
% To access the helper files that this example uses, add the mlWLANSystemSimulation folder to the MATLAB path.

addpath(genpath(fullfile(pwd,'mlWLANSystemSimulation')));

% Set the simulation time in milliseconds.

simulationTime = 0.02*1e3;

% Specify the total number of nodes (APs and STAs) in the network. This example uses one AP and two STAs.

numNodes = 3;

% Specify the x-, y-, and z-coordinates of the WLAN nodes in the Cartesian coordinate system. This example considers the first node as the AP and the next two nodes as the STAs.

nodePositions = [10 0 0; 20 0 0; 5 5 0];

% Load the default configuration for the WLAN nodes.

load('wlanNodeConfig.mat');

% Copy the default configuration for all of the nodes.

nodeConfig = repmat(wlanNodeConfig,1,numNodes);

% Initialize the node position of the AP and STAs.

for index = 1:1:numNodes
    nodeConfig(index).NodePosition = nodePositions(index,:);
end

% Load the default application traffic configuration for the nodes.

load('wlanTrafficConfig.mat');

% Initialize the nodes with uplink and downlink traffic configuration by copying the default configuration.

trafficConfig = repmat(wlanTrafficConfig,1,2);

% Configure the uplink voice traffic at the first AP from the first STA. Node ID 1 represents the AP, whereas node IDs 2 and 3 represent the first and second STAs, respectively.

trafficConfig(1).SourceNode = 2;                       % STA node ID
trafficConfig(1).DestinationNode = 1;                  % AP node ID
trafficConfig(1).AccessCategory = 3;                   % Best Effort (0), Background (1), Video (2), and Voice (3)

% Configure the downlink video traffic at the first AP to the second STA.

trafficConfig(2).SourceNode = 1;                       % AP node ID
trafficConfig(2).DestinationNode = 3;                  % STA node ID
trafficConfig(2).AccessCategory = 2;

% Create a WLAN network by using the hCreateWLANNodes helper function. The example configures each node with the preceding physical layer (PHY), medium access control (MAC), and application traffic configuration.

wlanNodes = hCreateWLANNodes(nodeConfig,trafficConfig);

% Initialize the visualization parameters.

visualizationInfo = struct;
visualizationInfo.Nodes = wlanNodes;

% Retrieve and view the statistics by using the hStatsLogger helper object.

statsLogger = hStatsLogger(visualizationInfo);

% Simulate the wireless network by using the hWirelessNetworkSimulator helper object.

networkSimulator = hWirelessNetworkSimulator(wlanNodes);         % Object that handles network simulation

% Run all of the nodes in the network for the specified simulation time.

run(networkSimulator,simulationTime);

% To display the statistics, enable the displayStatsInUITable workspace variable.

displayStatsInUITable = true;

% Display the statistics by using the getStatistics object function. For information about these statistics, see Statistics Captured in WLAN System-Level Simulation.

statistics = getStatistics(statsLogger,displayStatsInUITable);

% Visualize network statistics such as throughput, packet loss ratio, and latencies at each node by using the hPlotNetworkStats helper function.

hPlotNetworkStats(statistics,wlanNodes);

% At the end of the simulation, remove the mlWLANSystemSimulation folder from the path.

rmpath(genpath(fullfile(pwd,'mlWLANSystemSimulation')));

% Copyright 2021 The MathWorks, Inc.