Reliable Frequency Detection Using DSP Techniques

60,824

54

18

Posted

Introduction: Reliable Frequency Detection Using DSP Techniques

Accurate Frequency Detection is important for many projects such as Guitar/Piano Tuners, Vibration Analyzers, Heartrate Monitors, MEMs Sensor Analysis and Laboratory Instruments.

There have been many fine examples of projects that try to solve this problem, for example: Arduino Frequency Detection by amandaghassaei and Arduino Frequency Counter Library.

But they all use Time Domain techniques; analyzing the signal for features such as : Zero-Crossings, Peak Detection, Slope Detection etc..

Take a look at the Waveforms shown. One of them is recorded from a Piano playing Middle-C (C4) . The other is from a Synthesizer Playing Middle-C (C4). Clearly any good Time Domain algorithm will work well with the Piano waveform. But the Synthesizer waveform will not be identifiable that way because its very strong harmonic content makes the fundamental frequency undiscernable . It looks impossible to Identify the Frequency of this signal.

It is possible.

Using the technique I'm going to show you it was measured to be 259.91Hz ... only 0.09Hz away from an Exact Middle C Frequency of 260Hz.

UPDATE:

I got a lot of questions about how to use this to make a Guitar Tuner. SoI spent some time writing a tutorial on how to make a Guitar Tuner using this method. You can find it on my blog:
http://www.akellyirl.com/arduino-guitar-tuner/

========

By the way. If you like this Instructable, you might also like to read My Blog covering various projects and Tutorials.

========

Step 1: You Will Need

I used an Arduino because it will make a great basis for building a Frequency Detector with Analogue Input such as a Guitar Tuner or Heartrate Monitor.

But the principles apply to any platform.

To demonstrate the principles I'm going to use pre-recorded sound files captured as an array in a .h file. So we won't be needing any circuit for the Arduino this time.

Step 2: Autocorrelation

The trick we use to identify the frequency of a noisy signal is well known in the Mathematical World of  Digital Signal Processing (DSP), and is based on some pretty fancy maths. But the technique is not difficult to understand and better still it's super-easy to code. The core of it is just 3 lines of code.

What we need to do is to change the original signal into another one that highlights the periodicity of the original signal. So if it is indeed periodic, then that will stand out in the new signal and then we can measure that in the usual way using peak-detect or zero crossing detect.

What's the magic algorithm that does that?

It's Autocorrelation.

Imagine your signal is contained in a window or buffer. Now image you have an exact copy of that window or buffer with a time delay.

What Autocorrelation does is to measure the correlation (or similarity) between the signal and its delayed copy each time the copy is delayed by a sample period.

See the diagram. When the signal and the copy have no delay they are very similar (i.e. highly correlated) as shown in step 1, and therefore the autocorrelation value for delay = 0 is maximum.

Step 2 shows that when the copy is delayed significantly it doesn't look similar to the original in the overlapping area. Therefore the autocorrelation value for this delay is small.

Step 3 shows that when the copy is delayed even more the signal in the overlapping area is very similar to the original because the signal is periodic. Therefore the autocorrelation value for this delay shows a peak.

We can see that the distance in time between the maximum peak at the beginning and the first peak afterwards must be equal to the fundamental period of the waveform.

Now that we've emphasised the periodicity of the signal by Autocorrelation we just need to perform a Peak-Detect to measure the period.

Technically the "similarity" or correlation between the signal and its delayed copy is the sum of the product of the two signals.

For the technically minded all the details of Autocorrelation can be found here: http://en.wikipedia.org/wiki/Autocorrelation

Step 3: Autocorrleation Code

The core of the Autocorrelation Code is very short:
```
for(i=0; i < len; i++)
{
sum = 0;
for(k=0; k < len-i; k++) sum += (rawData[k]-128)*(rawData[k+i]-128)/256;
}
```
The data is in the rawData[] array. We subtract 128 from each value because it's 8bit unsigned and we require signed values.

The sum value is the result of each autocorrelation calculation, i.e. each point of the function. In order to save memory we don't save the output to an array. We're going to work on the individual sum values to find the first peak and therefore calculate the period.

