![](/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
MyClasses/Sampler.cpp@8:1f4bc859bc84, 2015-12-28 (annotated)
- Committer:
- MikamiUitOpen
- Date:
- Mon Dec 28 08:20:34 2015 +0000
- Revision:
- 8:1f4bc859bc84
- Parent:
- 7:6598a9b70e5a
- Child:
- 12:e5367ab82460
9
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MikamiUitOpen | 2:095b360e0f54 | 1 | //----------------------------------------------------------- |
MikamiUitOpen | 2:095b360e0f54 | 2 | // Class for sampling input signal |
MikamiUitOpen | 2:095b360e0f54 | 3 | // |
MikamiUitOpen | 8:1f4bc859bc84 | 4 | // 2015/12/28, Copyright (c) 2015 MIKAMI, Naoki |
MikamiUitOpen | 2:095b360e0f54 | 5 | //----------------------------------------------------------- |
MikamiUitOpen | 2:095b360e0f54 | 6 | |
MikamiUitOpen | 2:095b360e0f54 | 7 | #include "Sampler.hpp" |
MikamiUitOpen | 2:095b360e0f54 | 8 | |
MikamiUitOpen | 2:095b360e0f54 | 9 | namespace Mikami |
MikamiUitOpen | 2:095b360e0f54 | 10 | { |
MikamiUitOpen | 7:6598a9b70e5a | 11 | Sampler::Sampler(PinName pin, int fs, int nData) |
MikamiUitOpen | 2:095b360e0f54 | 12 | : TS_(1000000/fs), N_DATA_(nData), aIn_(pin), |
MikamiUitOpen | 8:1f4bc859bc84 | 13 | sn_(new int16_t[nData]), |
MikamiUitOpen | 7:6598a9b70e5a | 14 | buffer_(new int16_t[nData]) |
MikamiUitOpen | 7:6598a9b70e5a | 15 | { |
MikamiUitOpen | 8:1f4bc859bc84 | 16 | for (int n=0; n<nData; n++) sn_[n] = 32767; |
MikamiUitOpen | 2:095b360e0f54 | 17 | } |
MikamiUitOpen | 2:095b360e0f54 | 18 | |
MikamiUitOpen | 7:6598a9b70e5a | 19 | Sampler::~Sampler() |
MikamiUitOpen | 7:6598a9b70e5a | 20 | { |
MikamiUitOpen | 8:1f4bc859bc84 | 21 | delete[] sn_; |
MikamiUitOpen | 7:6598a9b70e5a | 22 | delete[] buffer_; |
MikamiUitOpen | 7:6598a9b70e5a | 23 | } |
MikamiUitOpen | 7:6598a9b70e5a | 24 | |
MikamiUitOpen | 8:1f4bc859bc84 | 25 | // Start sampling |
MikamiUitOpen | 8:1f4bc859bc84 | 26 | void Sampler::Start(bool onOff) |
MikamiUitOpen | 2:095b360e0f54 | 27 | { |
MikamiUitOpen | 2:095b360e0f54 | 28 | count_ = 0; |
MikamiUitOpen | 7:6598a9b70e5a | 29 | filled_ = false; |
MikamiUitOpen | 2:095b360e0f54 | 30 | trigger_ = false; |
MikamiUitOpen | 2:095b360e0f54 | 31 | xnM1_ = 32767; |
MikamiUitOpen | 8:1f4bc859bc84 | 32 | Invert(onOff); |
MikamiUitOpen | 8:1f4bc859bc84 | 33 | timer_.attach_us(this, &Sampler::Isr, TS_); |
MikamiUitOpen | 2:095b360e0f54 | 34 | } |
MikamiUitOpen | 2:095b360e0f54 | 35 | |
MikamiUitOpen | 2:095b360e0f54 | 36 | // Interrupt service routine for Ticker |
MikamiUitOpen | 2:095b360e0f54 | 37 | void Sampler::Isr() |
MikamiUitOpen | 2:095b360e0f54 | 38 | { |
MikamiUitOpen | 8:1f4bc859bc84 | 39 | int16_t xn = (this->*Rd)(); // Read from A0 |
MikamiUitOpen | 2:095b360e0f54 | 40 | if (!trigger_) |
MikamiUitOpen | 2:095b360e0f54 | 41 | { |
MikamiUitOpen | 2:095b360e0f54 | 42 | // Detect rising edge |
MikamiUitOpen | 2:095b360e0f54 | 43 | if ((xn > (xnM1_+512)) && (xn > 2048)) |
MikamiUitOpen | 2:095b360e0f54 | 44 | trigger_ = true; |
MikamiUitOpen | 2:095b360e0f54 | 45 | else |
MikamiUitOpen | 2:095b360e0f54 | 46 | xnM1_ = xn; |
MikamiUitOpen | 2:095b360e0f54 | 47 | return; |
MikamiUitOpen | 2:095b360e0f54 | 48 | } |
MikamiUitOpen | 8:1f4bc859bc84 | 49 | else |
MikamiUitOpen | 8:1f4bc859bc84 | 50 | buffer_[count_] = xn; |
MikamiUitOpen | 2:095b360e0f54 | 51 | |
MikamiUitOpen | 2:095b360e0f54 | 52 | if (++count_ >= N_DATA_) |
MikamiUitOpen | 7:6598a9b70e5a | 53 | { |
MikamiUitOpen | 7:6598a9b70e5a | 54 | timer_.detach(); |
MikamiUitOpen | 8:1f4bc859bc84 | 55 | memcpy(sn_, buffer_, N_DATA_*sizeof(int16_t)); |
MikamiUitOpen | 8:1f4bc859bc84 | 56 | filled_ = true; // Permits spectrum analysis |
MikamiUitOpen | 7:6598a9b70e5a | 57 | } |
MikamiUitOpen | 2:095b360e0f54 | 58 | } |
MikamiUitOpen | 2:095b360e0f54 | 59 | } |
MikamiUitOpen | 8:1f4bc859bc84 | 60 |