Detect a specific frequency/tone from raw wave-data

前端 未结 6 1963
天涯浪人
天涯浪人 2020-12-03 04:03

I am reading a raw wave stream coming from the microphone.
(This part works as I can send it to the speaker and get a nice echo.)

For simplicity lets say I want

6条回答
  •  囚心锁ツ
    2020-12-03 04:27

    Very nice implementation of Goertzel is there. C# modification:

    private double GoertzelFilter(float[] samples, double freq, int start, int end)
        {
            double sPrev = 0.0;
            double sPrev2 = 0.0;
            int i;
            double normalizedfreq = freq / SIGNAL_SAMPLE_RATE;
            double coeff = 2 * Math.Cos(2 * Math.PI * normalizedfreq);
            for (i = start; i < end; i++)
            {
                double s = samples[i] + coeff * sPrev - sPrev2;
                sPrev2 = sPrev;
                sPrev = s;
            }
            double power = sPrev2 * sPrev2 + sPrev * sPrev - coeff * sPrev * sPrev2;
            return power;
        }
    

    Works great for me.

提交回复
热议问题