%% Model DL-SCH and PDSCH
% This example shows how to construct the physical downlink shared channel (PDSCH). It also demonstrates how to generate a transport block, perform downlink shared channel (DL-SCH) coding to create a codeword, perform physical channel coding to create the physical channel, and map the complex symbols to the resource grid.
% Specify cell-wide settings as fields in the structure enb. Many of the functions used in this example require a subset of these settings.

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

% Configure the PDSCH. In addition to the cell-wide settings specified in enb, you must define other parameters related to the modulation and channel transmission configuration, pdsch, such as the radio network temporary identifier (RNTI), to generate the PDSCH.

pdsch.NTxAnts = 4;
pdsch.NLayers = 4;
pdsch.TxScheme = 'TxDiversity';
pdsch.Modulation = {'QPSK'};
pdsch.RV = 0;
pdsch.RNTI = 1;

% In this example, you use a single codeword to form the PDSCH symbols. However, in LTE, up to two codewords can be combined to form the PDSCH. Each codeword can be modulated with a different scheme. Use a cell array to indicate the modulation scheme for each codeword.
% Determine how the PDSCH is mapped to resource elements by allocating the physical resource blocks (PRBs). A column vector containing the indices of PDSCH allocated PRBs is required. In this example, assume full allocation; all resource blocks are allocated to the PDSCH. Specify this full subframe resource allocation using a column vector.

prbs = (0:enb.NDLRB-1).';

% The allocation specified in prbs is zero-based. In this case, assume that both slots in the subframe share the same resource allocation. To have different allocations for each slot, specify a two-column matrix where each column refers to each slot in the subframe.
% Generate the PDSCH indices. To do so, call the ltePDSCHIndices function for the cell-wide settings enb, the channel transmission configuration pdsch, and the physical resource block allocation prbs.

[pdschIndices,pdschIndInfo] = ltePDSCHIndices(enb,pdsch,prbs,{'1based'});

% The first output, pdschIndices, specifies the PDSCH indices. The second output, pdschIndInfo, provides additional information related to the PDSCH capacity.
% Determine DL-SCH payload and coded transport block size. These items are required for creating the PDSCH payload due to the rate matching portion of the DL-SCH transport block coding. There are the following two methods of determining coded transport block size and the DL-SCH payload size.
% Using the PDSCH indices information output, as shown in this example
% Using the reference measurement channel (RMC) transport block sizes as a guide
% The coded transport block size is one of the fields of the PDSCH indices information output, pdschIndInfo.

codedTrBlkSize = pdschIndInfo.G;

