Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #9895
| From | Matthias Odisio <matthias@wolfram.com> |
|---|---|
| Newsgroups | comp.soft-sys.math.mathematica |
| Subject | Re: moving average function |
| Date | 2012-05-01 09:23 +0000 |
| Organization | Steven M. Christensen and Associates, Inc and MathTensor, Inc. |
| Message-ID | <jnoa2a$53e$1@smc.vnet.net> (permalink) |
| References | <201204300840.EAA23629@smc.vnet.net> |
Bob,
On 4/30/12 3:40 AM, Robert McHugh wrote:
> Below is a moving average function that has the following features:
> 1. returns a list with the same length as the original length
> 2. provides a reasonable estimate for averages on the "sides" of the
> window.
>
> Have failed to figure out how to do this with ListConvolve and
> ListCorrelate, so I submit this with the hope that others can
> recommend how it might be improved. Also searched this website for
> alternatives but didn't find any that met the above criteria.
>
> I was motivated to do this in order to keep my code free of handling
> special cases related to the edges of the widow size. Note that in
> one particular case, I have data measured every minute and would like
> to compare the results of using averaging the data over window sizes
> of 61, 121, and 181.
>
> Recommendations for how to improve the function or alternatives are
> appreciated.
> Bob.
MeanFilter truncates the sliding window at the boundaries:
In[131]:= MeanFilter[listTest, 2]
Out[131]= {1/3 (a + b + c), 1/4 (a + b + c + d),
1/5 (a + b + c + d + e), 1/5 (b + c + d + e + f),
1/5 (c + d + e + f + g), 1/5 (d + e + f + g + h),
1/5 (e + f + g + h + i), 1/5 (f + g + h + i + j),
1/5 (g + h + i + j + k), 1/4 (h + i + j + k), 1/3 (i + j + k)}
Matthias Odisio
Wolfram Research
> movingAverageBalanced[list_List, nAvg_Integer?OddQ ] :=
> Module[{nHang, middle, left, right, all},
> nHang = (nAvg - 1)/2;
>
> middle = MovingAverage[list, nAvg];
>
> left = Total[ Take[list, 2 # - 1]] /(2 # - 1) & /@ Range[nHang];
> right =
> Reverse[Total[ Take[list, -( 2 # - 1)]] /(2 # - 1) & /@
> Range[nHang]];
> all = Join[left, middle, right] ;
> Return[all];
> ]
>
> Example
> listTest = {a, b, c, d, e, f, g, h, i, j, k};
> r = movingAverageBalanced[listTest, 5];
> r // TableForm
>
> {
> {a},
> {1/3 (a + b + c)},
> {1/5 (a + b + c + d + e)},
> {1/5 (b + c + d + e + f)},
> {1/5 (c + d + e + f + g)},
> {1/5 (d + e + f + g + h)},
> {1/5 (e + f + g + h + i)},
> {1/5 (f + g + h + i + j)},
> {1/5 (g + h + i + j + k)},
> {1/3 (i + j + k)},
> {k}
> }
>
Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread
Re: moving average function Matthias Odisio <matthias@wolfram.com> - 2012-05-01 09:23 +0000
csiph-web