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.hpp

Committer:
MikamiUitOpen
Date:
2015-12-10
Revision:
1:1656f55c2d5c
Parent:
MyClasses/sampler.hpp@ 0:5c237fdcba23
Child:
2:095b360e0f54

File content as of revision 1:1656f55c2d5c:

//-----------------------------------------------------------
//  Class for sampling input signal
//
//  2015/12/08, Copyright (c) 2015 MIKAMI, Naoki
//-----------------------------------------------------------

#ifndef F746_SAMPLER_HPP
#define F746_SAMPLER_HPP

#include "mbed.h"

namespace Mikami
{
    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() { delete[] buffer_; }

        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;
        }

        // Get sampled data
        int16_t* Get() { return buffer_; }
        
    private:
        const int N_DATA_;  // number of 1 frame data

        AnalogIn aIn_;      // Object of ADC
        Ticker& timer_;     // Reference for object of Ticker

        bool trigger_;
        bool filled_;
        bool inv_;
        int count_;
        int16_t xnM1_;
        
        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;
        }
    };
}
#endif  // F746_SAMPLER_HPP