Skip to content

Commit 38769de

Browse files
committed
Add check_1d_arrays decorator to various signal processing functions for input validation
1 parent 47b2934 commit 38769de

File tree

6 files changed

+33
-0
lines changed

6 files changed

+33
-0
lines changed

sigima/tools/signal/dynamic.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import numpy as np
1616
import scipy.optimize
1717

18+
from sigima.tools.checks import check_1d_arrays
19+
1820

1921
def sinusoidal_model(
2022
x: np.ndarray, a: float, f: float, phi: float, offset: float
@@ -23,6 +25,7 @@ def sinusoidal_model(
2325
return a * np.sin(2 * np.pi * f * x + phi) + offset
2426

2527

28+
@check_1d_arrays(x_evenly_spaced=True, x_sorted=True)
2629
def sinusoidal_fit(
2730
x: np.ndarray, y: np.ndarray
2831
) -> tuple[tuple[float, float, float, float], float]:
@@ -63,6 +66,7 @@ def optfunc(fitparams: np.ndarray, x: np.ndarray, y: np.ndarray) -> np.ndarray:
6366
return fitparams, residuals
6467

6568

69+
@check_1d_arrays(x_evenly_spaced=True, x_sorted=True)
6670
def sinus_frequency(x: np.ndarray, y: np.ndarray) -> float:
6771
"""Compute the frequency of a sinusoidal signal.
6872
@@ -77,6 +81,7 @@ def sinus_frequency(x: np.ndarray, y: np.ndarray) -> float:
7781
return fitparams[1]
7882

7983

84+
@check_1d_arrays(x_evenly_spaced=True, x_sorted=True)
8085
def enob(x: np.ndarray, y: np.ndarray, full_scale: float = 1.0) -> float:
8186
"""Compute Effective Number of Bits (ENOB).
8287
@@ -92,6 +97,7 @@ def enob(x: np.ndarray, y: np.ndarray, full_scale: float = 1.0) -> float:
9297
return -np.log2(residuals * np.sqrt(12) / full_scale)
9398

9499

100+
@check_1d_arrays(x_evenly_spaced=True, x_sorted=True)
95101
def sinad(
96102
x: np.ndarray,
97103
y: np.ndarray,
@@ -119,6 +125,7 @@ def sinad(
119125
return 20 * np.log10(powf / residuals)
120126

121127

128+
@check_1d_arrays(x_evenly_spaced=True, x_sorted=True)
122129
def thd(
123130
x: np.ndarray,
124131
y: np.ndarray,
@@ -159,6 +166,7 @@ def thd(
159166
return 20 * np.log10(sumharm / powfund)
160167

161168

169+
@check_1d_arrays(x_evenly_spaced=True, x_sorted=True)
162170
def sfdr(
163171
x: np.ndarray,
164172
y: np.ndarray,
@@ -189,6 +197,7 @@ def sfdr(
189197
return 20 * np.log10(powfund / maxspike)
190198

191199

200+
@check_1d_arrays(x_evenly_spaced=True, x_sorted=True)
192201
def snr(
193202
x: np.ndarray,
194203
y: np.ndarray,

sigima/tools/signal/features.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
import numpy as np
1010

11+
from sigima.tools.checks import check_1d_arrays
12+
1113

1214
def find_zero_crossings(y: np.ndarray) -> np.ndarray:
1315
"""
@@ -23,6 +25,7 @@ def find_zero_crossings(y: np.ndarray) -> np.ndarray:
2325
return zero_crossing_indices
2426

2527

28+
@check_1d_arrays(x_sorted=True)
2629
def find_first_x_at_y_value(x: np.ndarray, y: np.ndarray, y_value: float) -> float:
2730
"""Find the first x value where the signal reaches a given y value (interpolated).
2831
@@ -52,6 +55,7 @@ def find_first_x_at_y_value(x: np.ndarray, y: np.ndarray, y_value: float) -> flo
5255
return np.nan # not found
5356

5457

58+
@check_1d_arrays(x_sorted=True)
5559
def find_y_at_x_value(x: np.ndarray, y: np.ndarray, x_value: float) -> float:
5660
"""Find the y value at a given x value using linear interpolation.
5761
@@ -77,6 +81,7 @@ def find_y_at_x_value(x: np.ndarray, y: np.ndarray, x_value: float) -> float:
7781
return float(np.interp(x_value, x_valid, y_valid))
7882

7983

84+
@check_1d_arrays(x_sorted=True)
8085
def find_x_at_value(x: np.ndarray, y: np.ndarray, value: float) -> np.ndarray:
8186
"""Find the x values where the y value is the closest to the given value using
8287
linear interpolation to deduce the precise x value.

sigima/tools/signal/interpolation.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
import numpy as np
1313
import scipy.interpolate
1414

15+
from sigima.tools.checks import check_1d_arrays
1516

17+
18+
@check_1d_arrays(x_sorted=True)
1619
def interpolate(
1720
x: np.ndarray,
1821
y: np.ndarray,

sigima/tools/signal/peakdetection.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
import numpy as np
1111

12+
from sigima.tools.checks import check_1d_arrays
13+
1214

1315
def peak_indices(
1416
y, thres: float = 0.3, min_dist: int = 1, thres_abs: bool = False
@@ -107,6 +109,7 @@ def peak_indices(
107109
return peaks
108110

109111

112+
@check_1d_arrays
110113
def xpeak(x: np.ndarray, y: np.ndarray) -> float:
111114
"""Return default peak X-position (assuming a single peak).
112115

sigima/tools/signal/pulse.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
import numpy as np
1515
import scipy.optimize
1616

17+
from sigima.tools.checks import check_1d_arrays
1718
from sigima.tools.signal import features, fitmodels, peakdetection
1819

1920
# MARK: Pulse analysis -----------------------------------------------------------------
2021

2122

23+
@check_1d_arrays(x_sorted=True)
2224
def full_width_at_y(
2325
data: np.ndarray, level: float
2426
) -> tuple[float, float, float, float]:
@@ -38,6 +40,7 @@ def full_width_at_y(
3840
return crossings[0], level, crossings[-1], level
3941

4042

43+
@check_1d_arrays(x_sorted=True)
4144
def fwhm(
4245
data: np.ndarray,
4346
method: Literal["zero-crossing", "gauss", "lorentz", "voigt"] = "zero-crossing",
@@ -101,6 +104,7 @@ def func(params):
101104
return fit_model_class.half_max_segment(amp, sigma, mu, base)
102105

103106

107+
@check_1d_arrays(x_sorted=True)
104108
def fw1e2(data: np.ndarray) -> tuple[float, float, float, float]:
105109
"""Compute Full Width at 1/e² of the input data (using a Gaussian model fitting).
106110

sigima/tools/signal/stability.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99

1010
import numpy as np
1111

12+
from sigima.tools.checks import check_1d_arrays
1213

14+
15+
@check_1d_arrays(x_evenly_spaced=True)
1316
def allan_variance(x: np.ndarray, y: np.ndarray, tau_values: np.ndarray) -> np.ndarray:
1417
"""
1518
Calculate the Allan variance for given time and measurement values at specified
@@ -63,6 +66,7 @@ def allan_variance(x: np.ndarray, y: np.ndarray, tau_values: np.ndarray) -> np.n
6366
return np.array(allan_var)
6467

6568

69+
@check_1d_arrays(x_evenly_spaced=True)
6670
def allan_deviation(x: np.ndarray, y: np.ndarray, tau_values: np.ndarray) -> np.ndarray:
6771
"""
6872
Calculate the Allan deviation for given time and measurement values at specified
@@ -79,6 +83,7 @@ def allan_deviation(x: np.ndarray, y: np.ndarray, tau_values: np.ndarray) -> np.
7983
return np.sqrt(allan_variance(x, y, tau_values))
8084

8185

86+
@check_1d_arrays(x_evenly_spaced=True)
8287
def overlapping_allan_variance(
8388
x: np.ndarray, y: np.ndarray, tau_values: np.ndarray
8489
) -> np.ndarray:
@@ -117,6 +122,7 @@ def overlapping_allan_variance(
117122
return np.array(overlapping_var)
118123

119124

125+
@check_1d_arrays(x_evenly_spaced=True)
120126
def modified_allan_variance(
121127
x: np.ndarray, y: np.ndarray, tau_values: np.ndarray
122128
) -> np.ndarray:
@@ -157,6 +163,7 @@ def modified_allan_variance(
157163
return np.array(mod_allan_var)
158164

159165

166+
@check_1d_arrays(x_evenly_spaced=True)
160167
def hadamard_variance(
161168
x: np.ndarray, y: np.ndarray, tau_values: np.ndarray
162169
) -> np.ndarray:
@@ -195,6 +202,7 @@ def hadamard_variance(
195202
return np.array(hadamard_var)
196203

197204

205+
@check_1d_arrays(x_evenly_spaced=True)
198206
def total_variance(x: np.ndarray, y: np.ndarray, tau_values: np.ndarray) -> np.ndarray:
199207
"""
200208
Calculate the Total variance for given time and measurement values.
@@ -233,6 +241,7 @@ def total_variance(x: np.ndarray, y: np.ndarray, tau_values: np.ndarray) -> np.n
233241
return np.array(total_var)
234242

235243

244+
@check_1d_arrays(x_evenly_spaced=True)
236245
def time_deviation(x: np.ndarray, y: np.ndarray, tau_values: np.ndarray) -> np.ndarray:
237246
"""
238247
Calculate the Time Deviation (TDEV) for given time and measurement values.

0 commit comments

Comments
 (0)