不韋 呂 / Mbed 2 deprecated UIT2_VariableFIR_LPFHPF

Dependencies:   UIT_ACM1602NI UIT_ADDA mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers WindowingDesignLH.cpp Source File

WindowingDesignLH.cpp

00001 //------------------------------------------------------------------------------
00002 //  Design of FIR filter of LPF and HPF using window method
00003 //
00004 //  2014/12/08, Copyright (c) 2014 MIKAMI, Naoki
00005 //------------------------------------------------------------------------------
00006 
00007 #include "WindowingDesignLH.hpp"
00008 
00009 namespace Mikami
00010 {
00011     WindowingDesign::WindowingDesign(int order, float fs)
00012             : FS_(fs), PI_FS_(3.1415926536f/fs)
00013     {
00014         order_ = order;
00015         if ((order % 2) != 0)
00016         {
00017             fprintf(stderr, "order must be even.");
00018             return;
00019         }
00020         hm_ = new float[order/2+1];
00021         wn_ = new float[order/2+1];    
00022         
00023         HammWindow();
00024     }
00025         
00026     void WindowingDesign::Design(int order, Type pb, float fc,
00027                                  float hk[])
00028     {       
00029         if (pb == LPF) fC_ = fc;
00030         if (pb == HPF) fC_ = 0.5f*FS_ - fc;
00031     
00032         if (order != order_)
00033         {
00034             order_ = order_;
00035             if (hm_ != NULL) delete[] hm_;
00036             hm_ = new float[order/2+1];
00037             if (wn_ != NULL) delete[] wn_;
00038             wn_ = new float[order/2+1];
00039             HammWindow();
00040         }
00041 
00042         // Calculate coefficients for LPF
00043         LpfCoefficients();
00044         // If HPF, transform coefficients
00045         if (pb != LPF) ToHpf();
00046         
00047         for (int k=0; k<=order/2; k++)
00048             hk[k] = hm_[order/2-k];
00049     }
00050 
00051     // Calculation of coefficients for LPF
00052     void WindowingDesign::LpfCoefficients()
00053     {
00054         float w = 2.0f*PI_FS_*fC_;
00055         hm_[0] = 2.0f*fC_/FS_;
00056         for (int k=1; k<=order_/2; k++)
00057             hm_[k] = (sinf(k*w)/(PI_*k))*wn_[k];  
00058     }
00059     
00060     // Transform LPF to HPF
00061     void WindowingDesign::ToHpf()
00062     {
00063         for (int k=1; k<=order_/2; k+=2)
00064             hm_[k] = -hm_[k];
00065     }    
00066 
00067     // Hamming window
00068     void WindowingDesign::HammWindow()
00069     {
00070         float pi2OvM = 2.0f*PI_/(float)(order_ + 1);
00071         for (int n=0; n<=order_/2; n++)
00072             wn_[n] = 0.54f + 0.46f*cosf(pi2OvM*n);
00073     }
00074 }
00075