To use:
import filters
Module filters
This module allows you to filter sensor data using various linear and non-linear filtering techniques.
# A class to perform simple first order high pass filtering on a sensor value
class HighPassFilter
# Create a high pass filter object with a given alpha.
def __init__(self,alpha)
# Process a new value with this filter object and return the output value
def on_value(self,value_in)
# Make a high-pass filter with this cutoff frequency.
@staticmethod
def make_from_cutoff(cutoff_frequency,time_between_samples)
# Make a high-pass filter with a particular time constant
@staticmethod
def make_from_time_constant(time_constant,time_between_samples)
# A class to perform simple first order lowpass filtering on a sensor value
class LowPassFilter
# Create a low pass filter object with a given alpha.
def __init__(self,alpha)
# Process a new value with this filter object and return the output value
def on_value(self,value_in)
# Make a low-pass filter with this cutoff frequency .
@staticmethod
def make_from_cutoff(cutoff_frequency,time_between_samples)
# Make a low-pass filter with a particular time constant.
@staticmethod
def make_from_time_constant(time_constant,time_between_samples)
# A class to perform median filtering on a sensor value
class MedianFilter
# Create a median filter object with a given block size
def __init__(self,block_size)
# Process a new value with this filter object and return the output value
def on_value(self,new_value)
# A class to perform a sliding average (mean)
class SlidingAverageFilter
# Create a sliding average object with a given block size
def __init__(self,block_size)
# Process a new value with this filter object and return the output value
def on_value(self,new_value)
# A class to perform a mean (average) on blocks of sensor data
class BlockMeanFilter
# Process a new value with this filter object and return the output value
def on_value(self,new_value)
# A class to perform a median over blocks of sensor values.
class BlockMedianFilter
# Process a new value with this filter object and return the output value
def on_value(self,new_value)
# A class to perform a maximum on blocks of data
class BlockMaxFilter
# Process a new value with this filter object and return the output value
def on_value(self,new_value)
# A class to perform a minimum on blocks of data
class BlockMinFilter
# Process a new value with this filter object and return the output value
def on_value(self,new_value)
class filters.HighPassFilter
class HighPassFilter:
# Create a high pass filter object with a given alpha.
def __init__(self,alpha)
# Process a new value with this filter object and return the output value
def on_value(self,value_in)
# Make a high-pass filter with this cutoff frequency.
@staticmethod
def make_from_cutoff(cutoff_frequency,time_between_samples)
# Make a high-pass filter with a particular time constant
@staticmethod
def make_from_time_constant(time_constant,time_between_samples)
Description
A class to perform simple first order high pass filtering on a sensor value
filters.HighPassFilter.init
# filters.HighPassFilter.__init__
def __init__(
self,
alpha
)
Create a high pass filter object with a given alpha.
You may want to calculate alpha based on cutoff frequency
or time constant, in that case, you can use the static methods
make_from_cutoff
and make_from_time_constant
to make the filter
instead of using the constructor.
Parameters
- alpha(float)
The filter constant alpha
Returns
- filter(HighPassFilter)
A high pass filter object
filters.HighPassFilter.on_value
# filters.HighPassFilter.on_value
def on_value(
self,
value_in
)
Process a new value with this filter object and return the output value
Parameters
- value_in(float)
Input value to filter
Returns
- filtered_value(float)
Filter output value
filters.HighPassFilter.make_from_cutoff
# filters.HighPassFilter.make_from_cutoff
@staticmethod
def make_from_cutoff(
cutoff_frequency,
time_between_samples
)
Make a high-pass filter with this cutoff frequency.
Parameters
- cutoff_frequency(float)
Cutoff frequency in HZ - time_between_samples(float)
seconds between samples
Returns
- filter(HighPassFilter)
High-pass filter object
filters.HighPassFilter.make_from_time_constant
# filters.HighPassFilter.make_from_time_constant
@staticmethod
def make_from_time_constant(
time_constant,
time_between_samples
)
Make a high-pass filter with a particular time constant
Parameters
- time_constant(float)
Time constant in seconds - time_between_samples(float)
Seconds between samples
Returns
- filter(HighPassFilter)
High-pass filter object
class filters.LowPassFilter
class LowPassFilter:
# Create a low pass filter object with a given alpha.
def __init__(self,alpha)
# Process a new value with this filter object and return the output value
def on_value(self,value_in)
# Make a low-pass filter with this cutoff frequency .
@staticmethod
def make_from_cutoff(cutoff_frequency,time_between_samples)
# Make a low-pass filter with a particular time constant.
@staticmethod
def make_from_time_constant(time_constant,time_between_samples)
Description
A class to perform simple first order lowpass filtering on a sensor value
filters.LowPassFilter.init
# filters.LowPassFilter.__init__
def __init__(
self,
alpha
)
Create a low pass filter object with a given alpha.
You may want to calculate alpha based on cutoff frequency
or time constant, in that case, you can use the static methods
make_from_cutoff
and make_from_time_constant
to make the filter
instead of using the constructor.
Parameters
- alpha(float)
The filter constant alpha
Returns
- filter(LowPassFilter)
A low pass filter object
filters.LowPassFilter.on_value
# filters.LowPassFilter.on_value
def on_value(
self,
value_in
)
Process a new value with this filter object and return the output value
Parameters
- value_in(float)
Input value to filter
Returns
- filtered_value(float)
Filter output value
filters.LowPassFilter.make_from_cutoff
# filters.LowPassFilter.make_from_cutoff
@staticmethod
def make_from_cutoff(
cutoff_frequency,
time_between_samples
)
Make a low-pass filter with this cutoff frequency .
Parameters
- cutoff_frequency(float)
Cutoff frequency in HZ - time_between_samples(float)
Seconds between samples
Returns
- filter(LowPassFilter)
Low-pass filter object
filters.LowPassFilter.make_from_time_constant
# filters.LowPassFilter.make_from_time_constant
@staticmethod
def make_from_time_constant(
time_constant,
time_between_samples
)
Make a low-pass filter with a particular time constant.
Parameters
- time_constant(float)
Time constant in seconds - time_between_samples(float)
Seconds between samples
Returns
- filter(LowPassFilter)
Low-pass filter object
class filters.MedianFilter
class MedianFilter:
# Create a median filter object with a given block size
def __init__(self,block_size)
# Process a new value with this filter object and return the output value
def on_value(self,new_value)
Description
A class to perform median filtering on a sensor value
filters.MedianFilter.init
# filters.MedianFilter.__init__
def __init__(
self,
block_size
)
Create a median filter object with a given block size
Parameters
- block_size(int)
The number of previous samples that we take a median over
filters.MedianFilter.on_value
# filters.MedianFilter.on_value
def on_value(
self,
new_value
)
Process a new value with this filter object and return the output value
Parameters
- value_in(float)
Input value to filter
Returns
- filtered_value(float)
Median of current sliding buffer
class filters.SlidingAverageFilter
class SlidingAverageFilter:
# Create a sliding average object with a given block size
def __init__(self,block_size)
# Process a new value with this filter object and return the output value
def on_value(self,new_value)
Description
A class to perform a sliding average (mean) on a sensor value
filters.SlidingAverageFilter.init
# filters.SlidingAverageFilter.__init__
def __init__(
self,
block_size
)
Create a sliding average object with a given block size
Parameters
- block_size(int)
The number of previous samples that we sliding average over
filters.SlidingAverageFilter.on_value
# filters.SlidingAverageFilter.on_value
def on_value(
self,
new_value
)
Process a new value with this filter object and return the output value
Parameters
- value_in(float)
Input value to filter
Returns
- filtered_value(float)
Mean of current sliding average buffer
class filters.BlockMeanFilter
class BlockMeanFilter:
# Process a new value with this filter object and return the output value
def on_value(self,new_value)
Description
A class to perform a mean (average) on blocks of sensor data
Sensor values are only output once per block_size input samples.
filters.BlockMeanFilter.on_value
# filters.BlockMeanFilter.on_value
def on_value(
self,
new_value
)
Process a new value with this filter object and return the output value
Parameters
- value_in(float)
Input value to filter
Returns
- filtered_value(float)
Mean of the current block, or None if we are mid-block
class filters.BlockMedianFilter
class BlockMedianFilter:
# Process a new value with this filter object and return the output value
def on_value(self,new_value)
Description
A class to perform a median over blocks of sensor values.
Sensor values are only output once per block_size input samples.
filters.BlockMedianFilter.on_value
# filters.BlockMedianFilter.on_value
def on_value(
self,
new_value
)
Process a new value with this filter object and return the output value
Parameters
- value_in(float)
Input value to filter
Returns
- filtered_value(float or None)
Median of the current block, or None if we are mid block
class filters.BlockMaxFilter
class BlockMaxFilter:
# Process a new value with this filter object and return the output value
def on_value(self,new_value)
Description
A class to perform a maximum on blocks of data
Sensor values are only output once per block_size input samples.
filters.BlockMaxFilter.on_value
# filters.BlockMaxFilter.on_value
def on_value(
self,
new_value
)
Process a new value with this filter object and return the output value
Parameters
- value_in(float)
Input value to filter
Returns
- filtered_value(float)
Maximum of this block, or None if mid block
class filters.BlockMinFilter
class BlockMinFilter:
# Process a new value with this filter object and return the output value
def on_value(self,new_value)
Description
A class to perform a minimum on blocks of data
Sensor values are only output once per block_size input samples.
filters.BlockMinFilter.on_value
# filters.BlockMinFilter.on_value
def on_value(
self,
new_value
)
Process a new value with this filter object and return the output value
Parameters
- value_in(float)
Input value to filter
Returns
- filtered_value(float or None)
Minimum of this block, or None if mid block