Skip to content

Filter your signals

Full example code: examples/signal/signals_filters.py

To process our signals, we can apply filters to them. For a list of available filters

# Show available filters
print(Signals.__filters__)
[
    'crosscorrelation',
    'derivative',
    'envelope',
    'filtfilt',
    'farfield',
    'movingaverage',
    'normalization',
    'removebaseline',
    'removeharmonics',
    'roney',
    'upperthreshold',
    'hermitesplinenormalization'
]

For more info on what each filter does and which parameters are needed, please check our API Reference guide.

Applying filters

To apply a filter, simply set the needed parameters and call the apply_filter or apply_filters method.

ecg_signals = Signals(ecg_array)
ecg_signals.set_averaging_window_length(20)
ecg_signals.apply_filter('normalization')
ecg_signals.apply_filter('moving_average')

Or multiple filters in one call.

filters = ['normalization', 'moving_average']
ecg_signals.apply_filters(filters)

Add your own filter

You can easily add your own filter by creating a child class of SignalFilter. In the basic example below, our custom filter applies two existing filters consecutively. You can manipulate the signal data in any way you wish though, as long as your filter method returns the Signals object.

class MyCustomFilter(SignalFilter):
    def __init__(self, signals):
        super().__init__(signals)

    def filter(self):
        signals = self.signals_cls
        signals.apply_filter('normalization')
        signals.apply_filter('moving_average')
        return signals

To be able to call your filter using a simple string, we have to add it to our Signals filter list.

from dgm.signal.filters.signal_filter import SignalFilter

# Add your custom filter to the Signal filter dictionary
Signals.add_filter(MyCustomFilter, name="mycustomfilter")

That's all! Now you can call your own filter using the name you gave it!

# Example using our custom filter
ecg_signals = (
    Signals(ecg_array)
    .set_averaging_window_length(20)
    .apply_filter("mycustomfilter")
)

← Previous: Create a Signals object Next: Add annotations to your signals →