File Exchange

image thumbnail

Moving variance

version 1.0 (358 Bytes) by

Calculates moving variance of a time series.

4.85714
7 Ratings

4 Downloads

Updated

No License

Moving variance

v=movingvar(x,m)

x is the timeseries.
m is the window length.
v is the variance.

Aslak Grinsted 2005

Comments and Ratings (7)

Paul Mellor

I believe n2 and m2 are the wrong way round: the longer sequence to remove is at the end not the start (try with a constant vector e.g. ones(1000,1)).

Also v can end up with a small negative value, which is not good for a variance. So I suggest:

v=filter2(f,x.^2)-filter2(f,x).^2;
n2=floor(m/2);
m2=ceil(m/2)-1;
v=max(0, v([zeros(1,m2)+m2+1,(m2+1):(n-n2),zeros(1,n2)+(n-n2)],:));

Tyler

Tyler (view profile)

Maty

Maty (view profile)

Ilayda

Ilayda (view profile)

xin wang

Jonathan Tinker

Excellent, does just what I needed, without using a loop. NB/ this is the biased variance.

MATLAB Release
MATLAB 7 (R14)
Acknowledgements

Inspired: downsample_ts, scatstat2 2D local statistics

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

» Watch video