View License

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

» Watch video

Highlights from
movingstd & movingstd2

4.8 | 14 ratings Rate this file 46 Downloads (last 30 days) File Size: 3.54 KB File ID: #9428 Version: 1.4

movingstd & movingstd2


John D'Errico (view profile)


21 Dec 2005 (Updated )

A (fast) windowed std on a time series (or array)

| Watch this File

File Information

Occasionally I see a request for computation of a running, windowed standard deviation. This is easily accomplished using filter and the alternative formula for the standard deviation:
std = sqrt((sum(x.^2) - n*xbar.^2)/(n-1)).
movingstd allows you to specify forward, backward or central windows of any desired length. It patches the ends, shortening the window as necessary.
movingstd2 is now included, allowing a moving window standard deviation on 2-dimensional arrays. (I suppose I could easily enough implement movingstdn if that becomes of interest.)


This file inspired Slidefun.

MATLAB release MATLAB 7.0.1 (R14SP1)
MATLAB Search Path
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (29)
24 Jul 2016 E. Cheynet

08 Apr 2016 John D'Errico

John D'Errico (view profile)

Just uploaded a new release which includes movingstd2. That tool only allows central windows. It would seem to make no sense at all to have a window that is a forward window in one dimension and central in the other dimension.

As well, the window size is always square, although that would be easily remedied if I see requests. The moving window is truncated around the edges as necessary.

Finally, which I did not write movingstdn, that too would be a trivial extension if there are people asking.

Comment only
08 Apr 2016 Robert

Robert (view profile)

Another nice function John, thanks. As suggested by Ka Ho Tam, would be nice if this works on 2D data. Would that be possible?

Comment only
29 Mar 2016 Ka Ho Tam

Would be nice if this works on 2D data.

Comment only
19 Aug 2015 Nathan Orloff

John... For what's worth, your functions kick butt!

Comment only
19 Aug 2015 Nathan Orloff

John... For what's worth, your functions kick butt!

16 Oct 2014 John D'Errico

John D'Errico (view profile)

Uploaded a new version to replace strmatch

Comment only
16 Oct 2014 Erik

Erik (view profile)

Very nice function and quite useful with the little extras.

Please note the strmatch function being used will be replaced in a future version of MATLAB. Replacing it with strncmp fixes the warning. Please update the file some time in the future.

14 Feb 2014 Maty

Maty (view profile)

Sorry my mistake

Comment only
14 Feb 2014 Maty

Maty (view profile)

The vector s contains all NaN. Probably the file needs an update for the newest MATLAB version.

Comment only
28 Jan 2014 John D'Errico

John D'Errico (view profile)

Keith - I just submitted a fix for that case. The new version should appear online in the morning. It happens because the result was essentially zero for some elements, but floating point arithmetic errors can drive a value less than zero, then a sqrt is taken.

Comment only
28 Jan 2014 Keith

Keith (view profile)

Great function, but I just ran into a complex standard deviation. Not sure if it's my end or not.

22 Jan 2014 John D'Errico

John D'Errico (view profile)

Lennart - No, filter definitely does not assume that. However, nothing stops you from using this code as is with a minor trick.

1. Copy the correct number of points (based on the window size) at each end of the series to the other end.

2. Compute the windowed SD

3. Delete the created end point values.

Comment only
22 Jan 2014 Lennart

If my series are periodical, the ends meet and the special case defined in this function is not correct. Does the filter function assume this and can I just delete the special case part?

02 Aug 2012 Reza Farrahi Moghaddam

07 May 2011 John D'Errico

John D'Errico (view profile)

Nayomi - If x is an image, I assume that you mean it is an array. Note that this function is defined explicitly for use on vector input x. If you desire to use it on each row (or column) of an array, you would use an external loop over the rows or columns.

Comment only
07 May 2011 nayomi ranamuka

I used this function as follows.
Image for x.
I= movingstd(x,k,windowmode)

Comment only
07 May 2011 nayomi ranamuka

Following error is generated while executing this code.
Error in ==> movingstd at 89
x = x - mean(x);

Please Can you suggest a solution for this problem.

Comment only
17 Jun 2010 John D'Errico

John D'Errico (view profile)

I'm unsure what is meant here. The behavior of filter at the edges is not material, since I explicitly repair those elements along the edges as is appropriate. This is already noted in the help:

% All windowmodes adjust the window width near the ends of
% the series as necessary.

The internal comments also indicate what was done to repair the behavior of filter at the ends.

So I think/thought that this question is already covered, but perhaps the help should be more explicit to reflect what is done. I'll submit a new version that does this.

Comment only
17 Jun 2010 Julien Emile-Geay

Very nice. Performs as advertised, and quickly!
Is a reflection condition imposed implicitly by "filter.m" at the edges ? If so it would be nice to mention, just so that users are aware of all assumptions.

24 Sep 2009 John D'Errico

John D'Errico (view profile)

Simplest is to replace the nans by interpolation. Use my inpaint_nans (also on the file exchange) to do this. Then apply this code to compute the moving standard deviation. High variance sections of the curve will still be high variance after this process. The virtue of this scheme is that both parts are fully vectorized codes.

The alternative is to use a loop, computing a windowed standard deviation on varying numbers of points by excluding the nan elements. This scheme may be tripped up if you have enough nan elements. Of course, with very many nan elements, no scheme will be perfect.

Comment only
24 Sep 2009 Rosie

Rosie (view profile)

This is just what I need except I have NaNs in my can I deal with these?

Comment only
11 Sep 2009 Adam

Adam (view profile)

20 Aug 2009 Ralph

Ralph (view profile)

23 Jul 2007 Sophia Zhao

09 Jun 2007 Israel Solares-Moya

fantastic.. just saved me a few hours!

22 Mar 2007 Mikko Pirinen

well made and very useful

16 Aug 2006 John O'Leary

21 Dec 2005 Michael Robbins


17 Jun 2010 1.1

Documentation change to explicitly state the behavior at the end of the series.

29 Jan 2014 1.2

Fix the case of complex std results due to floating point arithmetic.

16 Oct 2014 1.3

strncmpi instead of strmatch for tests
Pre-centered and pre-scaled now

08 Apr 2016 1.4

movingstd2 included

Contact us