Skip to main content

Efficient FIR realtime filter

Project description

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.


Install the python package with pip:

pip3 install fir1

You can also install from source:

git clone
cd fir1
python3 install


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 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)

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
    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 ".../", 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:

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fir1- (21.2 kB view hashes)

Uploaded source

Built Distributions

fir1- (34.9 kB view hashes)

Uploaded 1 7 1 1

fir1- (32.6 kB view hashes)

Uploaded cp310

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page