RTXI  2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
firideal.cpp
Go to the documentation of this file.
1 /*
2  * Functions for FirIdealFilter
3  * Design ideal lowpass, highpass, bandpass, or bandstop FIR filter
4  */
5 
6 #include "firideal.h"
7 #include "fir_dsgn.h"
8 #include "misdefs.h"
9 #include "typedefs.h"
10 #include <cstdio>
11 #include <gsl/gsl_math.h>
12 #include <math.h>
13 #include <stdlib.h>
14 
15 FirIdealFilter::FirIdealFilter(int num_taps, double lambda1, double lambda2,
16  int filtertype)
17  : LinearPhaseFirDesign(num_taps)
18 {
19  if (num_taps % 2) {
20  DefineFilter(num_taps, lambda1, lambda2, filtertype);
21  } else {
22  printf("Error in FIR window filter: even number of taps requested.");
23  }
24 }
25 
26 void
27 FirIdealFilter::DefineFilter(int num_taps, double lambda1, double lambda2,
28  int band_config)
29 {
30  if (num_taps % 2) {
31  Fir_Type = 1;
32  band_config++;
33  Band_Config = (BAND_CONFIG_TYPE)band_config;
34  lambda1 *= M_PI;
35  lambda2 *= M_PI;
36  switch (Band_Config) {
37  case _LOWPASS_RESP_:
38  Ideal_Lowpass(lambda1);
39  break;
40  case _BANDPASS_RESP_:
41  Ideal_Bandpass(lambda1, lambda2);
42  break;
43  case _HIGHPASS_RESP_:
44  Ideal_Highpass(lambda1);
45  break;
46  case _BANDSTOP_RESP_:
47  Ideal_Bandstop(lambda1, lambda2);
48  break;
49  } // end of switch on Band_Config
50  } else {
51  printf("Error in FIR window filter: no filter made!");
52  }
53 }
54 
55 void
57 {
58  int n, n_max;
59  double m;
60 
61  if (Num_Taps % 2) {
62  n_max = (Num_Taps - 1) / 2;
63  Imp_Resp_Coeff[n_max] = lambda1 / M_PI;
64  } else {
65  n_max = Num_Taps / 2;
66  }
67 
68  for (n = 0; n < n_max; n++) {
69  m = n - (double)(Num_Taps - 1.0) / 2.0;
70  Imp_Resp_Coeff[n] = sin(m * lambda1) / (m * M_PI);
72  }
73 }
74 
75 void
77 {
78  int n, n_max;
79  double m;
80 
81  if (Num_Taps % 2) {
82  n_max = (Num_Taps - 1) / 2;
83  Imp_Resp_Coeff[n_max] = 1.0 - lambda1 / M_PI;
84  } else {
85  n_max = Num_Taps / 2;
86  }
87 
88  for (n = 0; n < n_max; n++) {
89  m = n - (double)(Num_Taps - 1.0) / 2.0;
90  Imp_Resp_Coeff[n] = -sin(m * lambda1) / (m * M_PI);
92  }
93 }
94 
95 void
96 FirIdealFilter::Ideal_Bandpass(double lambda1, double lambda2)
97 {
98  int n, n_max;
99  double m;
100 
101  if (Num_Taps % 2) {
102  n_max = (Num_Taps - 1) / 2;
103  Imp_Resp_Coeff[n_max] = (lambda2 - lambda1) / M_PI;
104  } else {
105  n_max = Num_Taps / 2;
106  }
107 
108  for (n = 0; n < n_max; n++) {
109  m = n - (double)(Num_Taps - 1.0) / 2.0;
110  Imp_Resp_Coeff[n] = (sin(m * lambda2) - sin(m * lambda1)) / (m * M_PI);
111  Imp_Resp_Coeff[Num_Taps - 1 - n] = Imp_Resp_Coeff[n];
112  }
113  return;
114 }
115 
116 void
117 FirIdealFilter::Ideal_Bandstop(double lambda1, double lambda2)
118 {
119  int n, n_max;
120  double m;
121 
122  if (Num_Taps % 2) {
123  n_max = (Num_Taps - 1) / 2;
124  Imp_Resp_Coeff[n_max] = 1.0 + (lambda1 - lambda2) / M_PI;
125  } else {
126  n_max = Num_Taps / 2;
127  }
128 
129  for (n = 0; n < n_max; n++) {
130  m = n - (double)(Num_Taps - 1.0) / 2.0;
131  Imp_Resp_Coeff[n] = (sin(m * lambda1) - sin(m * lambda2)) / (m * M_PI);
132  Imp_Resp_Coeff[Num_Taps - 1 - n] = Imp_Resp_Coeff[n];
133  }
134  return;
135 }
double lambda2
Definition: firideal.h:32
double * Imp_Resp_Coeff
Definition: fir_dsgn.h:75
void Ideal_Bandpass(double lambda1, double lambda2)
Definition: firideal.cpp:96
void DefineFilter(int num_taps, double lambda1, double lambda2, int band_config)
Definition: firideal.cpp:27
double lambda1
Definition: firideal.h:31
void Ideal_Bandstop(double lambda1, double lambda2)
Definition: firideal.cpp:117
enum typesOfResponse BAND_CONFIG_TYPE
Definition: typedefs.h:20
void Ideal_Highpass(double lambda1)
Definition: firideal.cpp:76
FirIdealFilter(int num_taps, double lambda1, double lambda2, int filtertype)
Definition: firideal.cpp:15
void Ideal_Lowpass(double lambda1)
Definition: firideal.cpp:56
BAND_CONFIG_TYPE Band_Config
Definition: lin_dsgn.h:26