Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #9897
| From | Ray Koopman <koopman@sfu.ca> |
|---|---|
| Newsgroups | comp.soft-sys.math.mathematica |
| Subject | Re: moving average function |
| Date | 2012-05-01 09:24 +0000 |
| Organization | Steven M. Christensen and Associates, Inc and MathTensor, Inc. |
| Message-ID | <jnoa47$547$1@smc.vnet.net> (permalink) |
| References | <jnlj94$n3k$1@smc.vnet.net> |
On Apr 30, 1:42 am, Robert McHugh <rtmphon...@gmail.com> 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.
>
> 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}
>
> }
bma[data_List, n_Integer] := bma[data, Table[1,{n}]]
bma[data_List, wts_List] :=
ListCorrelate[wts, data, {-1,1}, 0]/
ListCorrelate[wts, Table[1,{Length@data}], {-1,1}, 0]
bma[testList, 5]
a
(a + b)/2
(a + b + c)/3
(a + b + c + d)/4
(a + b + c + d + e)/5
(b + c + d + e + f)/5
(c + d + e + f + g)/5
(d + e + f + g + h)/5
(e + f + g + h + i)/5
(f + g + h + i + j)/5
(g + h + i + j + k)/5
(h + i + j + k)/4
(i + j + k)/3
(j + k)/2
k
bma[testList, {1,2,3,2,1}]
a
(2*a + b)/3
(3*a + 2*b + c)/6
(2*a + 3*b + 2*c + d)/8
(a + 2*b + 3*c + 2*d + e)/9
(b + 2*c + 3*d + 2*e + f)/9
(c + 2*d + 3*e + 2*f + g)/9
(d + 2*e + 3*f + 2*g + h)/9
(e + 2*f + 3*g + 2*h + i)/9
(f + 2*g + 3*h + 2*i + j)/9
(g + 2*h + 3*i + 2*j + k)/9
(h + 2*i + 3*j + 2*k)/8
(i + 2*j + 3*k)/6
(j + 2*k)/3
k
Back to comp.soft-sys.math.mathematica | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
moving average function Robert McHugh <rtmphone09@gmail.com> - 2012-04-30 08:42 +0000
Re: moving average function Ray Koopman <koopman@sfu.ca> - 2012-05-01 09:24 +0000
Re: moving average function rtmphone09@gmail.com - 2012-05-02 09:47 +0000
Re: moving average function Ray Koopman <koopman@sfu.ca> - 2012-05-03 08:41 +0000
Re: moving average function rtmphone09@gmail.com - 2012-05-03 08:34 +0000
Re: moving average function "Oleksandr Rasputinov" <oleksandr_rasputinov@ymail.com> - 2012-05-01 09:25 +0000
csiph-web