26.2 Statistics on Sliding Windows of Data

It is often useful to calculate descriptive statistics over a subsection (i.e., window) of a full dataset. Octave provides the function movfun which will call an arbitrary function handle with windows of data and accumulate the results. Many of the most commonly desired functions, such as the moving average over a window of data (movmean), are already provided.

y = movfun (fcn, x, wlen)
y = movfun (fcn, x, [nb, na])
y = movfun (…, "property", value)

Apply function fcn to a moving window of length wlen on data x.

If wlen is a scalar, the function fcn is applied to a moving window of length wlen. When wlen is an odd number the window is symmetric and includes (wlen - 1) / 2 elements on either side of the central element. For example, when calculating the output at index 5 with a window length of 3, movfun uses data elements [4, 5, 6]. If wlen is an even number, the window is asymmetric and has wlen/2 elements to the left of the central element and wlen/2 - 1 elements to the right of the central element. For example, when calculating the output at index 5 with a window length of 4, movfun uses data elements [3, 4, 5, 6].

If wlen is an array with two elements [nbna], the function is applied to a moving window -nb:na. This window includes nb number of elements before the current element and na number of elements after the current element. The current element is always included. For example, given wlen = [3, 0], the data used to calculate index 5 is [2, 3, 4, 5].

During calculations the data input x is reshaped into a 2-dimensional wlen-by-N matrix and fcn is called on this new matrix. Therefore, fcn must accept an array input argument and apply the computation along dimension 1, i.e., down the columns of the array.

When applied to an array (possibly multi-dimensional) with n columns, fcn may return a result in either of two formats: Format 1) an array of size 1-by-n-by-dim3-by-…-by-dimN. This is the typical output format from Octave core functions. Type demo ("movfun", 5) for an example of this use case. Format 2) a row vector of length n * numel_higher_dims where numel_higher_dims is prod (size (x)(3:end)). The output of fcn for the i-th input column must be found in the output at indices i:n:(n*numel_higher_dims). This format is useful when concatenating functions into arrays, or when using nthargout. Type demo ("movfun", 6) for an example of this case.

The calculation can be controlled by specifying property/value pairs. Valid properties are

"dim"

Operate along the dimension specified, rather than the default of the first non-singleton dimension.

"Endpoints"

This property controls how results are calculated at the boundaries (endpoints) of the window. Possible values are:

"shrink" (default)

The window is truncated at the beginning and end of the array to exclude elements for which there is no source data. For example, with a window of length 3, y(1) = fcn (x(1:2)), and y(end) = fcn (x(end-1:end)).

"discard"

Any y values that use a window extending beyond the original data array are deleted. For example, with a 10-element data vector and a window of length 3, the output will contain only 8 elements. The first element would require calculating the function over indices [0, 1, 2] and is therefore discarded. The last element would require calculating the function over indices [9, 10, 11] and is therefore discarded.

"fill"

Any window elements outside the data array are replaced by NaN. For example, with a window of length 3, y(1) = fcn ([NaN, x(1:2)]), and y(end) = fcn ([x(end-1:end), NaN]). This option usually results in y having NaN values at the boundaries, although it is influenced by how fcn handles NaN, and also by the property "nancond".

user_value

Any window elements outside the data array are replaced by the specified value user_value which must be a numeric scalar. For example, with a window of length 3, y(1) = fcn ([user_value, x(1:2)]), and y(end) = fcn ([x(end-1:end), user_value]). A common choice for user_value is 0.

"same"

Any window elements outside the data array are replaced by the value of x at the boundary. For example, with a window of length 3, y(1) = fcn ([x(1), x(1:2)]), and y(end) = fcn ([x(end-1:end), x(end)]).

"periodic"

The window is wrapped so that any missing data elements are taken from the other side of the data. For example, with a window of length 3, y(1) = fcn ([x(end), x(1:2)]), and y(end) = fcn ([x(end-1:end), x(1)]).

Note that for some of these choices, the window size at the boundaries is not the same as for the central part, and fcn must work in these cases.

"nancond"

Controls whether NaN and NA values should be included (value: "includenan"), or excluded (value: "omitnan"), from the data passed to fcn. The default is "includenan". Caution: The "omitnan" option is not yet implemented.

"outdim"

A row vector that selects which dimensions of the calculation will appear in the output y. This is only useful when fcn returns an N-dimensional array in Format 1. The default is to return all output dimensions.

