biquad_filter
Biquad Filter Layer API
This module provides classes to build biquad filter layers.
Functions:
-
get_butter_sos
–Compute biquad filter coefficients as SOS
Classes:
-
CascadedBiquadFilter
–Cascaded biquad filter layer
Classes
CascadedBiquadFilter
CascadedBiquadFilter(lowcut: float | None = None, highcut: float | None = None, sample_rate: float = 1000, order: int = 3, forward_backward: bool = False, **kwargs)
Implements a 2nd order cascaded biquad filter using direct form 1 structure.
See here for more information on the direct form 1 structure.
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.
-
forward_backward
(bool
, default:False
) –Apply filter forward and backward.
Example:
# Create sine wave at 10 Hz with 1000 Hz sampling rate
t = np.linspace(0, 1, 1000, endpoint=False)
x = np.sin(2 * np.pi * 10 * t)
# Add noise at 100 Hz and 2 Hz
x_noise = x + 0.5 * np.sin(2 * np.pi * 100 * t) + 0.5 * np.sin(2 * np.pi * 2 * t)
x_noise = x_noise.reshape(-1, 1).astype(np.float32)
x_noise = keras.ops.convert_to_tensor(x_noise)
# Create bandpass filter
lyr = nse.layers.preprocessing.CascadedBiquadFilter(lowcut=5, highcut=15, sample_rate=1000, forward_backward=True)
y = lyr(x_noise).numpy().squeeze()
x_noise = x_noise.numpy().squeeze()
plt.plot(x, label="Original")
plt.plot(x_noise, label="Noisy")
plt.plot(y, label="Filtered")
plt.legend()
plt.show()
Source code in neuralspot_edge/layers/preprocessing/biquad_filter.py
Functions
augment_sample
Applies the cascaded biquad filter to the input samples.
Source code in neuralspot_edge/layers/preprocessing/biquad_filter.py
Functions
get_butter_sos
get_butter_sos(lowcut: float | None = None, highcut: float | None = None, sample_rate: float = 1000, order: int = 3) -> npt.NDArray
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