help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Weighted Moving Average


From: Przemek Klosowski
Subject: Re: Weighted Moving Average
Date: Fri, 10 Aug 2007 11:20:56 -0400 (EDT)

   Hi, I can do a function on Weighted Moving Average where the value are
   take in automatic mode? this my idea

   #y=[y1,y2,y3,y4,y5]
   function wma(y)
   (y1+2*y2+y3)/4
   (y2+2*y3+y4)/4
   etc
   etc
   end function

   I could not repeat the formula (y1+2*y2+y3)/4 (because if the long of
   vector is different i must change the function) but have only one
   formula that use the formula for all values of vector

You have to think about your data in a different way if you want to
use the Matlab/octave efficiently. The data is represented as vectors
or matrices, and you should do all operations on the entire
data---don't think of elements y1, y2, etc.  but rather deal with the
entire vector y. You will have to mix up vector elements from
different positions, so you need to construct shifted versions of the
vector.  For instance, y(2:end) is the vector whose first element is
the second element of y.

When you do it this way, it forces you to recognize various problems
that get swept under the rug otherwise; for instance, what is the
meaning of your weighted average for y1, which does not have an
earlier data point?

One approach might be to duplicate the first and last point:

   temp = [y(1) y y(end)]
   average = (temp(1:end-2)+2*temp(2:end-1)+temp(3:end))/4

or give up and admit that you can only calculate the average on a
subset of points:

   average=(y(1:end-2) + 2*y(2:end-1) + y(3:end))/4

There is an octave function called filter() which can apply arbitrary
linear filter; it's fairly complicated because it allows linear
feedback which you aren't interested in so you'd use a specific form
of a feedback vector b=[1 0 0 0 0 0...]

   average = filter([1 2 1]/4,1,y) )

Finally, Octave has some filters built in; my favorite is the
Savitzky-Golay 2-moment preserving filter

        average = sgolayfilt(y,2,3)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]