Programming Note: The property "outdim" can be used to save memory when the output of fcn has many dimensions, or when a wrapper to the base function that selects the desired outputs is too costly. When memory is not an issue, the easiest way to select output dimensions is to first calculate the complete result with movfun and then filter that result with indexing. If code complexity is not an issue then a wrapper can be created using anonymous functions. For example, if basefcn is a function returning a K-dimensional row output, and only dimension D is desired, then the following wrapper could be used.

fcn = @(x) basefcn (x)(:,size(x,2) * (D-1) + (1:size(x,2)));
y = movfun (@fcn, …);

See also: movslice, prepad, postpad, permute, reshape.

slcidx = movslice (N, wlen)
[slcidx, C, Cpre, Cpost, win] = movslice (…)

Generate indices to slice a vector of length N in to windows of length wlen.

FIXME: Document inputs N, wlen

FIXME: Document outputs slcidx, C, Cpre, Cpost, win.

See also: movfun.

y = movmad (x, wlen)
y = movmad (x, [na, nb])
y = movmad (…, dim)
y = movmad (…, "nancond")
y = movmad (…, property, value)

Calculate the moving mean absolute deviation over a sliding window of length wlen on data x.

If wlen is a scalar, the function mad is applied to a moving window of length wlen. When wlen is an odd number the window is symmetric and includes (wlen - 1) / 2 elements on either side of the central element. For example, when calculating the output at index 5 with a window length of 3, movmad uses data elements [4, 5, 6]. If wlen is an even number, the window is asymmetric and has wlen/2 elements to the left of the central element and wlen/2 - 1 elements to the right of the central element. For example, when calculating the output at index 5 with a window length of 4, movmad uses data elements [3, 4, 5, 6].

If wlen is an array with two elements [nbna], the function is applied to a moving window -nb:na. This window includes nb number of elements before the current element and na number of elements after the current element. The current element is always included. For example, given wlen = [3, 0], the data used to calculate index 5 is [2, 3, 4, 5].

If the optional argument dim is given, operate along this dimension.

The optional string argument "nancond" controls whether NaN and NA values should be included ("includenan"), or excluded ("omitnan"), from the data passed to mad. The default is "includenan". Caution: the "omitnan" option is not yet implemented.

The calculation can be controlled by specifying property/value pairs. Valid properties are

"Endpoints"

This property controls how results are calculated at the boundaries (endpoints) of the window. Possible values are:

"shrink" (default)

The window is truncated at the beginning and end of the array to exclude elements for which there is no source data. For example, with a window of length 3, y(1) = mad (x(1:2)), and y(end) = mad (x(end-1:end)).

"discard"

Any y values that use a window extending beyond the original data array are deleted. For example, with a 10-element data vector and a window of length 3, the output will contain only 8 elements. The first element would require calculating the function over indices [0, 1, 2] and is therefore discarded. The last element would require calculating the function over indices [9, 10, 11] and is therefore discarded.

"fill"

Any window elements outside the data array are replaced by NaN. For example, with a window of length 3, y(1) = mad ([NaN, x(1:2)]), and y(end) = mad ([x(end-1:end), NaN]). This option usually results in y having NaN values at the boundaries, although it is influenced by how mad handles NaN, and also by the property "nancond".

user_value

Any window elements outside the data array are replaced by the specified value user_value which must be a numeric scalar. For example, with a window of length 3, y(1) = mad ([user_value, x(1:2)]), and y(end) = mad ([x(end-1:end), user_value]). A common choice for user_value is 0.

"same"

Any window elements outside the data array are replaced by the value of x at the boundary. For example, with a window of length 3, y(1) = mad ([x(1), x(1:2)]), and y(end) = mad ([x(end-1:end), x(end)]).

"periodic"

The window is wrapped so that any missing data elements are taken from the other side of the data. For example, with a window of length 3, y(1) = mad ([x(end), x(1:2)]), and y(end) = mad ([x(end-1:end), x(1)]).

"SamplePoints"

Caution: This option is not yet implemented.

Programming Note: This function is a wrapper which calls movfun. For additional options and documentation, See movfun.

See also: movfun, movslice, movmax, movmean, movmedian, movmin, movprod, movstd, movsum, movvar.

y = movmax (x, wlen)
y = movmax (x, [na, nb])
y = movmax (…, dim)
y = movmax (…, "nancond")
y = movmax (…, property, value)

