audiounity-game-enginesignal-processingpitch-trackingpitch-detection

Amplitude of audio signal harmonics in Unity3D


I have managed to calculate the pitch of audio input from microphone using the GetSpectrumData function. But now I need to get the amplitudes of the first 7 harmonics of audio (Project requirement) I have very less knowledge of Audio dsp. Only thing I understood is that harmonics are multiples of the fundamental frequency. But how will I get the amplitudes of the harmonics.

Thanks


Solution

  • First you need to figure out which FFT bin your fundamental frequency is in. Say it resides in bin# 10. The harmonics will reside in integer multiples of that bin so the 2nd harmonic will be in bin 20, 3rd in bin 30 and so on. For each of these harmonic bins you need to compute the amplitude. Depending on the window function you used in the FFT you will need to include a small number of bins in the calculation (google spectral leakage if you're interested).

    double computeAmpl(double[] spectrum, int windowHalfLen, int peakBin, int harmonic)
    {
        double sumOfSquares = 0.0;
        for (int bin = peakBin-windowHalfLen; bin <= peakBin+windowHalfLen; bin++)
        {
            sumOfSquares += spectrum[bin] * spectrum[bin];
        }
        return sqrt(sumOfSquares);
    }
    

    As I mentioned the window half length depends on the window. Some common ones are: