![](/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@10:351d70a4efde, 2015-12-31 (annotated)
- Committer:
- MikamiUitOpen
- Date:
- Thu Dec 31 09:17:58 2015 +0000
- Revision:
- 10:351d70a4efde
- Parent:
- 9:99c55850cb41
- Child:
- 11:a8420871920e
- Child:
- 12:e5367ab82460
11
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 | 10:351d70a4efde | 5 | // 2015/12/31, 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 | 8:1f4bc859bc84 | 16 | const int N_DATA = 260; // Number of data to be analyzed |
MikamiUitOpen | 8:1f4bc859bc84 | 17 | const int N_FFT = 512; // Number of date for FFT |
MikamiUitOpen | 8:1f4bc859bc84 | 18 | const int X_WAV = 44; // Origin for x axis of waveform |
MikamiUitOpen | 8:1f4bc859bc84 | 19 | const int Y_WAV = 36; // Origin for y axis of waveform |
MikamiUitOpen | 8:1f4bc859bc84 | 20 | const int X0 = 40; // Origin for x axis of spectrum |
MikamiUitOpen | 8:1f4bc859bc84 | 21 | const int Y0 = 234; // Origin for y axis of spectrum |
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 | 8:1f4bc859bc84 | 24 | const int W_DB = 60; // Range in dB to be displayed |
MikamiUitOpen | 8:1f4bc859bc84 | 25 | const int X0_BTN = 340; // Holizontal position for left of buttons |
MikamiUitOpen | 0:5c237fdcba23 | 26 | |
MikamiUitOpen | 2:095b360e0f54 | 27 | const int FS = 10000; // Sampling frequency: 10 kHz |
MikamiUitOpen | 0:5c237fdcba23 | 28 | |
MikamiUitOpen | 8:1f4bc859bc84 | 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 | 8:1f4bc859bc84 | 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 | 10:351d70a4efde | 70 | // Cepstral smoothing: highest quefrency = 50 |
MikamiUitOpen | 9:99c55850cb41 | 71 | Selector analyzer(disp, N_DATA, N_FFT, 14, 50); |
MikamiUitOpen | 0:5c237fdcba23 | 72 | |
MikamiUitOpen | 2:095b360e0f54 | 73 | // Wait for "RUN" button touched |
MikamiUitOpen | 10:351d70a4efde | 74 | while (!runStop.Touched(0, TOUCHED_COLOR)) {} |
MikamiUitOpen | 2:095b360e0f54 | 75 | |
MikamiUitOpen | 6:b27ef8d98edc | 76 | // Start of spectrum analyzing |
MikamiUitOpen | 10:351d70a4efde | 77 | method.DrawAll(ORIGINAL_COLOR); |
MikamiUitOpen | 5:98ec9dd54144 | 78 | int inv = 0; // 0: "NORM", 1: "INV" |
MikamiUitOpen | 2:095b360e0f54 | 79 | int select = -1; |
MikamiUitOpen | 10:351d70a4efde | 80 | int stop = 0; |
MikamiUitOpen | 10:351d70a4efde | 81 | input.Start(inv == 1); |
MikamiUitOpen | 10:351d70a4efde | 82 | |
MikamiUitOpen | 0:5c237fdcba23 | 83 | while (true) |
MikamiUitOpen | 7:6598a9b70e5a | 84 | { |
MikamiUitOpen | 10:351d70a4efde | 85 | runStop.GetTouchedNumber(stop, TOUCHED_COLOR); |
MikamiUitOpen | 10:351d70a4efde | 86 | |
MikamiUitOpen | 10:351d70a4efde | 87 | if (stop == 0) |
MikamiUitOpen | 0:5c237fdcba23 | 88 | { |
MikamiUitOpen | 2:095b360e0f54 | 89 | normInv.GetTouchedNumber(inv, TOUCHED_COLOR); |
MikamiUitOpen | 10:351d70a4efde | 90 | normInv.Draw(inv, TOUCHED_COLOR); |
MikamiUitOpen | 10:351d70a4efde | 91 | normInv.Redraw(1-inv); |
MikamiUitOpen | 2:095b360e0f54 | 92 | if (input.Filled()) |
MikamiUitOpen | 0:5c237fdcba23 | 93 | { |
MikamiUitOpen | 7:6598a9b70e5a | 94 | // Restart |
MikamiUitOpen | 8:1f4bc859bc84 | 95 | input.Start(inv == 1); |
MikamiUitOpen | 0:5c237fdcba23 | 96 | |
MikamiUitOpen | 7:6598a9b70e5a | 97 | // Waveform display |
MikamiUitOpen | 7:6598a9b70e5a | 98 | waveDisp.Execute(sn); |
MikamiUitOpen | 7:6598a9b70e5a | 99 | // Spectrum analysis and display |
MikamiUitOpen | 8:1f4bc859bc84 | 100 | analyzer.Execute(sn, select); |
MikamiUitOpen | 0:5c237fdcba23 | 101 | } |
MikamiUitOpen | 2:095b360e0f54 | 102 | else |
MikamiUitOpen | 2:095b360e0f54 | 103 | if (method.GetTouchedNumber(select, TOUCHED_COLOR)) |
MikamiUitOpen | 8:1f4bc859bc84 | 104 | analyzer.Execute(sn, select); |
MikamiUitOpen | 10:351d70a4efde | 105 | } |
MikamiUitOpen | 10:351d70a4efde | 106 | else |
MikamiUitOpen | 10:351d70a4efde | 107 | { |
MikamiUitOpen | 7:6598a9b70e5a | 108 | // Check "STOP" button touched |
MikamiUitOpen | 10:351d70a4efde | 109 | if (runStop.GetTouchedNumber(stop)) |
MikamiUitOpen | 7:6598a9b70e5a | 110 | normInv.DrawAll(INACTIVE_COLOR, INACTIVE_TEXT_COLOR); |
MikamiUitOpen | 10:351d70a4efde | 111 | if (method.GetTouchedNumber(select, TOUCHED_COLOR)) |
MikamiUitOpen | 10:351d70a4efde | 112 | analyzer.Execute(sn, select); |
MikamiUitOpen | 0:5c237fdcba23 | 113 | } |
MikamiUitOpen | 0:5c237fdcba23 | 114 | } |
MikamiUitOpen | 0:5c237fdcba23 | 115 | } |