Realtime spectrogram for DISCO-F746NG. On-board MEMS microphone is used for input sound signal. リアルタイムスペクトログラム.入力:MEMSマイク

Dependencies:   F746_GUI F746_SAI_IO UIT_FFT_Real

Files at this revision

API Documentation at this revision

Comitter:
MikamiUitOpen
Date:
Mon Apr 10 13:43:07 2017 +0000
Parent:
5:c0877670b0ac
Commit message:
7

Changed in this revision

BSP_DISCO_F746NG.lib Show diff for this revision Revisions of this file
F746_GUI.lib Show annotated file Show diff for this revision Revisions of this file
LCD_DISCO_F746NG.lib Show diff for this revision Revisions of this file
MySpectrogram/AnalysisBase.cpp Show annotated file Show diff for this revision Revisions of this file
MySpectrogram/AnalysisBase.hpp Show annotated file Show diff for this revision Revisions of this file
MySpectrogram/FFT_Analysis.cpp Show annotated file Show diff for this revision Revisions of this file
MySpectrogram/FFT_Analysis.hpp Show annotated file Show diff for this revision Revisions of this file
MySpectrogram/Hamming.hpp Show annotated file Show diff for this revision Revisions of this file
MySpectrogram/MethodCollection.hpp Show annotated file Show diff for this revision Revisions of this file
TS_DISCO_F746NG.lib Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/BSP_DISCO_F746NG.lib	Fri Mar 17 02:06:23 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/teams/ST/code/BSP_DISCO_F746NG/#5a395e126678
--- a/F746_GUI.lib	Fri Mar 17 02:06:23 2017 +0000
+++ b/F746_GUI.lib	Mon Apr 10 13:43:07 2017 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/MikamiUitOpen/code/F746_GUI/#d7f9f667fa68
+https://mbed.org/users/MikamiUitOpen/code/F746_GUI/#e6648167e8d3
--- a/LCD_DISCO_F746NG.lib	Fri Mar 17 02:06:23 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://developer.mbed.org/teams/ST/code/LCD_DISCO_F746NG/#d44525b1de98
--- a/MySpectrogram/AnalysisBase.cpp	Fri Mar 17 02:06:23 2017 +0000
+++ b/MySpectrogram/AnalysisBase.cpp	Mon Apr 10 13:43:07 2017 +0000
@@ -1,7 +1,7 @@
 //-------------------------------------------------------
 //  Base abstract class for spectrum analysis
 //
-//  2016/07/23, Copyright (c) 2016 MIKAMI, Naoki
+//  2017/03/30, Copyright (c) 2017 MIKAMI, Naoki
 //-------------------------------------------------------
 
 #include "AnalysisBase.hpp"
@@ -21,7 +21,7 @@
             
         // Windowing
         wHm_.Execute(xData_, wData_);
-            
+
         Analyze(wData_, db);
     }
 }
--- a/MySpectrogram/AnalysisBase.hpp	Fri Mar 17 02:06:23 2017 +0000
+++ b/MySpectrogram/AnalysisBase.hpp	Mon Apr 10 13:43:07 2017 +0000
@@ -1,7 +1,7 @@
 //-------------------------------------------------------
 //  Base abstract class for spectrum analysis (Header)
 //
-//  2016/07/23, Copyright (c) 2016 MIKAMI, Naoki
+//  2017/03/30, Copyright (c) 2017 MIKAMI, Naoki
 //-------------------------------------------------------
 
 #ifndef BASE_ANALYZER_HPP
--- a/MySpectrogram/FFT_Analysis.cpp	Fri Mar 17 02:06:23 2017 +0000
+++ b/MySpectrogram/FFT_Analysis.cpp	Mon Apr 10 13:43:07 2017 +0000
@@ -1,7 +1,7 @@
 //-------------------------------------------------------
 //  Class for spectrum analysis using FFT
 //
