Re: Moving average type process
- To: mathgroup at smc.vnet.net
- Subject: [mg18277] Re: Moving average type process
- From: Daniel Lichtblau <danl at wolfram.com>
- Date: Fri, 25 Jun 1999 15:05:23 -0400
- Organization: Wolfram Research, Inc.
- References: <7kpbdq$4at@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Virgil Stokes wrote:
>
> I wish to perform the following "moving average" type process on
> a list to generate a new list:
>
> Inputs:
> wtlist = {w1,w2,w3} -- weight list
> inlist = {a,b,c,d,e,f} -- any other list (>= 3 elements)
>
> Output:
> outlist = {w1*a+w2*b+w3*c, w1*b+w2*c+w3*d, w1*c+w2*d+w3*e, w1*d+w2*e+w3*f}
>
> Note, outlist will always contain 2 less (Length[wtlist]/2) elements
> than in the input list (inlist).
>
> If w1=w2=w3=x, then
> the following works fine:
>
> outlist = x*Drop[Plus@@NestList[RotateRight,inlist,2],2]
>
> This is a weighted (from wtlist) sum over another list of arbitrary
> length (inlist). I would like to get a "fast" function for doing this when
> the weights are not equal.
>
> -- Virgil
In version 4 one can use ListCorrelate to do this directly.
In[4]:= ??ListCorrelate
ListCorrelate[ker, list] forms the correlation of the kernel ker with
list.
ListCorrelate[ker, list, k] forms the cyclic correlation in which the
kth
element of ker is aligned with each element in list.
ListCorrelate[ker,
list, {kL, kR}] forms the cyclic correlation whose first element
contains
list[[1]] ker[[kL]] and whose last element contains list[[-1]]
ker[[kR]].
ListCorrelate[ker, list, klist, p] forms the correlation in which
list is
padded at each end with repetitions of the element p.
ListCorrelate[ker,
list, klist, {p1, p2, ... }] forms the correlation in which list is
padded
at each end with cyclic repetitions of the pi. ListCorrelate[ker,
list,
klist, padding, g, h] forms a generalized correlation in which g is
used in
place of Times and h in place of Plus. ListCorrelate[ker, list,
klist,
padding, g, h, lev] forms a correlation using elements at level lev
in ker
and list.
Attributes[ListCorrelate] = {Protected}
In[5]:= wtlist = {w1,w2,w3};
In[6]:= inlist = {a,b,c,d,e,f};
In[7]:= outlist = ListCorrelate[wtlist, inlist]
Out[7]= {a w1 + b w2 + c w3, b w1 + c w2 + d w3, c w1 + d w2 + e w3,
> d w1 + e w2 + f w3}
Daniel Lichtblau
Wolfram Research