%% Sidelink Broadcast Channel Modulation
% Perform sidelink SC-FDMA modulation of one subframe containing a sidelink broadcast transmission. Any resource elements present in the last SC-FDMA symbol of the subframe are not modulated, so the resulting waveform magnitude is zero during that SC-FDMA symbol. Plot the magnitude of the resulting time-domain waveform and the transmitted resource grid magnitude.
%% Create a UE settings structure and an empty resource grid

ue.NSLRB = 6;
ue.CyclicPrefixSL = 'Extended';
ue.InCoverage = 1;
ue.DuplexMode = 'FDD';
ue.NFrame = 0;
ue.NSubframe = 0;
ue.NSLID = 42;

grid = lteSLResourceGrid(ue);

%
%% Transmit the PSBCH
% Populate the PSBCH resource grid with an encoded SL-MIB message, and its DM-RS. Perform sidelink SC-FDMA modulation.

grid(ltePSBCHIndices(ue)) = ltePSBCH(ue,lteSLBCH(ue,lteSLMIB(ue)));
grid(ltePSBCHDRSIndices(ue)) = ltePSBCHDRS(ue);

[waveform,info] = lteSLSCFDMAModulate(ue,grid);

%
% Calculate the expected RMS for each SC-FDMA symbol from the resource grid prior to modulation.

rms = sqrt(sum(abs((grid./double(info.Nfft)).^2)));

%
% Plot the waveform magnitude overlaying the RMS for each SC-FDMA symbol. Plot the transmitted resource grid magnitude.

t = (0:size(waveform,1))/info.SamplingRate;
figure

subplot(2,1,1)
hold on

plot(t(1:end-1),abs(waveform),'r');
n = cumsum([1 info.CyclicPrefixLengths + info.Nfft]);
n = [n(1:end-1); n(2:end)];
rmsplot = repmat(rms,[2 1]);

plot(t(n(:)),rmsplot(:),'b')
xlabel('time (s)')
ylabel('magnitude')
title('Waveform vs. Time')
legend('Waveform magnitude','RMS per resource grid SC-FDMA symbol')

subplot(2,1,2)
imagesc(abs(grid))
title('Resource Grid Magnitude')
xlabel('SC-FDMA symbol index'); 
ylabel('subcarrier index');

% Copyright 2012 The MathWorks, Inc.