![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
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@7:6598a9b70e5a, 2015-12-20 (annotated)
- Committer:
- MikamiUitOpen
- Date:
- Sun Dec 20 13:38:22 2015 +0000
- Revision:
- 7:6598a9b70e5a
- Parent:
- 6:b27ef8d98edc
- Child:
- 8:1f4bc859bc84
8
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MikamiUitOpen | 6:b27ef8d98edc | 1 | //------------------------------------------------ |
MikamiUitOpen | 0:5c237fdcba23 | 2 | // Realtime spectrum analyzer |
MikamiUitOpen | 0:5c237fdcba23 | 3 | // Input: A0 (CN5) |
MikamiUitOpen | 0:5c237fdcba23 | 4 | // |
MikamiUitOpen | 7:6598a9b70e5a | 5 | // 2015/12/20, Copyright (c) 2015 MIKAMI, Naoki |
MikamiUitOpen | 6:b27ef8d98edc | 6 | //------------------------------------------------ |
MikamiUitOpen | 0:5c237fdcba23 | 7 | |
MikamiUitOpen | 0:5c237fdcba23 | 8 | #include "button_group.hpp" |
MikamiUitOpen | 1:1656f55c2d5c | 9 | #include "Sampler.hpp" |
MikamiUitOpen | 1:1656f55c2d5c | 10 | #include "WaveformDisplay.hpp" |
MikamiUitOpen | 0:5c237fdcba23 | 11 | #include "AnalysisSelector.hpp" |
MikamiUitOpen | 0:5c237fdcba23 | 12 | using namespace Mikami; |
MikamiUitOpen | 0:5c237fdcba23 | 13 | |
MikamiUitOpen | 0:5c237fdcba23 | 14 | int main() |
MikamiUitOpen | 0:5c237fdcba23 | 15 | { |
MikamiUitOpen | 2:095b360e0f54 | 16 | const int N_DATA = 260; // number of data to be analyzed |
MikamiUitOpen | 2:095b360e0f54 | 17 | const int N_FFT = 512; // number of date for FFT |
MikamiUitOpen | 2:095b360e0f54 | 18 | const int X_WAV = 44; // Origin for waveform of x axis |
MikamiUitOpen | 2:095b360e0f54 | 19 | const int Y_WAV = 36; // Origin for waveform of y axis |
MikamiUitOpen | 2:095b360e0f54 | 20 | const int X0 = 40; // Origin for spectrum of x axis |
MikamiUitOpen | 2:095b360e0f54 | 21 | const int Y0 = 234; // Origin for spectrum of y axis |
MikamiUitOpen | 2:095b360e0f54 | 22 | const float DB1 = 2.4f; // Pixels for 1 dB |
MikamiUitOpen | 2:095b360e0f54 | 23 | const int BIN = 1; // Pixels per bin |
MikamiUitOpen | 2:095b360e0f54 | 24 | const int W_DB = 60; // Width in dB to be displayed |
MikamiUitOpen | 2:095b360e0f54 | 25 | const int X0_BTN = 340; // Origin for button of x axis |
MikamiUitOpen | 0:5c237fdcba23 | 26 | |
MikamiUitOpen | 2:095b360e0f54 | 27 | const int FS = 10000; // Sampling frequency: 10 kHz |
MikamiUitOpen | 0:5c237fdcba23 | 28 | |
MikamiUitOpen | 0:5c237fdcba23 | 29 | const uint32_t BACK_COLOR = 0xFF006A6C; // teal green |
MikamiUitOpen | 0:5c237fdcba23 | 30 | const uint32_t INACTIVE_COLOR = BACK_COLOR & 0xD0FFFFFF; |
MikamiUitOpen | 0:5c237fdcba23 | 31 | const uint32_t TOUCHED_COLOR = 0xFF7F7FFF; |
MikamiUitOpen | 0:5c237fdcba23 | 32 | const uint32_t ORIGINAL_COLOR = 0xFF0068B7; |
MikamiUitOpen | 0:5c237fdcba23 | 33 | const uint32_t INACTIVE_TEXT_COLOR = LCD_COLOR_GRAY; |
MikamiUitOpen | 0:5c237fdcba23 | 34 | const uint32_t AXIS_COLOR = 0xFFCCFFFF; |
MikamiUitOpen | 0:5c237fdcba23 | 35 | const uint32_t LINE_COLOR = LCD_COLOR_CYAN; |
MikamiUitOpen | 7:6598a9b70e5a | 36 | |
MikamiUitOpen | 7:6598a9b70e5a | 37 | LCD_DISCO_F746NG lcd; // Object for LCD display |
MikamiUitOpen | 7:6598a9b70e5a | 38 | TS_DISCO_F746NG ts; // Object for touch pannel |
MikamiUitOpen | 7:6598a9b70e5a | 39 | Sampler input(A0, FS, N_DATA); // Object for sampling |
MikamiUitOpen | 7:6598a9b70e5a | 40 | int16_t* sn = input.Get(); // input data |
MikamiUitOpen | 0:5c237fdcba23 | 41 | |
MikamiUitOpen | 0:5c237fdcba23 | 42 | lcd.Clear(BACK_COLOR); |
MikamiUitOpen | 0:5c237fdcba23 | 43 | |
MikamiUitOpen | 0:5c237fdcba23 | 44 | // Setting of button group |
MikamiUitOpen | 0:5c237fdcba23 | 45 | const string RUN_STOP[2] = {"RUN", "STOP"}; |
MikamiUitOpen | 2:095b360e0f54 | 46 | ButtonGroup runStop(lcd, ts, X0_BTN, 10, 60, 40, |
MikamiUitOpen | 0:5c237fdcba23 | 47 | ORIGINAL_COLOR, BACK_COLOR, |
MikamiUitOpen | 0:5c237fdcba23 | 48 | 2, RUN_STOP, 5, 0, 2); |
MikamiUitOpen | 0:5c237fdcba23 | 49 | runStop.Draw(1, INACTIVE_COLOR, INACTIVE_TEXT_COLOR); |
MikamiUitOpen | 0:5c237fdcba23 | 50 | |
MikamiUitOpen | 0:5c237fdcba23 | 51 | const string NORM_INV[2] = {"NORM", "INV"}; |
MikamiUitOpen | 2:095b360e0f54 | 52 | ButtonGroup normInv(lcd, ts, X0_BTN, 65, 60, 40, |
MikamiUitOpen | 0:5c237fdcba23 | 53 | ORIGINAL_COLOR, BACK_COLOR, |
MikamiUitOpen | 0:5c237fdcba23 | 54 | 2, NORM_INV, 5, 0, 2); |
MikamiUitOpen | 0:5c237fdcba23 | 55 | normInv.DrawAll(INACTIVE_COLOR, INACTIVE_TEXT_COLOR); |
MikamiUitOpen | 0:5c237fdcba23 | 56 | |
MikamiUitOpen | 0:5c237fdcba23 | 57 | const string METHOD[3] = {"FFT", "Linear Pred.", "Cepstrum"}; |
MikamiUitOpen | 2:095b360e0f54 | 58 | ButtonGroup method(lcd, ts, X0_BTN, 120, 125, 40, |
MikamiUitOpen | 0:5c237fdcba23 | 59 | ORIGINAL_COLOR, BACK_COLOR, |
MikamiUitOpen | 0:5c237fdcba23 | 60 | 3, METHOD, 0, 5, 1); |
MikamiUitOpen | 0:5c237fdcba23 | 61 | method.DrawAll(INACTIVE_COLOR, INACTIVE_TEXT_COLOR); |
MikamiUitOpen | 0:5c237fdcba23 | 62 | // End of button group setting |
MikamiUitOpen | 0:5c237fdcba23 | 63 | |
MikamiUitOpen | 2:095b360e0f54 | 64 | WaveformDisplay waveDisp(lcd, X_WAV, Y_WAV, N_DATA, 10, |
MikamiUitOpen | 2:095b360e0f54 | 65 | AXIS_COLOR, LINE_COLOR, BACK_COLOR); |
MikamiUitOpen | 2:095b360e0f54 | 66 | |
MikamiUitOpen | 0:5c237fdcba23 | 67 | SpectrumDisplay disp(lcd, N_FFT, X0, Y0, DB1, BIN, W_DB, FS, |
MikamiUitOpen | 0:5c237fdcba23 | 68 | AXIS_COLOR, LINE_COLOR, BACK_COLOR); |
MikamiUitOpen | 0:5c237fdcba23 | 69 | // Linear prediction: order = 14 |
MikamiUitOpen | 0:5c237fdcba23 | 70 | // Cepstral smoothing: lifter length = 50 |
MikamiUitOpen | 2:095b360e0f54 | 71 | Selector selector(disp, method, N_DATA, N_FFT, 14, 50, |
MikamiUitOpen | 7:6598a9b70e5a | 72 | TOUCHED_COLOR); |
MikamiUitOpen | 0:5c237fdcba23 | 73 | |
MikamiUitOpen | 2:095b360e0f54 | 74 | // Wait for "RUN" button touched |
MikamiUitOpen | 2:095b360e0f54 | 75 | while (!runStop.Touched(0)) {} |
MikamiUitOpen | 7:6598a9b70e5a | 76 | method.DrawAll(ORIGINAL_COLOR); |
MikamiUitOpen | 2:095b360e0f54 | 77 | |
MikamiUitOpen | 6:b27ef8d98edc | 78 | // Start of spectrum analyzing |
MikamiUitOpen | 5:98ec9dd54144 | 79 | int inv = 0; // 0: "NORM", 1: "INV" |
MikamiUitOpen | 2:095b360e0f54 | 80 | int select = -1; |
MikamiUitOpen | 0:5c237fdcba23 | 81 | while (true) |
MikamiUitOpen | 7:6598a9b70e5a | 82 | { |
MikamiUitOpen | 2:095b360e0f54 | 83 | if (runStop.GetCurrentColor(0) != TOUCHED_COLOR) |
MikamiUitOpen | 0:5c237fdcba23 | 84 | { |
MikamiUitOpen | 0:5c237fdcba23 | 85 | if (runStop.Touched(0, TOUCHED_COLOR)) |
MikamiUitOpen | 0:5c237fdcba23 | 86 | { |
MikamiUitOpen | 7:6598a9b70e5a | 87 | input.Start(); |
MikamiUitOpen | 2:095b360e0f54 | 88 | normInv.Draw(inv, TOUCHED_COLOR); |
MikamiUitOpen | 2:095b360e0f54 | 89 | normInv.Redraw(1-inv); |
MikamiUitOpen | 0:5c237fdcba23 | 90 | } |
MikamiUitOpen | 2:095b360e0f54 | 91 | if (method.GetTouchedNumber(select, TOUCHED_COLOR)) |
MikamiUitOpen | 2:095b360e0f54 | 92 | selector.Execute(sn, select); |
MikamiUitOpen | 0:5c237fdcba23 | 93 | } |
MikamiUitOpen | 7:6598a9b70e5a | 94 | |
MikamiUitOpen | 2:095b360e0f54 | 95 | if (runStop.GetCurrentColor(0) == TOUCHED_COLOR) |
MikamiUitOpen | 0:5c237fdcba23 | 96 | { |
MikamiUitOpen | 2:095b360e0f54 | 97 | normInv.GetTouchedNumber(inv, TOUCHED_COLOR); |
MikamiUitOpen | 2:095b360e0f54 | 98 | |
MikamiUitOpen | 2:095b360e0f54 | 99 | if (input.Filled()) |
MikamiUitOpen | 0:5c237fdcba23 | 100 | { |
MikamiUitOpen | 7:6598a9b70e5a | 101 | // Restart |
MikamiUitOpen | 7:6598a9b70e5a | 102 | input.InvertEnable(inv == 1); |
MikamiUitOpen | 7:6598a9b70e5a | 103 | input.Start(); |
MikamiUitOpen | 0:5c237fdcba23 | 104 | |
MikamiUitOpen | 7:6598a9b70e5a | 105 | // Waveform display |
MikamiUitOpen | 7:6598a9b70e5a | 106 | waveDisp.Execute(sn); |
MikamiUitOpen | 7:6598a9b70e5a | 107 | // Spectrum analysis and display |
MikamiUitOpen | 7:6598a9b70e5a | 108 | selector.Execute(sn, select); |
MikamiUitOpen | 0:5c237fdcba23 | 109 | } |
MikamiUitOpen | 2:095b360e0f54 | 110 | else |
MikamiUitOpen | 2:095b360e0f54 | 111 | if (method.GetTouchedNumber(select, TOUCHED_COLOR)) |
MikamiUitOpen | 2:095b360e0f54 | 112 | selector.Execute(sn, select); |
MikamiUitOpen | 7:6598a9b70e5a | 113 | |
MikamiUitOpen | 7:6598a9b70e5a | 114 | // Check "STOP" button touched |
MikamiUitOpen | 7:6598a9b70e5a | 115 | if (runStop.Touched(1, TOUCHED_COLOR)) |
MikamiUitOpen | 7:6598a9b70e5a | 116 | normInv.DrawAll(INACTIVE_COLOR, INACTIVE_TEXT_COLOR); |
MikamiUitOpen | 0:5c237fdcba23 | 117 | } |
MikamiUitOpen | 0:5c237fdcba23 | 118 | } |
MikamiUitOpen | 0:5c237fdcba23 | 119 | } |