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.2 2000/06/20 17:54:51 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
49 SignalFilter::SignalFilter (const FilterType filt_type, double bw, double xmin, double xmax, int n, double param, const DomainType domain, int numint)
51 m_vecFilter = new double [n];
52 m_filterType = filt_type;
55 double xinc = (xmax - xmin) / (n - 1);
57 if (m_filterType == FILTER_SHEPP) {
59 double c = - 4. / (a * a);
60 int center = (n - 1) / 2;
62 m_vecFilter[center] = 4. / (a * a);
64 for (int i = 1; i <= sidelen; i++ )
65 m_vecFilter [center + i] = m_vecFilter [center - i] = c / (4 * (i * i) - 1);
66 } else if (domain == D_FREQ) {
69 for (x = xmin, i = 0; i < n; x += xinc, i++)
70 m_vecFilter[i] = frequencyResponse (x, param);
71 } else if (domain == D_SPATIAL) {
74 for (x = xmin, i = 0; i < n; x += xinc, i++)
76 m_vecFilter[i] = spatialResponseAnalytic (x, param);
78 m_vecFilter[i] = spatialResponseCalc (x, param, numint);
80 sys_error (ERR_WARNING, "Illegal domain %d [filt_generate]", domain);
84 SignalFilter::~SignalFilter (void)
91 * filter_spatial_response_calc Calculate filter by discrete inverse fourier
92 * transform of filters's frequency
96 * y = filter_spatial_response_calc (filt_type, x, m_bw, param, n)
97 * double y Filter's response in spatial domain
98 * int filt_type Type of filter (definitions in ct.h)
99 * double x Spatial position to evaluate filter
100 * double m_bw Bandwidth of window
101 * double param General parameter for various filters
102 * int n Number of points to calculate integrations
106 SignalFilter::spatialResponseCalc (double x, double param, int n) const
108 return (spatialResponseCalc (m_filterType, m_bw, x, param, n));
112 SignalFilter::spatialResponseCalc (FilterType fType, double bw, double x, double param, int n)
116 if (fType == FILTER_TRIANGLE) {
123 double zinc = (zmax - zmin) / (n - 1);
127 for (int i = 0; i < n; i++, z += zinc)
128 q[i] = frequencyResponse (fType, bw, z, param) * cos (TWOPI * z * x);
130 double y = 2 * integrateSimpson (zmin, zmax, q, n);
137 * filter_frequency_response Return filter frequency response
140 * h = filter_frequency_response (filt_type, u, m_bw, param)
141 * double h Filters frequency response at u
142 * int filt_type Type of filter
143 * double u Frequency to evaluate filter at
144 * double m_bw Bandwidth of filter
145 * double param General input parameter for various filters
149 SignalFilter::frequencyResponse (double u, double param) const
151 return frequencyResponse (m_filterType, m_bw, u, param);
156 SignalFilter::frequencyResponse (FilterType fType, double bw, double u, double param)
159 double au = fabs (u);
162 case FILTER_BANDLIMIT:
168 case FILTER_ABS_BANDLIMIT:
174 case FILTER_TRIANGLE:
184 q = cos(PI * u / bw);
186 case FILTER_ABS_COSINE:
190 q = au * cos(PI * u / bw);
193 q = bw * sinc (PI * bw * u, 1.);
195 case FILTER_ABS_SINC:
196 q = au * bw * sinc (PI * bw * u, 1.);
198 case FILTER_G_HAMMING:
202 q = param + (1 - param) * cos (TWOPI * u / bw);
204 case FILTER_ABS_G_HAMMING:
208 q = au * (param + (1 - param) * cos(TWOPI * u / bw));
212 sys_error (ERR_WARNING, "Frequency response for filter %d not implemented [filter_frequency_response]", fType);
221 * filter_spatial_response_analytic Calculate filter by analytic inverse fourier
222 * transform of filters's frequency
226 * y = filter_spatial_response_analytic (filt_type, x, m_bw, param)
227 * double y Filter's response in spatial domain
228 * int filt_type Type of filter (definitions in ct.h)
229 * double x Spatial position to evaluate filter
230 * double m_bw Bandwidth of window
231 * double param General parameter for various filters
235 SignalFilter::spatialResponseAnalytic (double x, double param) const
237 return spatialResponseAnalytic (m_filterType, m_bw, x, param);
241 SignalFilter::spatialResponseAnalytic (FilterType fType, double bw, double x, double param)
244 double u = TWOPI * x;
247 double b2 = TWOPI / bw;
250 case FILTER_BANDLIMIT:
251 q = bw * sinc(u * w, 1.0);
253 case FILTER_TRIANGLE:
254 temp = sinc (u * w, 1.0);
255 q = bw * temp * temp;
258 q = sinc(b-u,w) + sinc(b+u,w);
260 case FILTER_G_HAMMING:
261 q = 2 * param * sin(u*w)/u + (1-param) * (sinc(b2-u, w) + sinc(b2+u, w));
263 case FILTER_ABS_BANDLIMIT:
264 q = 2 * integral_abscos (u, w);
266 case FILTER_ABS_COSINE:
267 q = integral_abscos(b-u,w) + integral_abscos(b+u,w);
269 case FILTER_ABS_G_HAMMING:
270 q = 2 * param * integral_abscos(u,w) +
271 (1-param)*(integral_abscos(u-b2,w)+integral_abscos(u+b2,w));
275 q = 4. / (PI * bw * bw);
277 q = fabs ((2 / bw) * sin (u * w)) * sinc (u * w, 1.) * sinc (u * w, 1.);
280 if (fabs (x) < bw / 2)
285 case FILTER_ABS_SINC:
287 sys_error (ERR_WARNING, "Analytic filter type %d not implemented [filter_spatial_response_analytic]", fType);
297 * sinc Return sin(x)/x function
301 * double v sinc value
305 * v = sin(x * mult) / x;
310 * integral_abscos Returns integral of u*cos(u)
313 * q = integral_abscos (u, w)
314 * double q Integral value
315 * double u Integration variable
316 * double w Upper integration boundary
319 * Returns the value of integral of u*cos(u)*dV for V = 0 to w
323 SignalFilter::integral_abscos (double u, double w)
325 if (fabs (u) > F_EPSILON)
326 return (cos(u * w) - 1) / (u * u) + w / u * sin (u * w);
333 * convolve Discrete convolution of two functions
336 * r = convolve (f1, f2, dx, n, np, func_type)
337 * double r Convolved result
338 * double f1[], f2[] Functions to be convolved
339 * double dx Difference between successive x values
340 * int n Array index to center convolution about
341 * int np Number of points in f1 array
342 * int func_type EVEN or ODD or EVEN_AND_ODD function f2
345 * f1 is the projection data, its indices range from 0 to np - 1.
346 * The index for f2, the filter, ranges from -(np-1) to (np-1).
347 * There are 3 ways to handle the negative vertices of f2:
348 * 1. If we know f2 is an EVEN function, then f2[-n] = f2[n].
349 * All filters used in reconstruction are even.
350 * 2. If we know f2 is an ODD function, then f2[-n] = -f2[n]
351 * 3. If f2 is both ODD AND EVEN, then we must store the value of f2
352 * for negative indices. Since f2 must range from -(np-1) to (np-1),
353 * if we add (np - 1) to f2's array index, then f2's index will
354 * range from 0 to 2 * (np - 1), and the origin, x = 0, will be
355 * stored at f2[np-1].
359 SignalFilter::convolve (const double func[], const double dx, const int n, const int np, const FunctionSymmetry func_type) const
363 if (func_type == FUNC_BOTH) {
364 #if UNOPTIMIZED_CONVOLUTION
365 for (int i = 0; i < np; i++)
366 sum += func[i] * m_vecFilter[n - i + (np - 1)];
368 double* f2 = m_vecFilter + n + (np - 1);
369 for (int i = 0; i < np; i++)
370 sum += *func++ * *f2--;
372 } else if (func_type == FUNC_EVEN) {
373 for (int i = 0; i < np; i++) {
375 sum += func[i] * m_vecFilter[k];
377 } else if (func_type == FUNC_ODD) {
378 for (int i = 0; i < np; i++) {
381 sum -= func[i] * m_vecFilter[k];
383 sum += func[i] * m_vecFilter[k];
386 sys_error (ERR_WARNING, "Illegal function type %d [convolve]", func_type);
393 SignalFilter::convolve (const float func[], const double dx, const int n, const int np, const FunctionSymmetry func_type) const
397 if (func_type == FUNC_BOTH) {
398 #if UNOPTIMIZED_CONVOLUTION
399 for (int i = 0; i < np; i++)
400 sum += func[i] * m_vecFilter[n - i + (np - 1)];
402 double* f2 = m_vecFilter + n + (np - 1);
403 for (int i = 0; i < np; i++)
404 sum += *func++ * *f2--;
406 } else if (func_type == FUNC_EVEN) {
407 for (int i = 0; i < np; i++) {
409 sum += func[i] * m_vecFilter[k];
411 } else if (func_type == FUNC_ODD) {
412 for (int i = 0; i < np; i++) {
415 sum -= func[i] * m_vecFilter[k];
417 sum += func[i] * m_vecFilter[k];
420 sys_error (ERR_WARNING, "Illegal function type %d [convolve]", func_type);