%% NB-IoT PRACH Waveform Generation
% This example shows how to generate LTE-Advanced Pro Release 15 narrowband internet of things (NB-IoT) waveforms consisting of the narrowband physical random access channel (NPRACH) for frame structure type 1 by using LTE Toolbox™.
%% Introduction
% 3GPP introduced a new air interface, NB-IoT, optimized for low data rate machine type communications in LTE-Advanced Pro Release 13. NB-IoT provides cost and power efficiency improvements as it avoids the need for complex signaling overhead required for other LTE based systems.
% You can use LTE Toolbox to generate standard compliant NB-IoT uplink complex baseband waveforms, representing the 180 kHz narrowband carrier, suitable for test and measurement applications. The NB-IoT uplink physical layer channels and signals are:
% Narrowband demodulation reference signal (DM-RS)
% Narrowband physical uplink shared channel (NPUSCH)
% Narrowband physical random access channel (NPRACH)
% In this example, we demonstrate NPRACH allocation in the resource grid and the generation of NPRACH waveforms. Specifically, this example considers the frequency-division duplexing aspect of NB-IoT PRACH defined in LTE Advanced-Pro Release 15. This is referred to as frame structure type 1 in TS 36.211 Section 10.0.1 [ 1 ].
% The example outputs the complex baseband waveform along with the populated grid containing the NPRACH signal.
%% NPRACH Allocation
% This section provides an overall description of the NPRACH allocation in time and frequency.
% From a time point of view, an NPRACH transmission is defined as NRep repetitions of NPRACH preambles, in which an NPRACH preamble is defined as a set of P symbol groups. TS 36.211 Section 10.1.6.1 defines a symbol group as a sequence of N identical symbols preceded by a cyclic prefix. For this reason, the symbol group, rather than the OFDM symbol, can be considered as the atomic unit of NPRACH. The duration of an NPRACH symbol group is a function of the preamble format, as described in TS 36.211 Table 10.1.6.1-1. This figure shows a schematic of the symbols groups for the three NPRACH preamble formats.
%
% From a frequency point of view, the NPRACH is characterized by single-tone transmission with frequency hopping and a subcarrier spacing defined in TS 36.211 Table 10.1.6.2-1. Each symbol group occupies a single subcarrier, following the frequency hopping pattern described in TS 36.211 Section 10.1.6.1. SubcarrierOffset defines the frequency location of the first subcarrier allocated to NPRACH, whereas NumSubcarriers defines the total number of subcarriers allocated to NPRACH. Among those, the subcarrier location of the first symbol group of the first preamble is determined by NInit. For all subsequent preambles, the frequency of the first symbol group of each NPRACH preamble depends on the pseudorandom sequence defined in TS 36.211 Section 7.2 and initialized by NNCellID. This figure shows a schematic of the frequency hopping pattern for the first two repetitions of preamble format 0, given the NPRACH configuration used in this example. You can see that the first symbol groups of the two preambles occupy different subcarriers.
%
%% NPRACH Configuration
% In this section, we configure the parameters required for NPRACH generation. For a list of all the allowed ranges for the parameters in the ue and chs structures, see hNPRACH.

% Define the user equipment (UE) parameters
ue = struct();
ue.NBULSubcarrierSpacing = '15kHz'; % Uplink subcarrier spacing ('3.75kHz', '15kHz')
ue.NNCellID = 0;                    % Narrowband cell identity

% Define the channel parameters
chs = struct();
chs.NPRACHFormat = '0';    % Preamble format ('0','1','2')
chs.Periodicity = 80;      % NPRACH resource periodicity in ms
chs.SubcarrierOffset = 12; % Frequency location of the first subcarrier allocated to NPRACH
chs.NumSubcarriers = 24;   % Number of subcarriers allocated to NPRACH
chs.NRep = 8;              % Number of NPRACH repetitions
chs.StartTime = 8;         % NPRACH starting time in ms
chs.NInit = 2;             % Initial subcarrier for NPRACH
chs.NPRACHPower = 30;      % NPRACH power scaling in dB for plot visualization

%
%% NB-IoT PRACH Waveform Generation
% In this section, we create the NPRACH resource grid and the time-domain waveform. The example generates a waveform that spans chs.Periodicity milliseconds and is sampled with the same sample rate used for the uplink signals with subcarrier spacing given by ue.NBULSubcarrierSpacing.

[waveform,info,resourceGrid] = hNPRACH(ue,chs);

%
%% Plot Transmitted Grid
% Plot the NPRACH resource grid. You can observe the frequency hopping pattern of the NPRACH symbols. Note that the resource grid plot uses the power levels of the NPRACH to assign colors to the resource elements. To disable the plot of the resource grid, set the value of displayGrid to 'off'.

displayGrid = 'on'; % Display the NPRACH resource grid ('on','off')
hNPRACHResourceGridPlot(chs,resourceGrid,displayGrid);

%
%% Summary and Further Exploration
% This example shows how to generate a time-domain waveform and visualize the resource grid for a single NB-IoT PRACH configuration. The generated NPRACH waveform spans chs.Periodicity milliseconds and is sampled with the same sample rate used for the uplink signals with subcarrier spacing given by ue.NBULSubcarrierSpacing. This allows you to generate an NPRACH waveform and add it to an existing NB-IoT uplink waveform. For more information about the generation of an NB-IoT uplink waveform, see NB-IoT Uplink Waveform Generation.
%% Appendix
% This example uses these helper functions:
% hNPRACH.m
% hNPRACHInfo.m
%% Selected Bibliography
% 3GPP TS 36.211. "Physical channels and modulation", 3rd Generation Partnership Project; Technical Specification Group Radio Access Network.
%% Local functions
% This example uses this local function:

function hNPRACHResourceGridPlot(chs,resourceGrid,displayGrid)
%hNPRACHResourceGridPlot Plots the NPRACH resource grid
%   hNPRACHResourceGridPlot(CHS,RESOURCEGRID,DISPLAYGRID) plots the NPRACH
%   resource grid. Set DISPLAYGRID to 'off' to disable the plot of the
%   resource grid.

    if strcmpi(displayGrid,'on')
        figure
        
        % Create an image of the NPRACH resource grid
        im = image(resourceGrid);
        
        % Adjust plot colors, axes, and add title and labels
        cmap = parula(64);
        colormap(im.Parent,cmap);
        axis xy;
        xlabel('OFDM symbols')
        ylabel('Subcarriers')
        title(sprintf('NB-IoT PRACH RE Grid (Format = ''%s'')',chs.NPRACHFormat))
       
    end
end

% Copyright 2020 The MathWorks, Inc.
%