Category Archives: Software

Using the Bela to measure the frequency response

Bela is a maker platform for creating beautiful interactions. It consists of a Beaglebone black, with a shield or hat that has 2 audio inputs, 2 audio outputs, 8 analog inputs, and 8 analog outputs. It is complemented with a very slick web interface that allows you to write and very easily compile and run your code. And very cool is that the web interface features an oscilloscope.

I am planning to build a purely analog EEC/EMG/ECG amplifier, similar to this design on Instructables. As that involves making choices on the filter settings: a low-pass filter to remove electrode drift, a notch filter for line noise, high-pass anti-aliasing filter matched to audible frequencies. Hence I started thinking on how to determine the combined effect of all those filters, together with the multiple amplifier stages. It occurred to me that the Bela can act both as a signal generator and as a digital recorder and oscilloscope.

Bela and breadboard with fiter

On this GitHub page I am sharing a Bela project that outputs a sine wave on the analog output, which can be fed through an external circuit, and subsequently measured using the analog inputs. The project computes a real-time discrete Fourier transform of the output signal and compares the amplitude and phase to the input signal. Using a LaunchControl XL MIDI controller (or alternatively using a small EEGsynth path for an on-screen MIDI controller), I can select the frequency, and start/stop a sweep over the whole frequency range. The amplitude and phase response at each frequency is logged to disk.

Here you can see the frequency response when the Bela analog output is directly fed into the analog input. It is very nicely uniform with a unit gain and no observable phase shift up to the upper limit of 22050Hz.

Bode plot of frequency response

And here is the frequency response when the Bela audio (headphone) output is directly fed into the audio input. You can see that – as expected – it is DC-coupled with a high-pass filter and with an anti-aliasing filter at the high end.

Bode plot of frequency response

From the Bode plot figures it is clear that something funky is going on with the phase estimates. I suspect that to be due to numerical errors accumulating in my computation of the DFT. There are fancy algorithms for single bin sliding DFTs. However, I want the DFT algorithm to run in the (hard) real-time audio loop, which means that it should have a very low computational cost. Furthermore, I want it to be memory efficient, which means that I don’t want to hold a large buffer with many samples.

I also tried it with a simple passive first-order low-pass filter on a breadboard with a 100nF capacitor and a 10kOhm resistor, which should have a (theoretical) cutoff frequency of 159Hz. The resulting frequency response up to 5000Hz is given here:

Bode plot of frequency response

And If I connect the same capacitor and resistor to form a high-pass filter, I get the following frequency response up to 5000Hz . Note that the output of the high-pass filter cannot fully be recorded with the analog input (which is 0-4V only), hence I used the audio input.

Bode plot of frequency response