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.6 2000/07/02 18:21:39 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 * SignalFilter::SignalFilter Construct a signal
35 * f = SignalFilter (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 signal
41 * double param General input parameter to filters
42 * int domain FREQUENCY or SPATIAL domain wanted
43 * int numint Number if intervals for calculating discrete inverse fourier xform
44 * for spatial domain filters. For ANALYTIC solutions, use numint = 0
47 SignalFilter::SignalFilter (const char* filterName, const char* filterMethodName, double bw, double signalLength, int n, double param, const char* domainName, int numIntegral = 0)
50 m_vecFourierCosTable = NULL;
51 m_vecFourierSinTable = NULL;
52 m_idFilter = convertFilterNameToID (filterName);
53 if (m_idFilter == FILTER_INVALID) {
55 m_failMessage = "Invalid Filter name ";
56 m_failMessage += filterName;
59 m_idFilterMethod = convertFilterMethodNameToID (filterMethodName);
60 if (m_idFilterMethod == FILTER_METHOD_INVALID) {
62 m_failMessage = "Invalid filter method name ";
63 m_failMessage += filterMethodName;
66 m_idDomain = convertDomainNameToID (domainName);
67 if (m_idDomain == DOMAIN_INVALID) {
69 m_failMessage = "Invalid domain name ";
70 m_failMessage += domainName;
73 init (m_idFilter, m_idFilterMethod, bw, signalLength, n, param, m_idDomain, numIntegral);
76 SignalFilter::SignalFilter (const FilterID filterID, const FilterMethodID filterMethodID, double bw, double signalLength, int n, double param, const DomainID domainID, int numIntegral = 0)
78 init (filterID, filterMethodID, bw, signalLength, n, param, domainID, numIntegral);
81 SignalFilter::SignalFilter (const char* filterName, const char* domainName, double bw, double param, int numIntegral = 0)
86 m_vecFourierCosTable = NULL;
87 m_vecFourierSinTable = NULL;
88 m_filterParam = param;
89 m_numIntegral = numIntegral;
90 m_idFilter = convertFilterNameToID (filterName);
91 if (m_idFilter == FILTER_INVALID) {
93 m_failMessage = "Invalid Filter name ";
94 m_failMessage += filterName;
97 m_idDomain = convertDomainNameToID (domainName);
98 if (m_idDomain == DOMAIN_INVALID) {
100 m_failMessage = "Invalid domain name ";
101 m_failMessage += domainName;
107 SignalFilter::init (const FilterID filterID, const FilterMethodID filterMethodID, double bw, double signalLength, int n, double param, const DomainID domainID, int numint)
110 m_idFilter = filterID;
111 m_idDomain = domainID;
112 m_idFilterMethod = filterMethodID;
113 if (m_idFilter == FILTER_INVALID || m_idDomain == DOMAIN_INVALID || m_idFilterMethod == FILTER_METHOD_INVALID) {
117 m_nameFilter = convertFilterIDToName (m_idFilter);
118 m_nameDomain = convertDomainIDToName (m_idDomain);
119 m_nameFilterMethod = convertFilterMethodIDToName (m_idFilterMethod);
122 m_nFilterPoints = 2 * m_nSignalPoints - 1;
124 m_signalLength = signalLength;
125 m_xmin = -signalLength;
126 m_xmax = signalLength;
127 m_numIntegral = numint;
128 m_filterParam = param;
129 m_vecFilter = new double[n];
130 if (m_idFilterMethod == FILTER_METHOD_FOURIER) {
131 int nFourier = n * n + 1;
132 double angleIncrement = (2. * PI) / n;
133 m_vecFourierCosTable = new double[ nFourier ];
134 m_vecFourierSinTable = new double[ nFourier ];
135 for (int i = 0; i < nFourier; i++) {
136 m_vecFourierCosTable[i] = cos (angleIncrement * i);
137 m_vecFourierSinTable[i] = sin (angleIncrement * i);
141 double xinc = (m_xmax - m_xmin) / (m_nPoints - 1);
143 if (m_idFilter == FILTER_SHEPP) {
145 double c = - 4. / (a * a);
146 int center = (m_nPoints - 1) / 2;
147 int sidelen = center;
148 m_vecFilter[center] = 4. / (a * a);
150 for (int i = 1; i <= sidelen; i++ )
151 m_vecFilter [center + i] = m_vecFilter [center - i] = c / (4 * (i * i) - 1);
152 } else if (m_idDomain == DOMAIN_FREQUENCY) {
155 for (x = m_xmin, i = 0; i < m_nPoints; x += xinc, i++)
156 m_vecFilter[i] = frequencyResponse (x, param);
157 } else if (m_idDomain == DOMAIN_SPATIAL) {
160 for (x = m_xmin, i = 0; i < m_nPoints; x += xinc, i++)
162 m_vecFilter[i] = spatialResponseAnalytic (x, param);
164 m_vecFilter[i] = spatialResponseCalc (x, param, numint);
166 m_failMessage = "Illegal domain name ";
167 m_failMessage += m_idDomain;
172 SignalFilter::~SignalFilter (void)
175 delete m_vecFourierSinTable;
176 delete m_vecFourierCosTable;
180 const SignalFilter::FilterID
181 SignalFilter::convertFilterNameToID (const char *filterName)
183 FilterID filterID = FILTER_INVALID;
185 if (strcasecmp (filterName, FILTER_BANDLIMIT_STR) == 0)
186 filterID = FILTER_BANDLIMIT;
187 else if (strcasecmp (filterName, FILTER_HAMMING_STR) == 0)
188 filterID = FILTER_G_HAMMING;
189 else if (strcasecmp (filterName, FILTER_SINC_STR) == 0)
190 filterID = FILTER_SINC;
191 else if (strcasecmp (filterName, FILTER_COS_STR) == 0)
192 filterID = FILTER_COSINE;
193 else if (strcasecmp (filterName, FILTER_TRIANGLE_STR) == 0)
194 filterID = FILTER_TRIANGLE;
195 else if (strcasecmp (filterName, FILTER_ABS_BANDLIMIT_STR) == 0)
196 filterID = FILTER_ABS_BANDLIMIT;
197 else if (strcasecmp (filterName, FILTER_ABS_HAMMING_STR) == 0)
198 filterID = FILTER_ABS_G_HAMMING;
199 else if (strcasecmp (filterName, FILTER_ABS_SINC_STR) == 0)
200 filterID = FILTER_ABS_SINC;
201 else if (strcasecmp (filterName, FILTER_ABS_COS_STR) == 0)
202 filterID = FILTER_ABS_COSINE;
203 else if (strcasecmp (filterName, FILTER_SHEPP_STR) == 0)
204 filterID = FILTER_SHEPP;
210 SignalFilter::convertFilterIDToName (const FilterID filterID)
212 const char *name = "";
214 if (filterID == FILTER_SHEPP)
215 name = FILTER_SHEPP_STR;
216 else if (filterID == FILTER_ABS_COSINE)
217 name = FILTER_ABS_COS_STR;
218 else if (filterID == FILTER_ABS_SINC)
219 name = FILTER_ABS_SINC_STR;
220 else if (filterID == FILTER_ABS_G_HAMMING)
221 name = FILTER_ABS_HAMMING_STR;
222 else if (filterID == FILTER_ABS_BANDLIMIT)
223 name = FILTER_ABS_BANDLIMIT_STR;
224 else if (filterID == FILTER_COSINE)
225 name = FILTER_COS_STR;
226 else if (filterID == FILTER_SINC)
227 name = FILTER_SINC_STR;
228 else if (filterID == FILTER_G_HAMMING)
229 name = FILTER_HAMMING_STR;
230 else if (filterID == FILTER_BANDLIMIT)
231 name = FILTER_BANDLIMIT_STR;
232 else if (filterID == FILTER_TRIANGLE)
233 name = FILTER_TRIANGLE_STR;
238 const SignalFilter::FilterMethodID
239 SignalFilter::convertFilterMethodNameToID (const char* const filterMethodName)
241 FilterMethodID fmID = FILTER_METHOD_INVALID;
243 if (strcasecmp (filterMethodName, FILTER_METHOD_CONVOLUTION_STR) == 0)
244 fmID = FILTER_METHOD_CONVOLUTION;
245 else if (strcasecmp (filterMethodName, FILTER_METHOD_FOURIER_STR) == 0)
246 fmID = FILTER_METHOD_FOURIER;
247 else if (strcasecmp (filterMethodName, FILTER_METHOD_FFT_STR) == 0)
248 fmID = FILTER_METHOD_FFT;
249 else if (strcasecmp (filterMethodName, FILTER_METHOD_FFT_ZEROPAD_2_STR) == 0)
250 fmID = FILTER_METHOD_FFT_ZEROPAD_2;
251 else if (strcasecmp (filterMethodName, FILTER_METHOD_FFT_ZEROPAD_4_STR) == 0)
252 fmID = FILTER_METHOD_FFT_ZEROPAD_4;
253 else if (strcasecmp (filterMethodName, FILTER_METHOD_FFT_ZEROPAD_6_STR) == 0)
254 fmID = FILTER_METHOD_FFT_ZEROPAD_6;
260 SignalFilter::convertFilterMethodIDToName (const FilterMethodID fmID)
262 const char *name = "";
264 if (fmID == FILTER_METHOD_CONVOLUTION)
265 return (FILTER_METHOD_CONVOLUTION_STR);
266 else if (fmID == FILTER_METHOD_FOURIER)
267 return (FILTER_METHOD_FOURIER_STR);
268 else if (fmID == FILTER_METHOD_FFT)
269 return (FILTER_METHOD_FFT_STR);
270 else if (fmID == FILTER_METHOD_FFT_ZEROPAD_2)
271 return (FILTER_METHOD_FFT_ZEROPAD_2_STR);
272 else if (fmID == FILTER_METHOD_FFT_ZEROPAD_4)
273 return (FILTER_METHOD_FFT_ZEROPAD_4_STR);
274 else if (fmID == FILTER_METHOD_FFT_ZEROPAD_6)
275 return (FILTER_METHOD_FFT_ZEROPAD_6_STR);
280 const SignalFilter::DomainID
281 SignalFilter::convertDomainNameToID (const char* const domainName)
283 DomainID dID = DOMAIN_INVALID;
285 if (strcasecmp (domainName, DOMAIN_SPATIAL_STR) == 0)
286 dID = DOMAIN_SPATIAL;
287 else if (strcasecmp (domainName, DOMAIN_FREQUENCY_STR) == 0)
288 dID = DOMAIN_FREQUENCY;
294 SignalFilter::convertDomainIDToName (const DomainID domain)
296 const char *name = "";
298 if (domain == DOMAIN_SPATIAL)
299 return (DOMAIN_SPATIAL_STR);
300 else if (domain == DOMAIN_FREQUENCY)
301 return (DOMAIN_FREQUENCY_STR);
308 SignalFilter::filterSignal (const double input[], double output[], double dx, const int n) const
310 if (m_idFilterMethod == FILTER_METHOD_CONVOLUTION) {
311 for (int i = 0; i < n; i++)
312 output[i] = convolve (input, dx, i, n);
313 } else if (m_idFilterMethod == FILTER_METHOD_FOURIER) {
314 complex<double> fftSignal[n];
315 complex<double> complexOutput;
316 finiteFourierTransform (input, fftSignal, 1);
317 finiteFourierTransform (fftSignal, complexOutput, -1);
318 for (int i = 0; i < n; i++)
319 output[i] = complexOutput[i].mag();
324 SignalFilter::filterSignal (const float input[], double output[], double dx, const int n) const
326 if (m_idFilterMethod == FILTER_METHOD_CONVOLUTION) {
327 for (int i = 0; i < n; i++)
328 output[i] = convolve (input, dx, i, n);
334 SignalFilter::response (double x)
338 if (m_idDomain == DOMAIN_SPATIAL)
339 response = spatialResponse (m_idFilter, m_bw, x, m_filterParam, m_numIntegral);
340 else if (m_idDomain == DOMAIN_FREQUENCY)
341 response = frequencyResponse (m_idFilter, m_bw, x, m_filterParam);
348 SignalFilter::spatialResponse (FilterID filterID, double bw, double x, double param, int nIntegral = 0)
351 return spatialResponseAnalytic (filterID, bw, x, param);
353 return spatialResponseCalc (filterID, bw, x, param, nIntegral);
357 * filter_spatial_response_calc Calculate filter by discrete inverse fourier
358 * transform of filters's frequency
362 * y = filter_spatial_response_calc (filt_type, x, m_bw, param, n)
363 * double y Filter's response in spatial domain
364 * int filt_type Type of filter (definitions in ct.h)
365 * double x Spatial position to evaluate filter
366 * double m_bw Bandwidth of window
367 * double param General parameter for various filters
368 * int n Number of points to calculate integrations
372 SignalFilter::spatialResponseCalc (double x, double param, int nIntegral) const
374 return (spatialResponseCalc (m_idFilter, m_bw, x, param, nIntegral));
378 SignalFilter::spatialResponseCalc (FilterID filterID, double bw, double x, double param, int n)
382 if (filterID == FILTER_TRIANGLE) {
389 double zinc = (zmax - zmin) / (n - 1);
393 for (int i = 0; i < n; i++, z += zinc)
394 q[i] = frequencyResponse (filterID, bw, z, param) * cos (TWOPI * z * x);
396 double y = 2 * integrateSimpson (zmin, zmax, q, n);
403 * filter_frequency_response Return filter frequency response
406 * h = filter_frequency_response (filt_type, u, m_bw, param)
407 * double h Filters frequency response at u
408 * int filt_type Type of filter
409 * double u Frequency to evaluate filter at
410 * double m_bw Bandwidth of filter
411 * double param General input parameter for various filters
415 SignalFilter::frequencyResponse (double u, double param) const
417 return frequencyResponse (m_idFilter, m_bw, u, param);
422 SignalFilter::frequencyResponse (FilterID filterID, double bw, double u, double param)
425 double au = fabs (u);
428 case FILTER_BANDLIMIT:
434 case FILTER_ABS_BANDLIMIT:
440 case FILTER_TRIANGLE:
450 q = cos(PI * u / bw);
452 case FILTER_ABS_COSINE:
456 q = au * cos(PI * u / bw);
459 q = bw * sinc (PI * bw * u, 1.);
461 case FILTER_ABS_SINC:
462 q = au * bw * sinc (PI * bw * u, 1.);
464 case FILTER_G_HAMMING:
468 q = param + (1 - param) * cos (TWOPI * u / bw);
470 case FILTER_ABS_G_HAMMING:
474 q = au * (param + (1 - param) * cos(TWOPI * u / bw));
478 sys_error (ERR_WARNING, "Frequency response for filter %d not implemented [filter_frequency_response]", filterID);
487 * filter_spatial_response_analytic Calculate filter by analytic inverse fourier
488 * transform of filters's frequency
492 * y = filter_spatial_response_analytic (filt_type, x, m_bw, param)
493 * double y Filter's response in spatial domain
494 * int filt_type Type of filter (definitions in ct.h)
495 * double x Spatial position to evaluate filter
496 * double m_bw Bandwidth of window
497 * double param General parameter for various filters
501 SignalFilter::spatialResponseAnalytic (double x, double param) const
503 return spatialResponseAnalytic (m_idFilter, m_bw, x, param);
507 SignalFilter::spatialResponseAnalytic (FilterID filterID, double bw, double x, double param)
510 double u = TWOPI * x;
513 double b2 = TWOPI / bw;
516 case FILTER_BANDLIMIT:
517 q = bw * sinc(u * w, 1.0);
519 case FILTER_TRIANGLE:
520 temp = sinc (u * w, 1.0);
521 q = bw * temp * temp;
524 q = sinc(b-u,w) + sinc(b+u,w);
526 case FILTER_G_HAMMING:
527 q = 2 * param * sin(u*w)/u + (1-param) * (sinc(b2-u, w) + sinc(b2+u, w));
529 case FILTER_ABS_BANDLIMIT:
530 q = 2 * integral_abscos (u, w);
532 case FILTER_ABS_COSINE:
533 q = integral_abscos(b-u,w) + integral_abscos(b+u,w);
535 case FILTER_ABS_G_HAMMING:
536 q = 2 * param * integral_abscos(u,w) +
537 (1-param)*(integral_abscos(u-b2,w)+integral_abscos(u+b2,w));
541 q = 4. / (PI * bw * bw);
543 q = fabs ((2 / bw) * sin (u * w)) * sinc (u * w, 1.) * sinc (u * w, 1.);
546 if (fabs (x) < bw / 2)
551 case FILTER_ABS_SINC:
553 sys_error (ERR_WARNING, "Analytic filter type %d not implemented [filter_spatial_response_analytic]", filterID);
563 * sinc Return sin(x)/x function
567 * double v sinc value
571 * v = sin(x * mult) / x;
576 * integral_abscos Returns integral of u*cos(u)
579 * q = integral_abscos (u, w)
580 * double q Integral value
581 * double u Integration variable
582 * double w Upper integration boundary
585 * Returns the value of integral of u*cos(u)*dV for V = 0 to w
589 SignalFilter::integral_abscos (double u, double w)
591 return (fabs (u) > F_EPSILON
592 ? (cos(u * w) - 1) / (u * u) + w / u * sin (u * w)
598 * convolve Discrete convolution of two functions
601 * r = convolve (f1, f2, dx, n, np, func_type)
602 * double r Convolved result
603 * double f1[], f2[] Functions to be convolved
604 * double dx Difference between successive x values
605 * int n Array index to center convolution about
606 * int np Number of points in f1 array
607 * int func_type EVEN or ODD or EVEN_AND_ODD function f2
610 * f1 is the projection data, its indices range from 0 to np - 1.
611 * The index for f2, the filter, ranges from -(np-1) to (np-1).
612 * There are 3 ways to handle the negative vertices of f2:
613 * 1. If we know f2 is an EVEN function, then f2[-n] = f2[n].
614 * All filters used in reconstruction are even.
615 * 2. If we know f2 is an ODD function, then f2[-n] = -f2[n]
616 * 3. If f2 is both ODD AND EVEN, then we must store the value of f2
617 * for negative indices. Since f2 must range from -(np-1) to (np-1),
618 * if we add (np - 1) to f2's array index, then f2's index will
619 * range from 0 to 2 * (np - 1), and the origin, x = 0, will be
620 * stored at f2[np-1].
624 SignalFilter::convolve (const double func[], const double dx, const int n, const int np) const
628 #if UNOPTIMIZED_CONVOLUTION
629 for (int i = 0; i < np; i++)
630 sum += func[i] * m_vecFilter[n - i + (np - 1)];
632 double* f2 = m_vecFilter + n + (np - 1);
633 for (int i = 0; i < np; i++)
634 sum += *func++ * *f2--;
642 SignalFilter::convolve (const float func[], const double dx, const int n, const int np) const
646 #if UNOPTIMIZED_CONVOLUTION
647 for (int i = 0; i < np; i++)
648 sum += func[i] * m_vecFilter[n - i + (np - 1)];
650 double* f2 = m_vecFilter + n + (np - 1);
651 for (int i = 0; i < np; i++)
652 sum += *func++ * *f2--;
660 SignalFilter::finiteFourierTransform (const double input[], complex<double> output[], const int n, int direction)
667 double angleIncrement = 2 * PI / n;
668 for (int i = 0; i < n; i++) {
671 for (int j = 0; j < n; j++) {
672 double angle = i * j * angleIncrement * direction;
673 sumReal += input[i] * cos(angle);
674 sumImag += input[i] * sin(angle);
680 output[i] = complex<double> (sumReal, sumImag);
685 SignalFilter::finiteFourierTransform (const double input[], complex<double> output[], int direction) const
692 double angleIncrement = 2 * PI / m_nPoints;
693 for (int i = 0; i < m_nPoints; i++) {
694 double sumReal = 0, sumImag = 0;
695 for (int j = 0; j < m_nPoints; j++) {
696 int tableIndex = i * j;
698 sumReal += input[i] * m_vecFourierCosTable[tableIndex];
699 sumImag += input[i] * m_vecFourierSinTable[tableIndex];
701 sumReal += input[i] * m_vecFourierCosTable[tableIndex];
702 sumImag -= input[i] * m_vecFourierSinTable[tableIndex];
706 sumReal /= m_nPoints;
707 sumImag /= m_nPoints;
709 output[i] = complex<double> (sumReal, sumImag);