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