Realtime sound spectrogram using FFT or linear prediction. Spectrogram is displayed on the display of PC. リアルタイム・スペクトログラム.解析の手法:FFT,線形予測法.スペクトログラムは PC のディスプレー装置に表示される.PC 側のプログラム:F446_Spectrogram.

Dependencies:   Array_Matrix mbed SerialTxRxIntr F446_AD_DA UIT_FFT_Real

MySpectrogram/LinearPrediction.cpp

Committer:
MikamiUitOpen
Date:
2019-11-24
Revision:
7:5ba884060d3b
Parent:
0:a539141b9dec

File content as of revision 7:5ba884060d3b:

//-----------------------------------------------------
//  Class for linear prediction
//
//  2017/02/11, Copyright (c) 2017 MIKAMI, Naoki
//-----------------------------------------------------

#include "LinearPrediction.hpp"

namespace Mikami
{
    LinearPred::LinearPred(int nData, int order)
        : N_DATA_(nData), order_(order),
          r_(order+1), k_(order), am_(order) {}

    // Calculate linear-predictive coefficients
    bool LinearPred::Execute(const float x[], float a[],
                             float &em)
    {
        AutoCorr(x);
        return Durbin(a, em);
    }

    // Calculate auto-correlation
    void LinearPred::AutoCorr(const float x[])
    {
        for (int j=0; j<=order_; j++)
        {
            r_[j] = 0.0;
            for (int n=0; n<N_DATA_-j; n++)
                r_[j] = r_[j] + x[n]*x[n+j];
        }
    }

    // Levinson-Durbin algorithm
    bool LinearPred::Durbin(float a[], float &em)
    {
        // Initialization
        em = r_[0];

        // Repeat
        for (int m=0; m<order_; m++)
        {
            float w = r_[m+1];
            for (int j=0; j<=m-1; j++)
                w = w - r_[m-j]*a[j];

            k_[m] = w/em;
            em = em*(1 - k_[m]*k_[m]);

            if (em < 0) break;  // Error for negative squared sum of residual

            a[m] = k_[m];
            for (int j=0; j<=m-1; j++)
                am_[j] = a[j];
            for (int j=0; j<=m-1; j++)
                a[j] = am_[j] - k_[m]*am_[m-j-1];
        }

        if (em < 0) return false;
        else        return true;
    }
}