![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Version using MEMS microphone and CODEC for the program "F746_RealtimeSpectrumAnalyzer". "F746_RealtimeSpectrumAnalyzer" の入力を MEMS のマイクと CODEC に変更.このプログラムは Tomona Nanase さんが作成し DISCO-F746NG_Oscilloscope の名前で登録しているプログラムで, CODEC を使って入力する部分を参考にして作成.このプログラムの説明は,CQ出版社のインターフェース誌,2016年4月号に掲載.
Dependencies: BSP_DISCO_F746NG BUTTON_GROUP LCD_DISCO_F746NG TS_DISCO_F746NG UIT_FFT_Real mbed
main.cpp@0:0e5131366580, 2016-01-06 (annotated)
- Committer:
- MikamiUitOpen
- Date:
- Wed Jan 06 12:46:15 2016 +0000
- Revision:
- 0:0e5131366580
- Child:
- 1:ac0a67a0deec
1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MikamiUitOpen | 0:0e5131366580 | 1 | //------------------------------------------------ |
MikamiUitOpen | 0:0e5131366580 | 2 | // Realtime spectrum analyzer |
MikamiUitOpen | 0:0e5131366580 | 3 | // Input: MEMS microphone or CN11 |
MikamiUitOpen | 0:0e5131366580 | 4 | // |
MikamiUitOpen | 0:0e5131366580 | 5 | // 2016/01/06, Copyright (c) 2016 MIKAMI, Naoki |
MikamiUitOpen | 0:0e5131366580 | 6 | //------------------------------------------------ |
MikamiUitOpen | 0:0e5131366580 | 7 | |
MikamiUitOpen | 0:0e5131366580 | 8 | #include "main.h" |
MikamiUitOpen | 0:0e5131366580 | 9 | |
MikamiUitOpen | 0:0e5131366580 | 10 | using namespace Mikami; |
MikamiUitOpen | 0:0e5131366580 | 11 | |
MikamiUitOpen | 0:0e5131366580 | 12 | int16_t audio_in_buffer[BufferSize]; |
MikamiUitOpen | 0:0e5131366580 | 13 | __IO bool audio_in_buffer_captured = false; |
MikamiUitOpen | 0:0e5131366580 | 14 | __IO int32_t audio_in_buffer_offset = 0; |
MikamiUitOpen | 0:0e5131366580 | 15 | __IO int32_t audio_in_buffer_length = 0; |
MikamiUitOpen | 0:0e5131366580 | 16 | |
MikamiUitOpen | 0:0e5131366580 | 17 | int main() |
MikamiUitOpen | 0:0e5131366580 | 18 | { |
MikamiUitOpen | 0:0e5131366580 | 19 | const int FS = 8000; // Sampling frequency: 8 kHz |
MikamiUitOpen | 0:0e5131366580 | 20 | const int N_DATA = N_DATA_; // Number of data to be analyzed |
MikamiUitOpen | 0:0e5131366580 | 21 | const int N_FFT = 512; // Number of date for FFT |
MikamiUitOpen | 0:0e5131366580 | 22 | |
MikamiUitOpen | 0:0e5131366580 | 23 | const int X_WAV = 44; // Origin for x axis of waveform |
MikamiUitOpen | 0:0e5131366580 | 24 | const int Y_WAV = 36; // Origin for y axis of waveform |
MikamiUitOpen | 0:0e5131366580 | 25 | const int X0 = 40; // Origin for x axis of spectrum |
MikamiUitOpen | 0:0e5131366580 | 26 | const int Y0 = 234; // Origin for y axis of spectrum |
MikamiUitOpen | 0:0e5131366580 | 27 | const float DB1 = 2.4f; // Pixels for 1 dB |
MikamiUitOpen | 0:0e5131366580 | 28 | const int BIN = 1; // Pixels per bin |
MikamiUitOpen | 0:0e5131366580 | 29 | const int W_DB = 60; // Range in dB to be displayed |
MikamiUitOpen | 0:0e5131366580 | 30 | const int X0_BTN = 340; // Holizontal position for left of buttons |
MikamiUitOpen | 0:0e5131366580 | 31 | |
MikamiUitOpen | 0:0e5131366580 | 32 | const uint32_t BACK_COLOR = 0xFF006A6C; // Teal green |
MikamiUitOpen | 0:0e5131366580 | 33 | const uint32_t INACTIVE_COLOR = BACK_COLOR & 0xD0FFFFFF; |
MikamiUitOpen | 0:0e5131366580 | 34 | const uint32_t TOUCHED_COLOR = 0xFF7F7FFF; |
MikamiUitOpen | 0:0e5131366580 | 35 | const uint32_t ORIGINAL_COLOR = 0xFF0068B7; |
MikamiUitOpen | 0:0e5131366580 | 36 | const uint32_t INACTIVE_TEXT_COLOR = LCD_COLOR_GRAY; |
MikamiUitOpen | 0:0e5131366580 | 37 | const uint32_t AXIS_COLOR = 0xFFCCFFFF; |
MikamiUitOpen | 0:0e5131366580 | 38 | const uint32_t LINE_COLOR = LCD_COLOR_CYAN; |
MikamiUitOpen | 0:0e5131366580 | 39 | |
MikamiUitOpen | 0:0e5131366580 | 40 | LCD_DISCO_F746NG lcd; // Object for LCD display |
MikamiUitOpen | 0:0e5131366580 | 41 | TS_DISCO_F746NG ts; // Object for touch pannel |
MikamiUitOpen | 0:0e5131366580 | 42 | |
MikamiUitOpen | 0:0e5131366580 | 43 | lcd.Clear(BACK_COLOR); |
MikamiUitOpen | 0:0e5131366580 | 44 | |
MikamiUitOpen | 0:0e5131366580 | 45 | // Setting of button group |
MikamiUitOpen | 0:0e5131366580 | 46 | const string RUN_STOP[2] = {"RUN", "STOP"}; |
MikamiUitOpen | 0:0e5131366580 | 47 | ButtonGroup runStop(lcd, ts, X0_BTN, 10, 60, 40, |
MikamiUitOpen | 0:0e5131366580 | 48 | ORIGINAL_COLOR, BACK_COLOR, |
MikamiUitOpen | 0:0e5131366580 | 49 | 2, RUN_STOP, 5, 0, 2); |
MikamiUitOpen | 0:0e5131366580 | 50 | runStop.Draw(1, INACTIVE_COLOR, INACTIVE_TEXT_COLOR); |
MikamiUitOpen | 0:0e5131366580 | 51 | |
MikamiUitOpen | 0:0e5131366580 | 52 | const string MIC_LINE[2] = {"MIC", "LINE"}; |
MikamiUitOpen | 0:0e5131366580 | 53 | ButtonGroup micLine(lcd, ts, X0_BTN, 65, 60, 40, |
MikamiUitOpen | 0:0e5131366580 | 54 | ORIGINAL_COLOR, BACK_COLOR, |
MikamiUitOpen | 0:0e5131366580 | 55 | 2, MIC_LINE, 5, 0, 2); |
MikamiUitOpen | 0:0e5131366580 | 56 | micLine.DrawAll(INACTIVE_COLOR, INACTIVE_TEXT_COLOR); |
MikamiUitOpen | 0:0e5131366580 | 57 | |
MikamiUitOpen | 0:0e5131366580 | 58 | const string METHOD[3] = {"FFT", "Linear Pred.", "Cepstrum"}; |
MikamiUitOpen | 0:0e5131366580 | 59 | ButtonGroup method(lcd, ts, X0_BTN, 120, 125, 40, |
MikamiUitOpen | 0:0e5131366580 | 60 | ORIGINAL_COLOR, BACK_COLOR, |
MikamiUitOpen | 0:0e5131366580 | 61 | 3, METHOD, 0, 5, 1); |
MikamiUitOpen | 0:0e5131366580 | 62 | method.DrawAll(INACTIVE_COLOR, INACTIVE_TEXT_COLOR); |
MikamiUitOpen | 0:0e5131366580 | 63 | // End of button group setting |
MikamiUitOpen | 0:0e5131366580 | 64 | |
MikamiUitOpen | 0:0e5131366580 | 65 | WaveformDisplay waveDisp(lcd, X_WAV, Y_WAV, N_DATA, 10, |
MikamiUitOpen | 0:0e5131366580 | 66 | AXIS_COLOR, LINE_COLOR, BACK_COLOR); |
MikamiUitOpen | 0:0e5131366580 | 67 | |
MikamiUitOpen | 0:0e5131366580 | 68 | SpectrumDisplay disp(lcd, N_FFT, X0, Y0, DB1, BIN, W_DB, FS, |
MikamiUitOpen | 0:0e5131366580 | 69 | AXIS_COLOR, LINE_COLOR, BACK_COLOR); |
MikamiUitOpen | 0:0e5131366580 | 70 | // Linear prediction: order = 12 |
MikamiUitOpen | 0:0e5131366580 | 71 | // Cepstral smoothing: highest quefrency = 40 |
MikamiUitOpen | 0:0e5131366580 | 72 | Selector analyzer(disp, N_DATA, N_FFT, 12, 40); |
MikamiUitOpen | 0:0e5131366580 | 73 | |
MikamiUitOpen | 0:0e5131366580 | 74 | // Initialize the infinite loop procedure |
MikamiUitOpen | 0:0e5131366580 | 75 | int select = -1; |
MikamiUitOpen | 0:0e5131366580 | 76 | uint16_t inputDevice; |
MikamiUitOpen | 0:0e5131366580 | 77 | int mic = 0; |
MikamiUitOpen | 0:0e5131366580 | 78 | int stop = 0; |
MikamiUitOpen | 0:0e5131366580 | 79 | int16_t sn[N_DATA]; |
MikamiUitOpen | 0:0e5131366580 | 80 | |
MikamiUitOpen | 0:0e5131366580 | 81 | // Wait for "RUN" button touched |
MikamiUitOpen | 0:0e5131366580 | 82 | while (!runStop.Touched(0, TOUCHED_COLOR)) {} |
MikamiUitOpen | 0:0e5131366580 | 83 | |
MikamiUitOpen | 0:0e5131366580 | 84 | // Start of spectrum analyzing |
MikamiUitOpen | 0:0e5131366580 | 85 | method.DrawAll(ORIGINAL_COLOR); |
MikamiUitOpen | 0:0e5131366580 | 86 | |
MikamiUitOpen | 0:0e5131366580 | 87 | while (true) |
MikamiUitOpen | 0:0e5131366580 | 88 | { |
MikamiUitOpen | 0:0e5131366580 | 89 | if (runStop.Touched(0, TOUCHED_COLOR)) |
MikamiUitOpen | 0:0e5131366580 | 90 | { |
MikamiUitOpen | 0:0e5131366580 | 91 | inputDevice = (mic == 0) ? INPUT_DEVICE_DIGITAL_MICROPHONE_2 |
MikamiUitOpen | 0:0e5131366580 | 92 | : INPUT_DEVICE_INPUT_LINE_1; |
MikamiUitOpen | 0:0e5131366580 | 93 | InitRunAudioIn(inputDevice, I2S_AUDIOFREQ_16K); |
MikamiUitOpen | 0:0e5131366580 | 94 | micLine.Draw(mic, TOUCHED_COLOR); |
MikamiUitOpen | 0:0e5131366580 | 95 | micLine.Redraw(1-mic); |
MikamiUitOpen | 0:0e5131366580 | 96 | } |
MikamiUitOpen | 0:0e5131366580 | 97 | |
MikamiUitOpen | 0:0e5131366580 | 98 | runStop.GetTouchedNumber(stop, TOUCHED_COLOR); |
MikamiUitOpen | 0:0e5131366580 | 99 | if (stop == 0) |
MikamiUitOpen | 0:0e5131366580 | 100 | { |
MikamiUitOpen | 0:0e5131366580 | 101 | if (micLine.GetTouchedNumber(mic, TOUCHED_COLOR)) |
MikamiUitOpen | 0:0e5131366580 | 102 | { |
MikamiUitOpen | 0:0e5131366580 | 103 | StopAudioIn(); // Stop sampling |
MikamiUitOpen | 0:0e5131366580 | 104 | inputDevice = (mic == 0) ? INPUT_DEVICE_DIGITAL_MICROPHONE_2 |
MikamiUitOpen | 0:0e5131366580 | 105 | : INPUT_DEVICE_INPUT_LINE_1; |
MikamiUitOpen | 0:0e5131366580 | 106 | InitRunAudioIn(inputDevice, I2S_AUDIOFREQ_16K); |
MikamiUitOpen | 0:0e5131366580 | 107 | } |
MikamiUitOpen | 0:0e5131366580 | 108 | |
MikamiUitOpen | 0:0e5131366580 | 109 | if (audio_in_buffer_captured) |
MikamiUitOpen | 0:0e5131366580 | 110 | { |
MikamiUitOpen | 0:0e5131366580 | 111 | // Waveform display |
MikamiUitOpen | 0:0e5131366580 | 112 | int32_t index = audio_in_buffer_offset; // right mic. or left line |
MikamiUitOpen | 0:0e5131366580 | 113 | // int32_t index = audio_in_buffer_offset + 1; // left mic. or right line |
MikamiUitOpen | 0:0e5131366580 | 114 | Decimate(N_DATA, index, audio_in_buffer, sn); |
MikamiUitOpen | 0:0e5131366580 | 115 | waveDisp.Execute(sn); |
MikamiUitOpen | 0:0e5131366580 | 116 | // Spectrum analysis and display |
MikamiUitOpen | 0:0e5131366580 | 117 | analyzer.Execute(sn, select); |
MikamiUitOpen | 0:0e5131366580 | 118 | audio_in_buffer_captured = false; |
MikamiUitOpen | 0:0e5131366580 | 119 | } |
MikamiUitOpen | 0:0e5131366580 | 120 | else |
MikamiUitOpen | 0:0e5131366580 | 121 | if (method.GetTouchedNumber(select, TOUCHED_COLOR)) |
MikamiUitOpen | 0:0e5131366580 | 122 | analyzer.Execute(sn, select); |
MikamiUitOpen | 0:0e5131366580 | 123 | } |
MikamiUitOpen | 0:0e5131366580 | 124 | else |
MikamiUitOpen | 0:0e5131366580 | 125 | { |
MikamiUitOpen | 0:0e5131366580 | 126 | if (runStop.Touched(1)) |
MikamiUitOpen | 0:0e5131366580 | 127 | { |
MikamiUitOpen | 0:0e5131366580 | 128 | StopAudioIn(); // Stop sampling |
MikamiUitOpen | 0:0e5131366580 | 129 | micLine.DrawAll(INACTIVE_COLOR, INACTIVE_TEXT_COLOR); |
MikamiUitOpen | 0:0e5131366580 | 130 | } |
MikamiUitOpen | 0:0e5131366580 | 131 | if (method.GetTouchedNumber(select, TOUCHED_COLOR)) |
MikamiUitOpen | 0:0e5131366580 | 132 | analyzer.Execute(sn, select); |
MikamiUitOpen | 0:0e5131366580 | 133 | } |
MikamiUitOpen | 0:0e5131366580 | 134 | } |
MikamiUitOpen | 0:0e5131366580 | 135 | } |
MikamiUitOpen | 0:0e5131366580 | 136 |