Engee documentation

medfilt1

Page in progress.

One-dimensional median filtering.

Library

EngeeDSP

Syntax

Function call

  • y = medfilt1(x) — applies a third-order one-dimensional median filter to the input vector x. The function considers that the signal is equal to 0 beyond the boundary values. The output signal y has the same dimension as x.

  • y = medfilt1(x, n) — applies a one-dimensional median filter n-th order to x.

  • y = medfilt1(x, n, [], dim) — uses dimension dim the reason the filter works. Argument [] required for backward compatibility and ignored.

  • y = medfilt1(_, nanflag, padding) — uses nanflag to determine how the values are handled NaN in each segment, using any input arguments from the previous syntaxes. This syntax also defines the padding. padding — the type of filtering performed at the boundaries of the signal.

    Arguments nanflag and padding they can be located anywhere after x in the function call.

Arguments

Input arguments

# x — input signal

+ vector | the matrix | An N-dimensional array

Details

An input signal specified as a real vector, matrix, or multidimensional array.

Data types

Float32, Float64

# n — filter order

+ 3 (by default) | scalar

Details

The order of the one-dimensional median filter, given as a positive integer scalar:

  • If n If it’s an odd number, then y(k) — median x(k-(n-1)//2:k+(n-1)//2).

  • If n even, then y(k) — median x(k-n/2:k+(n/2)-1). In this case, the function sorts the numbers and calculates the arithmetic mean of the two average elements of the sorted list.

Data types

Float64

# dim — filtered measurement

+ scalar

Details

The filtered dimension, specified as a positive integer scalar. By default, the function works with the first dimension of a multidimensional x. In particular, if x is a matrix, the function filters its columns so that:

y(:,i) = medfilt1(x(:,i),n).

Data types

Float64

# nanflag — condition NaN
"includenan" (default) | "omitnan"

Details

Condition NaN, set as "includenan" or "omitnan":

  • "includenan" returns a filtered signal such that the median of any segment containing NaN, is also equal to NaN.

  • "omitnan" returns a filtered signal such that the median of any segment containing NaN, is equal to the median of values other than NaN. If all the segment elements are NaN, the result will be NaN.

# padding — filtering of boundary values

+ "zeropad" (by default) | "truncate"

Details

Filtering of boundary values, is defined as "zeropad" or "truncate":

  • "zeropad" considers the signal to be zero beyond the boundary values.

  • "truncate" calculates the medians of the smaller segments as the signal boundaries are reached.

Output arguments

# y — filtered signal

+ vector | the matrix | An N-dimensional array

Details

The filtered signal returned as a real vector, matrix, or multidimensional array. Dimension y the same as the x.

Data types

Float64

Examples

Noise reduction using median filtering

Details

Generate a sinusoidal signal with a sampling frequency 100 Hz during 1 seconds. Add a higher frequency sinusoid to simulate noise. Using the median filter 10-th order for smoothing the signal. Let’s build a graph.

import EngeeDSP.Functions: medfilt1

fs = 100
t = range(0, 1, step=1/fs)
x = sin.(2π * t * 3) .+ 0.25 * sin.(2π * t * 40)

y = medfilt1(x, 10)

p = plot(t, x, label="Original", linewidth=1.5)
plot!(p, t, y, label="Filtered", linewidth=2)


plot!(p, legend=:topright, framestyle=:box)

medfilt1 1

Literature

  1. Pratt, William K. Digital Image Processing. 4th Ed. Hoboken, NJ: John Wiley & Sons, 2007.