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
main.cpp
- Committer:
- MikamiUitOpen
- Date:
- 2015-12-10
- Revision:
- 1:1656f55c2d5c
- Parent:
- 0:5c237fdcba23
- Child:
- 2:095b360e0f54
File content as of revision 1:1656f55c2d5c:
//----------------------------------------------------------- // Realtime spectrum analyzer // Input: A0 (CN5) // // 2015/12/10, Copyright (c) 2015 MIKAMI, Naoki //----------------------------------------------------------- #include "button_group.hpp" #include "Sampler.hpp" #include "WaveformDisplay.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 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 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(&Isr, 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 invButton; // 0: "NORM", 1: "INV" normInv.GetTouchedNumber(invButton, TOUCHED_COLOR); // If input buffer filled, start analysis if (input_.Filled()) { sn = input_.Get(); waveDisp.Execute(sn); // spectrum analysis and display selector.Execute(sn, touchedNum); // 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 inv = (invButton == 0) ? input_.InvertEnable(false) : input_.InvertEnable(true); input_.Restart(); timer_.attach_us(&Isr, TS); // Enable timer interrupt } } } }