%plotResourceGrid Plots the resource grid.
% plotResourceGrid(txGrid) plots the resource grid and differentiates
% signals with different colours.
function plotResourceGrid(txGrid)
fftLen = 128; % FFT length
numSubCar = size(txGrid,1); % Number of subcarriers per symbol
symPerFrame = 36; % Number of OFDM symbols per frame
numOFDMSym = size(txGrid,2);
numFrames = numOFDMSym/symPerFrame;

% Just for plotting resource grid assigning different index to
% different signals.
ssMapIndex = 1;
rsMapIndex = 2;
headerMapIndex = 3;
pilotMapIndex = 4;
dataMapIndex = 5;
dcMapIndex = 6;
guardMapIndex = 7;

% Initialize resource grid
resourceGrid = zeros(numSubCar,symPerFrame);

% Load synchronous, reference, header signal map indices to resource grid
resourceGrid(6:numSubCar-5,1) = ssMapIndex;   % Load synchronous signal map index
resourceGrid(1:5,1) = guardMapIndex;   % Load guard map index at synchronous signal upper edge
resourceGrid(68:72,1) = guardMapIndex;   % Load guard map index at synchronous signal lower edge
resourceGrid(1:numSubCar,2:3) = rsMapIndex;   % Load reference signal map index
resourceGrid(1:numSubCar,2:3) = rsMapIndex;   % Load reference signal map index
resourceGrid(1:numSubCar,4) = headerMapIndex; % Load header signal map index

% Load data and pilots signal map indices to resource grid
resourceGrid(1:6:numSubCar,5:end) = pilotMapIndex; % Load pilot signal map index
modDataInd = 1:numSubCar;
modDataInd(1:6:numSubCar) = []; % remove the pilot indices from modData indices
resourceGrid(modDataInd,5:end) = dataMapIndex; % Load data signal map index

% Append left, and right guard, and DC map indices to the resource grid
numLgSc = (fftLen-numSubCar)/2;
numRgSc = numLgSc-1;
resGridFFTLen = [guardMapIndex*ones(numLgSc,symPerFrame);resourceGrid(1:numSubCar/2,:);...
    dcMapIndex*ones(1,symPerFrame);resourceGrid(numSubCar/2+1:end,:);...
    guardMapIndex*ones(numRgSc,symPerFrame)];
resGridFFTLen = repmat(resGridFFTLen,1,numFrames);
% Plot resource grid
% Fix RGB values for different signals in the resource grid
map = [0     1   0;...
    0.4  0.5  1;...
    1     0   0;...
    0     1   1;...
    1     1   0;...
    0.3  0.3  0.3;...
    1    0.71 0.75 ];
figure (1)
image(resGridFFTLen)
ax = gca;
ax.XTick = 0:15:36*numFrames;
ax.YTick = 0:6:128;
grid(ax);
axis('xy')
title('OFDM Resource Grid');
xlabel('OFDM Symbol');
ylabel('Subcarrier');
colormap(map);
colorbar('Ticks',[1 2 3 4 5 6 7]+0.5,...
    'TickLabels',{'SS','RS','Header','Pilot','Data','DC','Guard'},...
    'Direction','reverse');
end