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

3G开发

开发平台:

Visual C++

  1. //
  2. //  file = prog_14a.cpp
  3. //
  4.  #include <stdlib.h> 
  5.  #include <iostream> 
  6.  #include <fstream>
  7.  #include <math.h>
  8.  #include "misdefs.h" 
  9.  #include "fir_resp.h"
  10.  #include "fir_dsgn.h"
  11.  #include "BP_remezalg.h"
  12.  #include "string.h"
  13.  #include <strstream>
  14.    
  15.  #ifdef _DEBUG
  16.  ofstream DebugFile("remez.out", ios::out);
  17.  #endif
  18.  main()
  19.  {
  20.   BpRemezAlgorithm *remez;
  21.   FirFilterDesign *fir_filter;
  22.   FirFilterResponse *filter_response;
  23.   strstream *fnam_stream;
  24.   char fnam_strm_buf[255];
  25.   int filter_length;
  26.   int coeff_indx;
  27.   double ripple_ratio;
  28.   double passband_edge_freq_low;
  29.   double stopband_edge_freq_low;
  30.   double passband_edge_freq_high;
  31.   double stopband_edge_freq_high;
  32.   double *extremal_freqs;
  33.   double *filter_coeffs;
  34.   double noise_equiv_bw;
  35.   fstream *seq_file;
  36.   int file_seq_num;
  37. //  double *magnitude_response;
  38. //  double quant_factor;
  39.   int grid_density;
  40.   int i;
  41.   int op_mode;
  42.   fstream *coeff_file;
  43. //  char* coeff_fname;
  44.   cout << "desired mode ?n" << endl;
  45.   cout << "1 = remez" << endl;
  46.   cout << "2 = plot for existing coeffn" << endl;
  47.   cin >> op_mode;
  48.   //-------------------------------------------------------------
  49.   //  Plot Response of existing filter
  50. if(op_mode == 2) {
  51.       //coeff_file = new ifstream(coeff_fname, ios::in);
  52.       coeff_file = new fstream("interp_filt_coeff.txt", ios::in);
  53.       *coeff_file >> filter_length;
  54.       filter_coeffs = new double[filter_length];
  55.       for( i=0; i<filter_length; i++) {
  56.          *coeff_file >> filter_coeffs[i];
  57.       }
  58.    fir_filter = new FirFilterDesign( filter_length, 
  59.                                     FIR_SYM_EVEN_LEFT, 
  60.                                     filter_coeffs);
  61.   filter_response = new FirFilterResponse( fir_filter,
  62.                                            cin, cout);
  63.   filter_response->ComputeMagResp();
  64.   filter_response->NormalizeResponse();
  65.   filter_response->DumpMagResp();
  66.   exit(88);
  67.   }  // end of if(op_mode == 2)
  68.   //------------------------------------------------------------
  69.   //  Design filter using Remez 
  70.    if(op_mode == 1) {
  71.       seq_file = new fstream("FilterUtilMemory.txt", ios::in);
  72.       *seq_file >> file_seq_num;
  73.       seq_file->close();
  74.       delete seq_file;
  75.       file_seq_num++;
  76.       seq_file = new fstream("FilterUtilMemory.txt", ios::out);
  77.       *seq_file << file_seq_num << endl;
  78.       seq_file->close();
  79.       fnam_stream = new strstream(fnam_strm_buf, 255, ios::out);
  80.       for(i=0; i<255; i++) {
  81.          fnam_strm_buf[i] = NULL;
  82.       }
  83.       *fnam_stream << "remez_filt_coeff_" << file_seq_num << ".txt";
  84.       coeff_file = new fstream(fnam_strm_buf, ios::out);
  85.       delete fnam_stream;
  86.       read_filter_length:
  87.       cout << "filter length ?n (must be odd)" << endl;
  88.       cin >> filter_length;
  89.       if((filter_length%2) == 0) goto read_filter_length;
  90.   
  91.   cout << "ripple ratio?" << endl;
  92.   cin >> ripple_ratio;
  93.   
  94.   cout << "low stopband edge frequency?" << endl;
  95.   cin >> stopband_edge_freq_low;
  96.   cout << "low passband edge frequency?" << endl;
  97.   cin >> passband_edge_freq_low;
  98.   
  99.   cout << "high passband edge frequency?" << endl;
  100.   cin >> passband_edge_freq_high;
  101.   
  102.   cout << "high stopband edge frequency?" << endl;
  103.   cin >> stopband_edge_freq_high;
  104.   
  105. //  cout << "quantizing factor?n"
  106. //       << " ( 256 for 8 bits, 1024 for 10 bits, etc. )"
  107. //       << endl;
  108. //  cin >> quant_factor;
  109.     
  110.   cout << "Remez grid density?n"
  111.        << endl;
  112.   cin >> grid_density;
  113.     
  114.   extremal_freqs = new double[(filter_length+1)/2];
  115.   filter_coeffs = new double[filter_length];
  116.   
  117.   cout << "ripple_ratio = " << ripple_ratio << endl;
  118.   cout << "stopband_edge_freq_low = " << stopband_edge_freq_low << endl;
  119.   cout << "passband_edge_freq_low = " << passband_edge_freq_low << endl;
  120.   cout << "passband_edge_freq_high = " << passband_edge_freq_high << endl;
  121.   cout << "stopband_edge_freq_high = " << stopband_edge_freq_high << endl;
  122.   cout << "filter_length = " << filter_length << endl;
  123. //  cout << "quant_factor = " << quant_factor << endl;
  124.   
  125.   remez = new BpRemezAlgorithm( //cin, cout,
  126.                               grid_density,
  127.                               filter_length,
  128.                               stopband_edge_freq_low,
  129.                               passband_edge_freq_low,
  130.                               passband_edge_freq_high,
  131.                               stopband_edge_freq_high,
  132.                               ripple_ratio,
  133.                               &fir_filter);
  134.   fir_filter->CopyCoefficients(filter_coeffs);  
  135.   
  136.    #ifdef _DEBUG
  137.       for( coeff_indx=0; coeff_indx< filter_length; coeff_indx++){
  138.          DebugFile << "coeff[" << coeff_indx << "] = "
  139.                << filter_coeffs[coeff_indx] << endl;
  140.       }
  141.    #endif
  142.    *coeff_file << filter_length << endl;
  143.    for( coeff_indx=0; coeff_indx< filter_length; coeff_indx++) {
  144.       *coeff_file << filter_coeffs[coeff_indx] << endl;
  145.    }
  146.    *coeff_file << "ripple_ratio = " << ripple_ratio << endl;
  147.    *coeff_file << "passband_edge_freq = " << passband_edge_freq << endl;
  148.    *coeff_file << "stopband_edge_freq = " << stopband_edge_freq << endl;
  149.    *coeff_file << "grid_density = " << grid_density << endl;
  150. //   coeff_file->close();
  151.  
  152.    fnam_stream = new strstream(fnam_strm_buf, 255, ios::out);
  153.    for(i=0; i<255; i++) {
  154.       fnam_strm_buf[i] = NULL;
  155.    }
  156.    *fnam_stream << "remez_mag_resp_" << file_seq_num << ".txt";
  157.    //coeff_file = new fstream(fnam_strm_buf, ios::out);
  158.    filter_response = new FirFilterResponse( fir_filter,
  159.                                              1500, // num response points
  160.                                              1, // dB scale enabled
  161.                                              1, // normalize enabled
  162.                                              fnam_strm_buf);
  163.    noise_equiv_bw = filter_response->ComputeMagResp();
  164.    *coeff_file << "noise_equiv_bw = " << noise_equiv_bw << endl;
  165.    coeff_file->close();
  166.    filter_response->NormalizeResponse();
  167.    filter_response->DumpMagResp();
  168.    delete remez;
  169.    delete coeff_file;
  170.    exit(88);
  171.    }  // end of if(op_mode == 1)
  172.  
  173. //  delete [] magnitude_response;
  174. //  delete [] extremal_freqs;
  175. //  delete [] filter_coeffs;
  176.   
  177.   return 0;
  178.  }