File Exchange

image thumbnail

running average and standard deviation of vectors

version 1.1 (2.19 KB) by

computes the running average and standard deviation of vectors or matrices for a given window size.

4 Downloads

Updated

View License

[running_avg, running_std] = running_mean_std( data, filter_width )
returns the running mean and standard deviations of either a vecotor or matrix.

for the case of data = vector of length N, it will return a running average and
standard deviation for a window of width filter_width. The output vectors have length N; however,
to avoid edge effects NAN's are placed in the front and end: running_avg(
[1:N, end+1-N:end]) = NAN.

for the case of data = vector of 2XN or NX2, it will return a running average and
standard deviations for the first row (column) and the second row (colum) for a window of width filter_width.
The output vectors have length N; however, to avoid edge effects NAN's are placed in the front and end: running_avg([1:N, end+1-N:end]) = NAN.

for the case of data = matrix MXN (i.e. a b/w picture), it will return a running average and
standard deviations for a filter, which is a filled circle with diameter filter_width.

Comments and Ratings (1)

Giovanni

I Have a suggestion to the code. It fixes only code for 1d vectors. Using std=sqrt(avg(X^2)-(avg(x))^2)) it is possible to reduce the amount of points discarded at the edges to half the window length.

Suggested code:

if size( data, 2) == 1
    target = ones(filter_width, 1)/filter_width;
    running_avg = conv2( data, target, 'same' );
    running_std = sqrt( conv2( (data).^2, target, 'same' )-running_avg.^2 );
    % cropping invalid region
    running_avg([1:filter_width/2, end-filter_width/2+1:end]) = nan;
    running_std([1:filter_width/2, end-filter_width/2+1:end]) = nan;

Updates

1.1

expanded the range of input parameters, to include matrices (i.e. pictures).
Also extended the examples.

MATLAB Release
MATLAB 7.4 (R2007a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video