use math instead of table for log step calculation

pull/557/head
Holger Müller 2022-10-07 21:42:58 +02:00
rodzic 7b9dd5ab0a
commit d57ae78efa
3 zmienionych plików z 43 dodań i 20 usunięć

Wyświetl plik

@ -23,35 +23,29 @@ from typing import List
from PyQt5 import QtGui from PyQt5 import QtGui
from NanoVNASaver.RFTools import Datapoint
from NanoVNASaver.Charts.Chart import Chart from NanoVNASaver.Charts.Chart import Chart
from NanoVNASaver.Charts.Frequency import FrequencyChart from NanoVNASaver.Charts.Frequency import FrequencyChart
from NanoVNASaver.RFTools import Datapoint
from NanoVNASaver.SITools import log_floor_125
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@dataclass @dataclass
class TickVal: class TickVal:
count: int count: int = 0
first: float first: float = 0.0
step: float step: float = 0.0
def span2ticks(span: float, min_val: float) -> TickVal: def span2ticks(span: float, min_val: float) -> TickVal:
for spn, dbs in ((50.0, 10.0), logger.debug("span2ticks(%s, %s)", span, min_val)
(20.0, 5.0), span = abs(span)
(10.0, 2.0), step = log_floor_125(span / 5)
(5.0, 1.0), count = math.floor(span / step)
(2.0, 0.5), first = math.ceil(min_val / step) * step
(1.0, 0.2), if first == min_val:
(0.0, 0.1)): first += step
if span >= spn:
count = math.floor(span / dbs)
first = math.ceil(min_val / dbs) * dbs
step = dbs
if first == min_val:
first += dbs
break
return TickVal(count, first, step) return TickVal(count, first, step)

Wyświetl plik

@ -41,6 +41,16 @@ def round_floor(value: Real, digits: int = 0) -> Real:
return factor * math.floor(value / factor) return factor * math.floor(value / factor)
def log_floor_125(x: float) -> float:
log_base = 10**(math.floor(math.log10(x)))
log_factor = x / log_base
if log_factor >= 5:
return 5 * log_base
if log_factor >= 2:
return 2 * log_base
return log_base
class Format(NamedTuple): class Format(NamedTuple):
max_nr_digits: int = 6 max_nr_digits: int = 6
fix_decimals: bool = False fix_decimals: bool = False

Wyświetl plik

@ -21,7 +21,9 @@ from math import inf, nan
from decimal import Decimal # Needed for test_representation() from decimal import Decimal # Needed for test_representation()
# Import targets to be tested # Import targets to be tested
from NanoVNASaver.SITools import Format, Value, round_floor, round_ceil from NanoVNASaver.SITools import (
Format, Value, round_floor, round_ceil, log_floor_125
)
F_DEFAULT = Format() F_DEFAULT = Format()
@ -43,7 +45,7 @@ F_WITH_SPACE = Format(space_str=" ")
F_WITH_UNDERSCORE = Format(space_str="_") F_WITH_UNDERSCORE = Format(space_str="_")
class TestTSIToolsValue(unittest.TestCase): class TestSIToolsValue(unittest.TestCase):
def test_format_assertions(self): def test_format_assertions(self):
self.assertRaises(AssertionError, Value, fmt=F_ASSERT_DIGITS_0) self.assertRaises(AssertionError, Value, fmt=F_ASSERT_DIGITS_0)
@ -167,3 +169,20 @@ class TestTSIToolsValue(unittest.TestCase):
# TODO: test F_DIGITS_31 # TODO: test F_DIGITS_31
# F_WITH_SPACE # F_WITH_SPACE
# F_WITH_UNDERSCORE # F_WITH_UNDERSCORE
class TestSIToolsFunctions(unittest.TestCase):
def test_log_floor_125(self):
self.assertEqual(log_floor_125(1), 1)
self.assertEqual(log_floor_125(2), 2)
self.assertEqual(log_floor_125(5), 5)
self.assertEqual(log_floor_125(1000), 1000)
self.assertEqual(log_floor_125(2000), 2000)
self.assertEqual(log_floor_125(5000), 5000)
self.assertEqual(log_floor_125(.01), .01)
self.assertEqual(log_floor_125(.02), .02)
self.assertEqual(log_floor_125(.05), .05)
self.assertEqual(log_floor_125(1.9), 1)
self.assertEqual(log_floor_125(4.5), 2)
self.assertEqual(log_floor_125(9.9), 5)