Skip to content

Commit

Permalink
Fix frequency calculation at logscale sweeps (#701) (#705)
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter3579 authored Jun 21, 2024
1 parent bdaddc3 commit a2f9044
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ Contributors
* sysjoint-tek <[email protected]>
* Thomas de Lellis <[email protected]>
* zstadler <[email protected]>
* Peter Hackenberg <[email protected]>
10 changes: 5 additions & 5 deletions src/NanoVNASaver/Settings/Sweep.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import logging
from enum import Enum
from math import log
from math import exp, log
from threading import Lock
from typing import Iterator, NamedTuple

Expand Down Expand Up @@ -145,12 +145,12 @@ def check(self):
raise ValueError(f"Illegal sweep settings: {self}")

def _exp_factor(self, index: int) -> float:
return 1 - log(self.segments + 1 - index) / log(self.segments + 1)
return exp(log((self.start + self.span)/self.start) / self.segments * index)

def get_index_range(self, index: int) -> tuple[int, int]:
if self.properties.logarithmic:
start = round(self.start + self.span * self._exp_factor(index))
end = round(self.start + self.span * self._exp_factor(index + 1))
start = round(self.start * self._exp_factor(index))
end = round(self.start * self._exp_factor(index + 1))
else:
start = self.start + index * self.points * self.stepsize
end = start + (self.points - 1) * self.stepsize
Expand All @@ -160,7 +160,7 @@ def get_index_range(self, index: int) -> tuple[int, int]:
def get_frequencies(self) -> Iterator[int]:
for i in range(self.segments):
start, stop = self.get_index_range(i)
step = (stop - start) / self.points
step = (stop - start) / (self.points - 1)
freq = start
for _ in range(self.points):
yield round(freq)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_sweep.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ def test_sweep(self):
self.assertEqual(sweep.get_index_range(1), (12429117, 21170817))
data = list(sweep.get_frequencies())
self.assertEqual(data[0], 3600000)
self.assertEqual(data[-1], 29913383)
self.assertEqual(data[-1], 29999934) # should be close to 30000000
sweep = Sweep(segments=3, properties=Properties(logarithmic=True))
self.assertEqual(sweep.get_index_range(1), (9078495, 16800000))
self.assertEqual(sweep.get_index_range(1), (7298642, 14797272))
data = list(sweep.get_frequencies())
self.assertEqual(data[0], 3600000)
self.assertEqual(data[-1], 29869307)
self.assertEqual(data[-1], 30000000)

sweep2 = sweep.copy()
self.assertEqual(sweep, sweep2)
Expand Down

0 comments on commit a2f9044

Please sign in to comment.