%% Sidelink Demodulation
% Perform sidelink SC-FDMA modulation of one subframe containing the sidelink synchronization signals and add noise at an SNR of 3.0 dB. The demodulator zeros the resource elements in the last SC-FDMA symbol. This behavior is consistent with the operation of the SC-FDMA modulator which does not modulate the last SC-FDMA symbol of the subframe. Plot the received waveform and the demodulated resource grid magnitude.
% Create a UE settings structure.

ue.NSLRB = 15;
ue.CyclicPrefixSL = 'Normal';
ue.NSLID = 17;

%
% Populate the resource grid with PSSS and SSSS. Modulate the PSSS and SSSS.

txgrid = lteSLResourceGrid(ue);
txgrid(ltePSSSIndices(ue)) = ltePSSS(ue);
txgrid(lteSSSSIndices(ue)) = lteSSSS(ue);

[txwaveform,info] = lteSLSCFDMAModulate(ue,txgrid);

%
% Add AWGN with an SNR of 3.0 dB.

rxwaveform = awgn(txwaveform,3.0,'measured');

%
% Perform sidelink SC-FDMA demodulation.

rxgrid = lteSLSCFDMADemodulate(ue,rxwaveform);

%
% Calculate the RMS of each SC-FDMA symbol in the received resource grid.

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

%
% Plot the magnitude of the resulting time-domain waveform, overlaying the RMS for each SC-FDMA symbol after demodulation. Plot the demodulated resource grid magnitude.

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

subplot(2,1,1)
plot(t(1:end-1),abs(rxwaveform),'r')
hold on
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('RX Waveform vs. Time')
legend('RX waveform magnitude','RMS per demodulated SC-FDMA symbol')

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

% Copyright 2012 The MathWorks, Inc.