-//  2016/07/23, Copyright (c) 2015 MIKAMI, Naoki
+//  2017/03/30, Copyright (c) 2017 MIKAMI, Naoki
 //-------------------------------------------------------
 
 #include "FFT_Analysis.hpp"
--- a/MySpectrogram/FFT_Analysis.hpp	Fri Mar 17 02:06:23 2017 +0000
+++ b/MySpectrogram/FFT_Analysis.hpp	Mon Apr 10 13:43:07 2017 +0000
@@ -1,7 +1,7 @@
 //-------------------------------------------------------
 //  Class for spectrum analysis using FFT (Header)
 //
-//  2016/07/23, Copyright (c) 2016 MIKAMI, Naoki
+//  2017/03/30, Copyright (c) 2017 MIKAMI, Naoki
 //-------------------------------------------------------
 
 #ifndef FFT_ANALYZER_HPP
@@ -15,7 +15,6 @@
     {
     public:
         FftAnalyzer(int nData, int nFft);
-        virtual ~FftAnalyzer() {}
 
     private:
         Array<Complex> yFft_;   // output of FFT
@@ -27,5 +26,4 @@
         FftAnalyzer& operator=(const FftAnalyzer& );
     };
 }
-
 #endif  // FFT_ANALYZER_HPP
--- a/MySpectrogram/Hamming.hpp	Fri Mar 17 02:06:23 2017 +0000
+++ b/MySpectrogram/Hamming.hpp	Mon Apr 10 13:43:07 2017 +0000
@@ -1,7 +1,7 @@
 //-------------------------------------------------------------------
 //  Hamming windowing with zero-padding
 //
-//  2016/07/23, Copyright (c) 2016 MIKAMI, Naoki
+//  2017/04/10, Copyright (c) 2017 MIKAMI, Naoki
 //-------------------------------------------------------------------
 
 #ifndef HAMMING_WINDOW_HPP
@@ -23,10 +23,7 @@
             for (int k=0; k<nData; k++)
                 w_[k] = 0.54f - 0.46f*cosf(k*pi2L);
         }
