bundles / scipy 1.17.1 / scipy / signal / _fir_filter_design / firwin2
function
scipy.signal._fir_filter_design:firwin2
Signature
def firwin2 ( numtaps , freq , gain , * , nfreqs = None , window = hamming , antisymmetric = False , fs = None ) Summary
FIR filter design using the window method.
Extended Summary
From the given frequencies freq and corresponding gains gain, this function constructs an FIR filter with linear phase and (approximately) the given frequency response.
Parameters
numtaps: intThe number of taps in the FIR filter.
numtapsmust be less thannfreqs.freq: array_like, 1-DThe frequency sampling points. Typically 0.0 to 1.0 with 1.0 being Nyquist. The Nyquist frequency is half
fs. The values infreqmust be nondecreasing. A value can be repeated once to implement a discontinuity. The first value infreqmust be 0, and the last value must befs/2. Values 0 andfs/2must not be repeated.gain: array_likeThe filter gains at the frequency sampling points. Certain constraints to gain values, depending on the filter type, are applied, see Notes for details.
nfreqs: int, optionalThe size of the interpolation mesh used to construct the filter. For most efficient behavior, this should be a power of 2 plus 1 (e.g, 129, 257, etc). The default is one more than the smallest power of 2 that is not less than
numtaps.nfreqsmust be greater thannumtaps.window: string or (string, float) or float, or None, optionalDesired window to use. Default is
'hamming'. The window will be symmetric, unless a suffix'_periodic'is appended to the window name (e.g.,'hamming_perodic') Consult get_window for a list of windows and required parameters. IfNone, no window function is applied.antisymmetric: bool, optionalWhether resulting impulse response is symmetric/antisymmetric. See Notes for more details.
fs: float, optionalThe sampling frequency of the signal. Each frequency in
cutoffmust be between 0 andfs/2. Default is 2.
Returns
taps: ndarrayThe filter coefficients of the FIR filter, as a 1-D array of length
numtaps.
Notes
From the given set of frequencies and gains, the desired response is constructed in the frequency domain. The inverse FFT is applied to the desired response to create the associated convolution kernel, and the first numtaps coefficients of this kernel, scaled by window, are returned.
The FIR filter will have linear phase. The type of filter is determined by the value of 'numtaps` and antisymmetric flag. There are four possible combinations:
odd
numtaps,antisymmetricis False, type I filter is producedeven
numtaps,antisymmetricis False, type II filter is producedodd
numtaps,antisymmetricis True, type III filter is producedeven
numtaps,antisymmetricis True, type IV filter is produced
Magnitude response of all but type I filters are subjects to following constraints:
type II -- zero at the Nyquist frequency
type III -- zero at zero and Nyquist frequencies
type IV -- zero at zero frequency
Array API Standard Support
firwin2 has experimental support for Python Array API Standard compatible backends in addition to NumPy. Please consider testing these features by setting an environment variable SCIPY_ARRAY_API=1 and providing CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following combinations of backend and device (or other capability) are supported.
==================== ==================== ==================== Library CPU GPU ==================== ==================== ==================== NumPy ✅ n/a CuPy n/a ✅ PyTorch ✅ ⛔ JAX ⚠️ no JIT ⛔ Dask ⚠️ computes graph n/a ==================== ==================== ====================
See
dev-arrayapifor more information.
Examples
A lowpass FIR filter with a response that is 1 on [0.0, 0.5], and that decreases linearly on [0.5, 1.0] from 1 to 0:from scipy import signal taps = signal.firwin2(150, [0.0, 0.5, 1.0], [1.0, 1.0, 0.0]) print(taps[72:78])✓
See also
Aliases
-
scipy.signal.firwin2