![](/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@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 | 2:095b360e0f54 | 1 | //----------------------------------------------------------- |
MikamiUitOpen | 2:095b360e0f54 | 2 | // Class for sampling input signal |
MikamiUitOpen | 2:095b360e0f54 | 3 | // |
MikamiUitOpen | 7:6598a9b70e5a | 4 | // 2015/12/20, 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 | 7:6598a9b70e5a | 13 | xn_(new int16_t[nData]), |
MikamiUitOpen | 7:6598a9b70e5a | 14 | buffer_(new int16_t[nData]) |
MikamiUitOpen | 7:6598a9b70e5a | 15 | { |
MikamiUitOpen | 2:095b360e0f54 | 16 | Rd[0] = &Sampler::ReadNorm; // inphase |
MikamiUitOpen | 2:095b360e0f54 | 17 | Rd[1] = &Sampler::ReadInv; // out-of-phase |
MikamiUitOpen | 2:095b360e0f54 | 18 | sw_ = 0; |
MikamiUitOpen | 7:6598a9b70e5a | 19 | Set(false); |
MikamiUitOpen | 5:98ec9dd54144 | 20 | for (int n=0; n<nData; n++) |
MikamiUitOpen | 5:98ec9dd54144 | 21 | buffer_[n] = 32767; |
MikamiUitOpen | 2:095b360e0f54 | 22 | } |
MikamiUitOpen | 2:095b360e0f54 | 23 | |
MikamiUitOpen | 7:6598a9b70e5a | 24 | Sampler::~Sampler() |
MikamiUitOpen | 7:6598a9b70e5a | 25 | { |
MikamiUitOpen | 7:6598a9b70e5a | 26 | delete[] xn_; |
MikamiUitOpen | 7:6598a9b70e5a | 27 | delete[] buffer_; |
MikamiUitOpen | 7:6598a9b70e5a | 28 | } |
MikamiUitOpen | 7:6598a9b70e5a | 29 | |
MikamiUitOpen | 7:6598a9b70e5a | 30 | void Sampler::Start() |
MikamiUitOpen | 2:095b360e0f54 | 31 | { |
MikamiUitOpen | 2:095b360e0f54 | 32 | count_ = 0; |
MikamiUitOpen | 7:6598a9b70e5a | 33 | filled_ = false; |
MikamiUitOpen | 2:095b360e0f54 | 34 | timer_.attach_us(this, &Sampler::Isr, TS_); |
MikamiUitOpen | 2:095b360e0f54 | 35 | } |
MikamiUitOpen | 2:095b360e0f54 | 36 | |
MikamiUitOpen | 2:095b360e0f54 | 37 | void Sampler::Set(bool tf) |
MikamiUitOpen | 2:095b360e0f54 | 38 | { |
MikamiUitOpen | 2:095b360e0f54 | 39 | trigger_ = false; |
MikamiUitOpen | 2:095b360e0f54 | 40 | xnM1_ = 32767; |
MikamiUitOpen | 2:095b360e0f54 | 41 | filled_ = tf; |
MikamiUitOpen | 2:095b360e0f54 | 42 | } |
MikamiUitOpen | 2:095b360e0f54 | 43 | |
MikamiUitOpen | 2:095b360e0f54 | 44 | // Interrupt service routine for Ticker |
MikamiUitOpen | 2:095b360e0f54 | 45 | void Sampler::Isr() |
MikamiUitOpen | 2:095b360e0f54 | 46 | { |
MikamiUitOpen | 2:095b360e0f54 | 47 | if (!trigger_) |
MikamiUitOpen | 2:095b360e0f54 | 48 | { |
MikamiUitOpen | 2:095b360e0f54 | 49 | int16_t xn = (this->*Rd[sw_])(); |
MikamiUitOpen | 2:095b360e0f54 | 50 | // Detect rising edge |
MikamiUitOpen | 2:095b360e0f54 | 51 | if ((xn > (xnM1_+512)) && (xn > 2048)) |
MikamiUitOpen | 2:095b360e0f54 | 52 | trigger_ = true; |
MikamiUitOpen | 2:095b360e0f54 | 53 | else |
MikamiUitOpen | 2:095b360e0f54 | 54 | xnM1_ = xn; |
MikamiUitOpen | 2:095b360e0f54 | 55 | return; |
MikamiUitOpen | 2:095b360e0f54 | 56 | } |
MikamiUitOpen | 2:095b360e0f54 | 57 | |
MikamiUitOpen | 2:095b360e0f54 | 58 | buffer_[count_] = (this->*Rd[sw_])(); // Read from A0 |
MikamiUitOpen | 2:095b360e0f54 | 59 | |
MikamiUitOpen | 2:095b360e0f54 | 60 | if (++count_ >= N_DATA_) |
MikamiUitOpen | 7:6598a9b70e5a | 61 | { |
MikamiUitOpen | 7:6598a9b70e5a | 62 | timer_.detach(); |
MikamiUitOpen | 7:6598a9b70e5a | 63 | Set(true); // Permits spectrum analysis |
MikamiUitOpen | 7:6598a9b70e5a | 64 | memcpy(xn_, buffer_, N_DATA_*sizeof(int16_t)); |
MikamiUitOpen | 7:6598a9b70e5a | 65 | } |
MikamiUitOpen | 2:095b360e0f54 | 66 | } |
MikamiUitOpen | 2:095b360e0f54 | 67 | } |