trigger_analog: Add filters individually in DigitalFilter
Add new add_lowpass(), add_highpass(), add_notch(), and setup_initial_state() methods to DigitalFilter. Use these instead of creating the filters from the constructor. Also remove the unused filtfilt() method. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
1 parent
12097eb4a9
commit
f05c66cb9e
3 files changed
+35
-40
No files matched your search
@@ -160,9 +160,14 @@ class ContinuousTareFilter:
|
||||
|
||||
# create a filter design from the parameters
|
||||
def design_filter(self, error_func):
|
||||
return trigger_analog.DigitalFilter(self.sps, error_func, self.drift,
|
||||
self.drift_delay, self.buzz, self.buzz_delay, self.notches,
|
||||
self.notch_quality)
|
||||
df = trigger_analog.DigitalFilter(self.sps, error_func)
|
||||
if self.drift:
|
||||
df.add_highpass(self.drift, self.drift_delay)
|
||||
if self.buzz:
|
||||
df.add_lowpass(self.buzz, self.buzz_delay)
|
||||
for notch in self.notches:
|
||||
df.add_notch(notch, self.notch_quality)
|
||||
return df
|
||||
|
||||
|
||||
# Combine ContinuousTareFilter and SosFilter into an easy-to-use class
|
||||
|
||||
@@ -513,8 +513,8 @@ class EddyTap:
|
||||
# Create sos filter "design"
|
||||
cfg_error = self._printer.config_error
|
||||
sps = self._sensor_helper.get_samples_per_second()
|
||||
design = trigger_analog.DigitalFilter(sps, cfg_error,
|
||||
lowpass=25.0, lowpass_order=4)
|
||||
design = trigger_analog.DigitalFilter(sps, cfg_error)
|
||||
design.add_lowpass(25.0, 4)
|
||||
# Create the derivative (sample to sample difference) post filter
|
||||
self._filter_design = trigger_analog.DerivativeFilter(design)
|
||||
# Create SOS filter
|
||||
|
||||
@@ -28,54 +28,44 @@ def to_fixed_32(value, frac_bits=0):
|
||||
|
||||
# Digital filter designer and container
|
||||
class DigitalFilter:
|
||||
def __init__(self, sps, cfg_error, highpass=None, highpass_order=1,
|
||||
lowpass=None, lowpass_order=1, notches=None, notch_quality=2.0):
|
||||
def __init__(self, sps, cfg_error):
|
||||
self.filter_sections = []
|
||||
self.initial_state = []
|
||||
self.initial_state = None
|
||||
self.sample_frequency = sps
|
||||
# an empty filter can be created without SciPi/numpy
|
||||
if not (highpass or lowpass or notches):
|
||||
return
|
||||
self.cfg_error = cfg_error
|
||||
def get_scipy_signal(self):
|
||||
try:
|
||||
import scipy.signal as signal
|
||||
import numpy
|
||||
except:
|
||||
raise cfg_error("DigitalFilter require the SciPy module")
|
||||
if highpass:
|
||||
self.filter_sections.extend(
|
||||
self._butter(highpass, "highpass", highpass_order))
|
||||
if lowpass:
|
||||
self.filter_sections.extend(
|
||||
self._butter(lowpass, "lowpass", lowpass_order))
|
||||
if notches is None:
|
||||
notches = []
|
||||
for notch_freq in notches:
|
||||
self.filter_sections.append(self._notch(notch_freq, notch_quality))
|
||||
if len(self.filter_sections) > 0:
|
||||
self.initial_state = signal.sosfilt_zi(self.filter_sections)
|
||||
|
||||
raise self.cfg_error("DigitalFilter require the SciPy module")
|
||||
return signal
|
||||
def add_highpass(self, highpass, highpass_order):
|
||||
f = self._butter(highpass, "highpass", highpass_order)
|
||||
self.filter_sections.extend(f)
|
||||
def add_lowpass(self, lowpass, lowpass_order):
|
||||
f = self._butter(lowpass, "lowpass", lowpass_order)
|
||||
self.filter_sections.extend(f)
|
||||
def add_notch(self, notch_freq, notch_quality):
|
||||
signal = self.get_scipy_signal()
|
||||
b, a = signal.iirnotch(notch_freq, Q=notch_quality,
|
||||
fs=self.sample_frequency)
|
||||
f = signal.tf2sos(b, a)[0]
|
||||
self.filter_sections.append(f)
|
||||
def setup_initial_state(self):
|
||||
if not self.filter_sections:
|
||||
return
|
||||
self.initial_state = signal.sosfilt_zi(self.filter_sections)
|
||||
def _butter(self, frequency, btype, order):
|
||||
import scipy.signal as signal
|
||||
signal = self.get_scipy_signal()
|
||||
return signal.butter(order, Wn=frequency, btype=btype,
|
||||
fs=self.sample_frequency, output='sos')
|
||||
|
||||
def _notch(self, freq, quality):
|
||||
import scipy.signal as signal
|
||||
b, a = signal.iirnotch(freq, Q=quality, fs=self.sample_frequency)
|
||||
return signal.tf2sos(b, a)[0]
|
||||
|
||||
def get_filter_sections(self):
|
||||
return self.filter_sections
|
||||
|
||||
def get_initial_state(self):
|
||||
if self.initial_state is None:
|
||||
return [[0., 0.]] * len(self.filter_sections)
|
||||
return self.initial_state
|
||||
|
||||
def filtfilt(self, data):
|
||||
import scipy.signal as signal
|
||||
import numpy
|
||||
data = numpy.array(data)
|
||||
return signal.sosfiltfilt(self.filter_sections, data)
|
||||
|
||||
# Produce sample to sample difference (derivative) of a DigitalFilter
|
||||
class DerivativeFilter:
|
||||
def __init__(self, main_filter):
|
||||
|
||||
Reference in new issue
Block a user