Frequency measuring is done by counting moments when signal is crossing its average value.
Algorithm requires capturing at least 3 periods of signal. If less is captured flag bFreqValid will not be set and fFreq value will be undetermined.
When noise level in input signal is high there is a risk of detecting more crossing points than expected. To avoid this simple hysteresis scheme is used:
- difference between maximum (Vmax) and average (Vavg) and difference between average and minimum (Vmin) value is computed
- if Vmax - Vavg < Vavg - Vmin:
- hysteresis threshold (Vth) is calculated as Vavg - (Vavg - Vmin)/2
- moments when signal is raising through Vavg will be counted
- each raising through Vavg is taken into account only if signal falled earlier through Vth
- if Vmax - Vavg >= Vavg - Vmin:
- hysteresis threshold (Vth) is calculated as Vavg + (Vmax - Vavg)/2
- moments when signal is falling through Vavg will be counted
- each falling through Vavg is taken into account only if signal falleded earlier through Vth