-
Notifications
You must be signed in to change notification settings - Fork 0
Usage
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).
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:
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: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.
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.
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).
Fir1 fir = new Fir1(coeff);
where coeff is an array of double precision coefficients
and returns the fir filter class.
f = fir1.Fir1(coeff)
double b = fir.filter(a);
int b = fir.filter(a);
double b = fir.filter(a)
b = f.filter(a)
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:
-
void getCoeff(double* target, unsigned length) constcopies the FIR kernel into the given C array ofdoubles with lengthlength.If
lengthexceeds the length of the filter kernel, the result is zero-padded to fill the given array.If
lengthis smaller than the filter kernel, astd::out_of_rangeexception is thrown. -
std::vector<double> getCoeffVector() constreturns a copy of the filter kernel.
-
getCoeff(n : int) -> numpy.arrayas per the C++ method, following the zero-padding and exception-throwing behaviour of the C++. The returned array will havenelements. -
getCoeff() -> numpy.arrayadditional 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.
-
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 havenelements.
delete fir;
fir.release();
to release the underlying C++ class.