Calculate the moving maximum over a sliding window of length wlen on data x.

If wlen is a scalar, the function max is applied to a moving window of length wlen. When wlen is an odd number the window is symmetric and includes (wlen - 1) / 2 elements on either side of the central element. For example, when calculating the output at index 5 with a window length of 3, movmax uses data elements [4, 5, 6]. If wlen is an even number, the window is asymmetric and has wlen/2 elements to the left of the central element and wlen/2 - 1 elements to the right of the central element. For example, when calculating the output at index 5 with a window length of 4, movmax uses data elements [3, 4, 5, 6].

If wlen is an array with two elements [nbna], the function is applied to a moving window -nb:na. This window includes nb number of elements before the current element and na number of elements after the current element. The current element is always included. For example, given wlen = [3, 0], the data used to calculate index 5 is [2, 3, 4, 5].

If the optional argument dim is given, operate along this dimension.

The optional string argument "nancond" controls whether NaN and NA values should be included ("includenan"), or excluded ("omitnan"), from the data passed to max. The default is "includenan". Caution: the "omitnan" option is not yet implemented.

The calculation can be controlled by specifying property/value pairs. Valid properties are

"Endpoints"

This property controls how results are calculated at the boundaries (endpoints) of the window. Possible values are:

"shrink" (default)

The window is truncated at the beginning and end of the array to exclude elements for which there is no source data. For example, with a window of length 3, y(1) = max (x(1:2)), and y(end) = max (x(end-1:end)).

"discard"

Any y values that use a window extending beyond the original data array are deleted. For example, with a 10-element data vector and a window of length 3, the output will contain only 8 elements. The first element would require calculating the function over indices [0, 1, 2] and is therefore discarded. The last element would require calculating the function over indices [9, 10, 11] and is therefore discarded.

"fill"

Any window elements outside the data array are replaced by NaN. For example, with a window of length 3, y(1) = max ([NaN, x(1:2)]), and y(end) = max ([x(end-1:end), NaN]). This option usually results in y having NaN values at the boundaries, although it is influenced by how max handles NaN, and also by the property "nancond".

user_value

Any window elements outside the data array are replaced by the specified value user_value which must be a numeric scalar. For example, with a window of length 3, y(1) = max ([user_value, x(1:2)]), and y(end) = max ([x(end-1:end), user_value]). A common choice for user_value is 0.

"same"

Any window elements outside the data array are replaced by the value of x at the boundary. For example, with a window of length 3, y(1) = max ([x(1), x(1:2)]), and y(end) = max ([x(end-1:end), x(end)]).

"periodic"

The window is wrapped so that any missing data elements are taken from the other side of the data. For example, with a window of length 3, y(1) = max ([x(end), x(1:2)]), and y(end) = max ([x(end-1:end), x(1)]).

"SamplePoints"

Caution: This option is not yet implemented.

Programming Note: This function is a wrapper which calls movfun. For additional options and documentation, See movfun.

See also: movfun, movslice, movmad, movmean, movmedian, movmin, movprod, movstd, movsum, movvar.

y = movmean (x, wlen)
y = movmean (x, [na, nb])
y = movmean (…, dim)
y = movmean (…, "nancond")
y = movmean (…, property, value)

Calculate the moving average over a sliding window of length wlen on data x.

If wlen is a scalar, the function mean is applied to a moving window of length wlen. When wlen is an odd number the window is symmetric and includes (wlen - 1) / 2 elements on either side of the central element. For example, when calculating the output at index 5 with a window length of 3, movmean uses data elements [4, 5, 6]. If wlen is an even number, the window is asymmetric and has wlen/2 elements to the left of the central element and wlen/2 - 1 elements to the right of the central element. For example, when calculating the output at index 5 with a window length of 4, movmean uses data elements [3, 4, 5, 6].

If wlen is an array with two elements [nbna], the function is applied to a moving window -nb:na. This window includes nb number of elements before the current element and na number of elements after the current element. The current element is always included. For example, given wlen = [3, 0], the data used to calculate index 5 is [2, 3, 4, 5].

If the optional argument dim is given, operate along this dimension.

The optional string argument "nancond" controls whether NaN and NA values should be included ("includenan"), or excluded ("omitnan"), from the data passed to mean. The default is "includenan". Caution: the "omitnan" option is not yet implemented.

