The experiment using this program is introduced on "Interface" No.3, CQ publishing Co.,Ltd, 2015. 本プログラムを使った実験は,CQ出版社のインターフェース 2015年3月号で紹介しています.

Dependencies:   DSProcessingIO mbed

Files at this revision

API Documentation at this revision

Comitter:
CQpub0Mikami
Date:
Tue Jul 15 09:11:35 2014 +0000
Child:
1:1c24d4ad5f4e
Commit message:
1

Changed in this revision

Biquad.hpp Show annotated file Show diff for this revision Revisions of this file
DC_Cut_Coefficients.hpp Show annotated file Show diff for this revision Revisions of this file
DSProcessingIO.lib Show annotated file Show diff for this revision Revisions of this file
FreqConv.cpp Show annotated file Show diff for this revision Revisions of this file
HilbertTransform.hpp Show annotated file Show diff for this revision Revisions of this file
TwoPhaseGenerator.hpp Show annotated file Show diff for this revision Revisions of this file
coefsHilbert94.hpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Biquad.hpp	Tue Jul 15 09:11:35 2014 +0000
@@ -0,0 +1,45 @@
+//--------------------------------------------------------------
+// Biquad filter for IIR filter of cascade structure
+// Copyright (c) 2014 MIKAMI, Naoki, 2014/07/15
+//--------------------------------------------------------------
+
+#ifndef IIR_BIQUAD_HPP
+#define IIR_BIQUAD_HPP
+
+#include "mbed.h"
+
+namespace Mikami
+{
+    // 2nd order IIR filter
+    class Biquad
+    {
+    public:
+        struct Coefs { float a1, a2, b1, b2; };
+
+        Biquad() {}     // Default constructore
+ 
+        Biquad(const Coefs ck)
+            : a1_(ck.a1), a2_(ck.a2), b1_(ck.b1), b2_(ck.b2)
+        { Clear(); }       
+
+        float Execute(float xn)
+        {
+            float un = xn + a1_*un1_ + a2_*un2_;
+            float yn = un + b1_*un1_ + b2_*un2_;
+            
+            un2_ = un1_;
+            un1_ = un;
+            
+            return yn;
+        }
+ 
+        void Clear() { un1_ = un2_ = 0; }
+ 
+    private:
+        float a1_, a2_, b1_, b2_;
+        float un1_, un2_;
+ 
+        Biquad(const Biquad&);
+    };
+}
+#endif  // IIR_BIQUAD_HPP
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DC_Cut_Coefficients.hpp	Tue Jul 15 09:11:35 2014 +0000
@@ -0,0 +1,17 @@
+//--------------------------------------------------------------
+//  Coefficients for IIR filter, Order = 2
+//--------------------------------------------------------------
+// Highpass filter
+// Butterworth filter
+// Sampling frequency:     10.00 kHz
+// Cutoff frequency:        0.05 kHz
+// Ripples in passband:     0.50 dB
+
+#include "Biquad.hpp"
+
+using namespace Mikami;
+
+const Biquad::Coefs c1_ =
+    { 1.955578E+00f, -9.565437E-01f, -2.0f, 1.0f};
+const float g0_ = 9.780305E-01f;
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DSProcessingIO.lib	Tue Jul 15 09:11:35 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/CQpub0Mikami/code/DSProcessingIO/#c3f647a89947
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FreqConv.cpp	Tue Jul 15 09:11:35 2014 +0000
@@ -0,0 +1,46 @@
+//--------------------------------------------------------------
+// Frequency converter
+// Copyright (c) 2014 MIKAMI, Naoki,  2014/06/23
+//--------------------------------------------------------------
+
+#include "mbed.h"
+#include "AdcInternal.hpp"
+#include "MCP4922Single.hpp"
+#include "HilbertTransform.hpp"
+#include "coefsHilbert94.hpp"
+#include "DC_Cut_Coefficients.hpp"
+#include "Biquad.hpp"
+#include "TwoPhaseGenerator.hpp"
+
+using namespace Mikami;
+
+const float FS_ = 10.0e3f;
+Adc adc_(A0);
+Dac dac_(Dac::DAC_A);
+
+Ticker timer_;          // for timer interrupt
+
+Hilbert<ORDER_> ht(hm_);
+Biquad DcCut(c1_);      // DC cut filter
+TwoPhaseGenerator sinCos_(100.0f, FS_); // 100.0 Hz
+
+void TimerIsr()
+{
+    float yI, yQ, cosx, sinx;
+
+    float xn = adc_.Read(); // input
+    
+    xn = DcCut.Execute(g0_*xn);     // DC cut
+    ht.Execute(xn, yI, yQ);         // Hilbert transform
+    sinCos_.Generate(cosx, sinx);   // cos and sin generator
+    float yn = yI*cosx - yQ*sinx;   // conversion
+    
+    dac_.Write(yn);    // output
+}
+
+int main()
+{
+    timer_.attach_us(&TimerIsr, 1.0e6f/FS_);
+    while (true) {}    // infinite loop
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HilbertTransform.hpp	Tue Jul 15 09:11:35 2014 +0000
@@ -0,0 +1,44 @@
+//--------------------------------------------------------------
+// Hilbert transform filter
+// Copyright (c) 2014 MIKAMI, Naoki,  2014/06/23
+//--------------------------------------------------------------
+
+#ifndef HILBERT_TRANSFORM_HPP
+#define HILBERT_TRANSFORM_HPP
+
+#include "mbed.h"
+
+namespace Mikami
+{
+    template<int order> class Hilbert
+    {
+    private:
+        const float *const hm_; // pointer for filter coefficients
+        float xn_[order+1];     // buffer for inputs
+
+        Hilbert(const Hilbert&);
+        Hilbert& operator=(const Hilbert&);
+    public:
+        Hilbert(const float hk[]) : hm_(hk)
+        {
+            for (int k=0; k<=order; k++) xn_[k] = 0.0;
+        }
+        
+        // yI: in-phase signal
+        // yQ: quadrature signal
+        void Execute(float xin, float& yI, float& yQ)
+        {
+            yQ = 0.0;
+            xn_[0] = xin;
+
+            for (int k=0; k<=order/4; k++)
+                yQ = yQ + hm_[k]*(xn_[2*k] - xn_[order-2*k]);
+            yI = xn_[order/2];              // in-phase signal
+
+            for (int k=order; k>0; k--)
+                xn_[k] = xn_[k-1];          // move input signals        
+        }
+    };
+}
+#endif  // HILBERT_TRANSFORM_HPP
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TwoPhaseGenerator.hpp	Tue Jul 15 09:11:35 2014 +0000
@@ -0,0 +1,42 @@
+//--------------------------------------------------------------
+// Two phase sin generator class
+// Copyright (c) 2014 MIKAMI, Naoki,  2014/06/23
+//--------------------------------------------------------------
+
+#ifndef TWO_PHASE_GENERATOR_HPP
+#define TWO_PHASE_GENERATOR_HPP
+
+#include "mbed.h"
+
+namespace Mikami
+{
+    class TwoPhaseGenerator
+    {
+    public:
+        // Constructor
+        TwoPhaseGenerator(float f0, float fs)
+            : a1_(cosf(6.283185f*f0/fs)),
+              b1_(sinf(6.283185f*f0/fs)),
+              xn1_(1.0f), yn1_(0.0f) {}
+
+        // -1.0f <= x1, x2 <= 1.0f
+        void Generate(float& xn, float& yn)
+        {
+            yn = a1_*yn1_ + b1_*xn1_;     // sin
+            xn = -b1_*yn1_ + a1_*xn1_;    // cos
+
+            float c1 = 1.5f - 0.5f*(xn*xn + yn*yn);
+
+            xn1_ = c1*xn;
+            yn1_ = c1*yn;
+        }
+
+    private:
+        const float a1_, b1_;
+        float xn1_, yn1_;
+
+        TwoPhaseGenerator(const TwoPhaseGenerator&);
+        TwoPhaseGenerator& operator=(const TwoPhaseGenerator&);
+    };
+}
+#endif  // TWO_PHASE_GENERATOR_HPP
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/coefsHilbert94.hpp	Tue Jul 15 09:11:35 2014 +0000
@@ -0,0 +1,20 @@
+//--------------------------------------------------------------
+//  Coefficients of FIR filter for Hilbert transform
+//  order = 94
+//--------------------------------------------------------------
+
+// lower edge band frequency (kHz) 0.100000
+// upper edge band frequency (kHz) 4.900000
+// deviation                       0.021787
+// deviation [dB]                  0.187207
+
+const int ORDER_ = 94;
+const float hm_[(ORDER_-2)/4+1] = {
+         -1.299277E-02f, -4.568317E-03f, -5.370440E-03f,
+         -6.267365E-03f, -7.268236E-03f, -8.387631E-03f,
+         -9.634522E-03f, -1.103786E-02f, -1.260725E-02f,
+         -1.437928E-02f, -1.639191E-02f, -1.868889E-02f,
+         -2.133888E-02f, -2.443648E-02f, -2.811453E-02f,
+         -3.256408E-02f, -3.808325E-02f, -4.515709E-02f,
+         -5.462150E-02f, -6.805616E-02f, -8.885052E-02f,
+         -1.258209E-01f, -2.113017E-01f, -6.363176E-01f};
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Jul 15 09:11:35 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/04dd9b1680ae
\ No newline at end of file