Skip to content
rossGardiner edited this page Mar 31, 2022 · 1 revision

cmake

Add to your CMakeLists.txt either

target_link_libraries(myexecutable fir)

for the dynamic library or

target_link_libraries(myexecutable fir_static)

for the statically linked library.

You can also use find_package(fir).

Generating the FIR filter coefficients

Set the coefficients either with a C floating point array or with a text file containing the coefficients. The text file or the floating point array with the coefficients can easily be generated by Python or OCTAVE/MATLAB:

Python

Use the firwin command to generate the coefficients:

# Sampling rate
fs = 1000
# bandstop between 45 and 55 Hz:
f1 = 45
f2 = 55
b = signal.firwin(999,[f1/fs*2,f2/fs*2])

For fixed point you need to scale up the coefficients, for example by 15 bits: b*32768.

octave/MATLAB:

octave:1> h=fir1(100,0.1);

which creates the coefficients of a lowpass filter with 100 taps and normalised cutoff 0.1 to Nyquist.

Again, for fixed point "h" needs to be scaled.

Initialisation

C++ floating point FIR filter:

Fir1 fir("h.dat");

or import the coefficients as a const double array:

Fir1 fir(coefficients)

there is also an option to import a non-const array (for example generated with the ifft) and using std::vector.

C++ integer FIR filter:

Fir1fixed fir("h_fixed.dat",12);

where the coefficients have been scaled up by 2^12 and the filter will scale them down by this amount (with the help of a bitshift operation).

JAVA:

Fir1 fir = new Fir1(coeff);

where coeff is an array of double precision coefficients and returns the fir filter class.

Python

f = fir1.Fir1(coeff)

Realtime filtering

C++ double:

double b = fir.filter(a);

C++ integer:

int b = fir.filter(a);

JAVA:

double b = fir.filter(a)

Python

b = f.filter(a)

Utility methods

These functions are the same in C++, JAVA and Python:

  • getTaps() returns the length of the FIR filter kernel.
  • reset() sets all delay lines to zero.
  • zeroCoeff() sets all coefficients to zero.

Retreiving the coefficients/kernel from the FIR filter is different depending on the language used:

C++

  • void getCoeff(double* target, unsigned length) const copies the FIR kernel into the given C array of doubles with length length.

    If length exceeds the length of the filter kernel, the result is zero-padded to fill the given array.

    If length is smaller than the filter kernel, a std::out_of_range exception is thrown.

  • std::vector<double> getCoeffVector() const returns a copy of the filter kernel.

Python

  • getCoeff(n : int) -> numpy.array as per the C++ method, following the zero-padding and exception-throwing behaviour of the C++. The returned array will have n elements.
  • getCoeff() -> numpy.array additional to the C++ methods, this returns an numpy array which is a copy of the filter kernel. This is probably the default use case in Python.

JAVA

  • double[] getCoeff() returns a double array of the filter kernel.
  • double[] getCoeff(n : int) as per the C++ method, following the zero-padding and exception-throwing behaviour of the C++. The returned array will have n elements.

Destructor

C++

delete fir;

JAVA

fir.release();

to release the underlying C++ class.

Clone this wiki locally