Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

medfilt1

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

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

Signal Processing

v0.3-1
GPL-3
Authors
Geert van Boxtel [aut, cre] (Maintainer), Tom Short [aut] (Author of 'signal' package), Paul Kienzle [aut] (Majority of the original sources), Ben Abbott [ctb], Juan Aguado [ctb], Muthiah Annamalai [ctb], Leonardo Araujo [ctb], William Asquith [ctb], David Bateman [ctb], David Billinghurst [ctb], Juan Pablo Carbajal [ctb], André Carezia [ctb], Vincent Cautaerts [ctb], Eric Chassande-Mottin [ctb], Luca Citi [ctb], Dave Cogdell [ctb], Carlo de Falco [ctb], Carne Draug [ctb], Pascal Dupuis [ctb], John W. Eaton [ctb], R.G.H Eschauzier [ctb], Andrew Fitting [ctb], Alan J. Greenberger [ctb], Mike Gross [ctb], Daniel Gunyan [ctb], Kai Habel [ctb], Kurt Hornik [ctb], Jake Janovetz [ctb], Alexander Klein [ctb], Peter V. Lanspeary [ctb], Bill Lash [ctb], Friedrich Leissh [ctb], Laurent S. Mazet [ctb], Mike Miller [ctb], Petr Mikulik [ctb], Paolo Neis [ctb], Georgios Ouzounis [ctb], Sylvain Pelissier [ctb], Francesco Potortì [ctb], Charles Praplan [ctb], Lukas F. Reichlin [ctb], Tony Richardson [ctb], Asbjorn Sabo [ctb], Thomas Sailer [ctb], Rolf Schirmacher [ctb], Rolf Schirmacher [ctb], Ivan Selesnick [ctb], Julius O. Smith III [ctb], Peter L. Soendergaard [ctb], Quentin Spencer [ctb], Doug Stewart [ctb], P. Sudeepam [ctb], Stefan van der Walt [ctb], Andreas Weber [ctb], P. Sudeepam [ctb], Andreas Weingessel [ctb]
Initial release
2021-05-02

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.