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