The calculation can be controlled by specifying property/value pairs. Valid properties are

"Endpoints"

This property controls how results are calculated at the boundaries (endpoints) of the window. Possible values are:

"shrink" (default)

The window is truncated at the beginning and end of the array to exclude elements for which there is no source data. For example, with a window of length 3, y(1) = mean (x(1:2)), and y(end) = mean (x(end-1:end)).

"discard"

Any y values that use a window extending beyond the original data array are deleted. For example, with a 10-element data vector and a window of length 3, the output will contain only 8 elements. The first element would require calculating the function over indices [0, 1, 2] and is therefore discarded. The last element would require calculating the function over indices [9, 10, 11] and is therefore discarded.

"fill"

Any window elements outside the data array are replaced by NaN. For example, with a window of length 3, y(1) = mean ([NaN, x(1:2)]), and y(end) = mean ([x(end-1:end), NaN]). This option usually results in y having NaN values at the boundaries, although it is influenced by how mean handles NaN, and also by the property "nancond".

user_value

Any window elements outside the data array are replaced by the specified value user_value which must be a numeric scalar. For example, with a window of length 3, y(1) = mean ([user_value, x(1:2)]), and y(end) = mean ([x(end-1:end), user_value]). A common choice for user_value is 0.

"same"

Any window elements outside the data array are replaced by the value of x at the boundary. For example, with a window of length 3, y(1) = mean ([x(1), x(1:2)]), and y(end) = mean ([x(end-1:end), x(end)]).

"periodic"

The window is wrapped so that any missing data elements are taken from the other side of the data. For example, with a window of length 3, y(1) = mean ([x(end), x(1:2)]), and y(end) = mean ([x(end-1:end), x(1)]).

"SamplePoints"

Caution: This option is not yet implemented.

Programming Note: This function is a wrapper which calls movfun. For additional options and documentation, See movfun.

See also: movfun, movslice, movmad, movmax, movmedian, movmin, movprod, movstd, movsum, movvar.

y = movmedian (x, wlen)
y = movmedian (x, [na, nb])
y = movmedian (…, dim)
y = movmedian (…, "nancond")
y = movmedian (…, property, value)

Calculate the moving median over a sliding window of length wlen on data x.

If wlen is a scalar, the function movmedian is applied to a moving window of length wlen. When wlen is an odd number the window is symmetric and includes (wlen - 1) / 2 elements on either side of the central element. For example, when calculating the output at index 5 with a window length of 3, movmedian uses data elements [4, 5, 6]. If wlen is an even number, the window is asymmetric and has wlen/2 elements to the left of the central element and wlen/2 - 1 elements to the right of the central element. For example, when calculating the output at index 5 with a window length of 4, movmedian uses data elements [3, 4, 5, 6].

If wlen is an array with two elements [nbna], the function is applied to a moving window -nb:na. This window includes nb number of elements before the current element and na number of elements after the current element. The current element is always included. For example, given wlen = [3, 0], the data used to calculate index 5 is [2, 3, 4, 5].

If the optional argument dim is given, operate along this dimension.

The optional string argument "nancond" controls whether NaN and NA values should be included ("includenan"), or excluded ("omitnan"), from the data passed to movmedian. The default is "includenan". Caution: the "omitnan" option is not yet implemented.

The calculation can be controlled by specifying property/value pairs. Valid properties are

"Endpoints"

This property controls how results are calculated at the boundaries (endpoints) of the window. Possible values are:

"shrink" (default)

The window is truncated at the beginning and end of the array to exclude elements for which there is no source data. For example, with a window of length 3, y(1) = movmedian (x(1:2)), and y(end) = movmedian (x(end-1:end)).

"discard"

Any y values that use a window extending beyond the original data array are deleted. For example, with a 10-element data vector and a window of length 3, the output will contain only 8 elements. The first element would require calculating the function over indices [0, 1, 2] and is therefore discarded. The last element would require calculating the function over indices [9, 10, 11] and is therefore discarded.

"fill"

Any window elements outside the data array are replaced by NaN. For example, with a window of length 3, y(1) = movmedian ([NaN, x(1:2)]), and y(end) = movmedian ([x(end-1:end), NaN]). This option usually results in y having NaN values at the boundaries, although it is influenced by how movmedian handles NaN, and also by the property "nancond".

user_value

