0a610d6843370cb28443c5b582188715da046c24
[ctsim.git] / include / filter.h
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **      Name:         filter.h
5 **      Purpose:      Signal filter header file
6 **      Programmer:   Kevin Rosenberg
7 **      Date Started: June 2000
8 **
9 **  This is part of the CTSim program
10 **  Copyright (c) 1983-2001 Kevin Rosenberg
11 **
12 **  $Id: filter.h,v 1.24 2001/01/28 19:10:18 kevin Exp $
13 **
14 **  This program is free software; you can redistribute it and/or modify
15 **  it under the terms of the GNU General Public License (version 2) as
16 **  published by the Free Software Foundation.
17 **
18 **  This program is distributed in the hope that it will be useful,
19 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
20 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 **  GNU General Public License for more details.
22 **
23 **  You should have received a copy of the GNU General Public License
24 **  along with this program; if not, write to the Free Software
25 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 ******************************************************************************/
27
28 #ifndef FILTER_H
29 #define FILTER_H
30
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35 #ifdef HAVE_FFTW
36 #include <fftw.h>
37 #include <rfftw.h>
38 #endif
39
40
41 // CLASS IDENTIFICATION
42 //    SignalFilter       A filter used to process signals
43 //
44 // CONTAINS
45 //    signal vector
46 //
47 //    Can create either a time/spatial waveform or a frequency signal
48 //    Waveforms can be created either by direct calculation or by inverse fourier transform
49
50 class SignalFilter {
51  public:
52     static const int FILTER_INVALID;
53     static const int FILTER_ABS_BANDLIMIT;      // filter times |x|
54     static const int FILTER_ABS_SINC;
55     static const int FILTER_ABS_G_HAMMING;
56     static const int FILTER_ABS_COSINE;
57     static const int FILTER_SHEPP;
58     static const int FILTER_BANDLIMIT;
59     static const int FILTER_SINC;
60     static const int FILTER_G_HAMMING;
61     static const int FILTER_COSINE;
62     static const int FILTER_TRIANGLE;
63
64     static const int DOMAIN_INVALID;
65     static const int DOMAIN_FREQUENCY;
66     static const int DOMAIN_SPATIAL;
67     
68     SignalFilter (const char* szFilterName, double dFilterMinimum, double dFilterMaximum, int nFilterPoints, double dBandwidth, double dFilterParam, const char* szDomainName);
69
70     SignalFilter (const int idFilter, double dFilterMinimum, double dFilterMaximum, int nFilterPoints, double dBandwidth, double dFilterParam, const int idDomain);
71
72     SignalFilter (const char* szFilterName, const char* szDomainName, double dBandwidth, double dFilterParam);
73
74     ~SignalFilter (void);
75
76     double* getFilter (void) const
77       { return m_adFilter; }
78
79     bool fail(void) const       {return m_fail;}
80     const std::string& failMessage(void) const {return m_failMessage;}
81
82     const std::string& nameFilter(void) const   { return m_nameFilter;}
83     const std::string& nameDomain(void) const   { return m_nameDomain;}
84     const int idFilter(void) const      { return m_idFilter;}
85     const int idDomain(void) const      { return m_idDomain;}
86
87     int getNFilterPoints (void) const  { return m_nFilterPoints; }
88     const double getFilterMin(void) const {return m_dFilterMin;}
89     const double getFilterMax(void) const {return m_dFilterMax;}
90     const double getFilterIncrement(void) const {return m_dFilterInc;}
91     void copyFilterData(double *pdFilter, const int iStart, const int nPoints) const;
92
93     double response (double x);
94
95     static double spatialResponse (int fType, double bw, double x, double param);
96
97     static double frequencyResponse (int fType, double bw, double u, double param);
98
99     static double spatialResponseAnalytic (int fType, double bw, double x, double param);
100
101     static double spatialResponseCalc (int fType, double bw, double x, double param, int nIntegral);
102
103     static void setNumIntegral(int nIntegral) {N_INTEGRAL = nIntegral;}
104
105   static const int getFilterCount() {return s_iFilterCount;}
106   static const char** getFilterNameArray() {return s_aszFilterName;}
107   static const char** getFilterTitleArray() {return s_aszFilterTitle;}
108   static int convertFilterNameToID (const char* const filterName);
109   static const char* convertFilterIDToName (const int idFilter);
110   static const char* convertFilterIDToTitle (const int idFilter);
111
112   static const int getDomainCount() {return s_iDomainCount;}
113   static const char** getDomainNameArray() {return s_aszDomainName;}
114   static const char** getDomainTitleArray() {return s_aszDomainTitle;}
115   static int convertDomainNameToID (const char* const domainName);
116   static const char* convertDomainIDToName (const int idDomain);
117   static const char* convertDomainIDToTitle (const int idDomain);
118
119   static double sinc (double x)
120       { return (fabs(x) > F_EPSILON ? (sin (x) / x) : 1.0); }
121
122   static double sinc (double x, double mult)
123       { return (fabs(x) > F_EPSILON ? (sin (x * mult) / x) : 1.0); }
124
125  private:
126     int m_nFilterPoints;
127     double m_dBandwidth;
128     double m_dFilterParam;
129     double m_dFilterInc;
130     double m_dFilterMin;
131     double m_dFilterMax;
132     double* m_adFilter;
133
134     std::string m_nameFilter;
135     std::string m_nameDomain;
136     int m_idFilter;
137     int m_idDomain;
138
139     bool m_fail;
140     std::string m_failMessage;
141
142     static const char* s_aszFilterName[];
143     static const char* s_aszFilterTitle[];
144     static const int s_iFilterCount;
145     static const char* s_aszDomainName[];
146     static const char* s_aszDomainTitle[];
147     static const int s_iDomainCount;
148     static int N_INTEGRAL;
149
150     static const bool haveAnalyticSpatial (const int filterID);
151
152     void init (const int idFilter, double dFilterMin, double dFilterMax, int nFilterPoints, double dBandwidth, double dFilterParam, const int idDomain);
153
154     void createFrequencyFilter (double* x) const;
155     void createSpatialFilter (double* x) const;
156
157     double spatialResponseCalc (double x) const;
158     double spatialResponseAnalytic (double x) const;
159     double frequencyResponse (double u) const;
160
161     static double integral_abscos (double u, double w)
162     { return (fabs (u) > F_EPSILON ? (cos (u * w) - 1) / (u * u) + w / u * sin (u * w) : (w * w / 2)); }
163 };
164
165 #endif