LinearFilter

class wpimath.filter.LinearFilter(ffGains: List[float], fbGains: List[float])

Bases: pybind11_object

This class implements a linear, digital filter. All types of FIR and IIR filters are supported. Static factory methods are provided to create commonly used types of filters.

Filters are of the form: y[n] = (b0 x[n] + b1 x[n-1] + … + bP x[n-P]) - (a0 y[n-1] + a2 y[n-2] + … + aQ y[n-Q])

Where: y[n] is the output at time “n” x[n] is the input at time “n” y[n-1] is the output from the LAST time step (“n-1”) x[n-1] is the input from the LAST time step (“n-1”) b0 … bP are the “feedforward” (FIR) gains a0 … aQ are the “feedback” (IIR) gains IMPORTANT! Note the “-” sign in front of the feedback term! This is a common convention in signal processing.

What can linear filters do? Basically, they can filter, or diminish, the effects of undesirable input frequencies. High frequencies, or rapid changes, can be indicative of sensor noise or be otherwise undesirable. A “low pass” filter smooths out the signal, reducing the impact of these high frequency components. Likewise, a “high pass” filter gets rid of slow-moving signal components, letting you detect large changes more easily.

Example FRC applications of filters: - Getting rid of noise from an analog sensor input (note: the roboRIO’s FPGA can do this faster in hardware) - Smoothing out joystick input to prevent the wheels from slipping or the robot from tipping - Smoothing motor commands so that unnecessary strain isn’t put on electrical or mechanical components - If you use clever gains, you can make a PID controller out of this class!

For more on filters, we highly recommend the following articles: https://en.wikipedia.org/wiki/Linear_filter https://en.wikipedia.org/wiki/Iir_filter https://en.wikipedia.org/wiki/Fir_filter

Note 1: Calculate() should be called by the user on a known, regular period. You can use a Notifier for this or do it “inline” with code in a periodic function.

Note 2: For ALL filters, gains are necessarily a function of frequency. If you make a filter that works well for you at, say, 100Hz, you will most definitely need to adjust the gains if you then want to run it at 200Hz! Combining this with Note 1 - the impetus is on YOU as a developer to make sure Calculate() gets called at the desired, constant frequency!

Create a linear FIR or IIR filter.

Parameters:
  • ffGains – The “feedforward” or FIR gains.

  • fbGains – The “feedback” or IIR gains.

calculate(input: float) float

Calculates the next value of the filter.

Parameters:

input – Current input value.

Returns:

The filtered value at this step

static highPass(timeConstant: float, period: wpimath.units.seconds) wpimath.filter._filter.LinearFilter

Creates a first-order high-pass filter of the form: y[n] = gain x[n] + (-gain) x[n-1] + gain y[n-1] where gain = e:sup:-dt / T, T is the time constant in seconds

Note: T = 1 / (2 pi f) where f is the cutoff frequency in Hz, the frequency below which the input starts to attenuate.

This filter is stable for time constants greater than zero.

Parameters:
  • timeConstant – The discrete-time time constant in seconds.

  • period – The period in seconds between samples taken by the user.

lastValue() float

Returns the last value calculated by the LinearFilter.

Returns:

The last value.

static movingAverage(taps: int) wpimath.filter._filter.LinearFilter

Creates a K-tap FIR moving average filter of the form: y[n] = 1/k (x[k] + x[k-1] + … + x[0])

This filter is always stable.

Parameters:

taps – The number of samples to average over. Higher = smoother but slower @throws std::runtime_error if number of taps is less than 1.

reset(*args, **kwargs)

Overloaded function.

  1. reset(self: wpimath.filter._filter.LinearFilter) -> None

Reset the filter state.

  1. reset(self: wpimath.filter._filter.LinearFilter, inputBuffer: List[float], outputBuffer: List[float]) -> None

Resets the filter state, initializing internal buffers to the provided values.

These are the expected lengths of the buffers, depending on what type of linear filter used:

<table> <tr> <th>Type</th> <th>Input Buffer Size</th> <th>Output Buffer Size</th> </tr> <tr> <td>Unspecified</td> <td>size of ``ffGains``</td> <td>size of ``fbGains``</td> </tr> <tr> <td>Single Pole IIR</td> <td>1</td> <td>1</td> </tr> <tr> <td>High-Pass</td> <td>2</td> <td>1</td> </tr> <tr> <td>Moving Average</td> <td>``taps``</td> <td>0</td> </tr> <tr> <td>Finite Difference</td> <td>size of ``stencil``</td> <td>0</td> </tr> <tr> <td>Backward Finite Difference</td> <td>``Samples``</td> <td>0</td> </tr> </table>

Parameters:
  • inputBuffer – Values to initialize input buffer.

  • outputBuffer – Values to initialize output buffer. @throws std::runtime_error if size of inputBuffer or outputBuffer does not match the size of ffGains and fbGains provided in the constructor.

static singlePoleIIR(timeConstant: float, period: wpimath.units.seconds) wpimath.filter._filter.LinearFilter

Creates a one-pole IIR low-pass filter of the form: y[n] = (1 - gain) x[n] + gain y[n-1] where gain = e:sup:-dt / T, T is the time constant in seconds

Note: T = 1 / (2 pi f) where f is the cutoff frequency in Hz, the frequency above which the input starts to attenuate.

This filter is stable for time constants greater than zero.

Parameters:
  • timeConstant – The discrete-time time constant in seconds.

  • period – The period in seconds between samples taken by the user.