Frequency Detection
The instantaneous frequency is computed from the received audio based on the discovery of each peak. This is done by maintaining a “last peak” sample number, subtracting that value from the sample number of the current peak, and multiplying by the sample time. For example a 1 kHz tone sampled at 8 kHz precisely at its peaks, will result in 8 samples between each peak. So if a current peak is at sample number 8088, and the previous peak was sampled at sample number 8080, and since the sample time of 8000 Hz is 0.000125 sec, then:
- (8088 – 8080) * 0.000125 = 0.001, and
- 1 / 0.001 = 1000 Hz.
The system maintains a “Frequency Found” state and “Last Average Frequency” variable to aid in the determination of frequency changes. To improve accuracy, the system averages frequency measurements over several samples. This becomes especially important at higher frequencies where there are fewer samples per cycle, and the detected peak may actually be +/- 1 or more samples different than the location of the actual peak.
On each peak after initialization or reset, the measured peak-to-peak frequency is added to an accumulator, and when the configured number of measurements is reached, the average is computed. If the percent change from the “Last Average Frequency” exceeds an audio stream specific limit, an internal frequency change event is declared, and the Frequency Found state is set to true.
On all subsequent peaks, the instantaneous peak-to-peak frequency continues to be computed. If the instantaneous frequency differs from the current average frequency by a sufficient amount, the Frequency Found state is set to false, and the algorithm returns to its initial conditions searching for a stable frequency.
Such “resets” will ultimately result in a reported event of some type. If a new frequency is established, an internal frequency change event occurs. If the same frequency is found as the previous frequency, an “Unexpected Phase Change” event occurs. If the measurement is not successful, a “Synchronization Lost” event will ultimately occur.