andf1fir.cpp
上传用户:jtjnyq9001
上传日期:2014-11-21
资源大小:3974k
文件大小:6k
源码类别:

3G开发

开发平台:

Visual C++

  1. //
  2. //  File = andf1fir.cpp
  3. //
  4. #include <stdlib.h>
  5. #include <fstream>
  6. #include <math.h>
  7. #include "parmfile.h"
  8. #include "andf1fir.h"
  9. #include "fir_resp_w_noise_bw.h"
  10. #include "model_graph.h"
  11. #include "sigstuff.h"
  12. #include "syst_graph.h"
  13. #include "misdefs.h"
  14. extern ParmFile *ParmInput;
  15. //extern ActiveSystemGraph ActSystGraph;
  16. #ifdef _DEBUG
  17.   extern ofstream *DebugFile;
  18. #endif
  19. //======================================================
  20. AnlgDirectFormFir::AnlgDirectFormFir( char* instance_name,
  21.                               PracSimModel* outer_model,
  22.                               Signal<float>* in_sig,
  23.                               Signal<float>* out_sig )
  24.               :PracSimModel(instance_name,
  25.                             outer_model)
  26. {
  27.   In_Sig = in_sig;
  28.   Out_Sig = out_sig;
  29.   OPEN_PARM_BLOCK;
  30.    GET_BOOL_PARM(Bypass_Enabled);
  31.   GET_INT_PARM( Kind_Of_Filter_Resp );
  32.   GET_DOUBLE_PARM( Fixed_Gain );
  33.    if(Kind_Of_Filter_Resp == 3) {
  34.       Coeff_Fname = new char[64];
  35.       strcpy(Coeff_Fname, "");
  36.       GET_STRING_PARM(Coeff_Fname);
  37.    }
  38.   MAKE_OUTPUT(Out_Sig);
  39.   MAKE_INPUT(In_Sig);
  40.   SAME_RATE(In_Sig, Out_Sig);
  41. }
  42. //=============================================
  43. AnlgDirectFormFir::~AnlgDirectFormFir( void ){ };
  44. //===========================================
  45. void AnlgDirectFormFir::Initialize(void)
  46. {
  47.   *DebugFile << "Now in AnlgDirectFormFir::Initialize()" << endl;
  48.   Proc_Block_Size = In_Sig->GetBlockSize();
  49.   Samp_Intvl = In_Sig->GetSampIntvl();
  50.   char subord_name[40];
  51.   char response_fnam[120];
  52.   strcpy(subord_name,GetModelName());
  53.   strcat(subord_name,":fir_design");
  54.    double *coeff;
  55.   int i;
  56.   //Kind_Of_Filter_Resp = 0;
  57.   switch (Kind_Of_Filter_Resp)
  58.     {
  59.     case 0: // raised cosine
  60.       Filter_Design = new FirFilterDesign(  subord_name, 
  61.                                             Kind_Of_Filter_Resp,
  62.                                             Samp_Intvl);
  63.       //DebugFile << "Samp_Intvl = " << Samp_Intvl << endl;
  64.       //Filter_Design->DumpCoefficients(&DebugFile);
  65.       //exit(0);
  66.       break;
  67.     case 1: // root raised cosine
  68.       Filter_Design = new FirFilterDesign(  subord_name, 
  69.                                             Kind_Of_Filter_Resp,
  70.                                             Samp_Intvl);
  71.       //DebugFile << "Samp_Intvl = " << Samp_Intvl << endl;
  72.       //Filter_Design->DumpCoefficients(&DebugFile);
  73.       //exit(0);
  74.       break;
  75.     case 2: // pure delay
  76.       Filter_Design = new FirFilterDesign(  subord_name, 
  77.                                             Kind_Of_Filter_Resp,
  78.                                             Samp_Intvl);
  79.       //Filter_Design->DumpCoefficients(&DebugFile);
  80.       //exit(0);
  81.       break;
  82.    case 3: // custom
  83. /*      coeff[0] = coeff[24] = -0.00406909;
  84.       coeff[1] = coeff[23] = -0.0103673;
  85.       coeff[2] = coeff[22] = -0.00180157;
  86.       coeff[3] = coeff[21] = 0.0152348;
  87.       coeff[4] = coeff[20] = 0.00321361;
  88.       coeff[5] = coeff[19] = -0.0275724;
  89.       coeff[6] = coeff[18] = -0.00511918;
  90.       coeff[7] = coeff[17] = 0.0494654;
  91.       coeff[8] = coeff[16] = 0.00700851;
  92.       coeff[9] = coeff[15] = -0.0969915;
  93.       coeff[10] = coeff[14] = -0.00831958;
  94.       coeff[11] = coeff[13] = 0.315158;
  95.       coeff[12] = 0.50881;
  96.       */
  97. /*      coeff[0] = coeff[18] = -0.033637;
  98.       coeff[1] = coeff[17] = -0.023387;
  99.       coeff[2] = coeff[16] = 0.026728;
  100.       coeff[3] = coeff[15] = 0.050455;
  101.       coeff[4] = coeff[14] = 0.0;
  102.       coeff[5] = coeff[13] = 0.075683;
  103.       coeff[6] = coeff[12] = -0.062366;
  104.       coeff[7] = coeff[11] = 0.093549;
  105.       coeff[8] = coeff[10] = 0.302731;
  106.       coeff[9] = 0.40;
  107.       */
  108.       Coeff_File = new ifstream(Coeff_Fname, ios::in);
  109.       *Coeff_File >> Num_Taps;
  110.       coeff = new double[Num_Taps];
  111.       for( i=0; i<Num_Taps; i++) {
  112.          *Coeff_File >> coeff[i];
  113.       }
  114.       Filter_Design = new FirFilterDesign(  Num_Taps, 
  115.                                             coeff);      
  116.       break;
  117.     default:
  118.       // error 
  119.       cout << "illegal value for 'Kind_Of_Filter_Resp' " << endl;
  120.       exit(1);
  121.     } // end of switch on Kind_Of_Filter_Resp
  122.   Num_Taps = Filter_Design->GetNumTaps();
  123.   Coeff = new float[Num_Taps];
  124.   Filter_Design->CopyCoefficients(Coeff);
  125.   Input_Mem = new float[Num_Taps];
  126.   for(int n=0; n< Num_Taps; n++) Input_Mem[n] = 0.0;
  127.   //
  128.   //---------------------------------------------
  129.   // compute magnitude response of filter and write to a file
  130.   response_fnam[0] = 0;
  131.   strcpy(response_fnam,"resp_");
  132.   strcat(response_fnam,GetInstanceName());
  133.   strcat(response_fnam,".txt");
  134.   FirFilterResponse* response = 
  135.             new FirFilterResponse(  Filter_Design,
  136.                                     500,
  137.                                     1, //dB scale enabled
  138.                                     1, //normalization enabled
  139.                                     response_fnam);
  140.    response->ComputeMagResp();
  141.    response->DumpMagResp();
  142. }
  143. //=============================================
  144. int AnlgDirectFormFir::Execute(void)
  145. {
  146.   *DebugFile << "In AnlgDirectFormFir::Execute" << endl;
  147.   float* input_mem = Input_Mem;
  148.   float* coeff = Coeff;
  149.   int num_taps = Num_Taps;
  150.   float *out_sig, *in_sig;
  151.   int is, tap_num;
  152.   float sum;
  153.   Proc_Block_Size = In_Sig->GetValidBlockSize();
  154.   Out_Sig->SetValidBlockSize(Proc_Block_Size);
  155.   out_sig = GET_OUTPUT_PTR(Out_Sig);
  156.   in_sig = GET_INPUT_PTR(In_Sig);
  157.    if(Bypass_Enabled) {
  158.       for( is=0; is<Proc_Block_Size; is++) {
  159.          *out_sig++ = *in_sig++;
  160.       }
  161.    }
  162.    else {
  163.       for ( is=0; is<Proc_Block_Size; is++) {
  164.          for( tap_num = num_taps-1; tap_num > 0; tap_num--) {
  165.             input_mem[tap_num] = input_mem[tap_num-1];
  166.          }
  167.          input_mem[0] = *in_sig++;
  168.          sum = 0.0;
  169.          for( tap_num = 0; tap_num < num_taps; tap_num++) {
  170.             sum += coeff[tap_num] * input_mem[tap_num];
  171.          }
  172.          *out_sig++ = float(Fixed_Gain * sum);
  173.       }
  174.    }
  175.    return(_MES_AOK);
  176. }