Main Content

Why Does the Analyzer Choose the Wrong State Length?

If the state length of the algorithm depends on the inputs to the algorithm, make sure that you use inputs that choose the same state length when generating the MEX file and running the analyzer. Otherwise, the analyzer fails the verification.

The algorithm in the function FIR_Mean has no states when mean(input) > 0, and has states otherwise.

function [ Output ] = FIR_Mean( input )

persistent Filter
if isempty(Filter)
   Filter = dsp.FIRFilter('Numerator', fir1(12,0.4));

 if (mean(input) > 0)
     % stateless
     Output = mean(input);
     % this path contains states
    yFilt = Filter(input);
   Output = mean(yFilt);

When you invoke the automatic state length detection on this function, the analyzer detects a state length of 14 samples.

dspunfold FIR_Mean -args {randn(10,1)} -s auto -f true
Analyzing input MATLAB function FIR_Mean
Creating single-threaded MEX file FIR_Mean_st.mexw64
Searching for minimal state length (this might take a while)
Checking stateless ... Insufficient
Checking 10 ... Insufficient
Checking Infinite ... Sufficient
Checking 20 ... Sufficient
Checking 15 ... Sufficient
Checking 12 ... Insufficient
Checking 13 ... Insufficient
Checking 14 ... Sufficient
Minimal state length is 14
Creating multi-threaded MEX file FIR_Mean_mt.mexw64
Creating analyzer file FIR_Mean_analyzer

Run the analyzer function. Use an input with four different frames. Check if the output results match.

Analyzing multi-threaded MEX file FIR_Mean_mt.mexw64  ... 
Latency = 8 frames
Speedup = 0.5x
Warning: The output results of the multi-threaded MEX file FIR_Mean_mt.mexw64 do not match 
the output results of the single-threaded MEX file FIR_Mean_st.mexw64. Check that you 
provided the correct state length value to the dspunfold function when you generated the 
multi-threaded MEX file FIR_Mean_mt.mexw64. For best practices and possible solutions to 
this problem, see the 'Tips' section in the dspunfold function reference page. 
> In coder.internal.warning (line 8)
  In FIR_Mean_analyzer 

ans = 

    Latency: 8
    Speedup: 0.5040
       Pass: 0

Pass = 0, and the function throws a warning message indicating a possible reason for the verification failure.

Reason for Verification Failure

The state length of the algorithm depends on the input. When mean(input) > 0, the algorithm is stateless. Otherwise, the algorithm contains states. When generating the MEX file, the input arguments choose the code path with states. When the analyzer is called, the multi-frame input chooses the code path without states. Hence, the state length is different in both the cases leading to the verification failure.


The recommendation is to use inputs which choose the same state length when generating the MEX file and running the analyzer.

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

Related Topics