-        
-        // Destructor
-        virtual ~HammingWindow() {}
-        
+
         // Windowing
         void Execute(const float x[], float y[])
         {
@@ -37,7 +34,7 @@
     private:
         const int N_;
         const int NFFT_;
-                
+
         Array<float> w_;
 
         // disallow copy constructor and assignment operator
--- a/MySpectrogram/MethodCollection.hpp	Fri Mar 17 02:06:23 2017 +0000
+++ b/MySpectrogram/MethodCollection.hpp	Mon Apr 10 13:43:07 2017 +0000
@@ -1,7 +1,7 @@
 //--------------------------------------------------------------
-//  スペクトログラムで使う大域関数
+//  スペクトログラムで使うグローバル関数
 //
-//  2017/03/17, Copyright (c) 2017 MIKAMI, Naoki
+//  2017/04/10, Copyright (c) 2017 MIKAMI, Naoki
 //--------------------------------------------------------------
 
 #ifndef METHOD_COLLECTION_HPP
@@ -14,11 +14,11 @@
 
 namespace Mikami
 {
-    // 色相の違いで表示
+    // 強度を色相の違いに変換
     //      0.0 <= x <= 1.0
     uint32_t HueScale(float x)
     {
-        if (x >= 1) return LCD_COLOR_WHITE;
+        if (x > 1) return LCD_COLOR_WHITE;
         int r = 0;
         int b = 0;
 
@@ -34,8 +34,9 @@
                   uint16_t ms100, uint16_t px1kHz, LCD_DISCO_F746NG &lcd)
     {
         const uint16_t TICK = 5;    // 目盛線の長さ
+        lcd.SetTextColor(axisColor);
+
         // 横標軸
-        lcd.SetTextColor(axisColor);
         lcd.DrawHLine(x0, y0+TICK, w0);
         for (int n=0; n<=w0/ms100; n++)
             if ((n % 10)== 0) lcd.DrawVLine(x0+n*ms100, y0, 5);
@@ -44,9 +45,8 @@
             NumericLabel<int> num(x0+n*ms100, y0+TICK+3,
                                   "%1d", (int)(n*0.1f), Label::CENTER);
         Label time(x0+w0/2, y0+22, "TIME [s]", Label::CENTER);
-    
+
         // 縦標軸
-        lcd.SetTextColor(axisColor);
         lcd.DrawVLine(x0-TICK, y0-h0, h0);
         for (int n=0; n<=h0/px1kHz; n++)
             lcd.DrawHLine(x0-TICK, y0-n*px1kHz, TICK);
@@ -66,7 +66,7 @@
             NumericLabel<int> num(440, y0-(n*100)/4-5, "%2d", n*20);
         Label dB(432, y0-120, "[dB]");
 
-        for (int n=0; n<=100; n++)
+        for (int n=0; n<=101; n++)
         {
             lcd.SetTextColor(HueScale(n/100.0f));
             lcd.DrawHLine(460, y0-n, 16);
--- a/TS_DISCO_F746NG.lib	Fri Mar 17 02:06:23 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://developer.mbed.org/teams/ST/code/TS_DISCO_F746NG/#fe0cf5e2960f
--- a/main.cpp	Fri Mar 17 02:06:23 2017 +0000
+++ b/main.cpp	Mon Apr 10 13:43:07 2017 +0000
@@ -2,39 +2,22 @@
 //  リアルタイム・スペクトログラム
 //      入力: MEMS マイク
 //
-//  2017/03/17, Copyright (c) 2017 MIKAMI, Naoki
+//  2017/04/10, Copyright (c) 2017 MIKAMI, Naoki
 //------------------------------------------------
 
 #include "SAI_InOut.hpp"
 #include "F746_GUI.hpp"
 #include "MethodCollection.hpp"
-
-#include "Matrix.hpp"
-
 using namespace Mikami;
 
 int main()
 {
-    const int FS = I2S_AUDIOFREQ_16K;   // 標本化周波数: 16 kHz
+    const int FS = AUDIO_FREQUENCY_16K; // 標本化周波数:16 kHz
     const int N_FFT = 512;              // FFT の点数
-    const float FRAME = (N_FFT/(float)FS)*1000.0f;  // 1 フレームに対応する時間(単位:ms)
+    SaiIO mySai(SaiIO::INPUT, N_FFT+1, FS,          // 入力用
+                INPUT_DEVICE_DIGITAL_MICROPHONE_2); // 入力デバイス:MEMS マイク
 
-    const int X0    = 40;           // 表示領域の x 座標の原点
-    const int Y0    = 200;          // 表示領域の y 座標の原点
-    const int H0    = 160;          // 表示する際の周波数軸の長さ(5 kHz に対応)
-    const uint16_t PX_1KHZ = H0/5;  // 1 kHz に対応するピクセル数
-    const int W0    = 360;          // 横方向の全体の表示の幅(単位:ピクセル)
-    const int H_BAR = 2;            // 表示する際の 1 フレームに対応する横方向のピクセル数
-    const int SIZE_X = W0/H_BAR;
-    const uint16_t MS100 = 100*H_BAR/FRAME; // 100 ms に対応するピクセル数
-    const uint32_t AXIS_COLOR = LCD_COLOR_WHITE;//0xFFCCFFFF;
-    
-    Matrix<uint32_t> spectra(SIZE_X, H0+1, GuiBase::ENUM_BACK);
-
-    SaiIO mySai(SaiIO::INPUT, N_FFT+1, FS,
-                INPUT_DEVICE_DIGITAL_MICROPHONE_2);
-
-    LCD_DISCO_F746NG &lcd = GuiBase::GetLcd();  // LCD 表示器のオブジェクト
+    LCD_DISCO_F746NG &lcd = GuiBase::GetLcd();  // LCD 表示器のオブジェクトの参照を取得
     lcd.Clear(GuiBase::ENUM_BACK);
     Label myLabel1(240, 2, "Real-time spectrogram", Label::CENTER, Font16);
 
@@ -44,36 +27,45 @@
     const uint16_t B_H = 30;
     const string RUN_STOP[2] = {"RUN", "STOP"};
     ButtonGroup runStop(325, B_Y, B_W, B_H, 2, RUN_STOP, 0, 0, 2, 1);
-    
-    Button clear(430, B_Y, B_W, B_H, "CLEAR");
-    clear.Inactivate();
-    // ButtonGroup の設定(ここまで)
+
+    Button clearButton(430, B_Y, B_W, B_H, "CLEAR");
+    clearButton.Inactivate();
 
     // 座標軸
+    const uint16_t X0 = 40;         // 表示領域の x 座標の原点
+    const uint16_t Y0 = 200;        // 表示領域の y 座標の原点
+    const uint16_t PX_1KHZ = 32;    // 1 kHz に対応するピクセル数
+    const uint16_t H0 = PX_1KHZ*5;  // 周波数軸の長さ(5 kHz に対応)に対応するピクセル数
+    const uint16_t W0 = 360;        // 横方向の全体の表示の幅(単位:ピクセル)
+    const float FRAME = (N_FFT/(float)FS)*1000.0f;  // 1 フレームに対応する時間(単位:ms)
+    const uint16_t H_BAR = 2;       // 表示する際の 1 フレームに対応する横方向のピクセル数
+    const uint16_t MS100 = 100*H_BAR/FRAME; // 100 ms に対応するピクセル数
+    const uint32_t AXIS_COLOR = LCD_COLOR_WHITE;    
     DrawAxis(X0, Y0, W0, H0, AXIS_COLOR, MS100, PX_1KHZ, lcd);
 
-    Array<float> sn(N_FFT+1);
-    Array<float> db(N_FFT/2+1);
-
     // 色と dB の関係の表示
     ColorDb(Y0, AXIS_COLOR, lcd);
-        
+
+    Array<float> sn(N_FFT+1);   // スペクトル解析する信号を格納するバッファ
+    Array<float> db(N_FFT/2+1); // 計算された対数スペクトルを格納するバッファ
+    // スペクトルの大きさに対応する色データを格納する2次元配列
+    Matrix<uint32_t> spectra(W0/H_BAR, H0+1, GuiBase::ENUM_BACK);
     FftAnalyzer fftAnalyzer(N_FFT+1, N_FFT);
 
     // ループ内で使う変数の初期化
-    int stop = 1;       // 0: run, 1: stop
+    int stop = 0;       // 0: run, 1: stop
 
-    while(!runStop.GetTouchedNumber(stop)) {}
+    while(!runStop.Touched(0)) {}   // "RUN" をタッチするまで待つ
     // データ読み込み開始
     mySai.RecordIn();
-    
+    while(!mySai.IsCaptured()) {}
+
     while (true)
     {
         runStop.GetTouchedNumber(stop);
-
         if (stop == 0)
         {
-            clear.Inactivate();
+            clearButton.Inactivate();
             if (mySai.IsCaptured())
             {
                 // 1フレーム分の信号の入力
@@ -92,13 +84,14 @@
         }
         else
         {
-            if (!clear.IsActive()) clear.Activate();
-            if (clear.Touched())
+            clearButton.Activate();
+            if (clearButton.Touched())
             {
-                spectra.Fill(GuiBase::ENUM_BACK);   // スペクトルの表示をクリア
-                DisplaySpectrum(spectra, X0, Y0, H_BAR, lcd);
-                clear.Draw();
+                spectra.Fill(GuiBase::ENUM_BACK);   // スペクトルの表示をクリアするための処理
+                DisplaySpectrum(spectra, X0, Y0, H_BAR, lcd);   // スペクトルの表示をクリア
+                clearButton.Draw();
             }
         }
     }
 }
+