GPLIB++
MtuFilter.cpp
Go to the documentation of this file.
1 #include "MtuFilter.h"
2 #include <fstream>
3 #include <gsl/gsl_errno.h>
4 #include <gsl/gsl_spline.h>
5 #include "FatalException.h"
6 #include <cassert>
7 using namespace std;
8 
9 namespace gplib
10  {
11 
12  MtuFilter::~MtuFilter()
13  {
14  }
15 
16  void MtuFilter::GetData(const std::string filename)
17  {
18  ifstream filterfile;
19  ttsdata rawfreqs;
20  ttsdata rawimag, rawreal;
21  double currfreq, currreal, currimag, dummy;
22  //double absresponse,phaseresponse;
23  double *imag, *real, *freqs;
24 
25  FilterCoeff.clear();
26  filterfile.open(filename.c_str());
27  if (!filterfile)
28  {
29  throw FatalException("Filter-file does not exist: " + filename);
30  }
31  filterfile >> dummy;
32  while (filterfile.good())
33  {
34  filterfile >> currfreq;
35  filterfile.ignore(256, ',');
36  filterfile >> currreal;
37  filterfile.ignore(256, ',');
38  filterfile >> currimag;
39  if (filterfile.good())
40  {
41  rawfreqs.push_back(currfreq);
42  rawreal.push_back(currreal);
43  rawimag.push_back(currimag);
44  }
45  }
46  assert(rawreal.size() == rawimag.size());
47  assert(rawreal.size() == rawfreqs.size());
48  filterfile.close();
49 
50  real = new double[rawreal.size()];
51  imag = new double[rawimag.size()];
52  freqs = new double[rawfreqs.size()];
53 
54  for (unsigned int i = 0; i < rawreal.size(); ++i)
55  {
56  real[rawreal.size() - 1 - i] = rawreal.at(i);
57  imag[rawreal.size() - 1 - i] = rawimag.at(i);
58  freqs[rawreal.size() - 1 - i] = rawfreqs.at(i);
59  }
60 
61  gsl_interp_accel *accreal = gsl_interp_accel_alloc();
62  gsl_interp_accel *accimag = gsl_interp_accel_alloc();
63 
64  gsl_spline *realspline = gsl_spline_alloc(gsl_interp_cspline,
65  rawreal.size());
66  gsl_spline *imagspline = gsl_spline_alloc(gsl_interp_cspline,
67  rawimag.size());
68 
69  gsl_spline_init(realspline, freqs, real, rawreal.size());
70  gsl_spline_init(imagspline, freqs, imag, rawimag.size());
71 
72  for (int i = 0; i < seglength / 2 + 1; ++i)
73  {
74  currreal = gsl_spline_eval(realspline, i * freqstep, accreal);
75  currimag = gsl_spline_eval(imagspline, i * freqstep, accimag);
76  FilterCoeff.push_back(currreal + I * currimag);
77  }
78  delete[] freqs;
79  delete[] real;
80  delete[] imag;
81  gsl_spline_free(realspline);
82  gsl_spline_free(imagspline);
83  gsl_interp_accel_free(accreal);
84  gsl_interp_accel_free(accimag);
85  }
86 
87  void MtuFilter::WriteData(const std::string filename)
88  {
89  ofstream rawout;
90  rawout.open((filename + ".raw").c_str());
91  for (unsigned int i = 0; i < FilterCoeff.size(); ++i)
92  {
93  rawout << i * freqstep << " " << abs(FilterCoeff.at(i)) << " "
94  << arg(FilterCoeff.at(i)) << endl;
95  }
96  rawout.close();
97  }
98  }
The basic exception class for all errors that arise in gplib.