filter function

Filter a signal

Filter a signal

Apply a 1-D digital filter compatible with 'Matlab' and 'Octave'.

filter(filt, ...) ## Default S3 method: filter(filt, a, x, zi = NULL, ...) ## S3 method for class 'Arma' filter(filt, x, ...) ## S3 method for class 'Ma' filter(filt, x, ...) ## S3 method for class 'Sos' filter(filt, x, ...) ## S3 method for class 'Zpg' filter(filt, x, ...)

Arguments

  • filt: For the default case, the moving-average coefficients of an ARMA filter (normally called b), specified as a numeric or complex vector. Generically, filt specifies an arbitrary filter operation.
  • ...: additional arguments (ignored).
  • a: the autoregressive (recursive) coefficients of an ARMA filter, specified as a numeric or complex vector. If a[1] is not equal to 1, then filter normalizes the filter coefficients by a[1]. Therefore, a[1] must be nonzero.
  • x: the input signal to be filtered, specified as a numeric or complex vector or matrix. If x is a matrix, each column is filtered.
  • zi: If zi is provided, it is taken as the initial state of the system and the final state is returned as zf. The state vector is a vector or a matrix (depending on x) whose length or number of rows is equal to the length of the longest coefficient vector b or a minus one. If zi is not supplied (NULL), the initial state vector is set to all zeros. Alternatively, zi may be the character string "zf", which specifies to return the final state vector even though the initial state vector is set to all zeros. Default: NULL.

Returns

The filtered signal, of the same dimensions as the input signal. In case the zi input argument was specified, a list with two elements is returned containing the variables y, which represents the output signal, and zf, which contains the final state vector or matrix.

Details

The filter is a direct form II transposed implementation of the standard linear time-invariant difference equation:

where N = length(a) - 1 and M = length(b) - 1.

The initial and final conditions for filter delays can be used to filter data in sections, especially if memory limitations are a consideration. See the examples.

Examples

bf <- butter(3, 0.1) # 10 Hz low-pass filter t <- seq(0, 1, len = 100) # 1 second sample x <- sin(2* pi * t * 2.3) + 0.25 * rnorm(length(t)) # 2.3 Hz sinusoid+noise z <- filter(bf, x) # apply filter plot(t, x, type = "l") lines(t, z, col = "red") ## specify initial conditions ## from Python scipy.signal.lfilter() documentation t <- seq(-1, 1, length.out = 201) x <- (sin(2 * pi * 0.75 * t * (1 - t) + 2.1) + 0.1 * sin(2 * pi * 1.25 * t + 1) + 0.18 * cos(2 * pi * 3.85 * t)) h <- butter(3, 0.05) lab <- max(length(h$b), length(h$a)) - 1 zi <- filtic(h$b, h$a, rep(1, lab), rep(1, lab)) z1 <- filter(h, x) z2 <- filter(h, x, zi * x[1]) plot(t, x, type = "l") lines(t, z1, col = "red") lines(t, z2$y, col = "green") legend("bottomright", legend = c("Original signal", "Filtered without initial conditions", "Filtered with initial conditions"), lty = 1, col = c("black", "red", "green"))

See Also

filter_zi, sosfilt (preferred because it avoids numerical problems).

Author(s)

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

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