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.7 2000/07/03 11:02:06 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, filterMin, filterMax, 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 filterMin, filterMax 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 signalIncrement, 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, signalIncrement, n, param, m_idDomain, numIntegral);
76 SignalFilter::SignalFilter (const FilterID filterID, const FilterMethodID filterMethodID, double bw, double signalIncrement, int n, double param, const DomainID domainID, int numIntegral = 0)
78 init (filterID, filterMethodID, bw, signalIncrement, n, param, domainID, numIntegral);
81 SignalFilter::SignalFilter (const char* filterName, const char* domainName, double bw, double param, int numIntegral = 0)
87 m_vecFourierCosTable = NULL;
88 m_vecFourierSinTable = NULL;
89 m_filterParam = param;
90 m_numIntegral = numIntegral;
91 m_idFilter = convertFilterNameToID (filterName);
92 if (m_idFilter == FILTER_INVALID) {
94 m_failMessage = "Invalid Filter name ";
95 m_failMessage += filterName;
98 m_idDomain = convertDomainNameToID (domainName);
99 if (m_idDomain == DOMAIN_INVALID) {
101 m_failMessage = "Invalid domain name ";
102 m_failMessage += domainName;
108 SignalFilter::init (const FilterID filterID, const FilterMethodID filterMethodID, double bw, double signalIncrement, int n, double param, const DomainID domainID, int numint)
111 m_idFilter = filterID;
112 m_idDomain = domainID;
113 m_idFilterMethod = filterMethodID;
114 if (m_idFilter == FILTER_INVALID || m_idDomain == DOMAIN_INVALID || m_idFilterMethod == FILTER_METHOD_INVALID) {
118 m_nameFilter = convertFilterIDToName (m_idFilter);
119 m_nameDomain = convertDomainIDToName (m_idDomain);
120 m_nameFilterMethod = convertFilterMethodIDToName (m_idFilterMethod);
123 m_nFilterPoints = 2 * m_nSignalPoints - 1;
125 m_signalInc = signalIncrement;
126 m_filterMin = -signalIncrement * (m_nSignalPoints - 1);
127 m_filterMax = signalIncrement * (m_nSignalPoints - 1);
128 m_filterInc = (m_filterMax - m_filterMin) / (m_nFilterPoints - 1);
129 m_numIntegral = numint;
130 m_filterParam = param;
131 m_vecFilter = new double[ m_nFilterPoints ];
132 if (m_idFilterMethod == FILTER_METHOD_FOURIER) {
133 int nFourier = n * n + 1;
134 double angleIncrement = (2. * PI) / n;
135 m_vecFourierCosTable = new double[ nFourier ];
136 m_vecFourierSinTable = new double[ nFourier ];
137 for (int i = 0; i < nFourier; i++) {
138 m_vecFourierCosTable[i] = cos (angleIncrement * i);
139 m_vecFourierSinTable[i] = sin (angleIncrement * i);
143 if (m_idFilter == FILTER_SHEPP) {
145 double c = - 4. / (a * a);
146 int center = (m_nFilterPoints - 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_filterMin, i = 0; i < m_nFilterPoints; x += m_filterInc, i++)
156 m_vecFilter[i] = frequencyResponse (x, param);
157 } else if (m_idDomain == DOMAIN_SPATIAL) {
160 for (x = m_filterMin, i = 0; i < m_nFilterPoints; x += m_filterInc, 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 float input[], double output[]) const
310 if (m_idFilterMethod == FILTER_METHOD_CONVOLUTION) {
311 for (int i = 0; i < m_nSignalPoints; i++)
312 output[i] = convolve (input, m_signalInc, i, m_nSignalPoints);
313 } else if (m_idFilterMethod == FILTER_METHOD_FOURIER) {
314 complex<double> fftSignal[m_nSignalPoints];
315 complex<double> complexOutput;
316 finiteFourierTransform (input, fftSignal, 1);
317 // finiteFourierTransform (fftSignal, complexOutput, -1);
318 // for (int i = 0; i < m_nSignalPoints; i++)
319 // output[i] = complexOutput[i].hypot();
324 SignalFilter::response (double x)
328 if (m_idDomain == DOMAIN_SPATIAL)
329 response = spatialResponse (m_idFilter, m_bw, x, m_filterParam, m_numIntegral);
330 else if (m_idDomain == DOMAIN_FREQUENCY)
331 response = frequencyResponse (m_idFilter, m_bw, x, m_filterParam);
338 SignalFilter::spatialResponse (FilterID filterID, double bw, double x, double param, int nIntegral = 0)
341 return spatialResponseAnalytic (filterID, bw, x, param);
343 return spatialResponseCalc (filterID, bw, x, param, nIntegral);
347 * filter_spatial_response_calc Calculate filter by discrete inverse fourier
348 * transform of filters's frequency
352 * y = filter_spatial_response_calc (filt_type, x, m_bw, param, n)
353 * double y Filter's response in spatial domain
354 * int filt_type Type of filter (definitions in ct.h)
355 * double x Spatial position to evaluate filter
356 * double m_bw Bandwidth of window
357 * double param General parameter for various filters
358 * int n Number of points to calculate integrations
362 SignalFilter::spatialResponseCalc (double x, double param, int nIntegral) const
364 return (spatialResponseCalc (m_idFilter, m_bw, x, param, nIntegral));
368 SignalFilter::spatialResponseCalc (FilterID filterID, double bw, double x, double param, int n)
372 if (filterID == FILTER_TRIANGLE) {
379 double zinc = (zmax - zmin) / (n - 1);
383 for (int i = 0; i < n; i++, z += zinc)
384 q[i] = frequencyResponse (filterID, bw, z, param) * cos (TWOPI * z * x);
386 double y = 2 * integrateSimpson (zmin, zmax, q, n);
393 * filter_frequency_response Return filter frequency response
396 * h = filter_frequency_response (filt_type, u, m_bw, param)
397 * double h Filters frequency response at u
398 * int filt_type Type of filter
399 * double u Frequency to evaluate filter at
400 * double m_bw Bandwidth of filter
401 * double param General input parameter for various filters
405 SignalFilter::frequencyResponse (double u, double param) const
407 return frequencyResponse (m_idFilter, m_bw, u, param);
412 SignalFilter::frequencyResponse (FilterID filterID, double bw, double u, double param)
415 double au = fabs (u);
418 case FILTER_BANDLIMIT:
424 case FILTER_ABS_BANDLIMIT:
430 case FILTER_TRIANGLE:
440 q = cos(PI * u / bw);
442 case FILTER_ABS_COSINE:
446 q = au * cos(PI * u / bw);
449 q = bw * sinc (PI * bw * u, 1.);
451 case FILTER_ABS_SINC:
452 q = au * bw * sinc (PI * bw * u, 1.);
454 case FILTER_G_HAMMING:
458 q = param + (1 - param) * cos (TWOPI * u / bw);
460 case FILTER_ABS_G_HAMMING:
464 q = au * (param + (1 - param) * cos(TWOPI * u / bw));
468 sys_error (ERR_WARNING, "Frequency response for filter %d not implemented [filter_frequency_response]", filterID);
477 * filter_spatial_response_analytic Calculate filter by analytic inverse fourier
478 * transform of filters's frequency
482 * y = filter_spatial_response_analytic (filt_type, x, m_bw, param)
483 * double y Filter's response in spatial domain
484 * int filt_type Type of filter (definitions in ct.h)
485 * double x Spatial position to evaluate filter
486 * double m_bw Bandwidth of window
487 * double param General parameter for various filters
491 SignalFilter::spatialResponseAnalytic (double x, double param) const
493 return spatialResponseAnalytic (m_idFilter, m_bw, x, param);
497 SignalFilter::spatialResponseAnalytic (FilterID filterID, double bw, double x, double param)
500 double u = TWOPI * x;
503 double b2 = TWOPI / bw;
506 case FILTER_BANDLIMIT:
507 q = bw * sinc(u * w, 1.0);
509 case FILTER_TRIANGLE:
510 temp = sinc (u * w, 1.0);
511 q = bw * temp * temp;
514 q = sinc(b-u,w) + sinc(b+u,w);
516 case FILTER_G_HAMMING:
517 q = 2 * param * sin(u*w)/u + (1-param) * (sinc(b2-u, w) + sinc(b2+u, w));
519 case FILTER_ABS_BANDLIMIT:
520 q = 2 * integral_abscos (u, w);
522 case FILTER_ABS_COSINE:
523 q = integral_abscos(b-u,w) + integral_abscos(b+u,w);
525 case FILTER_ABS_G_HAMMING:
526 q = 2 * param * integral_abscos(u,w) +
527 (1-param)*(integral_abscos(u-b2,w)+integral_abscos(u+b2,w));
531 q = 4. / (PI * bw * bw);
533 q = fabs ((2 / bw) * sin (u * w)) * sinc (u * w, 1.) * sinc (u * w, 1.);
536 if (fabs (x) < bw / 2)
541 case FILTER_ABS_SINC:
543 sys_error (ERR_WARNING, "Analytic filter type %d not implemented [filter_spatial_response_analytic]", filterID);
553 * sinc Return sin(x)/x function
557 * double v sinc value
561 * v = sin(x * mult) / x;
566 * integral_abscos Returns integral of u*cos(u)
569 * q = integral_abscos (u, w)
570 * double q Integral value
571 * double u Integration variable
572 * double w Upper integration boundary
575 * Returns the value of integral of u*cos(u)*dV for V = 0 to w
579 SignalFilter::integral_abscos (double u, double w)
581 return (fabs (u) > F_EPSILON
582 ? (cos(u * w) - 1) / (u * u) + w / u * sin (u * w)
588 * convolve Discrete convolution of two functions
591 * r = convolve (f1, f2, dx, n, np, func_type)
592 * double r Convolved result
593 * double f1[], f2[] Functions to be convolved
594 * double dx Difference between successive x values
595 * int n Array index to center convolution about
596 * int np Number of points in f1 array
597 * int func_type EVEN or ODD or EVEN_AND_ODD function f2
600 * f1 is the projection data, its indices range from 0 to np - 1.
601 * The index for f2, the filter, ranges from -(np-1) to (np-1).
602 * There are 3 ways to handle the negative vertices of f2:
603 * 1. If we know f2 is an EVEN function, then f2[-n] = f2[n].
604 * All filters used in reconstruction are even.
605 * 2. If we know f2 is an ODD function, then f2[-n] = -f2[n]
606 * 3. If f2 is both ODD AND EVEN, then we must store the value of f2
607 * for negative indices. Since f2 must range from -(np-1) to (np-1),
608 * if we add (np - 1) to f2's array index, then f2's index will
609 * range from 0 to 2 * (np - 1), and the origin, x = 0, will be
610 * stored at f2[np-1].
614 SignalFilter::convolve (const double func[], const double dx, const int n, const int np) const
618 #if UNOPTIMIZED_CONVOLUTION
619 for (int i = 0; i < np; i++)
620 sum += func[i] * m_vecFilter[n - i + (np - 1)];
622 double* f2 = m_vecFilter + n + (np - 1);
623 for (int i = 0; i < np; i++)
624 sum += *func++ * *f2--;
632 SignalFilter::convolve (const float func[], const double dx, const int n, const int np) const
636 #if UNOPTIMIZED_CONVOLUTION
637 for (int i = 0; i < np; i++)
638 sum += func[i] * m_vecFilter[n - i + (np - 1)];
640 double* f2 = m_vecFilter + n + (np - 1);
641 for (int i = 0; i < np; i++)
642 sum += *func++ * *f2--;
650 SignalFilter::finiteFourierTransform (const float input[], complex<double> output[], const int n, int direction)
657 double angleIncrement = 2 * PI / n;
658 for (int i = 0; i < n; i++) {
661 for (int j = 0; j < n; j++) {
662 double angle = i * j * angleIncrement * direction;
663 sumReal += input[i] * cos(angle);
664 sumImag += input[i] * sin(angle);
670 output[i] = complex<double> (sumReal, sumImag);
675 SignalFilter::finiteFourierTransform (const float input[], complex<double> output[], int direction) const
682 double angleIncrement = 2 * PI / m_nSignalPoints;
683 for (int i = 0; i < m_nSignalPoints; i++) {
684 double sumReal = 0, sumImag = 0;
685 for (int j = 0; j < m_nSignalPoints; j++) {
686 int tableIndex = i * j;
688 sumReal += input[i] * m_vecFourierCosTable[tableIndex];
689 sumImag += input[i] * m_vecFourierSinTable[tableIndex];
691 sumReal += input[i] * m_vecFourierCosTable[tableIndex];
692 sumImag -= input[i] * m_vecFourierSinTable[tableIndex];
696 sumReal /= m_nSignalPoints;
697 sumImag /= m_nSignalPoints;
699 output[i] = complex<double> (sumReal, sumImag);