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:
0:5c237fdcba23
Child:
1:1656f55c2d5c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Dec 09 05:05:00 2015 +0000
@@ -0,0 +1,149 @@
+//-----------------------------------------------------------
+//  Realtime spectrum analyzer
+//      Input: A0 (CN5)
+//
+//  2015/12/09, Copyright (c) 2015 MIKAMI, Naoki
+//-----------------------------------------------------------
+
+#include "button_group.hpp"
+#include "sampler.hpp"
+#include "waveform_display.hpp"
+#include "AnalysisSelector.hpp"
+
+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 TimerIsr()
+{
+    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 FS = 10000;       // Sampling frequency: 10 kHz
+    const int TS = 1000000/FS;  // Sampling period: 100 micro sec.
+
+    const uint32_t BACK_COLOR          = 0xFF006A6C;    // teal green
+    const uint32_t INACTIVE_COLOR      = BACK_COLOR & 0xD0FFFFFF;
+    const uint32_t TOUCHED_COLOR       = 0xFF7F7FFF;
+    const uint32_t ORIGINAL_COLOR      = 0xFF0068B7;
+    const uint32_t INACTIVE_TEXT_COLOR = LCD_COLOR_GRAY;
+    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.Clear(BACK_COLOR);
+
+    // Setting of button group
+    const string RUN_STOP[2] = {"RUN", "STOP"};
+    ButtonGroup runStop(lcd, ts, X0_BUTTON, 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,
+                        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,
+                       ORIGINAL_COLOR, BACK_COLOR,
+                       3, METHOD, 0, 5, 1);
+    method.DrawAll(INACTIVE_COLOR, INACTIVE_TEXT_COLOR);
+    // End of button group setting
+
+    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);
+
+    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;
+    while (true)
+    {
+        if (!runSelected)
+        {
+            if (runStop.Touched(0, TOUCHED_COLOR))
+            {
+                input_.ClearCount();
+                timer_.attach_us(&TimerIsr, TS);   // Enable timer interrupt
+                runSelected = true;
+                for (int n=0; n<3; n++)
+                    if (n != touchedNum) method.Redraw(n);
+            }
+            int touchedNum2 = touchedNum;
+            method.GetTouchedNumber(touchedNum2);
+            if ( (touchedNum != -1) && (touchedNum != touchedNum2) )
+                selector.Execute(sn, touchedNum);
+        }
+        
+        if (runSelected)
+        {
+            // "NORM" or "INV"
+            if (normInv.GetCurrentColor(0) == INACTIVE_COLOR)
+            {
+                for (int n=0; n<2; n++) normInv.Redraw(n);
+                if (!inv)
+                    normInv.Draw(0, TOUCHED_COLOR);
+                else
+                    normInv.Draw(1, TOUCHED_COLOR);                
+            }
+
+            int button;
+            if (normInv.GetTouchedNumber(button, TOUCHED_COLOR))
+            {
+                if (button == 0)    
+                    inv = input_.InvertEnable(false);
+                else
+                    inv = input_.InvertEnable(true);
+            }
+
+            // If input buffer filled, start analysis
+            if (input_.Filled())
+            {
+                sn = input_.Get();
+                waveDisp.Execute(sn);
+  
+                selector.Execute(sn, touchedNum);   // spectrum analysis and display
+
+                // Check "STOP" button touched
+                if (runStop.Touched(1, TOUCHED_COLOR))
+                {
+                    timer_.detach();                // Disable timer interrupt
+                    runSelected = false;
+                    normInv.DrawAll(INACTIVE_COLOR, INACTIVE_TEXT_COLOR);
+                }
+
+                // Restart sampling
+                input_.Restart();
+                timer_.attach_us(&TimerIsr, TS);    // Enable timer interrupt
+            }
+        }
+    }
+}