[Top][All Lists]

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

Re: filter() not giving me correct answers for first n-1 days

From: James Sherman Jr.
Subject: Re: filter() not giving me correct answers for first n-1 days
Date: Fri, 17 Sep 2010 00:07:54 -0400

Hi Tim,

Someone a few months ago had a similar problem with the filter function.  The problem is that the 4th argument to the filter function (the initial conditions of the filter, SI it is called) don't specify x(n) for n < 0, like you would intuitively think so.  They represent the internal states of the filter, which are less than intuitive to say the least. (And isn't documented in the help text of the filter function).

Anyway, the essence is that to make a "more" efficient filter you can implement a filter where there are these internal states.  And you need to do a precomputing step if you want to find the initial states.  These are just back of the envelope calculations, so I provide no guarantees.  I also assume that a = 1 (i.e. no feedback) which is true for a moving average filter as you have in your example.

Let, b be the vectors of coefficients in your filter be of length n and initial_x be a vector of the values from t = -n+1 to t=-1.  Then, I did

tmp_var = conv(initial_x, b);
si = tmp_var(n:end);

z = filter(b, 1, x, si);

I think should give you want you want.  I just tested this in the corner case where I set all the initial_x variables were constant (so I'd know that the moving average of them should be that constant) and it behaved as expected.  But like I said before, I'd use with caution till you try it out a bit.  I make no guarantees.

It should be possible (and probably pretty easy, but I'm just not seeing it now) to allow an a coefficient vector thats not equal to 1 and still solve for the si variable, but it just hit midnight here and I need some sleep.

I hope this helps.

On Thu, Sep 16, 2010 at 2:41 PM, Tim Rueth <address@hidden> wrote:
Octave experts:
I have been using filter() to compute a weighted moving average, ma1, of a vector, cls_v, with a weighting factor, alpha.  I just found out that the first num_days-1 of the moving average are not correct.  I'm assuming I don't have a filter coefficient quite correct or something, but I'm stuck.  I tried to understand the formula in help, but got lost.  Any takers?
Here's my current code:
    coef_v = (num_days : -1 : 1).^alpha;
    coef_v = coef_v/sum(coef_v);               # sum vector coefficients to equal 1
    si = repmat(cls_v(1), 1, num_days - 1);
    ma1 = filter(coef_v, 1, cls_v, si);

Help-octave mailing list

reply via email to

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