Code covered by the BSD License  

Highlights from
running average and standard deviation of vectors

Be the first to rate this file! 19 Downloads (last 30 days) File Size: 2.19 KB File ID: #42994
image thumbnail

running average and standard deviation of vectors

by

 

09 Aug 2013 (Updated )

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

| Watch this File

File Information
Description

[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.

Required Products MATLAB
MATLAB release MATLAB 7.4 (R2007a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (1)
23 Apr 2014 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
09 Aug 2013

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

Contact us