Skip to content

Commit 4f69b36

Browse files
committed
Proposed change to get correct number of output levels, and handle non-symmetric limits.
Closes modelica#4459
1 parent 35ad28d commit 4f69b36

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

Modelica/Clocked/RealSignals/Sampler/Utilities/Internal/Quantization.mo

+6-3
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,22 @@ extends Clocked.RealSignals.Interfaces.PartialClockedSISO;
99
parameter Integer bits(min=1)=8
1010
"Number of bits of quantization (if quantized = true)";
1111
protected
12-
parameter Real resolution = if quantized then ((yMax - yMin)/2^bits) else 0;
12+
parameter Real resolution = if quantized then ((yMax - yMin)/(2^bits-1)) else 0;
1313
equation
1414

1515
if quantized then
16-
y = resolution*floor(abs(u/resolution) + 0.5)*
17-
(if u >= 0 then +1 else -1);
16+
y = resolution*floor(((u-yMin)/resolution) + 0.5)+yMin;
1817
else
1918
y = u;
2019
end if;
2120
annotation (Documentation(info="<html>
2221
<p>
2322
The clocked Real input signal is value discretized
2423
(the discretization is defined by parameter <strong>bits</strong>).
24+
25+
This is a mid-riser quantization, which for a symmetric interval imply that it will not output zero.
2526
</p>
27+
</html>", revisions="<html>
28+
<p>2024-09-04: Corrected off-by-one error in number of output levels, and handle non-symmetric limits.</p>
2629
</html>"));
2730
end Quantization;

0 commit comments

Comments
 (0)