Skip to content

Commit 7dc6d07

Browse files
committed
Improve sample frequency estimation
1 parent 20ed9b7 commit 7dc6d07

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

eitprocessing/datahandling/loading/draeger.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
from __future__ import annotations
22

3+
import math
34
import mmap
45
import sys
56
import warnings
67
from functools import partial
78
from typing import TYPE_CHECKING, NamedTuple
89

910
import numpy as np
11+
import scipy as sp
1012

1113
from eitprocessing.datahandling.continuousdata import ContinuousData
1214
from eitprocessing.datahandling.datacollection import DataCollection
@@ -24,6 +26,7 @@
2426

2527
load_draeger_data = partial(load_eit_data, vendor=Vendor.DRAEGER)
2628
NAN_VALUE_INDICATOR = -1e30
29+
SAMPLE_FREQUENCY_ESTIMATION_PRECISION = 4
2730

2831

2932
def load_from_single_path(
@@ -184,15 +187,27 @@ def load_from_single_path(
184187

185188
def _estimate_sample_frequency(time: np.ndarray, sample_frequency: float | None) -> float:
186189
"""Estimate the sample frequency from the time axis, and check with provided sample frequency."""
187-
estimated_sample_frequency = round((len(time) - 1) / (time[-1] - time[0]), 4)
190+
unrounded_estimated_sample_frequency = 1 / sp.stats.linregress(np.arange(len(time)), time).slope
191+
192+
# Rounds to the number of digits, rather than the number of decimals
193+
estimated_sample_frequency = round(
194+
unrounded_estimated_sample_frequency,
195+
-math.ceil(np.log10(abs(unrounded_estimated_sample_frequency))) + SAMPLE_FREQUENCY_ESTIMATION_PRECISION,
196+
)
188197

189198
if sample_frequency is None:
190199
return estimated_sample_frequency
191200

192-
if sample_frequency != estimated_sample_frequency:
201+
if not np.isclose(
202+
sample_frequency, unrounded_estimated_sample_frequency, rtol=10**-SAMPLE_FREQUENCY_ESTIMATION_PRECISION, atol=0
203+
):
193204
msg = (
194-
f"Provided sample frequency ({sample_frequency}) does not match "
195-
f"the estimated sample frequency ({estimated_sample_frequency})."
205+
"Provided sample frequency "
206+
f"({sample_frequency:.{SAMPLE_FREQUENCY_ESTIMATION_PRECISION + 2}f} Hz) "
207+
"does not match the estimated sample frequency "
208+
f"({unrounded_estimated_sample_frequency:.{SAMPLE_FREQUENCY_ESTIMATION_PRECISION + 2}f} Hz) "
209+
f"within {SAMPLE_FREQUENCY_ESTIMATION_PRECISION} digits. "
210+
"Note that the estimate might not be as accurate for very short signals."
196211
)
197212
warnings.warn(msg, RuntimeWarning, stacklevel=2)
198213

@@ -249,7 +264,7 @@ def _read_frame(
249264
index is non-negative. When the index is negative, no data is saved. In
250265
any case, the event marker is returned.
251266
"""
252-
frame_time = round(reader.float64() * 24 * 60 * 60, 3)
267+
frame_time = reader.float64() * 24 * 60 * 60
253268
_ = reader.float32()
254269
frame_pixel_impedance = reader.npfloat32(length=1024)
255270
frame_pixel_impedance = np.reshape(frame_pixel_impedance, (32, 32), "C")

0 commit comments

Comments
 (0)