%% Model HARQ Indicator and PHICH
% This example shows how to implement the HARQ Indicator (HI) and physical HI channel (PHICH). You create the processing chain of coding hybrid indicator values, create the PHICH, and map it to a resource grid.
% Set up the cell-wide settings. Create a structure and specify the cell-wide settings as its fields.

enb.NDLRB = 9;
enb.CyclicPrefix = 'Normal';
enb.PHICHDuration = 'Normal';
enb.Ng = 'Sixth';
enb.CellRefP = 4;
enb.NCellID = 1;
enb.NSubframe = 0;
enb.DuplexMode = 'FDD';

% Many of the functions used in this example require a subset of the preceding settings specified.
%
% To generate PHICH resource information, use the ltePHICHInfo function.

phichInfo = ltePHICHInfo(enb);

% The function returns phichInfo, a structure containing the relevant data required to define PHICH sets. The elements and values of the structure are:
%  \begin{array}{|l|l|c|} \hline
% \mathbf{Structure\ Element}&\begin{array}{l}\mathbf{Description}\end{array}&\mathbf{Value}\\
% \hline\mathrm{NREG}&\begin{array}{l}\mathrm{Number\ of\ resource\ element\ groups\ used\ to\ map\ the\ PHICHs.}\end{array} & 3\\
% \hline\mathrm{NRE}&\begin{array}{l}\mathrm{Number\ of\ resource\ elements\ used\ to\ map\ the\ PHICHs.}\end{array} & 12\\
% \hline\mathrm{NPHICH}&\begin{array}{l}\mathrm{Maximum\ number\ of\ PHICH\ that\ can\ be\ used.}\end{array} & 8\\
% \hline\mathrm{NGroups}&\begin{array}{l}\mathrm{Maximum\ number\ of\ PHICH\ groups\ that\ can\ be\ used.}\end{array} & 1\\
% \hline\mathrm{NMappingUnits}&\begin{array}{l}\mathrm{Number\ of\ PHICH\ mapping\ units\ used\ to\ map\ the}\\\mathrm{maximum\ number\ of\ PHICH\ groups.}\end{array} & 1\\
% \hline\mathrm{NSequences}&\begin{array}{l}\mathrm{Maximum\ number\ of\ orthogonal\ sequences\ that\ can}\\\mathrm{be\ used\ within\ each\ group.}\end{array} & 8\\
% \hline\mathrm{PHICHDuration}&\begin{array}{l}\mathrm{Number\ of\ OFDM\ symbols\ used\ to\ map\ the\ PHICH.}\end{array} & 1\\
% \hline\end{array}
%
% Generate the HARQ indicator (HI) set. An HI set consists of a HARQ indicator value, 1 for ACK and 0 for NACK, and a PHICH index pair that contains the PHICH group index, n_{\textrm{PHICH}}^{\textrm{group}}, and the orthogonal sequence index, n_{\textrm{PHICH}}^{\textrm{seq}}, for the PHICH containing HI. The values of n_{\textrm{PHICH}}^{\textrm{group}} and n_{\textrm{PHICH}}^{\textrm{seq}} can be determined using the PHICH resource dimension information returned by theltePHICHInfo function. The number of groups determines acceptable values of the PHICH group index and the number of sequences determines acceptable values of sequence indexes.

HISet = [[0 0 1];[0 1 0];[0 4 0];[0 7 1]];

% In this example, you create one PHICH group containing four PHICHs with the indices:
%  \begin{array}{|c|c|l|} \hline
% \mathbf{PHICH\ Group\ Index} & \mathbf{PHICH\ Sequence\ Index} & \mathbf{HARQ\ Indicator\ Value}\\
% \hline 0 & 0 & 1\ -\ ACK\\
% \hline 0 & 1 & 0\ -\ NACK\\
% \hline 0 & 4 & 0\ -\ NACK\\
% \hline 0 & 7 & 1\ -\ ACK\\
% \hline \end{array}
% In the LTE Toolbox™, a HI set matrix is used to define the HI and PHICH index pair for each HI within the subframe. The HI set matrix defines a single PHICH in terms of n_{\textrm{PHICH}}^{\textrm{group}}, n_{\textrm{PHICH}}^{\textrm{seq}}, and the HARQ indicator.
%
% Generate the PHICH complex symbols from the cell-wide settings configuration and HARQ indicator matrix. To perform the required channel coding, modulation, scrambling, layer mapping, precoding, and combining of the PHICH groups, call the ltePHICH function.

phichSymbols = ltePHICH(enb,HISet);
disp(phichSymbols)

% The resulting vector, phichSymbols, has 12 rows and four columns. Each column contains the complex symbols to be mapped to the resource grids for each of the four antenna ports.
%
% To generate the PHICH mapping indices, use the ltePHICHIndices function. These mapping indices are required to map the complex PHICH symbols to the subframe resource grid.

phichIndices = ltePHICHIndices(enb);
disp(phichIndices)

% This function returns a matrix with four columns, one for each antenna port. The rows contain the indices in linear form for mapping the PHICH symbols to the subframe resource grid.
%
% To generate a subframe resource grid, use the lteDLResourceGrid function. This function creates an empty resource grid for one subframe.

subframe = lteDLResourceGrid(enb);
disp(size(subframe))

%
% Map the complex PHICH symbols to the resource grid by assigning phichSymbols to the phichIndices locations in subframe.

subframe(phichIndices) = phichSymbols;

% Copyright 2019 The MathWorks, Inc.