Any window elements outside the data array are replaced by the specified value user_value which must be a numeric scalar. For example, with a window of length 3, y(1) = movmedian ([user_value, x(1:2)]), and y(end) = movmedian ([x(end-1:end), user_value]). A common choice for user_value is 0.

"same"

Any window elements outside the data array are replaced by the value of x at the boundary. For example, with a window of length 3, y(1) = movmedian ([x(1), x(1:2)]), and y(end) = movmedian ([x(end-1:end), x(end)]).

"periodic"

The window is wrapped so that any missing data elements are taken from the other side of the data. For example, with a window of length 3, y(1) = movmedian ([x(end), x(1:2)]), and y(end) = movmedian ([x(end-1:end), x(1)]).

"SamplePoints"

Caution: This option is not yet implemented.

Programming Note: This function is a wrapper which calls movfun. For additional options and documentation, See movfun.

See also: movfun, movslice, movmad, movmax, movmean, movmin, movprod, movstd, movsum, movvar.

y = movmin (x, wlen)
y = movmin (x, [na, nb])
y = movmin (…, dim)
y = movmin (…, "nancond")
y = movmin (…, property, value)

Calculate the moving minimum over a sliding window of length wlen on data x.

If wlen is a scalar, the function min is applied to a moving window of length wlen. When wlen is an odd number the window is symmetric and includes (wlen - 1) / 2 elements on either side of the central element. For example, when calculating the output at index 5 with a window length of 3, movmin uses data elements [4, 5, 6]. If wlen is an even number, the window is asymmetric and has wlen/2 elements to the left of the central element and wlen/2 - 1 elements to the right of the central element. For example, when calculating the output at index 5 with a window length of 4, movmin uses data elements [3, 4, 5, 6].

If wlen is an array with two elements [nbna], the function is applied to a moving window -nb:na. This window includes nb number of elements before the current element and na number of elements after the current element. The current element is always included. For example, given wlen = [3, 0], the data used to calculate index 5 is [2, 3, 4, 5].

If the optional argument dim is given, operate along this dimension.

The optional string argument "nancond" controls whether NaN and NA values should be included ("includenan"), or excluded ("omitnan"), from the data passed to min. The default is "includenan". Caution: the "omitnan" option is not yet implemented.

The calculation can be controlled by specifying property/value pairs. Valid properties are

"Endpoints"

This property controls how results are calculated at the boundaries (endpoints) of the window. Possible values are:

"shrink" (default)

The window is truncated at the beginning and end of the array to exclude elements for which there is no source data. For example, with a window of length 3, y(1) = min (x(1:2)), and y(end) = min (x(end-1:end)).

"discard"

Any y values that use a window extending beyond the original data array are deleted. For example, with a 10-element data vector and a window of length 3, the output will contain only 8 elements. The first element would require calculating the function over indices [0, 1, 2] and is therefore discarded. The last element would require calculating the function over indices [9, 10, 11] and is therefore discarded.

"fill"

Any window elements outside the data array are replaced by NaN. For example, with a window of length 3, y(1) = min ([NaN, x(1:2)]), and y(end) = min ([x(end-1:end), NaN]). This option usually results in y having NaN values at the boundaries, although it is influenced by how min handles NaN, and also by the property "nancond".

user_value

Any window elements outside the data array are replaced by the specified value user_value which must be a numeric scalar. For example, with a window of length 3, y(1) = min ([user_value, x(1:2)]), and y(end) = min ([x(end-1:end), user_value]). A common choice for user_value is 0.

"same"

Any window elements outside the data array are replaced by the value of x at the boundary. For example, with a window of length 3, y(1) = min ([x(1), x(1:2)]), and y(end) = min ([x(end-1:end), x(end)]).

"periodic"

The window is wrapped so that any missing data elements are taken from the other side of the data. For example, with a window of length 3, y(1) = min ([x(end), x(1:2)]), and y(end) = min ([x(end-1:end), x(1)]).

"SamplePoints"

Caution: This option is not yet implemented.

Programming Note: This function is a wrapper which calls movfun. For additional options and documentation, See movfun.

See also: movfun, movslice, movmad, movmax, movmean, movmedian, movprod, movstd, movsum, movvar.

y = movprod (x, wlen)
y = movprod (x, [na, nb])
y = movprod (…, dim)
y = movprod (…, "nancond")
y = movprod (…, property, value)

