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