Realtime spectrum analyzer. Using FFT, linear prediction, or cepstrum smoothing. Version using MEMS microphone and CODEC, named "F746_RealtimeSpectrumAnalyzer_MEMS_Mic" is registered. リアルタイム スペクトル解析器.解析の手法:FFT,線形予測法,ケプストラムによる平滑化の3種類.このプログラムの説明は,CQ出版社のインターフェース誌,2016年4月号に掲載.外付けのマイクまたは他の信号源等を A0 に接続する.線形予測法,ケプストラムは,スペクトル解析の対象を音声信号に想定してパラメータを設定している.MEMS マイクと CODEC を使ったバージョンを "F746_RealtimeSpectrumAnalyzer_MEMS_Mic" として登録.

Dependencies:   BSP_DISCO_F746NG BUTTON_GROUP LCD_DISCO_F746NG TS_DISCO_F746NG UIT_FFT_Real mbed

Revision:
2:095b360e0f54
Parent:
1:1656f55c2d5c
Child:
3:7c26b701f363
--- a/main.cpp	Thu Dec 10 04:56:04 2015 +0000
+++ b/main.cpp	Sun Dec 13 07:20:14 2015 +0000
@@ -2,7 +2,7 @@
 //  Realtime spectrum analyzer
 //      Input: A0 (CN5)
 //
-//  2015/12/10, Copyright (c) 2015 MIKAMI, Naoki
+//  2015/12/13, Copyright (c) 2015 MIKAMI, Naoki
 //-----------------------------------------------------------
 
 #include "button_group.hpp"
@@ -12,31 +12,20 @@
 
 using namespace Mikami;
 
