%% Perform NB-IoT Uplink Single-Tone SC-FDMA Demodulation
% Perform NB-IoT uplink single-tone SC-FDMA demodulation with 3.75-kHz subcarrier spacing.
% Specify the number of slots for waveform generation.

NSlots = 10;

% Initialize the required cell-wide settings by specifying the NB-IoT subcarrier spacing as a field in the structure ue.

ue.NBULSubcarrierSpacing = '3.75kHz';

% Specify the zero-based NB-IoT subcarrier indices field as a scalar, indicating single-tone SC-FDMA demodulation. This configuration requires specification of the modulation type, number of slots per resource unit (RU), number of RUs, and number of repetitions for a codeword.

chs.NBULSubcarrierSet = 41;    % Indicate single-tone demodulation
chs.Modulation = 'BPSK';       % Specify modulation type as BPSK
chs.NULSlots = 4;              % Set four slots per RU
chs.NRU = 1;                   % Specify one RU
chs.NRep = 4;                  % Repeat codeword four times

% Set the input grid to start from the third time slot in an NPUSCH bundle.

chs.SlotIdx = 2;

% Specify a random array of bits and map the values to BPSK-modulated symbols by using the lteSymbolModulate function. Perform uplink precoding on the modulated symbols by using the lteULPrecode function.

bits = randi([0,1],7*NSlots,1);
symbols =  lteSymbolModulate(bits,chs.Modulation);
precodedSymbols = lteULPrecode(symbols,1,'Subcarrier');

% Initialize the resource element (RE) grid using the precoded symbols.

reGrid = zeros(48,7*NSlots);
NSC = length(chs.NBULSubcarrierSet);
reGrid(chs.NBULSubcarrierSet+1,:) = reshape(precodedSymbols,NSC,[]);

% Generate the time-domain waveform for demodulation by using the lteSCFDMAModulate function.

[waveform,info] = lteSCFDMAModulate(ue,chs,reGrid);

% Get the resource array by performing SC-FDMA demodulation on the waveform.

grid = lteSCFDMADemodulate(ue,chs,waveform);

% Copyright 2018 The MathWorks, Inc.