Skip to content

[BUG] LowPassFilter returns wrong values at startupΒ #433

Open
@sylque

Description

@sylque

Not sure what kind of actual impact this might have on the rest of the library, but I believe there is something wrong in the LowPassFilter class:

LowPassFilter::LowPassFilter(float time_constant)
    : ...
    , y_prev(0.0f)
{
    ...
}


float LowPassFilter::operator() (float x)
{
    ...
    float y = alpha*y_prev + (1.0f - alpha)*x;
    y_prev = y;
	...
    return y;
}

Because y_prev is initialized to zero, the first calls to operator() returns the wrong result. At the first call, y_prev should probably be initialized with x instead of zero. In other words, the first call to LowPassFilter(5) should return 5 and not a weighted average of 5 with zero.

Notice that, in LowPassFilter::operator(), you can also see:

if(dt > 0.3f) {
        y_prev = x;
        timestamp_prev = timestamp;
        return x;
    }

So if you are lucky enough to have spent 0.3 seconds between the class construction and the first call to operator(), then y_prev is correctly initialized.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions