Signal Processing
This submodule contains lots of underlying signal processing functionality used by the higher-level modules. This includes filtering routines, noise injection, and transformations.
physiokit.signal.filter
filter_signal(data, lowcut=None, highcut=None, sample_rate=1000, order=2, axis=-1, forward_backward=True)
Apply SOS filter to signal using butterworth design and cascaded filter.
Parameters:
-
data
(NDArray
) –Signal
-
lowcut
(float | None
, default:None
) –Lower cutoff in Hz. Defaults to None.
-
highcut
(float | None
, default:None
) –Upper cutoff in Hz. Defaults to None.
-
sample_rate
(float
, default:1000
) –Sampling rate in Hz Defaults to 1000 Hz.
-
order
(int
, default:2
) –Filter order. Defaults to 2.
-
forward_backward
(bool
, default:True
) –Apply filter forward and backwards. Defaults to True.
Returns:
-
NDArray
–npt.NDArray: Filtered signal
Source code in physiokit/signal/filter.py
generate_arm_biquad_sos(lowcut, highcut, sample_rate, order=3, var_name='biquadFilter')
Generate ARM CMSIS second order section coefficients.
Parameters:
-
lowcut
(float
) –Lower cutoff in Hz.
-
highcut
(float
) –Upper cutoff in Hz.
-
sample_rate
(float
) –Sampling rate in Hz.
-
order
(int
, default:3
) –Filter order. Defaults to 3.
-
var_name
(str
, default:'biquadFilter'
) –Variable name. Defaults to 'biquadFilter'.
Returns:
-
str
(str
) –ARM CMSIS second order section coefficients.
Source code in physiokit/signal/filter.py
get_butter_sos(lowcut=None, highcut=None, sample_rate=1000, order=3)
cached
Compute biquad filter coefficients as SOS. This function caches. For lowpass, lowcut is required and highcut is ignored. For highpass, highcut is required and lowcut is ignored. For bandpass, both lowcut and highcut are required.
Parameters:
-
lowcut
(float | None
, default:None
) –Lower cutoff in Hz. Defaults to None.
-
highcut
(float | None
, default:None
) –Upper cutoff in Hz. Defaults to None.
-
sample_rate
(float
, default:1000
) –Sampling rate in Hz. Defaults to 1000 Hz.
-
order
(int
, default:3
) –Filter order. Defaults to 3.
Returns:
-
NDArray
–npt.NDArray: SOS
Source code in physiokit/signal/filter.py
moving_gradient_filter(data, sample_rate=1000, sig_window=0.1, avg_window=1.0, sig_prom_weight=1.5, mode='nearest', fval=0)
Compute moving gradient filter to identify peaks in stream of data.
Parameters:
-
data
(array
) –Data stream.
-
sample_rate
(float
, default:1000
) –Sampling rate in Hz. Defaults to 1000 Hz.
-
sig_window
(float
, default:0.1
) –Window size in seconds to compute signal gradient. Defaults to 0.1 s.
-
avg_window
(float
, default:1.0
) –Window size in seconds to compute average gradient. Defaults to 1.0 s.
-
sig_prom_weight
(float
, default:1.5
) –Weight to compute minimum signal height. Defaults to 1.5.
Returns:
-
array
(NDArray
) –Moving gradient filter.
Source code in physiokit/signal/filter.py
normalize_signal(data, eps=0.001, axis=-1)
Normalize signal about its mean and std.
Parameters:
-
data
(NDArray
) –Signal
-
eps
(float
, default:0.001
) –Epsilon added to st. dev. Defaults to 1e-3.
-
axis
(int
, default:-1
) –Axis to normalize along. Defaults to -1.
Returns:
-
NDArray
–npt.NDArray: Normalized signal
Source code in physiokit/signal/filter.py
quotient_filter_mask(data, mask=None, iterations=2, lowcut=0.8, highcut=1.2)
Applies a quotient filter to identify outliers from list.
Parameters:
-
data
(NDArray
) –Signal
-
mask
(NDArray | None
, default:None
) –Rejection mask. Defaults to None.
-
iterations
(int
, default:2
) –iterations to apply. Defaults to 2.
-
lowcut
(float
, default:0.8
) –Lower cutoff ratio. Defaults to 0.8.
-
highcut
(float
, default:1.2
) –Upper cutoff ratio. Defaults to 1.2.
Returns:
-
NDArray
–npt.NDArray: Rejection mask 0=accept, 1=reject.
Source code in physiokit/signal/filter.py
remove_baseline_wander(data, cutoff=0.05, quality=0.005, sample_rate=1000, axis=-1, forward_backward=True)
Remove baseline wander from signal using a notch filter.
Parameters:
-
data
(NDArray
) –Signal
-
cutoff
(float
, default:0.05
) –Cutoff frequency in Hz. Defaults to 0.05.
-
quality
(float
, default:0.005
) –Quality factor. Defaults to 0.005.
-
sample_rate
(float
, default:1000
) –Sampling rate in Hz. Defaults to 1000 Hz.
-
axis
(int
, default:-1
) –Axis to filter along. Defaults to 0.
-
forward_backward
(bool
, default:True
) –Apply filter forward and backwards. Defaults to True.
Returns:
-
NDArray
–npt.NDArray: Filtered signal
Source code in physiokit/signal/filter.py
resample_categorical(data, sample_rate, target_rate, axis=0)
Resample categorical data using nearest neighbor.
Parameters:
-
data
(NDArray
) –Signal
-
sample_rate
(float
) –Signal sampling rate
-
target_rate
(float
) –Target sampling rate
-
axis
(int
, default:0
) –Axis to resample along. Defaults to 0.
Returns:
-
NDArray
–npt.NDArray: Resampled signal
Source code in physiokit/signal/filter.py
resample_signal(data, sample_rate=1000, target_rate=500, axis=-1)
Resample signal using scipy FFT-based resample routine.
NOTE: For very large signals, this may be slow. Consider using resample_poly instead.
Parameters:
-
data
(NDArray
) –Signal
-
sample_rate
(float
, default:1000
) –Signal sampling rate. Defaults to 1000 Hz.
-
target_rate
(float
, default:500
) –Target sampling rate. Defaults to 500 Hz.
-
axis
(int
, default:-1
) –Axis to resample along. Defaults to -1.
Returns:
-
NDArray
–npt.NDArray: Resampled signal
Source code in physiokit/signal/filter.py
physiokit.signal.noise
Add various noise sources to signal.
add_baseline_wander(data, amplitude=0.1, frequency=0.05, sample_rate=1000, signal_sd=None)
Add baseline wander to signal.
Parameters:
-
data
(NDArray
) –Signal
-
amplitude
(float
, default:0.1
) –Amplitude in st dev. Defaults to 0.1.
-
frequency
(float
, default:0.05
) –Baseline wander frequency. Defaults to 0.05 Hz.
-
sample_rate
(float
, default:1000
) –Sample rate in Hz. Defaults to 1000 Hz.
-
signal_sd
(float | None
, default:None
) –Signal standard deviation. Defaults to None.
Returns:
-
NDArray
–npt.NDArray: Signal w/ baseline wander
Source code in physiokit/signal/noise.py
add_burst_noise(data, amplitude=1, frequency=100, num_bursts=1, sample_rate=1000, signal_sd=None)
Add high frequency burst noise to signal.
Parameters:
-
data
(NDArray
) –Signal
-
amplitude
(float
, default:1
) –Amplitude in st dev. Defaults to 1.
-
frequency
(float
, default:100
) –High frequency burst in Hz. Defaults to 100 Hz.
-
num_bursts
(int
, default:1
) –bursts to inject. Defaults to 1.
-
sample_rate
(float
, default:1000
) –Sample rate in Hz. Defaults to 1000 Hz.
-
signal_sd
(float | None
, default:None
) –Signal standard deviation. Defaults to None.
Returns:
-
NDArray
–npt.NDArray: Signal w/ burst noise
Source code in physiokit/signal/noise.py
add_emg_noise(data, scale=1e-05, sample_rate=1000)
Add EMG noise to signal.
Parameters:
-
data
(NDArray
) –Signal
-
scale
(float
, default:1e-05
) –Noise scale. Defaults to 1e-5.
-
sample_rate
(float
, default:1000
) –Sampling rate in Hz. Defaults to 1000.
Returns:
-
NDArray
–npt.NDArray: Signal with EMG noise
Source code in physiokit/signal/noise.py
add_lead_noise(data, scale=0.001)
Add lead noise to signal.
Parameters:
-
data
(NDArray
) –Signal
-
scale
(float
, default:0.001
) –Noise scale. Defaults to 1.
Returns:
-
NDArray
–npt.NDArray: Signal with lead noise
Source code in physiokit/signal/noise.py
add_motion_noise(data, amplitude=0.2, frequency=0.5, sample_rate=1000, signal_sd=None)
Add motion noise to signal.
Parameters:
-
data
(NDArray
) –Signal
-
amplitude
(float
, default:0.2
) –Amplitude in st dev. Defaults to 0.2.
-
frequency
(float
, default:0.5
) –Motion frequency in Hz. Defaults to 0.5 Hz.
-
sample_rate
(float
, default:1000
) –Sample rate in Hz. Defaults to 1000 Hz.
-
signal_sd
(float | None
, default:None
) –Signal standard deviation. Defaults to None.
Returns:
-
NDArray
–npt.NDArray: Signal w/ motion noise
Source code in physiokit/signal/noise.py
add_noise_sources(data, amplitudes, frequencies, noise_shapes, sample_rate=1000, signal_sd=None)
Add multiple noise sources to signal.
Parameters:
-
data
(NDArray
) –Signal
-
amplitudes
(list[float]
) –Amplitudes in st dev.
-
frequencies
(list[float]
) –Frequencies in Hz.
-
noise_shapes
(list[str]
) –Noise shapes.
-
sample_rate
(float
, default:1000
) –Sample rate in Hz. Defaults to 1000 Hz.
-
signal_sd
(float | None
, default:None
) –Signal standard deviation. Defaults to None.
Returns:
-
NDArray
–npt.NDArray: Signal w/ noise
Source code in physiokit/signal/noise.py
add_powerline_noise(data, amplitude=0.01, frequency=50, sample_rate=1000, signal_sd=None)
Add powerline noise to signal.
Parameters:
-
data
(NDArray
) –Signal
-
amplitude
(float
, default:0.01
) –Amplitude in st dev. Defaults to 0.01.
-
frequency
(float
, default:50
) –Powerline frequency in Hz. Defaults to 50 Hz.
-
sample_rate
(float
, default:1000
) –Sample rate in Hz. Defaults to 1000 Hz.
-
signal_sd
(float | None
, default:None
) –Signal standard deviation. Defaults to None.
Returns:
-
NDArray
–npt.NDArray: Signal w/ powerline noise
Source code in physiokit/signal/noise.py
add_random_scaling(data, lower=0.5, upper=2.0)
Randomly scale signal.
Parameters:
-
data
(NDArray
) –Signal
-
lower
(float
, default:0.5
) –Lower bound. Defaults to 0.5.
-
upper
(float
, default:2.0
) –Upper bound. Defaults to 2.0.
Returns:
-
NDArray
–npt.NDArray: Signal with random scaling
Source code in physiokit/signal/noise.py
add_signal_attenuation()
add_signal_cutout()
add_signal_shift(data, shift_amount=0.1)
Add signal shift augmentation to signal.
physiokit.signal.transform
compute_fft(data, sample_rate=1000, fft_len=None, window='blackman', axis=-1)
Compute FFT of RSP signal.
Parameters:
-
data
(array
) –RSP signal.
-
sample_rate
(float
, default:1000
) –Sampling rate in Hz. Defaults to 1000 Hz.
-
fft_len
(int | None
, default:None
) –FFT length. Defaults to None.
-
window
(str
, default:'blackman'
) –Window to apply. Defaults to 'blackman'.
-
axis
(int
, default:-1
) –Axis to compute FFT. Defaults to -1.
Returns:
-
tuple[NDArray, NDArray]
–tuple[array, array]: Frequencies and FFT of signal.
Source code in physiokit/signal/transform.py
rescale_signal(x, old_min, old_max, new_min, new_max, clip=True)
Rescale signal to new range.
Parameters:
-
x
(NDArray
) –Signal
-
old_min
(float
) –Old minimum
-
old_max
(float
) –Old maximum
-
new_min
(float
) –New minimum
-
new_max
(float
) –New maximum
-
clip
(bool
, default:True
) –Clip values to range. Defaults to True.
Returns:
-
NDArray
–npt.NDArray: Rescaled signal