Implement Upsampler for HDL
This example shows how to use the Upsampler block to upsample data. This block supports scalar and vector inputs. You can generate HDL code from the subsystem used in this example.
Set Up Input Data Parameters
Set up workspace variables for the Simulink® model to use. These variables configure the Upsampler block inside the subsystem.
L = 8; % Upsample factor O = 0; % Sample offset scalar = true; if scalar vecSize = 1; outSize = 1; minCycles = L; else vecSize = 16; %#ok % Vector size must be a multiple or factor of L outSize = vecSize*L; minCycles = 1; end
Generate Reference Data from Function
Generate frames of random input samples and apply the upsample
function. Use the output as reference data against which to compare the output of the block.
totalsamples = 0; numFrames =1; dataSamples = cell(1,numFrames); framesize = zeros(1,numFrames); refOutput = []; WL = 0; % Word length FL = 0; % Fraction length for i = 1:numFrames framesize(i) = randi([5 200],1,1); dataSamples{i} = fi(randn(vecSize,framesize(i)),1,16,8); ref_upsample= upsample((dataSamples{i}(:)),L,O); refOutput = [refOutput,ref_upsample]; %#ok end
Run Model
Run the model to import the input signal variables from the MATLAB® workspace to the Upsampler block in the model. Insert the required number of idle cycles after each frame using the latency
variable to avoid invalid output data. Initialize the size of the output to accommodate the output data.
latency = 3;
sampleIn = zeros(vecSize,size(dataSamples{i},2)*minCycles);
validIn = upsample(true(1,length(dataSamples{i})),minCycles);
sampleIn(:,validIn) = dataSamples{i};
sampletime = 1;
simTime = length(validIn)+latency;
modelname = "HDLUpsampler";
open_system(modelname);
sim(modelname);
Compare Block Output with Function Output
Compare the output of the block with the output of the Upsampler
function.
sampleOut = squeeze(sampleOut_ts.Data); validOut = squeeze(validOut_ts.Data); if scalar HDLOutput = sampleOut(validOut); else HDLOutput = sampleOut(:,validOut); %#ok end HDLOutput = HDLOutput(:); fprintf('Upsampler\n'); difference = (abs(HDLOutput-refOutput(1:length(HDLOutput)))>0); fprintf(['\nTotal number of samples differed between Behavioral ' ... 'and HDL simulation: %d \n'],sum(difference));
Upsampler Total number of samples differed between Behavioral and HDL simulation: 0