⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fir_dsgn.cpp

📁 Digital filter designer s handbook C++ code source
💻 CPP
字号:
//
//  File = fir_dsgn.cpp
//

#include <math.h>
#include <stdlib.h>
#include <iomanip.h>
#include "misdefs.h"   
#include "fir_dsgn.h"

//=========================================
// default constructor
//-----------------------------------------

FirFilterDesign::FirFilterDesign( void )
{
 return;
} 

//===============================================
//  constructor that allocates array of
//  length num_taps to hold coefficients
//-----------------------------------------------

FirFilterDesign::FirFilterDesign( int num_taps )
{
 Num_Taps = num_taps;
 Imp_Resp_Coeff = new double[num_taps];
 Original_Coeff = new double[num_taps];
}


//==========================================================
// constructor that allocates array of length num_taps 
// and initializes this array to values contained in 
// input array *imp_resp_coeff 
//----------------------------------------------------------

FirFilterDesign::FirFilterDesign( int num_taps,
                                  double *imp_resp_coeff)
{
 Num_Taps = num_taps;
 Imp_Resp_Coeff = new double[num_taps];
 Original_Coeff = new double[num_taps];
 
 for(int n=0; n<num_taps; n++)
   {
    Imp_Resp_Coeff[n] = imp_resp_coeff[n];
    Original_Coeff[n] = imp_resp_coeff[n];
   }
 return;
} 


//============================================== 
// method to allocate coefficient array 
// after default constructor has been used 
//----------------------------------------------

void FirFilterDesign::Initialize( int num_taps )
{
 Num_Taps = num_taps;
 Imp_Resp_Coeff = new double[num_taps];
 Original_Coeff = new double[num_taps];
}

//============================================================
//  method to quantize coefficients
//------------------------------------------------------------

void FirFilterDesign::QuantizeCoefficients( long quant_factor,
                                            logical rounding_enabled )
{
 int n;
 long work_long;
 
 //-----------------------------------
 // if quant_factor == 0, then restore
 // coefficients to their original,
 // unquantized values
 
 if( quant_factor == 0)
   {
    for( n=0; n<Num_Taps; n++)
      {
       Imp_Resp_Coeff[n] = Original_Coeff[n];
      }
    return;
   }

 //-------------------------------------------
 // quantize the original coefficient values
    
 for( n=0; n< Num_Taps; n++)
  {
   if(rounding_enabled)
     {work_long = long((quant_factor * Original_Coeff[n])+0.5);}
   else
     {work_long = long(quant_factor * Original_Coeff[n]);}
     
   Imp_Resp_Coeff[n] = double(work_long)/double(quant_factor);
  }
 return;
}       


//============================================================
//  method to scale coefficients
//------------------------------------------------------------

void FirFilterDesign::ScaleCoefficients( double scale_factor )
{
 int n;
 for( n=0; n< Num_Taps; n++)
  {
   Original_Coeff[n] = scale_factor * Original_Coeff[n];
   Imp_Resp_Coeff[n] = Original_Coeff[n];
  }
 return;
}       

//======================================================
// copy coefficient values from array *Imp_Resp_Coeff
// to output array *coeff
//------------------------------------------------------
 
void FirFilterDesign::CopyCoefficients( double *coeff)
{
 for(int n=0; n<Num_Taps; n++)
   {
    coeff[n] = Imp_Resp_Coeff[n];
   }
 return;
}


//===================================
//  get number of filter taps
//-----------------------------------
 
int FirFilterDesign::GetNumTaps(void)
{
 return(Num_Taps);
}


//==============================================================
// dump complete set of coefficients to output_stream
//--------------------------------------------------------------

void FirFilterDesign::DumpCoefficients( ofstream* output_stream)
{ 
 output_stream->setf(ios::fixed, ios::floatfield);
 output_stream->precision(11);
 for(int n=0; n<Num_Taps; n++)
   {
    (*output_stream) << "h[" << n << "] = " 
                     << Imp_Resp_Coeff[n] << endl;
   }
 output_stream->precision(0);
 output_stream->setf(0, ios::floatfield);
 return;
}


//==============================================
//  get pointer to coefficient array
//----------------------------------------------
double* FirFilterDesign::GetCoefficients(void)
{
 cout << "in fs_dsgn, Imp_Resp_Coeff = " << (void*)Imp_Resp_Coeff << endl;
 return(Imp_Resp_Coeff);
}

//========================================================
// apply discrete-time window to filter coefficients
//--------------------------------------------------------

void FirFilterDesign::ApplyWindow( GenericWindow *window)
{
 for(int n=0; n<Num_Taps; n++)
   {
    Imp_Resp_Coeff[n] *= window->GetDataWinCoeff(n);
    Original_Coeff[n] = Imp_Resp_Coeff[n];
   }
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -