RTXI  2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
iir_dsgn.cpp
Go to the documentation of this file.
1 //
2 // File = iir_dsgn.cpp
3 //
4 
5 #include <iostream>
6 #include <math.h>
7 #include <stdlib.h>
8 
9 #include "iir_dsgn.h"
10 #include "misdefs.h"
11 #ifdef _DEBUG
12 extern std::ofstream DebugFile;
13 #endif
14 
15 //=========================================
16 // default constructor
17 //-----------------------------------------
18 
20 {
21  return;
22 }
23 //======================================================
24 // constructor that provides interactive initialization
25 //------------------------------------------------------
26 
27 IirFilterDesign::IirFilterDesign(std::istream& uin, std::ostream& uout)
28 {
29  uout << "We made it into interactive constructor" << std::endl;
30 }
31 //===============================================
32 // constructor that allocates arrays of
33 // length num_taps to hold coefficients
34 //-----------------------------------------------
35 
36 IirFilterDesign::IirFilterDesign(int num_numer_coeffs, int num_denom_coeffs)
37 {
38  Num_Numer_Coeffs = num_numer_coeffs;
39  Num_Denom_Coeffs = num_denom_coeffs;
40  Numer_Coeffs = new double[num_numer_coeffs];
41  Denom_Coeffs = new double[num_denom_coeffs + 1];
42  Orig_Numer_Coeffs = new double[num_numer_coeffs];
43  Orig_Denom_Coeffs = new double[num_denom_coeffs + 1];
44 }
45 
46 //==========================================================
47 // constructor that allocates array of length num_taps
48 // and initializes this array to values contained in
49 // input array *imp_resp_coeff
50 //----------------------------------------------------------
51 
52 IirFilterDesign::IirFilterDesign(int num_numer_coeffs, int num_denom_coeffs,
53  double* numer_coeffs, double* denom_coeffs)
54 {
55  int n;
56  Num_Numer_Coeffs = num_numer_coeffs;
57 #ifdef _DEBUG
58  DebugFile << "in IirFilterDesign, num_numer_coeffs = " << num_numer_coeffs
59  << std::endl;
60 #endif
61  Num_Denom_Coeffs = num_denom_coeffs;
62 #ifdef _DEBUG
63  DebugFile << "in IirFilterDesign, num_denom_coeffs = " << num_denom_coeffs
64  << std::endl;
65 #endif
66  Numer_Coeffs = new double[num_numer_coeffs];
67  Denom_Coeffs = new double[num_denom_coeffs + 1];
68  Orig_Numer_Coeffs = new double[num_numer_coeffs];
69  Orig_Denom_Coeffs = new double[num_denom_coeffs + 1];
70 
71  for (n = 0; n < num_numer_coeffs; n++) {
72  Numer_Coeffs[n] = numer_coeffs[n];
73  Orig_Numer_Coeffs[n] = numer_coeffs[n];
74  }
75  Denom_Coeffs[0] = 0.0;
76  Orig_Denom_Coeffs[0] = 0.0;
77  for (n = 1; n <= num_denom_coeffs; n++) {
78  Denom_Coeffs[n] = denom_coeffs[n];
79  Orig_Denom_Coeffs[n] = denom_coeffs[n];
80  }
81  return;
82 }
83 
84 //==============================================
85 // method to allocate coefficient arrays
86 // after default constructor has been used
87 //----------------------------------------------
88 
89 void
90 IirFilterDesign::Initialize(int num_numer_coeffs, int num_denom_coeffs)
91 {
92  Num_Numer_Coeffs = num_numer_coeffs;
93  Num_Denom_Coeffs = num_denom_coeffs;
94  Numer_Coeffs = new double[num_numer_coeffs];
95  Denom_Coeffs = new double[num_denom_coeffs + 1];
96  Orig_Numer_Coeffs = new double[num_numer_coeffs];
97  Orig_Denom_Coeffs = new double[num_denom_coeffs + 1];
98 }
99 
100 //============================================================
101 // method to quantize coefficients
102 //------------------------------------------------------------
103 
104 void
106  logical rounding_enabled)
107 {
108  int n;
109  long work_long;
110  double quan_err;
111 
112  //-----------------------------------
113  // if quant_factor == 0, then restore
114  // coefficients to their original,
115  // unquantized values
116 
117  if (quant_factor == 0) {
118  for (n = 0; n < Num_Numer_Coeffs; n++) {
120  }
121  for (n = 1; n <= Num_Denom_Coeffs; n++) {
123  }
124  return;
125  }
126 
127  //-------------------------------------------
128  // quantize the original coefficient values
129 
130  for (n = 0; n < Num_Numer_Coeffs; n++) {
131  if (rounding_enabled) {
132  work_long = long((quant_factor * Orig_Numer_Coeffs[n]) + 0.5);
133  } else {
134  work_long = long(quant_factor * Orig_Numer_Coeffs[n]);
135  }
136 
137  Numer_Coeffs[n] = double(work_long) / double(quant_factor);
138  quan_err = (Numer_Coeffs[n] - Orig_Numer_Coeffs[n]) / Orig_Numer_Coeffs[n];
139 #ifdef _DEBUG
140  DebugFile << "numer coeff " << n << " quant from " << Orig_Numer_Coeffs[n]
141  << "\n to " << Numer_Coeffs[n] << " error is " << quan_err
142  << std::endl;
143 #endif
144  }
145  for (n = 1; n <= Num_Denom_Coeffs; n++) {
146  if (rounding_enabled) {
147  work_long = long((quant_factor * Orig_Denom_Coeffs[n]) + 0.5);
148  } else {
149  work_long = long(quant_factor * Orig_Denom_Coeffs[n]);
150  }
151 
152  Denom_Coeffs[n] = double(work_long) / double(quant_factor);
153  quan_err = (Denom_Coeffs[n] - Orig_Denom_Coeffs[n]) / Orig_Denom_Coeffs[n];
154 #ifdef _DEBUG
155  DebugFile << "denom coeff " << n << " quant from " << Orig_Denom_Coeffs[n]
156  << "\n to " << Denom_Coeffs[n] << " error is " << quan_err
157  << std::endl;
158 #endif
159  }
160  return;
161 }
162 
163 //============================================================
164 // method to scale coefficients
165 //------------------------------------------------------------
166 
167 void
169 {
170  int n;
171  for (n = 0; n < Num_Numer_Coeffs; n++) {
172  Orig_Numer_Coeffs[n] = scale_factor * Orig_Numer_Coeffs[n];
173  Numer_Coeffs[n] = Orig_Numer_Coeffs[n];
174  }
175  for (n = 1; n <= Num_Denom_Coeffs; n++) {
176  Orig_Denom_Coeffs[n] = scale_factor * Orig_Denom_Coeffs[n];
177  Denom_Coeffs[n] = Orig_Denom_Coeffs[n];
178  }
179  return;
180 }
181 
182 //======================================================
183 // copy coefficient values from internal arrays
184 // to output arrays *numer_coeff and *denom_coeff
185 //------------------------------------------------------
186 
187 void
188 IirFilterDesign::CopyCoefficients(double* numer_coeff, double* denom_coeff)
189 {
190  int n;
191  for (n = 0; n < Num_Numer_Coeffs; n++) {
192  numer_coeff[n] = Numer_Coeffs[n];
193  }
194  for (n = 0; n <= Num_Denom_Coeffs; n++) {
195  denom_coeff[n] = Denom_Coeffs[n];
196  }
197  return;
198 }
199 
200 //==============================================
201 // get pointer to numerator coefficient array
202 //----------------------------------------------
203 double*
205 {
206  std::cout << "in iir_dsgn, Numer_Coeffs = " << (void*)Numer_Coeffs
207  << std::endl;
208  return (Numer_Coeffs);
209 }
210 
211 //==============================================
212 // get a copy of the denominator coeffcients
213 //----------------------------------------------
214 double*
216 {
217  std::cout << "in iir_dsgn, Denom_Coeffs = " << (void*)Denom_Coeffs
218  << std::endl;
219  double* ret_ptr = new double[Num_Denom_Coeffs + 1];
220  for (int i = 0; i <= Num_Denom_Coeffs; i++)
221  ret_ptr[i] = Denom_Coeffs[i];
222  return (ret_ptr);
223 }
224 
225 //==============================================
226 // set the denominator coeffcients
227 //----------------------------------------------
228 void
229 IirFilterDesign::SetDenomCoefficients(int num_coeffs, double* coeffs)
230 {
231  Num_Denom_Coeffs = num_coeffs;
232  delete[] Denom_Coeffs;
233  Denom_Coeffs = new double[num_coeffs + 1];
234  for (int i = 0; i <= Num_Denom_Coeffs; i++)
235  Denom_Coeffs[i] = coeffs[i];
236  return;
237 }
238 
239 //==========================================
240 void
242 {
243  Sampling_Interval = samp_intvl;
244  return;
245 }
246 
247 double
249 {
250  return (Sampling_Interval);
251 }
252 
253 //==========================================
254 // get number of numerator coefficients
255 //------------------------------------------
256 
257 int
259 {
260  return (Num_Numer_Coeffs);
261 }
262 
263 //==========================================
264 // get number of denominator coefficients
265 //------------------------------------------
266 
267 int
269 {
270  return (Num_Denom_Coeffs);
271 }
272 
273 //==============================================================
274 // dump complete set of coefficients to output_stream
275 //--------------------------------------------------------------
276 
277 void
278 IirFilterDesign::DumpCoefficients(std::ofstream* output_stream)
279 {
280  int n;
281  // output_stream->setf(ios::fixed, ios::floatfield);
282  // output_stream->precision(11);
283  for (n = 0; n < Num_Numer_Coeffs; n++) {
284  (*output_stream) << "b[" << n << "] = " << Numer_Coeffs[n] << std::endl;
285  }
286  for (n = 1; n <= Num_Denom_Coeffs; n++) {
287  (*output_stream) << "a[" << n << "] = " << Denom_Coeffs[n] << std::endl;
288  }
289  // output_stream->precision(0);
290  // output_stream->setf(0, ios::floatfield);
291  return;
292 }
double * Denom_Coeffs
Definition: iir_dsgn.h:91
void QuantizeCoefficients(long quant_factor, logical rounding_enabled)
Definition: iir_dsgn.cpp:105
void ScaleCoefficients(double scale_factor)
Definition: iir_dsgn.cpp:168
double * Orig_Numer_Coeffs
Definition: iir_dsgn.h:92
int logical
Definition: typedefs.h:4
double Sampling_Interval
Definition: iir_dsgn.h:94
void SetSamplingInterval(double sampling_interval)
Definition: iir_dsgn.cpp:241
double * GetNumerCoefficients(void)
Definition: iir_dsgn.cpp:204
std::ofstream DebugFile
double * GetDenomCoefficients(void)
Definition: iir_dsgn.cpp:215
int Num_Denom_Coeffs
Definition: iir_dsgn.h:88
int GetNumNumerCoeffs(void)
Definition: iir_dsgn.cpp:258
double * Orig_Denom_Coeffs
Definition: iir_dsgn.h:93
void Initialize(int num_numer_coeffs, int num_denom_coeffs)
Definition: iir_dsgn.cpp:90
void DumpCoefficients(std::ofstream *output_stream)
Definition: iir_dsgn.cpp:278
double * Numer_Coeffs
Definition: iir_dsgn.h:90
int GetNumDenomCoeffs(void)
Definition: iir_dsgn.cpp:268
void SetDenomCoefficients(int num_coeffs, double *coeffs)
Definition: iir_dsgn.cpp:229
int Num_Numer_Coeffs
Definition: iir_dsgn.h:87
void CopyCoefficients(double *numer_coeff, double *denom_coeff)
Definition: iir_dsgn.cpp:188
double GetSamplingInterval(void)
Definition: iir_dsgn.cpp:248