Skip to main content

Fir1

An efficient Finite Impulse Response (FIR) filter class written in C++ with python wrapper.

Adaptive filtering is also implemented using the Least Mean Square (LMS) or Normalised Least Mean Square (NLMS) algorithm.

Installation

Install the python package with pip:

pip install fir1

You can also install from source:

git clone https://github.com/berndporr/fir1
cd fir1
python setup.py install

Usage

Realtime filtering

The filter is a realtime filter which receives samples one by one so it can process data as it arrives from an ADC converter. This is simulated here with the for loop:

import fir1
b = signal.firwin(999,0.1)
f = fir1.Fir1(b)
for i in range(len(noisy_signal)):
    clean_signal[i] = f.filter(noisy_signal[i])

The constructor Fir1() receives the filter coefficients (= impulse response) and then filtering is performed with the method filter().

LMS adaptive filter

The file lms_50Hz_ecg_filter.py removes 50Hz from an ECG with the help of the lms filter. The filter learns its own frequency response from a reference 50Hz sine wave:

f = fir1.Fir1(NTAPS)
f.setLearningRate(LEARNING_RATE);

y= np.empty(len(ecg))
for i in range(len(ecg)):
    ref_noise = np.sin(2.0 * np.pi / 20.0 * i);
    canceller = f.filter(ref_noise)
    output_signal = ecg[i] - canceller
    f.lms_update(output_signal)
    y[i] = output_signal

You can query the filter coefficients using getCoeff. This is most useful to obtain the kernel of a trained adaptive filter:

>>> from fir1 import Fir1
>>> fir = Fir1([.25, -.5, 1, -.5, .25])
>>> fir.getCoeff()
array([ 0.25, -0.5 ,  1.  , -0.5 ,  0.25])

You may override the length of array to return and the result will be zero-padded. Specifying too small an array causes an exception to be raised:

>>> fir.getCoeff(8)
array([ 0.25, -0.5 ,  1.  , -0.5 ,  0.25,  0.  ,  0.  ,  0.  ])
>>> fir.getCoeff(3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".../fir1.py", line 97, in getCoeff
return _fir1.Fir1_getCoeff(self, *args)
RuntimeError: Fir1: target of getCoeff: too many weights to copy into target

Both the demo file and an explanation how the LMS filter works can be found on the homepage of the project:

https://github.com/berndporr/fir1

Metadata

Release files for fir1 1.8.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fir1 1.8.0.0
File Size Uploaded Provenance
fir1-1.8.0.0.tar.gz 21.1 kB Without provenance Details

Release files / fir1-1.8.0.0.tar.gz

Download URL fir1-1.8.0.0.tar.gz
Size 21.1 kB
Tags Source
SHA-256 checksum
How to use checksums
cb72f93f21f09b8757d3d4e0ac00421a76a63be677c4b61f4b0a67f8914ceedc
BLAKE2b-256 checksum
How to use checksums
7af89e486eaf25676efe30639d1aad11c30d245c47f4ca4b8aaaa2a0facf222d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Security for fir1 1.8.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page