1 /*****************************************************************************
5 ** Purpose: Routines for signal-procesing filters
6 ** Progammer: Kevin Rosenberg
7 ** Date Started: Aug 1984
9 ** This is part of the CTSim program
10 ** Copyright (C) 1983-2000 Kevin Rosenberg
12 ** $Id: filter.cpp,v 1.1 2000/06/19 02:59:34 kevin Exp $
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.
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.
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 ******************************************************************************/
32 * filter_generate Generate a filter
35 * f = filter_generate (filt_type, bw, xmin, xmax, n, param, domain, analytic)
36 * double f Generated filter vector
37 * int filt_type Type of filter wanted
38 * double bw Bandwidth of filter
39 * double xmin, xmax Filter limits
40 * int n Number of points in filter
41 * double param General input parameter to filters
42 * int domain FREQ or SPATIAL domain wanted
43 * int numint Number if intervals for calculating
44 * discrete inverse fourier xform
45 * for spatial domain filters. For
46 * ANALYTIC solutions, use numint = 0
50 filter_generate (const FilterType filt_type, double bw, double xmin, double xmax, int n, double param, const DomainType domain, int numint)
52 double *f = new double [n];
53 double xinc = (xmax - xmin) / (n - 1);
55 if (filt_type == FILTER_SHEPP) {
57 double c = - 4. / (a * a);
58 int center = (n - 1) / 2;
60 f[center] = 4. / (a * a);
62 for (int i = 1; i <= sidelen; i++ )
63 f [center + i] = f [center - i] = c / (4 * (i * i) - 1);
64 } else if (domain == D_FREQ) {
67 for (x = xmin, i = 0; i < n; x += xinc, i++)
68 f[i] = filter_frequency_response (filt_type, x, bw, param);
69 } else if (domain == D_SPATIAL) {
72 for (x = xmin, i = 0; i < n; x += xinc, i++)
74 f[i] = filter_spatial_response_analytic (filt_type, x, bw, param);
76 f[i] = filter_spatial_response_calc (filt_type, x, bw, param, numint);
78 sys_error (ERR_WARNING, "Illegal domain %d [filt_generate]", domain);
87 * filter_spatial_response_calc Calculate filter by discrete inverse fourier
88 * transform of filters's frequency
92 * y = filter_spatial_response_calc (filt_type, x, bw, param, n)
93 * double y Filter's response in spatial domain
94 * int filt_type Type of filter (definitions in ct.h)
95 * double x Spatial position to evaluate filter
96 * double bw Bandwidth of window
97 * double param General parameter for various filters
98 * int n Number of points to calculate integrations
102 filter_spatial_response_calc (int filt_type, double x, double bw, double param, int n)
106 if (filt_type == FILTER_TRIANGLE) {
113 double zinc = (zmax - zmin) / (n - 1);
117 for (int i = 0; i < n; i++, z += zinc)
118 q[i] = filter_frequency_response (filt_type, z, bw, param) * cos (TWOPI * z * x);
120 double y = 2 * integrateSimpson (zmin, zmax, q, n);
127 * filter_frequency_response Return filter frequency response
130 * h = filter_frequency_response (filt_type, u, bw, param)
131 * double h Filters frequency response at u
132 * int filt_type Type of filter
133 * double u Frequency to evaluate filter at
134 * double bw Bandwidth of filter
135 * double param General input parameter for various filters
139 filter_frequency_response (int filt_type, double u, double bw, double param)
142 double au = fabs (u);
145 case FILTER_BANDLIMIT:
151 case FILTER_ABS_BANDLIMIT:
157 case FILTER_TRIANGLE:
167 q = cos(PI * u / bw);
169 case FILTER_ABS_COSINE:
173 q = au * cos(PI * u / bw);
176 q = bw * sinc (PI * bw * u, 1.);
178 case FILTER_ABS_SINC:
179 q = au * bw * sinc (PI * bw * u, 1.);
181 case FILTER_G_HAMMING:
185 q = param + (1 - param) * cos (TWOPI * u / bw);
187 case FILTER_ABS_G_HAMMING:
191 q = au * (param + (1 - param) * cos(TWOPI * u / bw));
195 sys_error (ERR_WARNING,
196 "Frequency response for filter %d not implemented [filter_frequency_response]",
206 * filter_spatial_response_analytic Calculate filter by analytic inverse fourier
207 * transform of filters's frequency
211 * y = filter_spatial_response_analytic (filt_type, x, bw, param)
212 * double y Filter's response in spatial domain
213 * int filt_type Type of filter (definitions in ct.h)
214 * double x Spatial position to evaluate filter
215 * double bw Bandwidth of window
216 * double param General parameter for various filters
220 filter_spatial_response_analytic (int filt_type, double x, double bw, double param)
223 double u = TWOPI * x;
226 double b2 = TWOPI / bw;
229 case FILTER_BANDLIMIT:
230 q = bw * sinc(u * w, 1.0);
232 case FILTER_TRIANGLE:
233 temp = sinc (u * w, 1.0);
234 q = bw * temp * temp;
237 q = sinc(b-u,w) + sinc(b+u,w);
239 case FILTER_G_HAMMING:
240 q = 2 * param * sin(u*w)/u + (1-param) *
241 (sinc(b2-u, w) + sinc(b2+u, w));
243 case FILTER_ABS_BANDLIMIT:
244 q = 2 * integral_abscos (u, w);
246 case FILTER_ABS_COSINE:
247 q = integral_abscos(b-u,w) + integral_abscos(b+u,w);
249 case FILTER_ABS_G_HAMMING:
250 q = 2 * param * integral_abscos(u,w) +
251 (1-param)*(integral_abscos(u-b2,w)+integral_abscos(u+b2,w));
255 q = 4. / (PI * bw * bw);
257 q = fabs ((2 / bw) * sin (u * w)) * sinc (u * w, 1.) * sinc (u * w, 1.);
260 if (fabs (x) < bw / 2)
265 case FILTER_ABS_SINC:
267 sys_error (ERR_WARNING,
268 "Analytic filter type %d not implemented [filter_spatial_response_analytic]",
279 * sinc Return sin(x)/x function
283 * double v sinc value
287 * v = sin(x * mult) / x;
291 sinc (double x, double mult)
293 return (fabs(x) > F_EPSILON ? (sin (x * mult) / x) : 1.0);
298 * integral_abscos Returns integral of u*cos(u)
301 * q = integral_abscos (u, w)
302 * double q Integral value
303 * double u Integration variable
304 * double w Upper integration boundary
307 * Returns the value of integral of u*cos(u)*dV for V = 0 to w
311 integral_abscos (double u, double w)
313 if (fabs (u) > F_EPSILON)
314 return (cos(u * w) - 1) / (u * u) + w / u * sin (u * w);