Re: moving average function

*To*: mathgroup at smc.vnet.net*Subject*: [mg126347] Re: moving average function*From*: Dana DeLouis <dana01 at me.com>*Date*: Thu, 3 May 2012 04:35:58 -0400 (EDT)*Delivered-to*: l-mathgroup@mail-archive0.wolfram.com

> Have failed to figure out how to do this with ListConvolve and > ListCorrelate, Hi. Just some additional ideas. If you want to use ListCorrelate, and want to base decisions on Length, then zero's in the data could throw the results off. If you want to base it on Position, then you have to add logic to delete at both ends. Here's are two quick ideas if you wanted to go this route... {Based on Length... Intercept the Plus function by making it List} Fx[m_List,n_Integer]:=Module[{t,z}, t=ListCorrelate[Table[1,{n}],m,{n,-n},\[Infinity],Times,List] ; t=DeleteCases[t,\[Infinity],2]; t=DeleteCases[t,z_/;EvenQ[Length[z]]&& Length[z]<n]; Mean/@t ] {Based on Position} Gx[m_,n_]:=Module[{t,hx,r,k=Table[1,{n}]}, hx[x_,{y_}]:=x / Min[{y,n,Length[m]+n-y}]; t=MapIndexed[hx,ListCorrelate[k,m,{n,-n},0]]; r=List/@Range[2,n-1,2]; Delete[t,Join[r,-r]] ] data ={a,b,c,d,e,f,g,h,i,j,k}; Fx[data,5] 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 Same results for other function: Gx[data,5] == Fx[data,5] True // Easier to visualize... data2={1,2,3,4,5,0,5,4,3,2,1}; Fx[data2,5] {1,2,3,14/5,17/5,18/5,17/5,14/5,3,2,1} Fx[data2,5] == Gx[data2,5] True = = = = = = = = = = HTH :>) Dana DeLouis Mac & Math 8 = = = = = = = = = = On Apr 30, 4:42 am, Robert McHugh <rtmphon... at 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} >

**Re: Wrapping NDSolve within a function**

**Re: Importing large file into table and calculating takes a long time. How to improve efficiency?**

**Re: moving average function**

**Re: Question about Integration and citation**