Why does simulink generate warnings about quantization error when running my simulation?

30 views (last 30 days)
In simulink I am making a filter using single precision coefficients. When I simulate the design I receive the following warning:
"The parameter's value cannot be represented exactly using the run-time data type. A small quantization error has occurred."
I am entering the coefficients into gain blocks, with the data type set to single precision. I believe that everything in my design is set to single precision. Is there a setting that I am missing that is causing the quantization error?

Answers (1)

Andy Bartlett
Andy Bartlett on 9 Feb 2018
Hi, I assume the coefficients have been entered using with MATLAB code like this
coef = [0.05 0.15]
Since MATLAB defaults to double, these coefficients will be the most accurate representation of 0.05 and 0.15 that a double can hold. To see how close, you could do
mat2str(coef,18)
ans =
'[0.0500000000000000028 0.149999999999999994]'
If you've constructed your model to use coef with single precision run-time parameters, then the original doubles will need to be quantized to singles. Under the hood, the Simulink model is doing the equivalent of
runTimeCoef = single(coef)
To see what this produces, you could do
mat2str(runTimeCoef,18)
ans =
'[0.0500000007450580597 0.150000005960464478]'
As you can see, the before and after values are difference.
'[0.0500000000000000028 0.149999999999999994]'
'[0.0500000007450580597 0.150000005960464478]'
Hence, the warning about precision loss.
One way to avoid this is to pre-quantized the coefficients before supplying them to Simulink
coef = single([0.05 0.15])
The parameter value used by the Simulink model will still be identical
'[0.0500000007450580597 0.150000005960464478]'
but the warning would go away.
As of R2016b, you can also suppress individual warnings after you've reviewed them and determined the warning to be benign.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!