% In this example, codedTrBlkSize is 480. Alternatively, you could read the coded transport block size for a given modulation scheme, PRB allocation, and number of antennas from the RMC tables in TS 36.101, Annex A.3.3 and A.3.4. Once you know the coded transport block size, calculate the DL-SCH payload using the rules in TS 36.101, Annex A.2.1.2, titled, "Determination of payload size", with target code rate, R, equal to 1/3, and the number of bits per subframe given by codedTrBlkSize. Determine the payload size, A, such that the resulting coding rate is as close as possible to the desired coding rate, R, for a given coded transport block size, N_{\textrm{ch}}, as shown in the following equation.
% \min \;\left|R-\left(A+24\right)/N_{\textrm{ch}} \right|
% In this example, the payload size for 6 RBs calculated using the preceding equation is A=152. This is the value at which the error between the desired code rate and actual code rate is minimized.
% The payload size, A, must be one of a specific set for a specific number of resource blocks given in TS 36.213, Table 7.1.7.2.1 1 or 7.1.7.2.2 1 (in Section 7.1.7.2). These tables are represented by the lteTBS function. In this example, the payload size for 6 RBs that minimizes the error between the desired code rate and the actual code rate is A=152. This value was selected from table 7.1.7.2.2 1. Therefore, the payload size, transportBlkSize, is 152.
%
%% Alternative Method: Use RMCs to Determine Transport Block Sizes
% Alternatively, you could determine suitable payload size and coded transport block size from the tables in TS 36.101, Annex A.3.3 and A.3.4, titled "Reference Measurement Channels for PDSCH performance requirements." Despite the advantage of simply being able to read values from the tables, the channel bandwidths and PDSCH allocations are restricted to the RMCs available. For example, you can use Table A.3.3.2.2-1, titled "Fixed Reference Channel four antenna ports."
% To generate a PDSCH for a 1.4MHz channel bandwidth, four-antenna transmission with QPSK modulation, and a coding rate of , use the highlighted rows titled "Information Bit Payload" to find the DL-SCH payload size for each subframe, and "Binary Channel Bits," to find the coded transport block size for each subframe.
% \begin{array}{|l|l|l|l|l|l|l|l|} \hline
% \mathrm {Parameter}                                 & \mathrm {Unit} & \mathrm {Value}    &          &          &            &            &          \\
% \hline\begin{array}{l}\mathrm{Reference\ Channel}\end{array}           &      & \begin{array}{l}{R.12\ FDD}\end{array} & \begin{array}{l}{R.13\ FDD}\end{array} & \begin{array}{l}{R.14\ FDD}\end{array} & \begin{array}{l}{R.14-1\ FDD}\end{array} & \begin{array}{l}{R.14-2\ FDD}\end{array} & \begin{array}{l}{R.36\ FDD}\end{array} \\
% \begin{array}{l}\mathrm{Channel\ bandwidth}\end{array}                         & MHz  & 1.4      & 10       & 10       & 10         & 10         & 10       \\
% \begin{array}{l}\mathrm{Allocated\ resource\ blocks}\end{array}                 &      & 6        & 50       & 50       & 6          & 3          & 50       \\
% \begin{array}{l}\mathrm{Allocated\ subframes\ per\ Radio\ Frame }\end{array}      &      & 9        & 9        & 9        & 8          & 8          & 9        \\
% \begin{array}{l}\mathrm{Modulation}\end{array}                                &      & QPSK     & QPSK     & 16QAM    & 16QAM      & 16QAM      & 64QAM    \\
% \begin{array}{l}\mathrm{Target\ Coding\ Rate}\end{array}                        &      & 1/3      & 1/3      & 1/2      & 1/2        & 1/2        & 1/2      \\
% \begin{array}{l}\mathrm{Information\ Bit\ Payload}\end{array}                  &      &          &          &          &            &            &          \\
% \begin{array}{l}\mathrm{For\ Sub-Frames\ 1,\ 2,\ 3,\ 4,\ 6,\ 7,\ 8,\ and\ 9}\end{array} & Bits & 408      & 4392     & 12960    & 1544       & 744        & 18336    \\
% \begin{array}{l}\mathrm{For\ Sub-Frame\ 5}\end{array}                           & Bits & n/a      & n/a      & n/a      & n/a        & n/a        & n/a      \\
% \begin{array}{l}\mathrm{For\ Sub-Frame\ 0}\end{array}                           & Bits & 152      & 3264     & 11448    & n/a        & n/a        & 18336    \\
% \begin{array}{l}\mathrm{Number\ of\ Code\ Blocks}\end{array}                     &      &          &          &          &            &            &          \\
% \begin{array}{l}\mathrm{For\ Sub-Frames\ 1,\ 2,\ 3,\ 4,\ 6,\ 7,\ 8,\ and\ 9}\end{array} &      & 1        & 1        & 3        & 1          & 1          & 3        \\
% \begin{array}{l}\mathrm{For\ Sub-Frame\ 5}\end{array}                           &      & n/a      & n/a      & n/a      & n/a        & n/a        & n/a      \\
% \begin{array}{l}\mathrm{For\ Sub-Frame\ 0}\end{array}                           &      & 1        & 1        & 2        & n/a        & n/a        & 3        \\
% \begin{array}{l}\mathrm{Binary\ Channel\ Bits\ per\ Sub-Frame}\end{array}         &      &          &          &          &            &            &          \\
% \begin{array}{l}\mathrm{For\ Sub-Frames\ 1,\ 2,\ 3,\ 4,\ 6,\ 7,\ 8,\ and\ 9}\end{array} & Bits & 1248     & 12800    & 25600    & 3072       & 1536       & 38400    \\
% \begin{array}{l}\mathrm{For\ Sub-Frame\ 5}\end{array}                           & Bits & n/a      & n/a      & n/a      & n/a        & n/a        & n/a      \\
% \begin{array}{l}\mathrm{For\ Sub-Frame\ 0}\end{array}                           & Bits & 480      & 12032    & 24064    & n/a        & n/a        & 36096    \\
% \begin{array}{l}\mathrm{Max.\ Throughput\ averaged\ over\ 1\ frame}\end{array}     & Mbps & 0.342    & 3.876    & 11.513   & 1.235      & 0.595      & 16.502   \\
% \begin{array}{l}\mathrm{UE\ Category}\end{array}                               &      & \ge 1    & \ge 1    & \ge 2    & \ge 1      & \ge 1      & \ge 2   \\
% \end{array}
%
%
% Define a transport block of information bits, using the payload size, transportBlkSize, calculated in the last step.

dlschTransportBlk = round(rand(1,152));

% Create the PDSCH payload. To encode the transport block bits into a single codeword, call the lteDLSCH function for the cell-wide settings and channel transmission configuration. This process includes CRC calculation, code block segmentation and CRC insertion, turbo coding, rate matching, and code block concatenation.

codeword = lteDLSCH(enb,pdsch,codedTrBlkSize,dlschTransportBlk);

% Generate the PDSCH complex symbols by calling the ltePDSCH function for the specified cell-wide settings, channel transmission configuration, and codeword. This function applies scrambling, modulation, layer mapping, and precoding operations to the coded transport block.

pdschSymbols = ltePDSCH(enb,pdsch,codeword);

% The resulting matrix, pdschSymbols, has four columns. Each column contains the complex symbols to map to each antenna port.
% Generate the subframe resource grid by calling the lteDLResourceGrid function. This function creates an empty resource grid for one subframe.

subframe = lteDLResourceGrid(enb);

% Map the PDSCH symbols to the resource grid using the generated indices.

subframe(pdschIndices) = pdschSymbols;

% Copyright 2019 The MathWorks, Inc.