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@7:5ba884060d3b, 2019-11-24 (annotated)
- Committer:
- MikamiUitOpen
- Date:
- Sun Nov 24 11:14:01 2019 +0000
- Revision:
- 7:5ba884060d3b
- Parent:
- 0:a539141b9dec
8
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MikamiUitOpen | 0:a539141b9dec | 1 | //----------------------------------------------------- |
MikamiUitOpen | 0:a539141b9dec | 2 | // Class for linear prediction |
MikamiUitOpen | 0:a539141b9dec | 3 | // |
MikamiUitOpen | 0:a539141b9dec | 4 | // 2017/02/11, Copyright (c) 2017 MIKAMI, Naoki |
MikamiUitOpen | 0:a539141b9dec | 5 | //----------------------------------------------------- |
MikamiUitOpen | 0:a539141b9dec | 6 | |
MikamiUitOpen | 0:a539141b9dec | 7 | #include "LinearPrediction.hpp" |
MikamiUitOpen | 0:a539141b9dec | 8 | |
MikamiUitOpen | 0:a539141b9dec | 9 | namespace Mikami |
MikamiUitOpen | 0:a539141b9dec | 10 | { |
MikamiUitOpen | 0:a539141b9dec | 11 | LinearPred::LinearPred(int nData, int order) |
MikamiUitOpen | 0:a539141b9dec | 12 | : N_DATA_(nData), order_(order), |
MikamiUitOpen | 0:a539141b9dec | 13 | r_(order+1), k_(order), am_(order) {} |
MikamiUitOpen | 0:a539141b9dec | 14 | |
MikamiUitOpen | 0:a539141b9dec | 15 | // Calculate linear-predictive coefficients |
MikamiUitOpen | 0:a539141b9dec | 16 | bool LinearPred::Execute(const float x[], float a[], |
MikamiUitOpen | 0:a539141b9dec | 17 | float &em) |
MikamiUitOpen | 0:a539141b9dec | 18 | { |
MikamiUitOpen | 0:a539141b9dec | 19 | AutoCorr(x); |
MikamiUitOpen | 0:a539141b9dec | 20 | return Durbin(a, em); |
MikamiUitOpen | 0:a539141b9dec | 21 | } |
MikamiUitOpen | 0:a539141b9dec | 22 | |
MikamiUitOpen | 0:a539141b9dec | 23 | // Calculate auto-correlation |
MikamiUitOpen | 0:a539141b9dec | 24 | void LinearPred::AutoCorr(const float x[]) |
MikamiUitOpen | 0:a539141b9dec | 25 | { |
MikamiUitOpen | 0:a539141b9dec | 26 | for (int j=0; j<=order_; j++) |
MikamiUitOpen | 0:a539141b9dec | 27 | { |
MikamiUitOpen | 0:a539141b9dec | 28 | r_[j] = 0.0; |
MikamiUitOpen | 0:a539141b9dec | 29 | for (int n=0; n<N_DATA_-j; n++) |
MikamiUitOpen | 0:a539141b9dec | 30 | r_[j] = r_[j] + x[n]*x[n+j]; |
MikamiUitOpen | 0:a539141b9dec | 31 | } |
MikamiUitOpen | 0:a539141b9dec | 32 | } |
MikamiUitOpen | 0:a539141b9dec | 33 | |
MikamiUitOpen | 0:a539141b9dec | 34 | // Levinson-Durbin algorithm |
MikamiUitOpen | 0:a539141b9dec | 35 | bool LinearPred::Durbin(float a[], float &em) |
MikamiUitOpen | 0:a539141b9dec | 36 | { |
MikamiUitOpen | 0:a539141b9dec | 37 | // Initialization |
MikamiUitOpen | 0:a539141b9dec | 38 | em = r_[0]; |
MikamiUitOpen | 0:a539141b9dec | 39 | |
MikamiUitOpen | 0:a539141b9dec | 40 | // Repeat |
MikamiUitOpen | 0:a539141b9dec | 41 | for (int m=0; m<order_; m++) |
MikamiUitOpen | 0:a539141b9dec | 42 | { |
MikamiUitOpen | 0:a539141b9dec | 43 | float w = r_[m+1]; |
MikamiUitOpen | 0:a539141b9dec | 44 | for (int j=0; j<=m-1; j++) |
MikamiUitOpen | 0:a539141b9dec | 45 | w = w - r_[m-j]*a[j]; |
MikamiUitOpen | 0:a539141b9dec | 46 | |
MikamiUitOpen | 0:a539141b9dec | 47 | k_[m] = w/em; |
MikamiUitOpen | 0:a539141b9dec | 48 | em = em*(1 - k_[m]*k_[m]); |
MikamiUitOpen | 0:a539141b9dec | 49 | |
MikamiUitOpen | 0:a539141b9dec | 50 | if (em < 0) break; // Error for negative squared sum of residual |
MikamiUitOpen | 0:a539141b9dec | 51 | |
MikamiUitOpen | 0:a539141b9dec | 52 | a[m] = k_[m]; |
MikamiUitOpen | 0:a539141b9dec | 53 | for (int j=0; j<=m-1; j++) |
MikamiUitOpen | 0:a539141b9dec | 54 | am_[j] = a[j]; |
MikamiUitOpen | 0:a539141b9dec | 55 | for (int j=0; j<=m-1; j++) |
MikamiUitOpen | 0:a539141b9dec | 56 | a[j] = am_[j] - k_[m]*am_[m-j-1]; |
MikamiUitOpen | 0:a539141b9dec | 57 | } |
MikamiUitOpen | 0:a539141b9dec | 58 | |
MikamiUitOpen | 0:a539141b9dec | 59 | if (em < 0) return false; |
MikamiUitOpen | 0:a539141b9dec | 60 | else return true; |
MikamiUitOpen | 0:a539141b9dec | 61 | } |
MikamiUitOpen | 0:a539141b9dec | 62 | } |