fir2 function

Frequency sampling-based FIR filter design

Frequency sampling-based FIR filter design

Produce a FIR filter with arbitrary frequency response over frequency bands.

fir2(n, f, m, grid_n = 512, ramp_n = NULL, window = hamming(n + 1))

Arguments

  • n: filter order (1 less than the length of the filter).

  • f: vector of frequency points in the range from 0 to 1, where 1 corresponds to the Nyquist frequency. The first point of f must be 0 and the last point must be 1. f must be sorted in increasing order. Duplicate frequency points are allowed and are treated as steps in the frequency response.

  • m: vector of the same length as f containing the desired magnitude response at each of the points specified in f.

  • grid_n: length of ideal frequency response function. grid_n

    defaults to 512, and should be a power of 2 bigger than n.

  • ramp_n: transition width for jumps in filter response (defaults to grid_n / 20). A wider ramp gives wider transitions but has better stopband characteristics.

  • window: smoothing window. The returned filter is the same shape as the smoothing window. Default: hamming(n + 1).

Returns

The FIR filter coefficients, a vector of length n + 1, of class Ma.

Details

The function linearly interpolates the desired frequency response onto a dense grid and then uses the inverse Fourier transform and a Hamming window to obtain the filter coefficients.

Examples

f <- c(0, 0.3, 0.3, 0.6, 0.6, 1) m <- c(0, 0, 1, 1/2, 0, 0) fh <- freqz(fir2(100, f, m)) op <- par(mfrow = c(1, 2)) plot(f, m, type = "b", ylab = "magnitude", xlab = "Frequency") lines(fh$w / pi, abs(fh$h), col = "blue") # plot in dB: plot(f, 20*log10(m+1e-5), type = "b", ylab = "dB", xlab = "Frequency") lines(fh$w / pi, 20*log10(abs(fh$h)), col = "blue") par(op)

See Also

Ma, filter, fftfilt, fir1

Author(s)

Paul Kienzle, pkienzle@users.sf.net .

Conversion to R Tom Short,

adapted by Geert van Boxtel, G.J.M.vanBoxtel@gmail.com .

  • Maintainer: Geert van Boxtel
  • License: GPL-3
  • Last published: 2024-09-11