Using Audio Library Filters with I2C signal on Teensy 4.0?

srko

New member
Hi everyone,

New member here (apologies in advance if my post doesn't follow the group etiquette).

I'm using the Teensy 4.0 to read data from a sensor via I2C. My code is a slight variation of the sensor company's code; please find that code here. The delay() has been changed so the data is read in the range of 6-30 Hz, and I'm writing the data out via serial. Each value from the sensor is a short, and the values range from 255 to 1024 (no decimals or floating point).

There is some low frequency noise in the data, so I'd like to implement a low-pass filter on the Teensy to clean up the data. I'd like the filtering to be quick and energy-efficient (that's more important than perfect, high-fidelity filtering).

I read that the Teensy Audio Library has been optimized to use the DSP instructions and works well, but I couldn't figure out how to use the biquad or FIR filters from the Audio Library with non-audio inputs. In the Teensy audio example code for a biquad filter and example code for FIR filter, the input that was filtered was i2S. And in the Teensy Audio System Design tool, I wasn't sure if any of the input options would work for my system; in my code, each new sensor value is stored in a short. I also didn't know if I could change "AUDIO_SAMPLE_RATE_EXACT" to the frequency of my signal (I see that its in audiostream.h, not sure if I can modify Teensy core code). Additionally, I don't know enough about the DSP instructions to know if they'd be helpful for filtering my data values that are shorts, and not 32bit or 64bit.

Could you please share any advice on how to implement an energy efficient low pass filter for my sensor data? I'd prefer to use a library instead of seeking out the individual DSP instructions and writing something from scratch. To be clear, I am not using the Teensy audio board.

Thanks so much in advance! Also shoutout to all the developer and community members of Teensy - I've learned so much from y'all.

Best,
SK
 
If your sample rate is only 30Hz there's no need to worry about efficiency, the T4 is vastly more powerful than required for the task. There are sites out there that can generate a digital filter to a specification and generate C code (*). Just use floats, should be well accurate given the 10 bit samples as input.

I don't understand why you expect a low-pass filter to remove low frequency noise though. Perhaps clarify your requirements a bit more so we're not at cross-purposes.

(*) for instance http://jaggedplanet.com/iir/iir-explorer.asp
 
Thank you for your help and prompt reply, MarkT.

And sure, happy to elaborate on filter choice - any input is more than welcome since I'm a filtering rookie.

I didn't want to lose the DC offset with a high pass filter, and the signal I'm measuring is very slow (in the range of 0.1-5 Hz ). The bulk of the noise from the sensor is 15 Hz and less, so instead of implementing more elaborate bandpass filters, I figured I could use a low pass filter to filter out the relatively higher frequency noise >= 5Hz. In other words, the noise I'm filtering out is objectively a low frequency noise (5-15 Hz), but it is still greater than my signal, hence the low pass filter.

Another point worth noting is the signal I'm measuring is one I'm generating (I'm measuring force and I also control the rate at which I change force). If there are straightforward and computationally efficient ways to allow me to pass through just a few frequencies and filter out the rest (like inverse notch filters), I'd be interested in learning more about it.
 
Ah, so the noise is high frequency (from the signal's viewpoint) - that clarifies things. For minimum phase distortion a Bessel filter might be best (for IIR). A FIR filter will be linear phase of course, but with a lot more latency.
 
Back
Top