Skip to main content

FilterKernel

Namespace: RTPROCESSINGLIB  ·  Library: DSP Library

Python equivalent

mne.filter.create_filter in MNE-Python.

#include <dsp/filterkernel.h>

class UTILSLIB::FilterKernel

The FilterKernel class provides methods to create/design a FIR filter kernel.

The FilterKernel class provides methods to create/design a FIR filter kernel

src/examples/ex_dsp_filter/main.cpp
// Frequencies are normalised to Nyquist (500 Hz): 40 Hz cut-off, 10 Hz transition
const int typeLpf = FilterKernel::m_filterTypes.indexOf(FilterParameter("LPF"));
const int cosine = FilterKernel::m_designMethods.indexOf(FilterParameter("Cosine"));
const int tschebyscheff = FilterKernel::m_designMethods.indexOf(FilterParameter("Tschebyscheff"));
FilterKernel lowPass("lp40", typeLpf, 256, 40.0 / 500.0, 0.0, 10.0 / 500.0, kSFreq, cosine);
FilterKernel lowPassPm("lp40pm", typeLpf, 256, 40.0 / 500.0, 0.0, 10.0 / 500.0, kSFreq, tschebyscheff);

Public Methods​

FilterKernel()​

FilterKernel creates a default FilterKernel object.


FilterKernel(sFilterName, iFilterType, iOrder, dCenterfreq, dBandwidth, dParkswidth, dSFreq, iDesignMethod)​

Constructs a FilterKernel object.

Parameters:

  • sFilterName : const QString & Defines the name of the generated filter.

  • iFilterType : int Tyep of the filter: LPF, HPF, BPF, NOTCH (from enum FilterType).

  • iOrder : int Represents the order of the filter, the higher the higher is the stopband attenuation.

  • dCenterfreq : double Determines the center of the frequency - normed to sFreq/2 (nyquist).

  • dBandwidth : double Ignored if FilterType is set to LPF,HPF. if NOTCH/BPF: bandwidth of stop-/passband - normed to sFreq/2 (nyquist).

  • dParkswidth : double Determines the width of the filter slopes (steepness) - normed to sFreq/2 (nyquist).

  • dSFreq : double The sampling frequency.

  • iDesignMethod : int Specifies the design method to use. Choose between Cosind and Tschebyscheff.


prepareFilter(iDataSize)​

Prepares a filter kernel to be used wiht a specific data block length.

This is favorable to call before filtering, in order to avoid transforming the filter coefficients anew during filtering. This functions was introduced since one does not always know the data length of the data blocks to be filtered when designing the filter.

Parameters:

  • iDataSize : int The data size to setup the filters to.

applyConvFilter(vecData, bKeepOverhead)​

Applies the current filter to the input data using convolution in time domain.

Parameters:

  • vecData : const Eigen::RowVectorXd & Holds the data to be filtered.

  • bKeepOverhead : bool Whether the result should still include the overhead information in front and back of the data. Default is set to false.

Returns:

  • Eigen::RowVectorXd — the filtered data in form of a RowVectorXd.

applyFftFilter(vecData, bKeepOverhead)​

Applies the current filter to the input data using multiplication in frequency domain.

Parameters:

  • vecData : Eigen::RowVectorXd & Holds the data to be filtered. Gets overwritten with its filtered result.

  • bKeepOverhead : bool Whether the result should still include the overhead information in front and back of the data. Default is set to false.


getName()​


setName(sFilterName)​


getSamplingFrequency()​


setSamplingFrequency(dSFreq)​


getFilterOrder()​


setFilterOrder(iOrder)​


getCenterFrequency()​


setCenterFrequency(dCenterFreq)​


getBandwidth()​


setBandwidth(dBandwidth)​


getParksWidth()​


setParksWidth(dParksWidth)​


getHighpassFreq()​


setHighpassFreq(dHighpassFreq)​


getLowpassFreq()​


setLowpassFreq(dLowpassFreq)​


getCoefficients()​


setCoefficients(vecCoeff)​


getFftCoefficients()​


setFftCoefficients(vecFftCoeff)​


getDesignMethod()​


setDesignMethod(iDesignMethod)​


getFilterType()​


setFilterType(iFilterType)​


getShortDescription()​


Example​

Used in examples​

ex_dsp_filter​

FIR filter design, application and storage with the dsp library.

src/examples/ex_dsp_filter/main.cpp, line 105
// Frequencies are normalised to Nyquist (500 Hz): 40 Hz cut-off, 10 Hz transition
const int typeLpf = FilterKernel::m_filterTypes.indexOf(FilterParameter("LPF"));
const int cosine = FilterKernel::m_designMethods.indexOf(FilterParameter("Cosine"));
const int tschebyscheff = FilterKernel::m_designMethods.indexOf(FilterParameter("Tschebyscheff"));
FilterKernel lowPass("lp40", typeLpf, 256, 40.0 / 500.0, 0.0, 10.0 / 500.0, kSFreq, cosine);
FilterKernel lowPassPm("lp40pm", typeLpf, 256, 40.0 / 500.0, 0.0, 10.0 / 500.0, kSFreq, tschebyscheff);
for (const FilterKernel* kernel : {&lowPass, &lowPassPm}) {
const RowVectorXd taps = kernel->getCoefficients();
const double g10 = gain(taps, 10.0, kSFreq);
const double g100 = gain(taps, 100.0, kSFreq);
ok &= expect(std::fabs(g10 - 1.0) < 0.01 && g100 < 0.01,
QString("%1 (%2): |H(10 Hz)| %3, |H(100 Hz)| %4").arg(kernel->getName(), kernel->getDesignMethod().getName()).arg(g10).arg(g100));
}

ex_filtering​

Example for filtering data with a user defined FIR filter and writing the result to a file.

src/examples/ex_filtering/main.cpp, line 89
printf("Filtering...");
if (RTPROCESSINGLIB::filterFile(fileOut,
pRaw,
FilterKernel::m_filterTypes.indexOf(FilterParameter("BPF")),
10,
10,
0.1,
pRaw->info.sfreq,
1024,
UTILSLIB::FilterKernel::m_designMethods.indexOf(FilterParameter("Cosine")),
picks)) {
printf("[done]\n");
} else {
printf("[failed]\n");

ex_dsp_analysis​

DSP library: Welch PSD, FIR filtering, peak finding, envelope correlation, SPHARA and simulation.

src/examples/ex_dsp_analysis/main.cpp, line 115
FilterKernel lowPass = FirFilter::design(256, FirFilter::LowPass, 20.0, 0.0, sFreq, 5.0); // order, type, cutoff(s), Hz, transition
const RowVectorXd smooth = FirFilter::applyZeroPhase(tones, lowPass);
const double kept = toneAmplitude(smooth.segment(250, 500), 10.0, sFreq);
const double removed = toneAmplitude(smooth.segment(250, 500), 40.0, sFreq);
ok &= expect(smooth.size() == tones.size() && std::fabs(kept - 1.0) < 0.02 && removed < 0.01,
QString("FirFilter 20 Hz low-pass keeps 10 Hz (%1) and removes 40 Hz (%2)").arg(kept, 0, 'f', 4).arg(removed, 0, 'f', 4));

Authors of this file​