MATLAB Examples

Check for CRC Errors in Streaming Samples

This example shows how to use the CRC Decoder block to check encoded data, and how to compare the hardware-friendly design with the results from LTE Toolbox™. The workflow follows these steps:

  1. Generate frames of random input samples in MATLAB.
  2. Generate and append the CRC checksum using the LTE Toolbox function lteCRCEncode.
  3. Convert framed input data and checksum to a stream of samples and import it to Simulink®.
  4. To check the samples against the checksum using a hardware-friendly architecture, run the Simulink model. The model contains the LTE HDL Toolbox™ block CRC Decoder.
  5. Export the stream of samples back to the MATLAB® workspace.
  6. Convert the sample stream back to framed data, and compare the frames with the reference data.

Generate input data frames, then generate the CRC checksum using lteCRCEncode.

frameLength = 256;
numframes   = 2;
rng(0);

txframes     = cell(1,numframes);
txcodeword   = cell(1,numframes);
rxSoftframes = cell(1,numframes);

for ii = 1:numframes

    txframes{ii}  = randi([0 1],frameLength,1)>0.5;

    CRCType = '24B';
    CRCMask = 50;
    txcodeword{ii} = boolean(lteCRCEncode(txframes{ii},CRCType,CRCMask));

end

Serialize input data for the Simulink model. The CRC Decoder block does not require any space between frames, but the hardware-friendly algorithm adds latency of (3 * CRCLength / SampleSize) + 5 cycles. This example uses scalar input samples, so the latency is (3 * CRCLength) + 5.

idleCyclesBetweenSamples = 0;
idleCyclesBetweenFrames  = 77;
samplesizeIn             = 1;

[sampleIn,ctrlIn] = ltehdlFramesToSamples(...
    txcodeword,idleCyclesBetweenSamples,idleCyclesBetweenFrames,samplesizeIn);

Run the Simulink model.

sampletime = 1;
simTime = length(ctrlIn);
modelName = 'ltehdlCRCDecoderModel';
open_system(modelName);
sim(modelName);

The Simulink model exports sampleOut_ts and ctrlOut_ts back to the MATLAB workspace. Deserialize the output samples, and compare the framed data to the input frames.

sampleOut = sampleOut_ts.Data;
sampleOut = sampleOut(:);
ctrlOut = [squeeze(ctrlOut_ts.start.Data) ...
    squeeze(ctrlOut_ts.end.Data) ...
    squeeze(ctrlOut_ts.valid.Data)];
txhdlframes = ltehdlSamplesToFrames(sampleOut,ctrlOut);

fprintf('\nLTE CRC Decoder\n');
for ii = 1:numframes
    numBitsDiff = sum(double(txframes{ii})-double(txhdlframes{ii}));
    fprintf(['  Frame %d: Behavioral and ' ...
        'HDL simulation differ by %d bits\n'], ii, numBitsDiff);
end
Maximum frame size computed to be 256 samples.

LTE CRC Decoder
  Frame 1: Behavioral and HDL simulation differ by 0 bits
  Frame 2: Behavioral and HDL simulation differ by 0 bits