Why Does the Analyzer Choose a Zero State Length?

When the output of the algorithm does not change for any input given to the algorithm, the analyzer considers the algorithm stateless, even if it contains states. Make sure the inputs to the algorithm have an immediate effect on the output of the algorithm.

The function Input_Output uses an FIR filter that contains states.

function [output] = Input_Output(input)

persistent Filter
if isempty(Filter)
   Filter = dsp.FIRFilter('Numerator', (1:12));

y = Filter(input);

output = any(y(:)>0);


When you call automatic state length detection on this function, the analyzer detects a minimal state length of 0.

dspunfold Input_Output -args {randn(10,1)} -s auto -f true
Analyzing input MATLAB function Input_Output
Creating single-threaded MEX file Input_Output_st.mexw64
Searching for minimal state length (this might take a while)
Checking stateless ... Sufficient
Minimal state length is 0
Creating multi-threaded MEX file Input_Output_mt.mexw64
Creating analyzer file Input_Output_analyzer

The analyzer detects a zero state length because the output of the function is the same irrespective of the value of the input. When the analyzer tests the algorithm with zero state length, the outputs of the multithreaded MEX and single-threaded MEX match. Therefore, the analyzer considers the algorithm stateless and sets the minimal state length to zero.


To prevent the analyzer from choosing the wrong state length, rewrite your algorithm so that inputs have an immediate effect on the output. Also, choose inputs which stress the code path with maximal state length.

For best practices, see the 'Tips' section of dspunfold.

Related Topics