Unity Iir Online Filtering

by mariusrubo

Apply Notch filters or Butterworth Low-pass and High-pass filters to data as it arrives

( Crawled 2 months ago )


Apply Notch filters or Butterworth Low-pass and High-pass filters to data as it arrives


Data from external hardware like OpenBCI, Bitalino, Arduino etc. typically need to be filtered before they can be processed. When you need to analyze data after it is collected (e.g. in a psychological or medical experiment), there exist excellent packages both for MATLAB and R. If your Unity software needs to respond dynamically to input, one option is to send it to MATLAB, process the data there, and send it back. This project, however, aims at performing simple filtering of data directly in C#. Specifically, 2nd order Notch and Butterworth filters, but also other IIR filters can be implemented and used on data as it is streamed into Unity. For details see the comments in FilterData.cs.


The below image shows data that was streamed in from an OpenBCI. Nothing was being measured, to there should be a straight line. However, as you can see on the left part of the diagram, data is distorted by a 50 Hz electric hum. With a sample rate of 250 Hz, displaying data as points rather than lines makes it look like there are 5 different data streams in one. The middle part of the diagram shows the same kind of data, but with a 50Hz Notch filter as in the attached FilterData.cs file. In the right part of the diagram, data is furthermore filtered using a 100Hz Notch filter. As you can see, data resembles much more, but still not perfectly a straight line. This would be better achieved using the filtfilt-function, which can however not be applied in online filtering.

alt tag

Further Reading


These scripts run under the GPLv3 license.