Sending the sum values out to be plotted, we get the Autocorrelation function shown. Comparing to the original signal (also shown) it is clear that there is periodicity in the original signal and this has been clearly highlighted by the Autocorrelation function.

Step 4: Peak Detect

To detect the location of the first peak after the maximum we use a simple peak detector coded as a State Machine as follows:

```    // Peak Detect State Machine
if (pd_state == 2 && (sum-sum_old) <=0)
{
period = i;
pd_state = 3;
}
if (pd_state == 1 && (sum > thresh) && (sum-sum_old) > 0) pd_state = 2;
if (!i && pd_state == 0) {
thresh = sum * 0.5;
pd_state = 1;
}

```
The state machine moves from one state to the next when an event occurs as follows:

STATE0 : Set thresh the threshold under which value we'll ignore the data : NEW STATE = 1
STATE1 : look for the signal being above the threshold AND the slope of the signal is positive : NEW STATE = 2
STATE2 : look for  the slope of the signal is negative or zero. If so we've found the PEAK! : NEW STATE = 3

Step 5: Here's All of the Code

```#include "C4.h"

// Sample Frequency in Hz
const float sample_freq = 22050;

int len = sizeof(rawData);
int i,k;
long sum, sum_old;
int thresh = 0;
float freq_per = 0;
byte pd_state = 0;

void <b>setup</b>() {
<b>Serial</b>.begin(115200);

sum = 0;
pd_state = 0;
int period = 0;
// Autocorrelation
for(i=0; i < len; i++)
{
sum_old = sum;
sum = 0;
for(k=0; k < len-i; k++) sum += (rawData[k]-128)*(rawData[k+i]-128)/256;

// Peak Detect State Machine
if (pd_state == 2 && (sum-sum_old) <=0)
{
period = i;
pd_state = 3;
}
if (pd_state == 1 && (sum > thresh) && (sum-sum_old) > 0) pd_state = 2;
if (!i) {
thresh = sum * 0.5;
pd_state = 1;
}
}
// Frequency identified in Hz
freq_per = sample_freq/period;
<b>Serial</b>.println(freq_per);
}

void <b>loop</b>() {
// put your main code here, to run repeatedly:

}

```

Step 6: Files

The C4.h File contains the buffer we're analysing.

You can fill that by reading a Block of Data from the ADC in the Arduino. Or you can generate the data from a program such as Audacity.

Here's a link to the Files including several .h files containing Audio samples you can try out.

The samples were taken from the extensive collection captured by the University of Iowa Electronic Music Studios.

Recommendations

• Arduino Class

67,982 Enrolled

• Spotless Contest

We have a be nice policy.

Questions

if you dont mind, would you explain to me about the autocorrelation algorithm in detail ? i want to use that algorithm in my project but i can understand it clearly

Akellyirl,

Thank you for posting this tutorial on beat detection. It is detailed and informative as well as much appreciated. I have been working for a few months now on getting a solid audio input into the arduino to use this code. the audio is oscillating on a 2.5v line from 0v to 5v with about 2% noise at the moment. So after accomplishing this I wanted to see if I could use this code to analyze audio inputs freq. I put rawData to A0 but get an error on the line "for(k=0; k<len-i;k++) sum+=(rawData[k]...". I attempted assigning raw data as an unsigned char and an int.

can this code be adapted to analyze audio coming in at 0 to 5v with a 2.5v midline?

Rex

Hi Rex, did you ever figure this out? I'm trying to do the same thing right now.

No not with this set up. I got it working with the teensy board and audio sheild.

What you need to do is fill the rawData [] array with data from the ADC. You can use a for loop for that. You should also subtract the mean of the data so that it's centred at zero.

So is this done all at one time in the loop() function? I thought the ADC only returns one int value at a time. How do I fill an array of 970 values with one value from ADC?

This should work OK. I'd need more info on the error. Make sure the array is defined an unsigned char e.g. unsigned char rawData[970] = { };

akellyirl,

Could you please tell me how can I assign the input value of A0 to the array rawData?