Calculate the moving product over a sliding window of length wlen on data x.

If wlen is a scalar, the function movprod is applied to a moving window of length wlen. When wlen is an odd number the window is symmetric and includes (wlen - 1) / 2 elements on either side of the central element. For example, when calculating the output at index 5 with a window length of 3, movprod uses data elements [4, 5, 6]. If wlen is an even number, the window is asymmetric and has wlen/2 elements to the left of the central element and wlen/2 - 1 elements to the right of the central element. For example, when calculating the output at index 5 with a window length of 4, movprod uses data elements [3, 4, 5, 6].

If wlen is an array with two elements [nbna], the function is applied to a moving window -nb:na. This window includes nb number of elements before the current element and na number of elements after the current element. The current element is always included. For example, given wlen = [3, 0], the data used to calculate index 5 is [2, 3, 4, 5].

If the optional argument dim is given, operate along this dimension.

The optional string argument "nancond" controls whether NaN and NA values should be included ("includenan"), or excluded ("omitnan"), from the data passed to movprod. The default is "includenan". Caution: the "omitnan" option is not yet implemented.

The calculation can be controlled by specifying property/value pairs. Valid properties are

"Endpoints"

This property controls how results are calculated at the boundaries (endpoints) of the window. Possible values are:

"shrink" (default)

The window is truncated at the beginning and end of the array to exclude elements for which there is no source data. For example, with a window of length 3, y(1) = movprod (x(1:2)), and y(end) = movprod (x(end-1:end)).

"discard"

Any y values that use a window extending beyond the original data array are deleted. For example, with a 10-element data vector and a window of length 3, the output will contain only 8 elements. The first element would require calculating the function over indices [0, 1, 2] and is therefore discarded. The last element would require calculating the function over indices [9, 10, 11] and is therefore discarded.

"fill"

Any window elements outside the data array are replaced by NaN. For example, with a window of length 3, y(1) = movprod ([NaN, x(1:2)]), and y(end) = movprod ([x(end-1:end), NaN]). This option usually results in y having NaN values at the boundaries, although it is influenced by how movprod handles NaN, and also by the property "nancond".

user_value

Any window elements outside the data array are replaced by the specified value user_value which must be a numeric scalar. For example, with a window of length 3, y(1) = movprod ([user_value, x(1:2)]), and y(end) = movprod ([x(end-1:end), user_value]). A common choice for user_value is 0.

"same"

Any window elements outside the data array are replaced by the value of x at the boundary. For example, with a window of length 3, y(1) = movprod ([x(1), x(1:2)]), and y(end) = movprod ([x(end-1:end), x(end)]).

"periodic"

The window is wrapped so that any missing data elements are taken from the other side of the data. For example, with a window of length 3, y(1) = movprod ([x(end), x(1:2)]), and y(end) = movprod ([x(end-1:end), x(1)]).

"SamplePoints"

Caution: This option is not yet implemented.

Programming Note: This function is a wrapper which calls movfun. For additional options and documentation, See movfun.

See also: movfun, movslice, movmad, movmax, movmean, movmedian, movmin, movstd, movsum, movvar.

y = movstd (x, wlen)
y = movstd (x, [na, nb])
y = movstd (…, dim)
y = movstd (…, "nancond")
y = movstd (…, property, value)

Calculate the moving standard deviation over a sliding window of length wlen on data x.

If wlen is a scalar, the function movstd is applied to a moving window of length wlen. When wlen is an odd number the window is symmetric and includes (wlen - 1) / 2 elements on either side of the central element. For example, when calculating the output at index 5 with a window length of 3, movstd uses data elements [4, 5, 6]. If wlen is an even number, the window is asymmetric and has wlen/2 elements to the left of the central element and wlen/2 - 1 elements to the right of the central element. For example, when calculating the output at index 5 with a window length of 4, movstd uses data elements [3, 4, 5, 6].

If wlen is an array with two elements [nbna], the function is applied to a moving window -nb:na. This window includes nb number of elements before the current element and na number of elements after the current element. The current element is always included. For example, given wlen = [3, 0], the data used to calculate index 5 is [2, 3, 4, 5].

If the optional argument dim is given, operate along this dimension.

The optional string argument "nancond" controls whether NaN and NA values should be included ("includenan"), or excluded ("omitnan"), from the data passed to movstd. The default is "includenan". Caution: the "omitnan" option is not yet implemented.

