FIR (finite impulse response) filter design.
firwin - window method for low pass, high pass, band pass and band stop filters, as scipy.signal.firwin, except that an even number of taps for a filter passing the Nyquist frequency is increased by one (scipy throws ValueError)firgain - frequency sampling method for an arbitrary piecewise gain, as scipy.signal.firwin2gammatone - gammatone filter of auditory models, as scipy.signal.gammatone with ftype='fir'Filters are vectors of coefficients (impulse responses); apply them with fastmath.signal/filter-signal or fastmath.signal/convolve. Windows come from fastmath.kernel/window (symmetric by default); the default window is :hann (scipy uses hamming in firwin).
FIR (finite impulse response) filter design. * [[firwin]] - window method for low pass, high pass, band pass and band stop filters, as `scipy.signal.firwin`, except that an even number of taps for a filter passing the Nyquist frequency is increased by one (scipy throws `ValueError`) * [[firgain]] - frequency sampling method for an arbitrary piecewise gain, as `scipy.signal.firwin2` * [[gammatone]] - gammatone filter of auditory models, as `scipy.signal.gammatone` with `ftype='fir'` Filters are vectors of coefficients (impulse responses); apply them with `fastmath.signal/filter-signal` or `fastmath.signal/convolve`. Windows come from `fastmath.kernel/window` (symmetric by default); the default window is `:hann` (scipy uses `hamming` in `firwin`).
(firgain
{:keys [taps freq-gain-pairs fs window antisymmetric? interpolator nfreqs]
:or {taps 15 fs 1.0 window :hann antisymmetric? false interpolator :linear}
:as opts})(firgain taps freq-gain-pairs opts)Design an FIR filter from a specified frequency-gain response using the frequency sampling method.
This function constructs an FIR filter by interpolating a set of desired frequency-gain points to create a continuous frequency response. It automatically handles the design of the four types of linear-phase FIR filters based on the number of taps and the symmetry requirements.
Parameters:
taps: The number of filter coefficients to generate.freq-gain-pairs: A sequence of [frequency, gain] pairs defining the desired filter shape. The first frequency must be 0.0 (DC) and the last must be the Nyquist frequency (fs/2).opts: A map of optional configuration:
:fs: Sampling frequency. Default is 1.0.:window: Window function name (keyword), function, or sequence of coefficients. Default is :hann.:antisymmetric?: Boolean. If true, designs an antisymmetric (Type III or IV) filter; otherwise, designs a symmetric (Type I or II) filter. Default is false.:interpolator: The interpolation method used to bridge the provided frequency points (e.g., :linear, :cubic).:nfreqs: Number of points of the frequency grid from 0 to Nyquist used for the inverse FFT (at least taps + 1). Default 1 + 2^ceil(log2(1.25 taps)); scipy.signal.firwin2 uses 1 + 2^ceil(log2(taps)), so results differ slightly unless :nfreqs is given.Output:
Returns a vector of doubles representing the FIR filter coefficients (impulse response). With the same :nfreqs and window it equals scipy.signal.firwin2(taps, freqs, gains, nfreqs, window, antisymmetric, fs).
Throws ex-info when the frequencies don't start at 0 and end at Nyquist, contain duplicates, or the gains violate the type constraints (type II: 0 at Nyquist, type III: 0 at DC and Nyquist, type IV: 0 at DC).
Design an FIR filter from a specified frequency-gain response using the frequency sampling method. This function constructs an FIR filter by interpolating a set of desired frequency-gain points to create a continuous frequency response. It automatically handles the design of the four types of linear-phase FIR filters based on the number of taps and the symmetry requirements. Parameters: * `taps`: The number of filter coefficients to generate. * `freq-gain-pairs`: A sequence of `[frequency, gain]` pairs defining the desired filter shape. The first frequency must be 0.0 (DC) and the last must be the Nyquist frequency (`fs/2`). * `opts`: A map of optional configuration: * `:fs`: Sampling frequency. Default is `1.0`. * `:window`: Window function name (keyword), function, or sequence of coefficients. Default is `:hann`. * `:antisymmetric?`: Boolean. If `true`, designs an antisymmetric (Type III or IV) filter; otherwise, designs a symmetric (Type I or II) filter. Default is `false`. * `:interpolator`: The interpolation method used to bridge the provided frequency points (e.g., `:linear`, `:cubic`). * `:nfreqs`: Number of points of the frequency grid from 0 to Nyquist used for the inverse FFT (at least `taps + 1`). Default `1 + 2^ceil(log2(1.25 taps))`; `scipy.signal.firwin2` uses `1 + 2^ceil(log2(taps))`, so results differ slightly unless `:nfreqs` is given. Output: Returns a vector of doubles representing the FIR filter coefficients (impulse response). With the same `:nfreqs` and window it equals `scipy.signal.firwin2(taps, freqs, gains, nfreqs, window, antisymmetric, fs)`. Throws `ex-info` when the frequencies don't start at 0 and end at Nyquist, contain duplicates, or the gains violate the type constraints (type II: 0 at Nyquist, type III: 0 at DC and Nyquist, type IV: 0 at DC).
(firgain-shift xs nyquist taps ftype)Linear phase terms of a firgain filter at frequencies xs: exp(-i pi/2 (taps-1) x / nyquist), multiplied by i for antisymmetric filters (types III and IV, ftype 3 or 4).
Linear phase terms of a [[firgain]] filter at frequencies `xs`: `exp(-i pi/2 (taps-1) x / nyquist)`, multiplied by `i` for antisymmetric filters (types III and IV, `ftype` 3 or 4).
(firwin {:keys [taps kind cutoff fs window p scale?]
:or {kind :lowpass cutoff 0.1 fs 1.0 window :hann p 4 scale? true}
:as opts})(firwin taps kind opts)Designs a Finite Impulse Response (FIR) filter using the window method.
This function constructs a linear-phase FIR filter by calculating the ideal impulse response (sinc function) for specified frequency bands and tapering it with a window function to reduce spectral leakage (Gibbs phenomenon). It provides a robust way to create standard filter types—low-pass, high-pass, band-pass, and band-stop—with controlled transition regions.
Input parameters:
taps - The number of filter coefficients (length of the filter). If nil, it is automatically estimated based on the :p parameter and the highest cutoff frequency. If the filter includes the Nyquist frequency, an even tap count is automatically incremented to preserve symmetry.kind - A keyword specifying the filter type: :lowpass, :highpass, :bandstop, or :bandpass.opts - A map of configuration options:
:fs - Sampling frequency. Default is 1.0.cutoff - A single frequency or a sequence of frequencies defining the filter transitions. Values must be between 0 and fs/2 (not inclusive). Default is 0.1.:window - The windowing strategy. Can be a keyword (e.g., :hann, :hamming, :blackman), a window function, or a sequence of pre-calculated coefficients. Default is :hann.:p - Number of cycles of the cutoff frequency used for automatic tap estimation taps = floor(p * fs / max_cutoff) (with cutoffs below fs/2). Default is 4.:scale? - Whether to normalize the filter coefficients. Can be true (auto-scale to unity gain at passband center), false, or a specific frequency ratio (0.0 to 1.0) to scale against. Default is true.For :lowpass with several cutoffs the largest is used, for :highpass the smallest.
Output:
Returns a vector of doubles representing the FIR filter coefficients (impulse response). Equals scipy.signal.firwin(taps, cutoff, window, pass_zero, scale, fs) for the same window, except for an even taps with a filter passing the Nyquist frequency: it is increased by one here, scipy throws ValueError.
Throws ex-info for an empty or out of range cutoff, an unknown kind, or a window sequence of another length than taps.
Designs a Finite Impulse Response (FIR) filter using the window method. This function constructs a linear-phase FIR filter by calculating the ideal impulse response (sinc function) for specified frequency bands and tapering it with a window function to reduce spectral leakage (Gibbs phenomenon). It provides a robust way to create standard filter types—low-pass, high-pass, band-pass, and band-stop—with controlled transition regions. Input parameters: * `taps` - The number of filter coefficients (length of the filter). If `nil`, it is automatically estimated based on the `:p` parameter and the highest cutoff frequency. If the filter includes the Nyquist frequency, an even tap count is automatically incremented to preserve symmetry. * `kind` - A keyword specifying the filter type: `:lowpass`, `:highpass`, `:bandstop`, or `:bandpass`. * `opts` - A map of configuration options: * `:fs` - Sampling frequency. Default is `1.0`. * `cutoff` - A single frequency or a sequence of frequencies defining the filter transitions. Values must be between 0 and `fs/2` (not inclusive). Default is `0.1`. * `:window` - The windowing strategy. Can be a keyword (e.g., `:hann`, `:hamming`, `:blackman`), a window function, or a sequence of pre-calculated coefficients. Default is `:hann`. * `:p` - Number of cycles of the cutoff frequency used for automatic tap estimation `taps = floor(p * fs / max_cutoff)` (with cutoffs below `fs/2`). Default is `4`. * `:scale?` - Whether to normalize the filter coefficients. Can be `true` (auto-scale to unity gain at passband center), `false`, or a specific frequency ratio (0.0 to 1.0) to scale against. Default is `true`. * Other keys are passed directly to the window creation function. For `:lowpass` with several cutoffs the largest is used, for `:highpass` the smallest. Output: Returns a vector of doubles representing the FIR filter coefficients (impulse response). Equals `scipy.signal.firwin(taps, cutoff, window, pass_zero, scale, fs)` for the same window, except for an even `taps` with a filter passing the Nyquist frequency: it is increased by one here, scipy throws `ValueError`. Throws `ex-info` for an empty or out of range cutoff, an unknown `kind`, or a window sequence of another length than `taps`.
(firwin-cutoff kind cutoff fs)Converts cutoff frequencies to the band edges used by firwin, normalized to the Nyquist frequency fs/2.
cutoff is one frequency or a sequence of frequencies (sorted, duplicates removed), each in (0, fs/2). Returns a flat sequence of pass band edges [low1 high1 low2 high2 ...] in [0, 1]:
:lowpass - [0 c], c the largest cutoff:highpass - [c 1], c the smallest cutoff:bandpass - pass bands between consecutive cutoffs, starting with a pass band at the first cutoff; an odd count passes up to the Nyquist frequency:bandstop - stop bands between consecutive cutoffs, starting with a pass band at 0; an odd count stops up to the Nyquist frequencyThrows ex-info for an empty cutoff, a cutoff outside (0, fs/2) or an unknown kind.
Converts cutoff frequencies to the band edges used by [[firwin]], normalized to the Nyquist frequency `fs/2`. `cutoff` is one frequency or a sequence of frequencies (sorted, duplicates removed), each in `(0, fs/2)`. Returns a flat sequence of pass band edges `[low1 high1 low2 high2 ...]` in `[0, 1]`: * `:lowpass` - `[0 c]`, `c` the largest cutoff * `:highpass` - `[c 1]`, `c` the smallest cutoff * `:bandpass` - pass bands between consecutive cutoffs, starting with a pass band at the first cutoff; an odd count passes up to the Nyquist frequency * `:bandstop` - stop bands between consecutive cutoffs, starting with a pass band at 0; an odd count stops up to the Nyquist frequency Throws `ex-info` for an empty cutoff, a cutoff outside `(0, fs/2)` or an unknown `kind`.
(firwin-scale h m cutoff scale?)Scales filter coefficients h (at positions m, centered at 0) to unit gain at one frequency, as scipy.signal.firwin with scale=True.
cutoff - normalized band edges from firwin-cutoff; the frequency is 0 when the first band starts at 0, 1 (Nyquist) when it ends at 1, otherwise the center of the first bandscale? - false returns h unchanged, true uses the frequency above, a number is the normalized frequency (0 to 1) to useScales filter coefficients `h` (at positions `m`, centered at 0) to unit gain at one frequency, as `scipy.signal.firwin` with `scale=True`. * `cutoff` - normalized band edges from [[firwin-cutoff]]; the frequency is 0 when the first band starts at 0, 1 (Nyquist) when it ends at 1, otherwise the center of the first band * `scale?` - `false` returns `h` unchanged, `true` uses the frequency above, a number is the normalized frequency (0 to 1) to use
(gammatone {:keys [cutoff fs taps order]
:or {cutoff 1000.0 fs 16000.0 order 4}})FIR gammatone filter (auditory filter model), as scipy.signal.gammatone(cutoff, 'fir', order, taps, fs).
The impulse response t^(order-1) exp(-2 pi b t) cos(2 pi cutoff t), with the equivalent rectangular bandwidth b = 1.019 (24.7 + cutoff / 9.26449) in Hz, is sampled at t = n / fs and normalized.
Options:
:cutoff - center frequency in Hz, 0 < cutoff < fs/2 (default 1000.0):fs - sample rate in Hz (default 16000.0):order - filter order, at least 1 (default 4):taps - number of coefficients (default max(15, 0.015 fs))Frequencies are in Hz because the bandwidth formula is. Returns a vector of doubles. Throws ex-info for an invalid :cutoff or :order.
FIR gammatone filter (auditory filter model), as `scipy.signal.gammatone(cutoff, 'fir', order, taps, fs)`. The impulse response `t^(order-1) exp(-2 pi b t) cos(2 pi cutoff t)`, with the equivalent rectangular bandwidth `b = 1.019 (24.7 + cutoff / 9.26449)` in Hz, is sampled at `t = n / fs` and normalized. Options: * `:cutoff` - center frequency in Hz, `0 < cutoff < fs/2` (default 1000.0) * `:fs` - sample rate in Hz (default 16000.0) * `:order` - filter order, at least 1 (default 4) * `:taps` - number of coefficients (default `max(15, 0.015 fs)`) Frequencies are in Hz because the bandwidth formula is. Returns a vector of doubles. Throws `ex-info` for an invalid `:cutoff` or `:order`.
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |