%% Generate and Map CSI-RS Symbols Used for Tracking
% Create a carrier configuration object with default properties.

carrier = nrCarrierConfig;

% Create a CSI-RS resource configuration object with CSI-RS parameters set for tracking. Specify four periodic NZP-CSI-RS resources in two consecutive slots. Specify for each slot to contain two periodic NZP-CSI-RS resources with periodicity set to 20. Set the offset for the first two resources to 0. Set the offset for the next two resources to 1. Set the row number to 1 and density to 'three' for all resources.

csirs = nrCSIRSConfig;
csirs.CSIRSType = {'nzp','nzp','nzp','nzp'};
csirs.CSIRSPeriod = {[20 0],[20 0],[20 1],[20 1]};
csirs.RowNumber = [1 1 1 1];
csirs.Density = {'three','three','three','three'};
csirs.SymbolLocations = {6,10,6,10};
csirs.SubcarrierLocations = {0,0,0,0};

% Generate CSI-RS symbols and indices for the default slot number of the carrier configuration object (slot number 0).

ind0 = nrCSIRSIndices(carrier,csirs);
sym0 = nrCSIRS(carrier,csirs);

% Map the symbols to a carrier grid of one slot duration.

gridSize = [12*carrier.NSizeGrid carrier.SymbolsPerSlot max(csirs.NumCSIRSPorts)];
slotgrid0 = complex(zeros(gridSize));
slotgrid0(ind0) = sym0;

% Change the absolute slot number in the carrier configuration from 0 to 1.

carrier.NSlot = 1; 

% Generate CSI-RS symbols and indices for slot number 1.

ind1 = nrCSIRSIndices(carrier,csirs);
sym1 = nrCSIRS(carrier,csirs);

% Map the symbols to another carrier grid of one slot duration.

slotgrid1 = complex(zeros(gridSize));
slotgrid1(ind1) = sym1;

% Concatenate the two slots to form the final grid.

grid = [slotgrid0 slotgrid1];

%  Plot the grid.

imagesc(abs(grid(:,:,1)));
axis xy;
title('CSI-RS as Tracking Reference Signal');
xlabel('OFDM Symbols');
ylabel('Subcarriers');

% Copyright 2019 The MathWorks, Inc.