The calculation can be controlled by specifying property/value pairs. Valid properties are

"Endpoints"

This property controls how results are calculated at the boundaries (endpoints) of the window. Possible values are:

"shrink" (default)

The window is truncated at the beginning and end of the array to exclude elements for which there is no source data. For example, with a window of length 3, y(1) = movstd (x(1:2)), and y(end) = movstd (x(end-1:end)).

"discard"

Any y values that use a window extending beyond the original data array are deleted. For example, with a 10-element data vector and a window of length 3, the output will contain only 8 elements. The first element would require calculating the function over indices [0, 1, 2] and is therefore discarded. The last element would require calculating the function over indices [9, 10, 11] and is therefore discarded.

"fill"

Any window elements outside the data array are replaced by NaN. For example, with a window of length 3, y(1) = movstd ([NaN, x(1:2)]), and y(end) = movstd ([x(end-1:end), NaN]). This option usually results in y having NaN values at the boundaries, although it is influenced by how movstd handles NaN, and also by the property "nancond".

user_value

Any window elements outside the data array are replaced by the specified value user_value which must be a numeric scalar. For example, with a window of length 3, y(1) = movstd ([user_value, x(1:2)]), and y(end) = movstd ([x(end-1:end), user_value]). A common choice for user_value is 0.

"same"

Any window elements outside the data array are replaced by the value of x at the boundary. For example, with a window of length 3, y(1) = movstd ([x(1), x(1:2)]), and y(end) = movstd ([x(end-1:end), x(end)]).

"periodic"

The window is wrapped so that any missing data elements are taken from the other side of the data. For example, with a window of length 3, y(1) = movstd ([x(end), x(1:2)]), and y(end) = movstd ([x(end-1:end), x(1)]).

"SamplePoints"

Caution: This option is not yet implemented.

Programming Note: This function is a wrapper which calls movfun. For additional options and documentation, See movfun.

See also: movfun, movslice, movmad, movmax, movmean, movmedian, movmin, movprod, movsum, movvar.

y = movsum (x, wlen)
y = movsum (x, [na, nb])
y = movsum (…, dim)
y = movsum (…, "nancond")
y = movsum (…, property, value)

Calculate the moving sum over a sliding window of length wlen on data x.

If wlen is a scalar, the function movsum is applied to a moving window of length wlen. When wlen is an odd number the window is symmetric and includes (wlen - 1) / 2 elements on either side of the central element. For example, when calculating the output at index 5 with a window length of 3, movsum uses data elements [4, 5, 6]. If wlen is an even number, the window is asymmetric and has wlen/2 elements to the left of the central element and wlen/2 - 1 elements to the right of the central element. For example, when calculating the output at index 5 with a window length of 4, movsum uses data elements [3, 4, 5, 6].

If wlen is an array with two elements [nbna], the function is applied to a moving window -nb:na. This window includes nb number of elements before the current element and na number of elements after the current element. The current element is always included. For example, given wlen = [3, 0], the data used to calculate index 5 is [2, 3, 4, 5].

If the optional argument dim is given, operate along this dimension.

The optional string argument "nancond" controls whether NaN and NA values should be included ("includenan"), or excluded ("omitnan"), from the data passed to movsum. The default is "includenan". Caution: the "omitnan" option is not yet implemented.

The calculation can be controlled by specifying property/value pairs. Valid properties are

"Endpoints"

This property controls how results are calculated at the boundaries (endpoints) of the window. Possible values are:

"shrink" (default)

The window is truncated at the beginning and end of the array to exclude elements for which there is no source data. For example, with a window of length 3, y(1) = movsum (x(1:2)), and y(end) = movsum (x(end-1:end)).

"discard"

Any y values that use a window extending beyond the original data array are deleted. For example, with a 10-element data vector and a window of length 3, the output will contain only 8 elements. The first element would require calculating the function over indices [0, 1, 2] and is therefore discarded. The last element would require calculating the function over indices [9, 10, 11] and is therefore discarded.

"fill"

Any window elements outside the data array are replaced by NaN. For example, with a window of length 3, y(1) = movsum ([NaN, x(1:2)]), and y(end) = movsum ([x(end-1:end), NaN]). This option usually results in y having NaN values at the boundaries, although it is influenced by how movsum handles NaN, and also by the property "nancond".

