Skip to content

Commit a713a85

Browse files
committed
Improve zero crossing detection
1 parent 6ce399a commit a713a85

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

tests/test_oscilloscope.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
FREQUENCY = 1000
1616
MICROSECONDS = 1e-6
17-
ABSTOL = 2 * (16.5 - (-16.5)) / (2 ** 10 - 1) # Two times CH1 resolution.
17+
ABSTOL = 4 * (16.5 - (-16.5)) / (2 ** 10 - 1) # Four times lowest CH1/CH2 resolution.
1818

1919

2020
@pytest.fixture
@@ -32,16 +32,22 @@ def scope(handler):
3232
return oscilloscope.Oscilloscope(handler)
3333

3434

35-
def count_zero_crossings(y):
35+
def count_zero_crossings(x, y):
36+
sample_rate = (np.diff(x)[0] * MICROSECONDS) ** -1
37+
samples_per_period = sample_rate / FREQUENCY
3638
zero_crossings = np.where(np.diff(np.sign(y)))[0]
37-
real_crossings = np.where(~(np.diff(zero_crossings) == 1))
39+
real_crossings = np.where(np.diff(zero_crossings) > samples_per_period * 0.01)
3840
real_crossings = np.append(real_crossings, True)
39-
zero_crossings = zero_crossings[real_crossings]
40-
return len(zero_crossings)
4141

42+
if len(real_crossings) % 1:
43+
if y[0] * y[-1] <= 0:
44+
return len(real_crossings) + 1
4245

43-
def verify_periods(y, channel, periods=1):
44-
zero_crossings = count_zero_crossings(y)
46+
return len(real_crossings)
47+
48+
49+
def verify_periods(x, y, channel, periods=1):
50+
zero_crossings = count_zero_crossings(x, y)
4551
assert zero_crossings == 2 * periods
4652
assert y[0] == pytest.approx(y[-1], abs=ABSTOL)
4753

@@ -55,8 +61,8 @@ def test_capture_one_12bit(scope):
5561

5662

5763
def test_capture_one_high_speed(scope):
58-
_, y = scope.capture(channels=1, samples=2000, timegap=0.5)
59-
verify_periods(y, scope._channels["CH1"])
64+
x, y = scope.capture(channels=1, samples=2000, timegap=0.5)
65+
verify_periods(x, y, scope._channels["CH1"])
6066

6167

6268
def test_capture_one_trigger(scope):
@@ -66,16 +72,16 @@ def test_capture_one_trigger(scope):
6672

6773

6874
def test_capture_two(scope):
69-
_, y1, y2 = scope.capture(channels=2, samples=500, timegap=2)
70-
verify_periods(y1, scope._channels["CH1"])
71-
verify_periods(y2, scope._channels["CH2"])
75+
x, y1, y2 = scope.capture(channels=2, samples=500, timegap=2)
76+
verify_periods(x, y1, scope._channels["CH1"])
77+
verify_periods(x, y2, scope._channels["CH2"])
7278

7379

7480
def test_capture_four(scope):
75-
_, y1, y2, y3, _ = scope.capture(channels=4, samples=500, timegap=2)
76-
verify_periods(y1, scope._channels["CH1"])
77-
verify_periods(y2, scope._channels["CH2"])
78-
verify_periods(y3, scope._channels["CH3"])
81+
x, y1, y2, y3, _ = scope.capture(channels=4, samples=500, timegap=2)
82+
verify_periods(x, y1, scope._channels["CH1"])
83+
verify_periods(x, y2, scope._channels["CH2"])
84+
verify_periods(x, y3, scope._channels["CH3"])
7985

8086

8187
def test_capture_invalid_channel_one(scope):

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy