Main Content

Plug Custom Channel into Wireless Network Simulator

This example shows you how to plug a custom channel into the wireless network simulator by using 5G Toolbox™ and the Communications Toolbox™ Wireless Network Simulation Library.

Using this example, you can:

  1. Create and configure a 5G network with new radio (NR) base station (gNB) and user equipment (UE) nodes.

  2. Establish a connection between the gNB and UE nodes.

  3. Create a custom channel, and plug it into the wireless network simulator.

  4. Simulate a 5G network, and retrieve the statistics of the gNB and UE nodes.

Check if the Communications Toolbox Wireless Network Simulation Library support package is installed. If the support package is not installed, MATLAB® returns an error with a link to download and install the support package.


Create a wireless network simulator.

networkSimulator = wirelessNetworkSimulator.init;

Create a gNB node with these specifications.

  • Position — [–100 100 0]

  • Channel bandwidth — 20 MHz

  • Subcarrier spacing — 30 KHz

  • Duplex mode — Frequency division duplex

gnb = nrGNB(Position=[-100 100 0],ChannelBandwidth=20e6,DuplexMode="FDD",SubcarrierSpacing=30e3);

Create two UE nodes, specifying their positions in Cartesian coordinates.

ue = nrUE(Position=[100 100 0; 5000 100 0]); % In Cartesian x, y, and z coordinates.

Connect the UE nodes to the gNB node and enable full-buffer traffic in the downlink direction.


Add the nodes to the network simulator.


Create an individual downlink clustered delay line (CDL) channel for each UE node.

% Retrieve OFDM information based on the number of resource blocks and subcarrier spacing
waveformInfo = nrOFDMInfo(gnb.NumResourceBlocks,gnb.SubcarrierSpacing/1e3);
channelFilteringFlag = strcmp(gnb.PHYAbstractionMethod,"none");

% Calculate the total number of nodes by adding the number of gNBs and UEs

% Initialize the CDL channel cell array, maximum channel delay matrix, and the path filter cell array
channelInfo.CDLChannels = cell(numNodes,numNodes);
channelInfo.MaxChannelDelayMatrix = zeros(numNodes,numNodes);
channelInfo.PathFilter = cell(numNodes,numNodes);

for i=1:length(ue)
    % Create a CDL channel model by specifying sample rate and channel filtering option
    channelInfo.CDLChannels{gnb.ID, ue(i).ID} = nrCDLChannel(SampleRate=waveformInfo.SampleRate,ChannelFiltering=channelFilteringFlag);
    % Set the transmit antenna array size
    channelInfo.CDLChannels{gnb.ID, ue(i).ID}.TransmitAntennaArray.Size = [1 1 1 1 1];
    % Set the receive antenna array size 
    channelInfo.CDLChannels{gnb.ID, ue(i).ID}.ReceiveAntennaArray.Size = [1 1 1 1 1];
    % Retrieve the channel information
    chInfo = info(channelInfo.CDLChannels{gnb.ID, ue(i).ID});
    % Calculate the maximum channel delay and update the delay matrix
    channelInfo.MaxChannelDelayMatrix(gnb.ID, ue(i).ID) = ceil(max(chInfo.PathDelays*waveformInfo.SampleRate)) + chInfo.ChannelFilterDelay;
    % Obtain path filters
    channelInfo.PathFilter{gnb.ID,ue(i).ID} = getPathFilters(channelInfo.CDLChannels{gnb.ID,ue(i).ID}).';

Add the custom channel to the wireless network simulator.


Specify the simulation time in seconds.

simulationTime = 0.05;

Run the simulation for the specified simulation time.


Obtain the statistics for the gNB and UE nodes.

gnbStats = statistics(gnb);
ueStats = statistics(ue);

Clear persistent channel context

clear getSetChannelModel;

Follow these steps to create a custom channel model

  • Create a custom function with this syntax: rxData = customFcnName(rxInfo,txData). The rxInfo input (a structure) is the receiver node information, and the txData input (a structure) specifies the transmitted packets. The simulator automatically passes information about the receiver node and the packets transmitted by a transmitter node as inputs to the custom function. For more information about this, see the addChannelModel object function.

  • Apply free space path loss between the base station and UE nodes.

  • Apply CDL channel to the transmitted packets.

function channelModelInfo = getSetChannelModel(varargin)
persistent channelModels;
if nargin == 1 % Set
    channelModels = varargin{1};
channelModelInfo = channelModels;

function outputData = addImpairment(rxInfo,txData)

outputData = txData;

% Calcultae path loss

distance = norm(txData.TransmitterPosition - rxInfo.Position);    % Distance between the transmitter and the receiver
lambda = physconst('LightSpeed')/txData.CenterFrequency;          % Wavelength calculation using the speed of light and center frequency.
pathLoss = fspl(distance, lambda);                                % Free space path loss
outputData.Power = outputData.Power - pathLoss;                   % Adjust the power of the output data based on the path loss

% Retrieve the channel model information
channelModelInfo = getSetChannelModel();

% Obtain the maximum channel delay matrix from the channel model information
maxChannelDelayMatrix = channelModelInfo.MaxChannelDelayMatrix;

% Obtain the path filter from the channel model information
pathFilter = channelModelInfo.PathFilter;

% Check if the CDL channel model exists for the given transmitter-receiver pair
if ~isempty(channelModelInfo.CDLChannels{txData.TransmitterID,rxInfo.ID})
    % A channel exists between the transmitter and receiver nodes
    channelModelInfo.CDLChannels{txData.TransmitterID,rxInfo.ID}.InitialTime = outputData.StartTime;
    if outputData.Abstraction == 0                           % Full PHY
        rxWaveform = [txData.Data; zeros(maxChannelDelayMatrix(txData.TransmitterID,rxInfo.ID), ...
        [outputData.Data,outputData.Metadata.Channel.PathGains, outputData.Metadata.Channel.SampleTimes] = ...
        outputData.Data = outputData.Data.*db2mag(-pathLoss);
        outputData.Duration = outputData.Duration + (1/outputData.SampleRate)*maxChannelDelayMatrix(txData.TransmitterID,rxInfo.ID);
    else                                                     % Abstract PHY
        channelModelInfo.CDLChannels{txData.TransmitterID,rxInfo.ID}.NumTimeSamples =  ...
            txData.Metadata.NumSamples + maxChannelDelayMatrix(txData.TransmitterID,rxInfo.ID);
        [outputData.Metadata.Channel.PathGains, outputData.Metadata.Channel.SampleTimes] = ...
    outputData.Metadata.Channel.PathFilters = ...
    % Set default values for channel parameters
    outputData.Metadata.Channel.PathGains = ...
        permute(ones(outputData.NumTransmitAntennas,rxInfo.NumReceiveAntennas),[3 4 1 2])/ ...
    outputData.Metadata.Channel.PathFilters = 1;
    outputData.Metadata.Channel.SampleTimes = 0;

    if outputData.Abstraction == 0                            % Full PHY
        outputData.Data = outputData.Data.*db2mag(-pathLoss);
        numTxAnts = outputData.NumTransmitAntennas;
        numRxAnts = rxInfo.NumReceiveAntennas;
        H = fft(eye(max([numTxAnts numRxAnts])));
        H = H(1:numTxAnts,1:numRxAnts);
        H = H/norm(H);
        outputData.Data = txData.Data*H;                      % Apply channel to the waveform

See Also


Related Topics