diff --git a/NanoVNASaver/RFTools.py b/NanoVNASaver/RFTools.py index 6debc82..c69086e 100644 --- a/NanoVNASaver/RFTools.py +++ b/NanoVNASaver/RFTools.py @@ -16,9 +16,9 @@ # along with this program. If not, see . import math import cmath -from numbers import Number, Real +from numbers import Number from typing import List, NamedTuple -from NanoVNASaver.SITools import Value, Format +from NanoVNASaver.SITools import Value, Format, clamp_value FMT_FREQ = Format() FMT_SHORT = Format(max_nr_digits=4) @@ -123,15 +123,6 @@ class Datapoint(NamedTuple): self.impedance(ref_impedance), self.freq) -def clamp_value(value: Real, rmin: Real, rmax: Real) -> Real: - assert rmin <= rmax - if value < rmin: - return rmin - if value > rmax: - return rmax - return value - - def groupDelay(data: List[Datapoint], index: int) -> float: idx0 = clamp_value(index - 1, 0, len(data) - 1) idx1 = clamp_value(index + 1, 0, len(data) - 1) diff --git a/NanoVNASaver/SITools.py b/NanoVNASaver/SITools.py index cbc198d..d442be7 100644 --- a/NanoVNASaver/SITools.py +++ b/NanoVNASaver/SITools.py @@ -3,7 +3,7 @@ # Copyright (C) 2019. Rune B. Broberg # # This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by +# it under the terms of the GNU General Public License as published bynanovna # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # @@ -18,12 +18,21 @@ from __future__ import annotations import math import decimal from typing import NamedTuple, Union -from numbers import Number +from numbers import Number, Real PREFIXES = ("y", "z", "a", "f", "p", "n", "ยต", "m", "", "k", "M", "G", "T", "P", "E", "Z", "Y") +def clamp_value(value: Real, rmin: Real, rmax: Real) -> Real: + assert rmin <= rmax + if value < rmin: + return rmin + if value > rmax: + return rmax + return value + + class Format(NamedTuple): max_nr_digits: int = 6 fix_decimals: bool = False @@ -144,12 +153,9 @@ class Value: decimal.Decimal(factor, context=Value.CTX)) except decimal.InvalidOperation: raise ValueError - # TODO: get formating out of RFTools to be able to import clamp - # and reuse code - if self._value < self.fmt.parse_clamp_min: - self._value = self.fmt.parse_clamp_min - elif self._value > self.fmt.parse_clamp_max: - self._value = self.fmt.parse_clamp_max + self._value = clamp_value(self._value, + self.fmt.parse_clamp_min, + self.fmt.parse_clamp_max) return self @property