Model a Communications Protocol by Using Chart Objects

A standalone Stateflow® chart is a MATLAB® class that defines the behavior of a finite state machine. 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

This example shows how to use a standalone Stateflow chart to model a frame-synchronization and symbol-detection component in a communications system. The input to the 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 State Decomposition.

To track the length of a pulse through several execution steps, the chart uses the count 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.

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:
sf_frame_Tester
..................................................
...........................
frame
.................
A
...............................................
B
.....................................
error
...............................................
B
.................
A..................................................
...........................
frame

See Also

Related Topics