Cutoff frequency variable LPF and HPF by 160th-order FIR filter designed by window method usin Hamming window for ST Nucleo F401RE.
Dependencies: UIT_ACM1602NI UIT_ADDA mbed
Revision 7:46327dcab1bf, committed 2014-12-08
- Comitter:
- MikamiUitOpen
- Date:
- Mon Dec 08 05:46:37 2014 +0000
- Parent:
- 6:e4b8e25573f3
- Commit message:
- 7
Changed in this revision
diff -r e4b8e25573f3 -r 46327dcab1bf WindowingDesignLpfHpf/WindowingDesignLH.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WindowingDesignLpfHpf/WindowingDesignLH.cpp Mon Dec 08 05:46:37 2014 +0000 @@ -0,0 +1,75 @@ +//------------------------------------------------------------------------------ +// Design of FIR filter of LPF and HPF using window method +// +// 2014/12/08, Copyright (c) 2014 MIKAMI, Naoki +//------------------------------------------------------------------------------ + +#include "WindowingDesignLH.hpp" + +namespace Mikami +{ + WindowingDesign::WindowingDesign(int order, float fs) + : FS_(fs), PI_FS_(3.1415926536f/fs) + { + order_ = order; + if ((order % 2) != 0) + { + fprintf(stderr, "order must be even."); + return; + } + hm_ = new float[order/2+1]; + wn_ = new float[order/2+1]; + + HammWindow(); + } + + void WindowingDesign::Design(int order, Type pb, float fc, + float hk[]) + { + if (pb == LPF) fC_ = fc; + if (pb == HPF) fC_ = 0.5f*FS_ - fc; + + if (order != order_) + { + order_ = order_; + if (hm_ != NULL) delete[] hm_; + hm_ = new float[order/2+1]; + if (wn_ != NULL) delete[] wn_; + wn_ = new float[order/2+1]; + HammWindow(); + } + + // Calculate coefficients for LPF + LpfCoefficients(); + // If HPF, transform coefficients + if (pb != LPF) ToHpf(); + + for (int k=0; k<=order/2; k++) + hk[k] = hm_[order/2-k]; + } + + // Calculation of coefficients for LPF + void WindowingDesign::LpfCoefficients() + { + float w = 2.0f*PI_FS_*fC_; + hm_[0] = 2.0f*fC_/FS_; + for (int k=1; k<=order_/2; k++) + hm_[k] = (sinf(k*w)/(PI_*k))*wn_[k]; + } + + // Transform LPF to HPF + void WindowingDesign::ToHpf() + { + for (int k=1; k<=order_/2; k+=2) + hm_[k] = -hm_[k]; + } + + // Hamming window + void WindowingDesign::HammWindow() + { + float pi2OvM = 2.0f*PI_/(float)(order_ + 1); + for (int n=0; n<=order_/2; n++) + wn_[n] = 0.54f + 0.46f*cosf(pi2OvM*n); + } +} +
diff -r e4b8e25573f3 -r 46327dcab1bf WindowingDesignLpfHpf/WindowingDesignLH.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WindowingDesignLpfHpf/WindowingDesignLH.hpp Mon Dec 08 05:46:37 2014 +0000 @@ -0,0 +1,52 @@ +//------------------------------------------------------------------------------ +// Design of FIR filter of LPF and HPF using window method -- Header +// +// 2014/11/09, Copyright (c) 2014 MIKAMI, Naoki +//------------------------------------------------------------------------------ + +#ifndef HAMMING_WINDOWING_DESIGN_HPP +#define HAMMING_WINDOWING_DESIGN_HPP + +#include "mbed.h" + +namespace Mikami +{ + class WindowingDesign + { + public: + struct Coefs { float a1, a2, b1; }; + enum Type { LPF, HPF }; + + // Constructor + WindowingDesign(int order, float fs); + + //Destructor + ~WindowingDesign() + { + delete[] hm_; + delete[] wn_; + } + + // Execution of design + void Design(int order, Type pb, float fc, float hk[]); + + private: + static const float PI_ = 3.1415926536f; + const float FS_; // Sampling frequency + const float PI_FS_; + + float *hm_; // For coefficients + float *wn_; // For Windwo + + int order_; // Order + float fC_; // Cutoff frequency + + // Calculation of coefficients for LPF + void LpfCoefficients(); + // Transform LPF to HPF + void ToHpf(); + // Hamming window + void HammWindow(); + }; +} +#endif // HAMMING_WINDOWING_DESIGN_HPP