Learn R Programming

gsignal (version 0.3-1)

filter: Filter a signal

Description

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

Usage

filter(filt, ...)

# S3 method for default filter(filt, a, x, zi = NULL, ...)

# S3 method for Arma filter(filt, x, ...)

# S3 method for Ma filter(filt, x, ...)

# S3 method for Sos filter(filt, x, ...)

# S3 method for Zpg filter(filt, x, ...)

# S3 method for Zpg filter(filt, x, ...)

Arguments

filt

For the default case, the moving-average coefficients of an ARMA filter (normally called <U+2018>b<U+2019>), specified as a vector. Generically, filt specifies an arbitrary filter operation.

...

additional arguments (ignored).

a

the autoregressive (recursive) coefficients of an ARMA filter, specified as a 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 vector or as a 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.

Value

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:

  N                  M
 SUM a(k+1)y(n-k) + SUM b(k+1)x(n-k) = 0;   1 <= n <= length(x)
 k=0                k=0
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.

See Also

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

Examples

Run this code
# NOT RUN {
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"))

# }

Run the code above in your browser using DataLab