medfilt1: 1-D median filtering

View source: R/medfilt1.R

medfilt1R Documentation

1-D median filtering

Description

Apply a running median of odd span to the input x

Usage

medfilt1(x, n = 3, MARGIN = 2, na.omit = FALSE, ...)

Arguments

x

Input signal, specified as a numeric vector, matrix or array.

n

positive integer width of the median window; must be odd. Default: 3

MARGIN

Vector giving the subscripts which the function will be applied over. E.g., for a matrix 1 indicates rows, 2 indicates columns, c(1, 2) indicates rows and columns. Where X has named dimnames, it can be a character vector selecting dimension names. Default: 2 (columns).

na.omit

logical indicating whether to omit missing values, or interpolate then using a cubic spline function (splinefun). Default: FALSE

...

other arguments passed to runmed

Details

This function computes a running median over the input x, using the runmed function. Because of that, it works a little differently than the 'Matlab' or 'Octave' versions (i.e., it does not produce exactly the same values).

missing values

The 'Mablab' and 'Octave' functions have a 'nanflag' option that allows to include or remove missing values. If inclusion is specifies, then the function returns a signal so that the median of any segment containing NAs is also NA. Because the 'runmed' function does not include an na.omit option, implementing this functionality would lead to a considerable speed loss. Instead, a na.omit parameter was implemented that allows either omitting NAs or interpolating them with a spline function.

endpoint filtering

Instead of the 'zeropad' and 'truncate' options to the 'padding' argument in the 'Matlab' and 'Octave' functions, the present version uses the standard endrule parameter of the 'runmed' function, with options keep, constant, or median.

Value

Filtered signal, returned as a numeric vector, matrix, or array, of the same size as x.

Author(s)

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

See Also

runmed, splinefun

Examples

## noise suppression
fs <- 100
t <- seq(0, 1, 1/fs)
x <- sin(2 * pi * t * 3) + 0.25 * sin(2 * pi * t * 40)
plot(t, x, type = "l", xlab = "", ylab = "")
y <- medfilt1(x, 11)
lines (t, y, col = "red")
legend("topright", c("Original", "Filtered"), lty = 1, col = 1:2)


gsignal documentation built on Sept. 12, 2024, 6:27 a.m.