prepare_digital_filter#
- gwpy.signal.filter_design.prepare_digital_filter(
- filt: FilterCompatible,
- *,
- analog: bool = False,
- sample_rate: QuantityLike = 1.0,
- unit: str | UnitBase = 'Hz',
- normalize_gain: bool = False,
- output: Literal['ba'] = 'zpk',
- gwpy.signal.filter_design.prepare_digital_filter(
- filt: FilterCompatible,
- *,
- analog: bool = False,
- sample_rate: QuantityLike = 1.0,
- unit: str | UnitBase = 'Hz',
- normalize_gain: bool = False,
- output: Literal['zpk'] = 'zpk',
- gwpy.signal.filter_design.prepare_digital_filter(
- filt: FilterCompatible,
- *,
- analog: bool = False,
- sample_rate: QuantityLike = 1.0,
- unit: str | UnitBase = 'Hz',
- normalize_gain: bool = False,
- output: Literal['sos'] = 'zpk',
Prepare a filter for digital filtering.
This function parses the input filter specification, optionally converts an analog filter to digital using prewarping + bilinear transform, and returns the filter in the requested format.
For incoming digital filters, this function basically does nothing.
- Parameters:
- filt
filterspecification Filter as
(b, a),(z, p, k), sos array, orltiobject.For digital filters (
analog=False), the input filter coefficients are assumed to already be in digital form (z-domain).For analog filters (
analog=True), the input filter coefficients should be in the units specified byunit(default: Hz).- analog
bool, optional When
True, the input filter is analog and will be converted to digital using prewarping + bilinear transform (GDS method). WhenFalse(default), the input filter is already digital.- sample_rate
float,Quantity, optional Sampling frequency (Hz) of the digital system. Required when
analog=True.- unit
str, optional For analogue ZPK filters, the units in which the zeros and poles are specified. Either
'Hz'or'rad/s'(default).- normalize_gain
bool, optional Whether to normalize the gain when converting from Hz to rad/s.
False(default): Multiply zeros/poles by -2π but leave gain unchanged. This matches the LIGO GDS ‘f’ plane convention (plane='f'ins2z()).True: Normalize gain to preserve frequency response magnitude. Gain is scaled by \(|∏p_i/∏z_i| · (2π)^{(n_p - n_z)}\). Use this when your filter was designed with the transfer function \(H(f) = k·∏(f-z_i)/∏(f-p_i)\) in Hz. This matches the LIGO GDS ‘n’ plane convention (plane='n'ins2z()).
Only used for analogue filters in Hz (
analog=True, unit="Hz").- prewarp
bool, optional If
True, apply prewarping before bilinear transform (default). IfFalse, skip prewarping (not recommended). Ignored whenanalog=False.- output
str, optional Desired output filter form:
'zpk': zero-pole-gain tuple (default)'ba': numerator-denominator tuple'sos': second-order sections array with separated gain
- filt
- Returns:
- filt_out
tuple Filter coefficients in the requested form:
output='ba': 2-tuple of (b, a) arraysoutput='zpk': 3-tuple of (zeros, poles, gain)output='sos': 2-tuple of (sos_array, gain)
For
output='sos', the returned SOS array has unit gain in all sections, and the overall gain is returned separately. This must be applied to the filter output:filtered_data * gain.
- filt_out
- Raises:
ValueErrorIf
analog=Truebutsample_rateis not provided.
See also
prepare_analog_filterPrepare analog filter without digital conversion.
scipy.signal.sosfiltApply SOS filter to data.
Notes
Prewarping
Unlike
scipy.signal.bilinearandscipy.signal.bilinear_zpkwhich do NOT perform prewarping, this function applies prewarping by default. Prewarping ensures the digital filter’s frequency response matches the analogue design at the pole/zero frequencies.Examples
Prepare a digital filter from analog specification:
>>> from gwpy.signal.filter_design import prepare_digital_filter >>> zpk = ([100], [10], 1.0) # 100 Hz zero, 10 Hz pole, gain 1.0 >>> sos = prepare_digital_filter( ... zpk, ... analog=True, ... sample_rate=4096, ... unit='Hz', ... output='sos', ... )
Apply to data:
>>> from scipy import signal >>> filtered = signal.sosfilt(sos, data)