Engee documentation

movmedian

The moving median.

Library

EngeeDSP

Syntax

Function call

  • M = movmedian(A,k) — returns local median values by k points where each median is calculated in a sliding window of length k, which moves through the neighboring elements of the array A. If k odd, the window is centered relative to the element in the current position. If k even, the window is centered relative to the current and previous elements. The window size is automatically truncated at the endpoints when there are not enough elements to fill it. When the window is truncated, the median is taken only for the elements filling the window. M has the same size as A.

    • If A — vector, then movmedian it acts on the length of the vector A.

    • If A — a multidimensional array, then movmedian it operates according to the first dimension A, the size of which is not equal to 1.

  • M = movmedian(A,[kb kf]) — calculates the median with a length window kb+kf+1, which includes the element in the current position, kb elements back and kf Let’s go ahead.

  • M = movmedian(___,dim) — defines the dimension of the matrix A, which is used to perform the operation for any of the previous syntax options. For example, if A — the matrix, then movmedian(A,k,2) performs an operation on the columns of the matrix A, calculating the moving median by k elements for each row.

  • M = movmedian(___,nanflag) — determines whether to include or exclude values NaN to the array A. For example, movmedian(A,k,"omitnan") ignores values NaN when calculating each median. By default movmedian includes values NaN.

  • M = movmedian(___,Name,Value) — sets additional parameters for the moving median using one or more name-value arguments. For example, if x is a vector of time values, then movmedian(A,k,"SamplePoints",x) calculates the moving median relative to the time values in x.

Arguments

Input arguments

# A — input data

+ vector | the matrix | multidimensional array

Details

Input data specified as a vector, matrix, or multidimensional array.

Типы данных

Float32, Float64, Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64, Bool

# k — window length

+ scalar

Details

The length of the window, set as a scalar. If k — a positive integer, the centered median includes the element in the current position and its neighbors.

For example, movmedian(A,3) calculates an array of values of the local three-point median.

movmedian 1

# [kb kf] is the length of the directional window

+ A two-element vector is a string

Details

The length of the directional window, set as a string vector containing two elements. If kb and kf — positive integers, the calculation is performed by kb+kf+1 the elements. The calculation includes an element in the current position, kb elements up to the current position and kf items after the current position.

For example, movmedian(A,[2 1]) calculates an array of values of the local four-point median.

movmedian 2

# dim — the dimension along which the operation is performed

+ a positive integer scalar

Details

The dimension along which the operation is performed is specified as a positive integer scalar. If no dimension is specified, the first dimension of the array is used by default, the size of which is not equal to 1.

Argument dim specifies the dimension along which the function is executed movmedian, that is, the direction in which the specified window is moving.

Consider the input matrix A size m on n:

  • movmedian(A,k,1) calculates the moving median by k elements for each column A and returns a matrix of size m on n.

    movmedian 3

  • movmedian(A,k,2) calculates the moving median by k elements for each row of the matrix A and returns a matrix of size m on n.

    movmedian 4

# nanflag — condition for missing value

+ "includemissing" (default) | "includenan" | "omitmissing" | "omitnan"

Details

The condition for processing a missing value, set by one of the following values:

  • "includemissing" or "includenan" — enable values NaN in A when calculating each median. If any element is in the window — NaN, then the corresponding element in MNaN. Values "includemissing" and "includenan" they behave the same way.

  • "omitmissing" or "omitnan" — ignore all values NaN in A and calculate each median using fewer points. If all the elements are in the window — NaN, then the corresponding element in MNaN. Values "omitmissing" and "omitnan" they behave the same way.

Name-value input arguments

Specify optional argument pairs in the format Name, Value, where Name — the name of the argument, and Value — the appropriate value. Name-value arguments should be placed after the other arguments, but the order of the pairs does not matter.

Use commas to separate the name and value, and Name put it in quotation marks.

Example: M = movmedian(A,k,"Endpoints","fill").

# Endpoints — a method for processing windows near endpoints

+ "shrink" (by default) | "discard" | "fill" | scalar

Details

The method of processing windows near endpoints, specified by one of the following options:

Meaning Description

"shrink"

Reduce the window size near the endpoints of the input data to include only existing elements.

"discard"

Do not display any median values if the window does not completely overlap existing elements.

"fill"

Replace non-existent elements with NaN.

scalar

Replace non-existent elements with the specified numeric or logical value.

# SamplePoints — sampling points for calculating medians

+ vector

Details

Sample points for calculating medians, set as a vector. The sample points represent the location of the data in A. The sample points do not have to be evenly distributed. By default, the vector of sample points is [1 2 3 …​ ].

Sliding windows are defined relative to the sampling points, which should be sorted and contain unique elements. For example, if t is the time vector corresponding to the input data, then movmedian(rand(1,10),3,"SamplePoints",t) it has a window representing the time interval between t[i]-1.5 and t[i]+1.5.

If the sampling points are not evenly spaced and the parameter is set Endpoints, then its value should be "shrink".

Output arguments

# M — output data

+ vector | the matrix | multidimensional array

Details

The output data returned as a vector, matrix, or multidimensional array.

Examples

Centered moving median of the vector

Details

Calculate the three-point centered moving median of the row vector. If there are less than three elements in the window at the ends of the array, calculate the average value for the available elements.

import EngeeDSP.Functions: movmedian

A = [4 8 6 -1 -2 -3 -1 3 4 5]
M = movmedian(A,3)
1×10 Matrix{Float64}:
 6.0  6.0  6.0  -1.0  -2.0  -2.0  -1.0  3.0  4.0  4.5

The moving median of the matrix

Details

Calculate the three-point centered moving median for each row of the matrix. The window starts from the first line, slides horizontally to the end of the line, then moves to the second line, and so on. The dimension argument is two, which allows you to slide through the columns of the matrix A.

A = [4 8 6; -1 -2 -3; -1 3 4]
3×3 Matrix{Int64}:
  4   8   6
 -1  -2  -3
 -1   3   4
import EngeeDSP.Functions: movmedian

M = movmedian(A,3,2)
3×3 Matrix{Float64}:
  6.0   6.0   7.0
 -1.5  -2.0  -2.5
  1.0   3.0   3.5

Moving median without missing values

Details

Creating a vector string containing the values NaN.

A = [4 8 NaN -1 -2 -3 NaN 3 4 5];

Calculate the three-point centered moving median of the vector, excluding the values NaN. For windows containing any value NaN, function movmedian calculates the value taking into account all the elements except NaN.

import EngeeDSP.Functions: movmedian

M = movmedian(A,3,"omitnan")
1×10 Matrix{Float64}:
 6.0  6.0  3.5  -1.5  -2.0  -2.5  0.0  3.5  4.0  4.5

Return only the medians of the full window

Details

Calculate the three-point centered moving median of the row vector, but discard all calculations using less than three points from the output data. In other words, we will return only the medians calculated for the full three-element window, discarding calculations at the endpoints.

import EngeeDSP.Functions: movmedian

A = [4 8 6 -1 -2 -3 -1 3 4 5];
M = movmedian(A,3,"Endpoints","discard")
1×8 Matrix{Float64}:
 6.0  6.0  -1.0  -2.0  -2.0  -1.0  3.0  4.0