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

Revision:
2:095b360e0f54
Parent:
1:1656f55c2d5c
Child:
4:99d4d5ea06a2
--- a/MyClasses/Sampler.hpp	Thu Dec 10 04:56:04 2015 +0000
+++ b/MyClasses/Sampler.hpp	Sun Dec 13 07:20:14 2015 +0000
@@ -1,7 +1,7 @@
 //-----------------------------------------------------------
-//  Class for sampling input signal
+//  Class for sampling input signal (Header)
 //
-//  2015/12/08, Copyright (c) 2015 MIKAMI, Naoki
+//  2015/12/11, Copyright (c) 2015 MIKAMI, Naoki
 //-----------------------------------------------------------
 
 #ifndef F746_SAMPLER_HPP
@@ -14,80 +14,45 @@
     class Sampler
     {
     public:
-        Sampler(PinName pin, Ticker& timer, int nData)
-            : N_DATA_(nData), aIn_(pin), timer_(timer),
-              buffer_(new int16_t[nData])
-        {
-            SetParams(false);
-            inv_ = false;
-        }
+        Sampler(PinName pin, int fs, int nData);
         
         ~Sampler() { delete[] buffer_; }
+        
+        void IntrEnable();
 
-        void Execute()
-        {
-            int16_t xn = Read();    
-            if (!trigger_)
-            {
-                // Detect rising edge
-                if ((xn > (xnM1_+512)) && (xn > 2048))
-                    trigger_ = true;
-                else
-                    xnM1_ = xn;
-                return;
-            }
-
-            buffer_[count_] = Read();   // Read from A0
-    
-            if (++count_ >= N_DATA_)
-            {
-                SetParams(true);    // Permits spectrum analysis
-                timer_.detach();    // Disable timer interrupt
-            }
-        }
-        
-        void ClearCount() { count_ = 0; }
-        
         bool Filled() { return filled_; }
         
         void Restart() { filled_ = false; }
         
-        bool InvertEnable(bool onOff)
-        {
-            inv_ = onOff;
-            return onOff;
-        }
+        void InvertEnable(bool onOff)
+        {   sw_ = onOff ? 1 : 0; }
 
-        // Get sampled data
+        // Get pointer for sampled data
         int16_t* Get() { return buffer_; }
         
     private:
+        const int TS_;      // sampling period
         const int N_DATA_;  // number of 1 frame data
 
         AnalogIn aIn_;      // Object of ADC
-        Ticker& timer_;     // Reference for object of Ticker
+        Ticker timer_;      // Object of Ticker
 
         bool trigger_;
         bool filled_;
-        bool inv_;
         int count_;
         int16_t xnM1_;
-        
+        int sw_;
         int16_t* buffer_;   // for sampled data
-        
-        int16_t Read()
-        {
-            return inv_ ? 32767 - aIn_.read_u16()
-                        : aIn_.read_u16() - 32767;
-        }
-        
-        void SetParams(bool tf)
-        {
-            trigger_ = false;
-            count_ = 0;
-            xnM1_ = 32767;
-            filled_ = tf;
-        }
+
+        // For input
+        int16_t (Sampler::*Rd[2])();
+        int16_t ReadNorm() { return aIn_.read_u16() - 32767; }
+        int16_t ReadInv()  { return 32767 - aIn_.read_u16(); }
+
+        void Set(bool tf);
+
+        // Interrupt service routine for Ticker
+        void Isr();
     };
 }
 #endif  // F746_SAMPLER_HPP