-const int N_DATA_ = 260;
-
-Ticker timer_;              // for timer interrupt
-Sampler input_(A0, timer_, N_DATA_);
-
-// Interrupt service routine for timer
-void Isr()
-{
-    input_.Execute();
-}
-
 int main()
 {
-    const int N_FFT = 512;      // number of date for FFT
-    const int X_WAV = 44;       // Origin for waveform of x axis
-    const int Y_WAV = 36;       // Origin for waveform of y axis
-    const int X0    = 40;       // Origin for spectrum of x axis
-    const int Y0    = 234;      // Origin for spectrum of y axis
-    const float DB1 = 2.4f;     // Pixels for 1 dB
-    const int BIN   = 1;        // Pixels per bin
-    const int W_DB  = 60;       // Width in dB to be displayed
-    const int X0_BUTTON = 340;  // Origin for button of x axis
+    const int N_DATA = 260; // number of data to be analyzed
+    const int N_FFT = 512;  // number of date for FFT
+    const int X_WAV = 44;   // Origin for waveform of x axis
+    const int Y_WAV = 36;   // Origin for waveform of y axis
+    const int X0    = 40;   // Origin for spectrum of x axis
+    const int Y0    = 234;  // Origin for spectrum of y axis
+    const float DB1 = 2.4f; // Pixels for 1 dB
+    const int BIN   = 1;    // Pixels per bin
+    const int W_DB  = 60;   // Width in dB to be displayed
+    const int X0_BTN = 340; // Origin for button of x axis
 
-    const int FS = 10000;       // Sampling frequency: 10 kHz
-    const int TS = 1000000/FS;  // Sampling period: 100 micro sec.
+    const int FS = 10000;   // Sampling frequency: 10 kHz
 
     const uint32_t BACK_COLOR          = 0xFF006A6C;    // teal green
     const uint32_t INACTIVE_COLOR      = BACK_COLOR & 0xD0FFFFFF;
@@ -46,101 +35,92 @@
     const uint32_t AXIS_COLOR          = 0xFFCCFFFF;
     const uint32_t LINE_COLOR          = LCD_COLOR_CYAN;
     
-    LCD_DISCO_F746NG lcd;   // Object for LCD display
-    TS_DISCO_F746NG ts;     // Object for touch pannel
+    LCD_DISCO_F746NG lcd;           // Object for LCD display
+    TS_DISCO_F746NG ts;             // Object for touch pannel
+    Sampler input(A0, FS, N_DATA);  // Object for sampling
 
     lcd.Clear(BACK_COLOR);
 
     // Setting of button group
     const string RUN_STOP[2] = {"RUN", "STOP"};
-    ButtonGroup runStop(lcd, ts, X0_BUTTON, 10, 60, 40,
+    ButtonGroup runStop(lcd, ts, X0_BTN, 10, 60, 40,
                         ORIGINAL_COLOR, BACK_COLOR,
                         2, RUN_STOP, 5, 0, 2);
     runStop.Draw(1, INACTIVE_COLOR, INACTIVE_TEXT_COLOR);
 
     const string NORM_INV[2] = {"NORM", "INV"};
-    ButtonGroup normInv(lcd, ts, X0_BUTTON, 65, 60, 40,
+    ButtonGroup normInv(lcd, ts, X0_BTN, 65, 60, 40,
                         ORIGINAL_COLOR, BACK_COLOR,
                         2, NORM_INV, 5, 0, 2);
     normInv.DrawAll(INACTIVE_COLOR, INACTIVE_TEXT_COLOR);
 
     const string METHOD[3] = {"FFT", "Linear Pred.", "Cepstrum"};
-    ButtonGroup method(lcd, ts, X0_BUTTON, 120, 125, 40,
+    ButtonGroup method(lcd, ts, X0_BTN, 120, 125, 40,
                        ORIGINAL_COLOR, BACK_COLOR,
                        3, METHOD, 0, 5, 1);
     method.DrawAll(INACTIVE_COLOR, INACTIVE_TEXT_COLOR);
     // End of button group setting
 
+    WaveformDisplay waveDisp(lcd, X_WAV, Y_WAV, N_DATA, 10,
+                             AXIS_COLOR, LINE_COLOR, BACK_COLOR);
+
     SpectrumDisplay disp(lcd, N_FFT, X0, Y0, DB1, BIN, W_DB, FS,
                          AXIS_COLOR, LINE_COLOR, BACK_COLOR);
     // Linear prediction:   order = 14
     // Cepstral smoothing:  lifter length = 50
-    Selector selector(disp, method, N_DATA_, N_FFT, 14, 50,
-                      TOUCHED_COLOR);
+    Selector selector(disp, method, N_DATA, N_FFT, 14, 50,
+                      TOUCHED_COLOR);   
 
-    WaveformDisplay waveDisp(lcd, X_WAV, Y_WAV, N_DATA_, 10,
-                             AXIS_COLOR, LINE_COLOR, BACK_COLOR);
-    
-    int16_t* sn;    // pointer of data to be analyzed
-    bool runSelected = false;
-    int touchedNum = -1;
-    bool inv = false;
+    // Wait for "RUN" button touched
+    while (!runStop.Touched(0)) {}
+
+    // Start spectrum analyzing
+    int inv = 0;        // 0: "NORM", 1: "INV"
+    int select = -1;
     while (true)
     {
-        if (!runSelected)
+        int16_t* sn;    // pointer of data to be analyzed
+        
+        if (runStop.GetCurrentColor(0) != TOUCHED_COLOR)
         {
             if (runStop.Touched(0, TOUCHED_COLOR))
             {
-                input_.ClearCount();
-                timer_.attach_us(&Isr, TS); // Enable timer interrupt
-                runSelected = true;
+                input.IntrEnable();
                 for (int n=0; n<3; n++)
-                    if (n != touchedNum) method.Redraw(n);
+                    if (n != select) method.Redraw(n);
+                normInv.Draw(inv, TOUCHED_COLOR);
+                normInv.Redraw(1-inv);
             }
-            int touchedNum2 = touchedNum;
-            method.GetTouchedNumber(touchedNum2);
-            if ( (touchedNum != -1) && (touchedNum != touchedNum2) )
-                selector.Execute(sn, touchedNum);
+            if (method.GetTouchedNumber(select, TOUCHED_COLOR))
+                selector.Execute(sn, select);
         }
         
-        if (runSelected)
+        if (runStop.GetCurrentColor(0) == TOUCHED_COLOR)
         {
-            // "NORM" or "INV"
-            if (normInv.GetCurrentColor(0) == INACTIVE_COLOR)
+            normInv.GetTouchedNumber(inv, TOUCHED_COLOR);
+
+            if (input.Filled())
             {
-                for (int n=0; n<2; n++) normInv.Redraw(n);
-                if (!inv)
-                    normInv.Draw(0, TOUCHED_COLOR);
-                else
-                    normInv.Draw(1, TOUCHED_COLOR);                
-            }
-
-            int invButton;     // 0: "NORM", 1: "INV"
-            normInv.GetTouchedNumber(invButton, TOUCHED_COLOR);
-
-            // If input buffer filled, start analysis
-            if (input_.Filled())
-            {
-                sn = input_.Get();
+                sn = input.Get();
                 waveDisp.Execute(sn);
   
                 // spectrum analysis and display
-                selector.Execute(sn, touchedNum);   
+                selector.Execute(sn, select);   
 
                 // Check "STOP" button touched
                 if (runStop.Touched(1, TOUCHED_COLOR))
+                    normInv.DrawAll(INACTIVE_COLOR,
+                                    INACTIVE_TEXT_COLOR);
+                else    // Restart sampling
                 {
-                    timer_.detach();        // Disable timer interrupt
-                    runSelected = false;
-                    normInv.DrawAll(INACTIVE_COLOR, INACTIVE_TEXT_COLOR);
+                    input.InvertEnable(inv == 1);
+                    input.Restart();
+                    input.IntrEnable();
                 }
-
-                // Restart sampling
-                inv = (invButton == 0) ? input_.InvertEnable(false)
-                                       : input_.InvertEnable(true);
-                input_.Restart();
-                timer_.attach_us(&Isr, TS); // Enable timer interrupt
             }
+            else
+                if (method.GetTouchedNumber(select, TOUCHED_COLOR))
+                    selector.Execute(sn, select);
         }
     }
 }