The technique we use in this guide to determine the frequency is pretty straight forward. It's sometimes called "zero crossing" or "mean crossing" as we use the time between crossings of a zero or mean level to estimate the frequency.
Let's look at this in more detail.
A fundamental assumption of this technique is that we are dealing with a sound which is a pure tone. That means it is a nice clean signal that just repeats over and over again at a single frequency. If we recorded such a signal with a microphone and plotted the results, it would look something like this:
This is an example of a signal with an offset, since the line is not crossing back and forth through the zero line on the graph. In this case, a zero level represents silence and all noises will have a positive value. So we end up with a signal oscillating back and forth around some offset.
There are several key features to this signal as shown below. The offset mentioned above is called the MEAN.
MEAN is just another word for average, and that is how this value can be determined - by averaging the entire sample. The PERIOD is the total time it takes to make one cycle of the pattern, which then repeats over and over. We are interested in the frequency, f, of this signal, which is simply the inverse of the PERIOD.
When we sample the microphone, we don't get the orange line above. Instead we get a bunch of individual data points, like the black circles below.
These will all fall on the orange line as shown. Just keep in mind that the microphone sample data is a series of discrete data points. We can then work with these data points to estimate the frequency.
Here's how the "mean crossing" technique works:
- Find the first point which is greater than the MEAN value plus the THRESHOLD. Set a "has crossed" flag to True.
- Find the first point after the "has crossed" flag has been set True that goes below the MEAN value. Store this location and clear the "has crossed" flag.
- We now repeat the process, so this is the next point like 1.
- And this is the next point like 2. Compute the delta between 2 and 4 and save it.
We do the above across the entire mic sample and then take an average of the deltas. The average delta is in terms of number of samples, N, in one cycle. The microphone sample rate sets the time, DT, between samples. So we can compute the PERIOD as simply N * DT. And the frequency of the mic data is then just the reciprocal of the PERIOD.