We want to process the electromagnetic field (EMF) data in real time to determine if it’s 60 Hz or maybe in another frequency range. Wow, having different colors to indicate the type of EMF around an appliance, that would be very interesting! But before getting carried away, there is some number crunching to do.

We could do an analog filter so the Arduino would not have any calculations do. That’s elegant but harder to modify later on, and it adds components. Same for adding a DSP (digital signal processing) chip.

Another way to do this is the Fourier transform to tell us what amplitude EMF we have at what frequency. Even the discrete “fast Fourier transform,” designed specifically for computers, involves floating-point multiplication and calculating sines and cosines. The FFT saves some work by keeping results in a lookup table but still. There are plenty of other things the Arduino needs to do, and these calculations will probably slow down the maximum sampling rate.

We could do a variant called the Goertzel algorithm that computes the amplitude at just one frequency. Here’s a good implementation of the Goertzel algorithm:

Goertzel algorithm from mstarlabs

Still a lot of number crunching. Would a result every two seconds be too slow for us? Maybe there is a more continuous way to apply this algorithm.

Alternatively, there’s digitally filtering out everything but the 60 Hz, then finding the amplitude of that filtered signal. Here’s a very good implementation of a digital 60 Hz bandpass filter. The application is getting a robot to locate a power plug by “feeling” the 60 Hz electric field. The best thing is that the algorithm was run on an ATmega 168 and was able to update about 50 times per second!

Here is my version of this bandpass filter:

It was generated using the following MATLAB code…