Utilities
Signal conditioning and scalar metrics.
Conditioning
dspkit.utils.detrend(x, order=1)
Remove a polynomial trend from a signal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
(array_like, shape(N))
|
Input signal. |
required |
order
|
int
|
Polynomial order.
|
1
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Detrended signal with the same shape as |
Source code in dspkit/utils.py
dspkit.utils.integrate(x, fs, detrend_after=True, detrend_order=1)
Cumulative time integration using the trapezoidal rule.
Typical use: acceleration → velocity, velocity → displacement.
Real sensor signals contain a small DC bias that grows unboundedly
when integrated. detrend_after=True (default) removes a linear
trend from the result, which suppresses this drift while preserving
the physically meaningful AC content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
(array_like, shape(N))
|
Input signal. |
required |
fs
|
float
|
Sampling frequency [Hz]. |
required |
detrend_after
|
bool
|
If |
True
|
detrend_order
|
int
|
Polynomial order for post-integration detrending (default 1 = linear). |
1
|
Returns:
| Type | Description |
|---|---|
(ndarray, shape(N))
|
Integrated signal. The first sample is set to zero (initial condition). |
Source code in dspkit/utils.py
dspkit.utils.differentiate(x, fs)
Numerical differentiation using central differences (numpy.gradient).
Uses second-order accurate central differences at interior points and first-order forward/backward differences at the edges. The output has the same length as the input.
Typical use: displacement → velocity, velocity → acceleration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
(array_like, shape(N))
|
Input signal. |
required |
fs
|
float
|
Sampling frequency [Hz]. |
required |
Returns:
| Type | Description |
|---|---|
(ndarray, shape(N))
|
Derivative, in units of [x_units * Hz]. |
Source code in dspkit/utils.py
Scalar metrics
dspkit.utils.rms(x)
Root mean square of a signal.
For a pure sine of amplitude A, RMS = A / sqrt(2).
dspkit.utils.peak(x)
dspkit.utils.crest_factor(x)
Crest factor: peak / RMS.
For a pure sine: sqrt(2) ~ 1.414. For white Gaussian noise: typically 3–4. High crest factor indicates impulsive content.