RTXI  2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
fir_resp.cpp
Go to the documentation of this file.
1 /*
2  * Functions for FirFilterResponse
3  * computes magnitude response
4  */
5 
6 #include "fir_resp.h"
7 #include "misdefs.h"
8 #include "typedefs.h"
9 #include <complex>
10 #include <gsl/gsl_math.h>
11 #include <math.h>
12 #include <stdlib.h>
13 
14 using namespace std;
15 //==================================================
16 // constructor with all configuration parameters
17 // passed in as arguments
18 //--------------------------------------------------
19 
21  int num_resp_pts, int db_scale_enabled,
22  int normalize_enabled,
23  char* resp_file_name)
24 {
25  Filter_Design = filter_design;
26  Num_Resp_Pts = num_resp_pts;
27  Db_Scale_Enabled = db_scale_enabled;
28  Normalize_Enabled = normalize_enabled;
29 
30  Num_Taps = Filter_Design->GetNumTaps();
31  Mag_Resp = new double[Num_Resp_Pts];
32 
33  return;
34 }
35 
36 //==================================================
37 // method to compute magnitude response
38 //--------------------------------------------------
39 void
41 {
42  int resp_indx, tap_indx;
43  double lambda;
44  complex<double> work(0.0, 0.0);
45 
46  double* coeff = Filter_Design->GetCoefficients();
47 
48  for (resp_indx = 0; resp_indx < Num_Resp_Pts; resp_indx++) {
49  lambda = resp_indx * M_PI / (double)Num_Resp_Pts;
50  work = complex<double>(0.0, 0.0);
51 
52  for (tap_indx = 0; tap_indx < Num_Taps; tap_indx++) {
53  work =
54  work + (coeff[tap_indx] * complex<double>(cos(tap_indx * lambda),
55  -sin(tap_indx * lambda)));
56  }
57 
58  if (Db_Scale_Enabled) {
59  Mag_Resp[resp_indx] = 20.0 * log10(norm(work));
60  } else {
61  Mag_Resp[resp_indx] = norm(work);
62  }
63  }
64  if (Normalize_Enabled)
65  NormalizeResponse();
66 
67  return;
68 }
69 
70 // method to normalize magnitude response
71 
72 void
74 {
75  int n;
76  double biggest;
77 
78  if (Db_Scale_Enabled) {
79  biggest = -100.0;
80 
81  for (n = 0; n < Num_Resp_Pts; n++) {
82  if (Mag_Resp[n] > biggest)
83  biggest = Mag_Resp[n];
84  }
85  for (n = 0; n < Num_Resp_Pts; n++) {
86  Mag_Resp[n] = Mag_Resp[n] - biggest;
87  }
88  } else {
89  biggest = 0.0;
90 
91  for (n = 0; n < Num_Resp_Pts; n++) {
92  if (Mag_Resp[n] > biggest)
93  biggest = Mag_Resp[n];
94  }
95  for (n = 0; n < Num_Resp_Pts; n++) {
96  Mag_Resp[n] = Mag_Resp[n] / biggest;
97  }
98  }
99  return;
100 }
101 
102 double*
104 {
105  return (Mag_Resp);
106 }
107 
108 void
110 {
111  double freq;
112  for (int n = 0; n < Num_Resp_Pts; n++) {
113  freq = (n * M_PI) / double(Num_Resp_Pts);
114  }
115  return;
116 }
117 
118 double
120 {
121  double peak;
122  int n, indexOfPeak;
123 
124  peak = -9999.0;
125  for (n = nBeg; n < nEnd; n++) {
126  if (Mag_Resp[n] > peak) {
127  peak = Mag_Resp[n];
128  indexOfPeak = n;
129  }
130  }
131  return (peak);
132 }
double * GetMagResp(void)
Definition: fir_resp.cpp:103
void DumpMagResp(void)
Definition: fir_resp.cpp:109
void NormalizeResponse(void)
Definition: fir_resp.cpp:73
FirFilterResponse(FirFilterDesign *filter_design, int num_resp_pts, int db_scale_enabled, int normalize_enabled, char *resp_file_name)
Definition: fir_resp.cpp:20
int GetNumTaps(void)
Definition: fir_dsgn.cpp:124
virtual void ComputeMagResp(void)
Definition: fir_resp.cpp:40
double GetIntervalPeak(int beg_indx, int end_indx)
Definition: fir_resp.cpp:119
double norm(const complex _z)
Definition: complex.h:113