main content

model a communications protocol by using chart objects -凯发k8网页登录

this example shows how to use a standalone stateflow® chart to model a frame-synchronization and symbol-detection component in a communications system. standalone charts implement classic chart semantics with matlab® as the action language. you can program the chart by using the full functionality of matlab, including those functions that are restricted for code generation in simulink®. for more information, see create stateflow charts for execution as matlab objects.

implement a symbol-detection algorithm

in this example, the input to the communications system consists of a binary signal of zeros and ones received every 10 milliseconds. the input signal can contain any combination of:

  • a 770-ms pulse (77 consecutive ones) to mark the beginning and end of a frame of data and to ensure system synchronization.

  • a 170-ms pulse (17 consecutive ones) to indicate symbol a.

  • a 470-ms pulse (47 consecutive ones) to indicate symbol b.

the file sf_frame_search.sfx defines a standalone stateflow chart that implements this communication protocol. the chart consists of two outer states in parallel decomposition. the initialize state resets the value of the local data symbol at the start of each execution step. the search state contains the logic that defines the symbol-detection algorithm. when this state detects one of the pulses allowed by the communication protocol, the name of the corresponding symbol is stored as symbol and displayed in the matlab command window. parallel decomposition enables the chart to preprocess the input data. for more information, see .

to track the length of a pulse through several execution steps, the chart uses the operator. this operator simplifies the design of the chart by eliminating the need for a manual counter. for example, the condition [count(pulse)==17] guards the outgoing transition from the substate newframe. this condition becomes true when the data pulse is one for 17 consecutive execution steps. in this case, the chart transitions to the couldbea substate. if this transition is followed by an input of zero, then the chart registers the reception of symbol a and transitions back to the newframe substate. otherwise, the chart transitions to the searchforb state from which the condition [count(pulse)==29] searches for an additional 29 ones to mark symbol b.

execute standalone chart

in the matlab script sf_frame_tester.m, the sample code generates a short signal consisting of several valid pulses and one transmission error. the error consists of a 470-ms pulse that is too long to represent symbol a and too short to represent symbol b.

%% test symbol detection algorithm
% generate a short signal consisting of several valid pulses and one
% transmission error.
f = sf_frame_search(pulse=0);        % create chart object
sendpulse(f,77);                     % frame marker
sendpulse(f,17);                     % a
sendpulse(f,47);                     % b
sendpulse(f,37);                     % transmission error
sendpulse(f,47);                     % b
sendpulse(f,17);                     % a
sendpulse(f,77);                     % frame marker
delete(f);                           % delete chart object
function sendpulse(f,n)
% send a pulse of n ones and one zero to chart object f.
for i = 1:n
    step(f,pulse=1);
    printdot(1)
end
printdot(0)
step(f,pulse=0);
    function printdot(x)
        persistent k
        if isempty(k)
            k = 1;
        end
        
        if x == 0
            fprintf("\n");
            k = 1;
        elseif k == 50
            fprintf(".\n");
            k = 1;
        else
            fprintf(".");
            k = k 1;
        end
    end
end

running the script produces these results in the matlab command window:

..................................................
...........................
frame
.................
a
...............................................
b
.....................................
error
...............................................
b
.................
a
..................................................
...........................
frame

during the simulation, the chart animation provides a visual indication of the runtime behavior of the algorithm.

see also

related topics

网站地图