Code covered by the BSD License  

Highlights from
Bit Packet Analyzer

image thumbnail

Bit Packet Analyzer

by

 

The Bit Packet Analyzer can be used to analyze bit streams for repeating patterns (e.g. preamble)

BitPacketAnalyzer_S(block)
function BitPacketAnalyzer_S(block)
% This is an S function implementation of the Bit Packet Analyzer for Simulink.
% Base don the S Function template
setup(block);

%endfunction

% Function: setup ===================================================
% Abstract:
%   Set up the S-function block's basic characteristics such as:
%   - Input ports
%   - Output ports
%   - Dialog parameters
%   - Options
%
%   Required         : Yes
%   C-Mex counterpart: mdlInitializeSizes
%
function setup(block)

% Register the number of ports.
block.NumInputPorts  = 1;
block.NumOutputPorts = 0; % it's a sink

% Set up the port properties to be inherited or dynamic.
block.SetPreCompInpPortInfoToDynamic;

% Override the input port properties.
block.InputPort(1).DatatypeID  = 8;  % boolean
block.InputPort(1).Complexity  = 'Real';
block.InputPort(1).SamplingMode = 'Frame';

% Register the parameters.
block.NumDialogPrms     = 2;
block.DialogPrmsTunable = {'Tunable','Nontunable'};



% Register the sample times.
block.SampleTimes = [-1, 0]; % Variable sample time

%% Set the block simStateCompliance to none.
% This will ensure that the handle store in dwork is not saved and restored
block.SimStateCompliance = 'HasNoSimState';

%% Register block methods
block.RegBlockMethod('CheckParameters',         @CheckPrms);
block.RegBlockMethod('SetInputPortDimensions',  @SetInpPortDims);
block.RegBlockMethod('Start',                   @Start);
block.RegBlockMethod('Outputs',                 @Output);

function CheckPrms(block)
% Check the validity of the parameters.
lenPack = block.DialogPrm(1).Data;
maxNpacks = block.DialogPrm(2).Data;

if sum(isnan(lenPack)+isnan(maxNpacks)+isinf(lenPack)+isinf(maxNpacks))
    error(message('wrong function parameters (lenPack OR maxNpacks)'));
end

function SetInpPortDims(block, idx, di)
block.InputPort(idx).Dimensions = di;

%% Prep figure
function Start(block)
  % Initialize the data.
  f = figure(108);
  set(f,'visible','off', 'WindowStyle','normal');
  ha = axes('Tag','FigAx','visible','off','XTickMode','manual');
  
%% Do the actual stuff
function Output(block)

%maxNpacks = 100;
inVector = block.InputPort(1).Data; % pass some bits to analysis procedure
lenPack = block.DialogPrm(1).Data;
maxNpacks = block.DialogPrm(2).Data;

%% Shape data
nElements = numel(inVector);

if (size(inVector,2) > 1) % if
    inVector = reshape(inVector,nElements,1);
end

numPackets = nElements/lenPack;
intNumPackets = ceil(numPackets);

if(numPackets > maxNpacks) % if there's more packets then nMax, truncate the input
    intNumPackets = maxNpacks;
    outVector = inVector(1:intNumPackets*lenPack);
elseif(numPackets ~= intNumPackets) %zero padding is needed
    nZeros = lenPack*intNumPackets - lenPack*numPackets;
    outVector = [inVector; zeros(nZeros,1)];
else
    outVector = inVector;
end

clear inVector numPackets;

% shape the data to show packets in lines
outVector = (reshape(outVector, lenPack, intNumPackets))';

%% Find vertices for patches
outVector = flipud(outVector);
[y,x]=find(outVector == 1);
x = x'; y = y';
xpatch = [x-1; x ; x; x-1];
ypatch = [y-1; y-1; y; y];

%% Plot patches
f = figure(108);
ha = findobj(f,'Tag','FigAx');

n = findobj(f,'Type','patch');
delete(n);

h = patch(xpatch,ypatch,'k');

set(h,'Parent', ha);
set(ha,'XTick', []);
set(ha,'YTick', []);
% axis(ha, 'tight');
set(ha,'visible','on');
set(f,'visible','on');

%set(get(ha,'Title'),'String','')
title(['Bit Packet Analysis, Packet Length = ', ...
    num2str(lenPack), '# of Packets = ', num2str(intNumPackets)]);

pause(3)

Contact us