user_value

Any window elements outside the data array are replaced by the specified value user_value which must be a numeric scalar. For example, with a window of length 3, y(1) = movsum ([user_value, x(1:2)]), and y(end) = movsum ([x(end-1:end), user_value]). A common choice for user_value is 0.

"same"

Any window elements outside the data array are replaced by the value of x at the boundary. For example, with a window of length 3, y(1) = movsum ([x(1), x(1:2)]), and y(end) = movsum ([x(end-1:end), x(end)]).

"periodic"

The window is wrapped so that any missing data elements are taken from the other side of the data. For example, with a window of length 3, y(1) = movsum ([x(end), x(1:2)]), and y(end) = movsum ([x(end-1:end), x(1)]).

"SamplePoints"

Caution: This option is not yet implemented.

Programming Note: This function is a wrapper which calls movfun. For additional options and documentation, See movfun.

See also: movfun, movslice, movmad, movmax, movmean, movmedian, movmin, movprod, movstd, movvar.

y = movvar (x, wlen)
y = movvar (x, [na, nb])
y = movvar (…, dim)
y = movvar (…, "nancond")
y = movvar (…, property, value)

Calculate the moving variance over a sliding window of length wlen on data x.

If wlen is a scalar, the function var is applied to a moving window of length wlen. When wlen is an odd number the window is symmetric and includes (wlen - 1) / 2 elements on either side of the central element. For example, when calculating the output at index 5 with a window length of 3, movvar uses data elements [4, 5, 6]. If wlen is an even number, the window is asymmetric and has wlen/2 elements to the left of the central element and wlen/2 - 1 elements to the right of the central element. For example, when calculating the output at index 5 with a window length of 4, movvar uses data elements [3, 4, 5, 6].

If wlen is an array with two elements [nbna], the function is applied to a moving window -nb:na. This window includes nb number of elements before the current element and na number of elements after the current element. The current element is always included. For example, given wlen = [3, 0], the data used to calculate index 5 is [2, 3, 4, 5].

If the optional argument dim is given, operate along this dimension.

The optional string argument "nancond" controls whether NaN and NA values should be included ("includenan"), or excluded ("omitnan"), from the data passed to var. The default is "includenan". Caution: the "omitnan" option is not yet implemented.

The calculation can be controlled by specifying property/value pairs. Valid properties are

"Endpoints"

This property controls how results are calculated at the boundaries (endpoints) of the window. Possible values are:

"shrink" (default)

The window is truncated at the beginning and end of the array to exclude elements for which there is no source data. For example, with a window of length 3, y(1) = var (x(1:2)), and y(end) = var (x(end-1:end)).

"discard"

Any y values that use a window extending beyond the original data array are deleted. For example, with a 10-element data vector and a window of length 3, the output will contain only 8 elements. The first element would require calculating the function over indices [0, 1, 2] and is therefore discarded. The last element would require calculating the function over indices [9, 10, 11] and is therefore discarded.

"fill"

Any window elements outside the data array are replaced by NaN. For example, with a window of length 3, y(1) = var ([NaN, x(1:2)]), and y(end) = var ([x(end-1:end), NaN]). This option usually results in y having NaN values at the boundaries, although it is influenced by how var handles NaN, and also by the property "nancond".

user_value

Any window elements outside the data array are replaced by the specified value user_value which must be a numeric scalar. For example, with a window of length 3, y(1) = var ([user_value, x(1:2)]), and y(end) = var ([x(end-1:end), user_value]). A common choice for user_value is 0.

"same"

Any window elements outside the data array are replaced by the value of x at the boundary. For example, with a window of length 3, y(1) = var ([x(1), x(1:2)]), and y(end) = var ([x(end-1:end), x(end)]).

"periodic"

The window is wrapped so that any missing data elements are taken from the other side of the data. For example, with a window of length 3, y(1) = var ([x(end), x(1:2)]), and y(end) = var ([x(end-1:end), x(1)]).

"SamplePoints"

Caution: This option is not yet implemented.

Programming Note: This function is a wrapper which calls movfun. For additional options and documentation, See movfun.

See also: movfun, movslice, movmad, movmax, movmean, movmedian, movmin, movprod, movstd, movsum.

© 1996–2020 John W. Eaton
Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.
Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions.
https://octave.org/doc/v5.2.0/Statistics-on-Sliding-Windows-of-Data.html