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.18 2000/07/15 08:36:13 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 ******************************************************************************/
31 int SignalFilter::N_INTEGRAL=500; //static member
34 * SignalFilter::SignalFilter Construct a signal
37 * f = SignalFilter (filt_type, bw, filterMin, filterMax, n, param, domain, analytic)
38 * double f Generated filter vector
39 * int filt_type Type of filter wanted
40 * double bw Bandwidth of filter
41 * double filterMin, filterMax Filter limits
42 * int nSignalPoints Number of points in signal
43 * double param General input parameter to filters
44 * int domain FREQUENCY or SPATIAL domain wanted
47 SignalFilter::SignalFilter (const char* filterName, const char* filterMethodName, double bw, double signalIncrement, int nSignalPoints, double param, const char* domainName, int zeropad = 0, int preinterpolationFactor = 1)
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, nSignalPoints, param, m_idDomain, zeropad, preinterpolationFactor);
76 SignalFilter::SignalFilter (const FilterID filterID, const FilterMethodID filterMethodID, double bw, double signalIncrement, int nSignalPoints, double param, const DomainID domainID, int zeropad = 0, int preinterpolationFactor = 1)
78 init (filterID, filterMethodID, bw, signalIncrement, nSignalPoints, param, domainID, zeropad, preinterpolationFactor);
81 SignalFilter::SignalFilter (const char* filterName, const char* domainName, double bw, double param)
87 m_vecFourierCosTable = NULL;
88 m_vecFourierSinTable = NULL;
89 m_filterParam = param;
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 signalIncrement, int nSignalPoints, double filterParam, const DomainID domainID, int zeropad, int preinterpolationFactor)
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_traceLevel = TRACE_NONE;
118 m_nameFilter = convertFilterIDToName (m_idFilter);
119 m_nameDomain = convertDomainIDToName (m_idDomain);
120 m_nameFilterMethod = convertFilterMethodIDToName (m_idFilterMethod);
122 m_nSignalPoints = nSignalPoints;
123 m_signalInc = signalIncrement;
124 m_filterParam = filterParam;
126 m_preinterpolationFactor = preinterpolationFactor;
128 m_vecFourierCosTable = NULL;
129 m_vecFourierSinTable = NULL;
132 if (m_idFilterMethod == FILTER_METHOD_FFT) {
134 m_idFilterMethod = FILTER_METHOD_RFFTW;
137 m_failMessage = "FFT not yet implemented";
142 if (m_idFilterMethod == FILTER_METHOD_FOURIER || m_idFilterMethod == FILTER_METHOD_FOURIER_TABLE || m_idFilterMethod == FILTER_METHOD_FFT
144 || m_idFilterMethod == FILTER_METHOD_FFTW || m_idFilterMethod == FILTER_METHOD_RFFTW
147 m_nFilterPoints = m_nSignalPoints;
149 double logBase2 = log(m_nSignalPoints) / log(2);
150 int nextPowerOf2 = static_cast<int>(floor(logBase2));
151 if (logBase2 != floor(logBase2))
153 nextPowerOf2 += (m_zeropad - 1);
154 m_nFilterPoints = 1 << nextPowerOf2;
155 if (m_traceLevel >= TRACE_TEXT)
156 cout << "nFilterPoints = " << m_nFilterPoints << endl;
158 m_nOutputPoints = m_nFilterPoints * m_preinterpolationFactor;
159 m_filterMin = -1. / (2 * m_signalInc);
160 m_filterMax = 1. / (2 * m_signalInc);
161 m_filterInc = (m_filterMax - m_filterMin) / m_nFilterPoints;
162 m_vecFilter = new double [m_nFilterPoints];
163 int halfFilter = m_nFilterPoints / 2;
164 for (int i = 0; i <= halfFilter; i++)
165 m_vecFilter[i] = static_cast<double>(i) / halfFilter/ (2. * m_signalInc);
166 for (int i = 1; i <= halfFilter; i++)
167 m_vecFilter[m_nFilterPoints - i] = static_cast<double>(i) / halfFilter / (2. * m_signalInc);
170 // precalculate sin and cosine tables for fourier transform
171 if (m_idFilterMethod == FILTER_METHOD_FOURIER_TABLE) {
172 int nFourier = max(m_nFilterPoints,m_nOutputPoints) * max(m_nFilterPoints, m_nOutputPoints) + 1;
173 double angleIncrement = (2. * PI) / m_nFilterPoints;
174 m_vecFourierCosTable = new double[ nFourier ];
175 m_vecFourierSinTable = new double[ nFourier ];
177 for (int i = 0; i < nFourier; i++) {
178 m_vecFourierCosTable[i] = cos (angle);
179 m_vecFourierSinTable[i] = sin (angle);
180 angle += angleIncrement;
185 if (m_idFilterMethod == FILTER_METHOD_FFTW || m_idFilterMethod == FILTER_METHOD_RFFTW) {
186 for (int i = 0; i < m_nFilterPoints; i++) //fftw uses unnormalized fft
187 m_vecFilter[i] /= m_nFilterPoints;
190 if (m_idFilterMethod == FILTER_METHOD_RFFTW) {
191 m_realPlanForward = rfftw_create_plan (m_nFilterPoints, FFTW_REAL_TO_COMPLEX, FFTW_ESTIMATE);
192 m_realPlanBackward = rfftw_create_plan (m_nOutputPoints, FFTW_COMPLEX_TO_REAL, FFTW_ESTIMATE);
193 m_vecRealFftInput = new fftw_real [ m_nFilterPoints ];
194 m_vecRealFftSignal = new fftw_real [ m_nOutputPoints ];
195 for (int i = 0; i < m_nFilterPoints; i++)
196 m_vecRealFftInput[i] = 0;
197 } else if (m_idFilterMethod == FILTER_METHOD_FFTW) {
198 m_complexPlanForward = fftw_create_plan (m_nFilterPoints, FFTW_FORWARD, FFTW_ESTIMATE);
199 m_complexPlanBackward = fftw_create_plan (m_nOutputPoints, FFTW_BACKWARD, FFTW_ESTIMATE);
200 m_vecComplexFftInput = new fftw_complex [ m_nFilterPoints ];
201 m_vecComplexFftSignal = new fftw_complex [ m_nOutputPoints ];
202 for (int i = 0; i < m_nFilterPoints; i++)
203 m_vecComplexFftInput[i].re = m_vecComplexFftInput[i].im = 0;
204 for (int i = 0; i < m_nOutputPoints; i++)
205 m_vecComplexFftSignal[i].re = m_vecComplexFftSignal[i].im = 0;
209 if (m_idFilterMethod == FILTER_METHOD_CONVOLUTION) {
210 m_nFilterPoints = 2 * m_nSignalPoints - 1;
211 m_filterMin = -m_signalInc * (m_nSignalPoints - 1);
212 m_filterMax = m_signalInc * (m_nSignalPoints - 1);
213 m_filterInc = (m_filterMax - m_filterMin) / (m_nFilterPoints - 1);
214 m_vecFilter = new double[ m_nFilterPoints ];
216 if (m_idFilter == FILTER_SHEPP) {
218 double c = - 4. / (a * a);
219 int center = (m_nFilterPoints - 1) / 2;
220 int sidelen = center;
221 m_vecFilter[center] = 4. / (a * a);
223 for (int i = 1; i <= sidelen; i++ )
224 m_vecFilter [center + i] = m_vecFilter [center - i] = c / (4 * (i * i) - 1);
225 } else if (m_idDomain == DOMAIN_FREQUENCY) {
228 for (x = m_filterMin, i = 0; i < m_nFilterPoints; x += m_filterInc, i++)
229 m_vecFilter[i] = frequencyResponse (x, m_filterParam);
230 } else if (m_idDomain == DOMAIN_SPATIAL) {
233 for (x = m_filterMin, i = 0; i < m_nFilterPoints; x += m_filterInc, i++)
234 if (haveAnalyticSpatial(m_idFilter))
235 m_vecFilter[i] = spatialResponseAnalytic (x, m_filterParam);
237 m_vecFilter[i] = spatialResponseCalc (x, m_filterParam);
239 m_failMessage = "Illegal domain name ";
240 m_failMessage += m_idDomain;
246 SignalFilter::~SignalFilter (void)
248 delete [] m_vecFilter;
249 delete [] m_vecFourierSinTable;
250 delete [] m_vecFourierCosTable;
253 if (m_idFilterMethod == FILTER_METHOD_FFTW) {
254 fftw_destroy_plan(m_complexPlanForward);
255 fftw_destroy_plan(m_complexPlanBackward);
256 delete [] m_vecComplexFftInput;
257 delete [] m_vecComplexFftSignal;
259 if (m_idFilterMethod == FILTER_METHOD_RFFTW) {
260 rfftw_destroy_plan(m_realPlanForward);
261 rfftw_destroy_plan(m_realPlanBackward);
262 delete [] m_vecRealFftInput;
263 delete [] m_vecRealFftSignal;
269 const SignalFilter::FilterID
270 SignalFilter::convertFilterNameToID (const char *filterName)
272 FilterID filterID = FILTER_INVALID;
274 if (strcasecmp (filterName, FILTER_BANDLIMIT_STR) == 0)
275 filterID = FILTER_BANDLIMIT;
276 else if (strcasecmp (filterName, FILTER_HAMMING_STR) == 0)
277 filterID = FILTER_G_HAMMING;
278 else if (strcasecmp (filterName, FILTER_SINC_STR) == 0)
279 filterID = FILTER_SINC;
280 else if (strcasecmp (filterName, FILTER_COS_STR) == 0)
281 filterID = FILTER_COSINE;
282 else if (strcasecmp (filterName, FILTER_TRIANGLE_STR) == 0)
283 filterID = FILTER_TRIANGLE;
284 else if (strcasecmp (filterName, FILTER_ABS_BANDLIMIT_STR) == 0)
285 filterID = FILTER_ABS_BANDLIMIT;
286 else if (strcasecmp (filterName, FILTER_ABS_HAMMING_STR) == 0)
287 filterID = FILTER_ABS_G_HAMMING;
288 else if (strcasecmp (filterName, FILTER_ABS_SINC_STR) == 0)
289 filterID = FILTER_ABS_SINC;
290 else if (strcasecmp (filterName, FILTER_ABS_COS_STR) == 0)
291 filterID = FILTER_ABS_COSINE;
292 else if (strcasecmp (filterName, FILTER_SHEPP_STR) == 0)
293 filterID = FILTER_SHEPP;
299 SignalFilter::convertFilterIDToName (const FilterID filterID)
301 const char *name = "";
303 if (filterID == FILTER_SHEPP)
304 name = FILTER_SHEPP_STR;
305 else if (filterID == FILTER_ABS_COSINE)
306 name = FILTER_ABS_COS_STR;
307 else if (filterID == FILTER_ABS_SINC)
308 name = FILTER_ABS_SINC_STR;
309 else if (filterID == FILTER_ABS_G_HAMMING)
310 name = FILTER_ABS_HAMMING_STR;
311 else if (filterID == FILTER_ABS_BANDLIMIT)
312 name = FILTER_ABS_BANDLIMIT_STR;
313 else if (filterID == FILTER_COSINE)
314 name = FILTER_COS_STR;
315 else if (filterID == FILTER_SINC)
316 name = FILTER_SINC_STR;
317 else if (filterID == FILTER_G_HAMMING)
318 name = FILTER_HAMMING_STR;
319 else if (filterID == FILTER_BANDLIMIT)
320 name = FILTER_BANDLIMIT_STR;
321 else if (filterID == FILTER_TRIANGLE)
322 name = FILTER_TRIANGLE_STR;
327 const SignalFilter::FilterMethodID
328 SignalFilter::convertFilterMethodNameToID (const char* const filterMethodName)
330 FilterMethodID fmID = FILTER_METHOD_INVALID;
332 if (strcasecmp (filterMethodName, FILTER_METHOD_CONVOLUTION_STR) == 0)
333 fmID = FILTER_METHOD_CONVOLUTION;
334 else if (strcasecmp (filterMethodName, FILTER_METHOD_FOURIER_STR) == 0)
335 fmID = FILTER_METHOD_FOURIER;
336 else if (strcasecmp (filterMethodName, FILTER_METHOD_FOURIER_TABLE_STR) == 0)
337 fmID = FILTER_METHOD_FOURIER_TABLE;
338 else if (strcasecmp (filterMethodName, FILTER_METHOD_FFT_STR) == 0)
339 fmID = FILTER_METHOD_FFT;
341 else if (strcasecmp (filterMethodName, FILTER_METHOD_FFTW_STR) == 0)
342 fmID = FILTER_METHOD_FFTW;
343 else if (strcasecmp (filterMethodName, FILTER_METHOD_RFFTW_STR) == 0)
344 fmID = FILTER_METHOD_RFFTW;
351 SignalFilter::convertFilterMethodIDToName (const FilterMethodID fmID)
353 const char *name = "";
355 if (fmID == FILTER_METHOD_CONVOLUTION)
356 return (FILTER_METHOD_CONVOLUTION_STR);
357 else if (fmID == FILTER_METHOD_FOURIER)
358 return (FILTER_METHOD_FOURIER_STR);
359 else if (fmID == FILTER_METHOD_FOURIER_TABLE)
360 return (FILTER_METHOD_FOURIER_TABLE_STR);
361 else if (fmID == FILTER_METHOD_FFT)
362 return (FILTER_METHOD_FFT_STR);
364 else if (fmID == FILTER_METHOD_FFTW)
365 return (FILTER_METHOD_FFTW_STR);
366 else if (fmID == FILTER_METHOD_RFFTW)
367 return (FILTER_METHOD_RFFTW_STR);
373 const SignalFilter::DomainID
374 SignalFilter::convertDomainNameToID (const char* const domainName)
376 DomainID dID = DOMAIN_INVALID;
378 if (strcasecmp (domainName, DOMAIN_SPATIAL_STR) == 0)
379 dID = DOMAIN_SPATIAL;
380 else if (strcasecmp (domainName, DOMAIN_FREQUENCY_STR) == 0)
381 dID = DOMAIN_FREQUENCY;
387 SignalFilter::convertDomainIDToName (const DomainID domain)
389 const char *name = "";
391 if (domain == DOMAIN_SPATIAL)
392 return (DOMAIN_SPATIAL_STR);
393 else if (domain == DOMAIN_FREQUENCY)
394 return (DOMAIN_FREQUENCY_STR);
400 SignalFilter::filterSignal (const float input[], double output[]) const
402 if (m_idFilterMethod == FILTER_METHOD_CONVOLUTION) {
403 for (int i = 0; i < m_nSignalPoints; i++)
404 output[i] = convolve (input, m_signalInc, i, m_nSignalPoints);
405 } else if (m_idFilterMethod == FILTER_METHOD_FOURIER) {
406 double inputSignal[m_nFilterPoints];
407 for (int i = 0; i < m_nSignalPoints; i++)
408 inputSignal[i] = input[i];
409 for (int i = m_nSignalPoints; i < m_nFilterPoints; i++)
410 inputSignal[i] = 0; // zeropad
411 complex<double> fftSignal[m_nFilterPoints];
412 finiteFourierTransform (inputSignal, fftSignal, m_nFilterPoints, -1);
413 for (int i = 0; i < m_nFilterPoints; i++)
414 fftSignal[i] *= m_vecFilter[i];
415 double inverseFourier[m_nFilterPoints];
416 finiteFourierTransform (fftSignal, inverseFourier, m_nFilterPoints, 1);
417 for (int i = 0; i < m_nSignalPoints; i++)
418 output[i] = inverseFourier[i];
419 } else if (m_idFilterMethod == FILTER_METHOD_FOURIER_TABLE) {
420 double inputSignal[m_nFilterPoints];
421 for (int i = 0; i < m_nSignalPoints; i++)
422 inputSignal[i] = input[i];
423 for (int i = m_nSignalPoints; i < m_nFilterPoints; i++)
424 inputSignal[i] = 0; // zeropad
425 complex<double> fftSignal[m_nFilterPoints];
426 finiteFourierTransform (inputSignal, fftSignal, -1);
427 for (int i = 0; i < m_nFilterPoints; i++)
428 fftSignal[i] *= m_vecFilter[i];
429 double inverseFourier[m_nFilterPoints];
430 finiteFourierTransform (fftSignal, inverseFourier, 1);
431 for (int i = 0; i < m_nSignalPoints; i++)
432 output[i] = inverseFourier[i];
435 else if (m_idFilterMethod == FILTER_METHOD_RFFTW) {
436 for (int i = 0; i < m_nSignalPoints; i++)
437 m_vecRealFftInput[i] = input[i];
439 fftw_real fftOutput [ m_nFilterPoints ];
440 rfftw_one (m_realPlanForward, m_vecRealFftInput, fftOutput);
441 for (int i = 0; i < m_nFilterPoints; i++)
442 m_vecRealFftSignal[i] = m_vecFilter[i] * fftOutput[i];
443 for (int i = m_nFilterPoints; i < m_nOutputPoints; i++)
444 m_vecRealFftSignal[i] = 0;
446 fftw_real ifftOutput [ m_nOutputPoints ];
447 rfftw_one(m_realPlanBackward, m_vecRealFftSignal, ifftOutput);
448 for (int i = 0; i < m_nSignalPoints * m_preinterpolationFactor; i++)
449 output[i] = ifftOutput[i];
450 } else if (m_idFilterMethod == FILTER_METHOD_FFTW) {
451 for (int i = 0; i < m_nSignalPoints; i++)
452 m_vecComplexFftInput[i].re = input[i];
454 fftw_complex fftOutput [ m_nFilterPoints ];
455 fftw_one(m_complexPlanForward, m_vecComplexFftInput, fftOutput);
456 for (int i = 0; i < m_nFilterPoints; i++) {
457 m_vecComplexFftSignal[i].re = m_vecFilter[i] * fftOutput[i].re;
458 m_vecComplexFftSignal[i].im = m_vecFilter[i] * fftOutput[i].im;
460 fftw_complex ifftOutput [ m_nOutputPoints ];
461 fftw_one(m_complexPlanBackward, m_vecComplexFftSignal, ifftOutput);
462 for (int i = 0; i < m_nSignalPoints * m_preinterpolationFactor; i++)
463 output[i] = ifftOutput[i].re;
469 SignalFilter::response (double x)
473 if (m_idDomain == DOMAIN_SPATIAL)
474 response = spatialResponse (m_idFilter, m_bw, x, m_filterParam);
475 else if (m_idDomain == DOMAIN_FREQUENCY)
476 response = frequencyResponse (m_idFilter, m_bw, x, m_filterParam);
483 SignalFilter::spatialResponse (FilterID filterID, double bw, double x, double param)
485 if (haveAnalyticSpatial(filterID))
486 return spatialResponseAnalytic (filterID, bw, x, param);
488 return spatialResponseCalc (filterID, bw, x, param, N_INTEGRAL);
492 * filter_spatial_response_calc Calculate filter by discrete inverse fourier
493 * transform of filters's frequency
497 * y = filter_spatial_response_calc (filt_type, x, m_bw, param, n)
498 * double y Filter's response in spatial domain
499 * int filt_type Type of filter (definitions in ct.h)
500 * double x Spatial position to evaluate filter
501 * double m_bw Bandwidth of window
502 * double param General parameter for various filters
503 * int n Number of points to calculate integrations
507 SignalFilter::spatialResponseCalc (double x, double param) const
509 return (spatialResponseCalc (m_idFilter, m_bw, x, param, N_INTEGRAL));
513 SignalFilter::spatialResponseCalc (FilterID filterID, double bw, double x, double param, int n)
517 if (filterID == FILTER_TRIANGLE) {
524 double zinc = (zmax - zmin) / (n - 1);
528 for (int i = 0; i < n; i++, z += zinc)
529 q[i] = frequencyResponse (filterID, bw, z, param) * cos (TWOPI * z * x);
531 double y = 2 * integrateSimpson (zmin, zmax, q, n);
538 * filter_frequency_response Return filter frequency response
541 * h = filter_frequency_response (filt_type, u, m_bw, param)
542 * double h Filters frequency response at u
543 * int filt_type Type of filter
544 * double u Frequency to evaluate filter at
545 * double m_bw Bandwidth of filter
546 * double param General input parameter for various filters
550 SignalFilter::frequencyResponse (double u, double param) const
552 return frequencyResponse (m_idFilter, m_bw, u, param);
557 SignalFilter::frequencyResponse (FilterID filterID, double bw, double u, double param)
560 double au = fabs (u);
563 case FILTER_BANDLIMIT:
569 case FILTER_ABS_BANDLIMIT:
575 case FILTER_TRIANGLE:
585 q = cos(PI * u / bw);
587 case FILTER_ABS_COSINE:
591 q = au * cos(PI * u / bw);
594 q = bw * sinc (PI * bw * u, 1.);
596 case FILTER_ABS_SINC:
597 q = au * bw * sinc (PI * bw * u, 1.);
599 case FILTER_G_HAMMING:
603 q = param + (1 - param) * cos (TWOPI * u / bw);
605 case FILTER_ABS_G_HAMMING:
609 q = au * (param + (1 - param) * cos(TWOPI * u / bw));
613 sys_error (ERR_WARNING, "Frequency response for filter %d not implemented [filter_frequency_response]", filterID);
622 * filter_spatial_response_analytic Calculate filter by analytic inverse fourier
623 * transform of filters's frequency
627 * y = filter_spatial_response_analytic (filt_type, x, m_bw, param)
628 * double y Filter's response in spatial domain
629 * int filt_type Type of filter (definitions in ct.h)
630 * double x Spatial position to evaluate filter
631 * double m_bw Bandwidth of window
632 * double param General parameter for various filters
636 SignalFilter::spatialResponseAnalytic (double x, double param) const
638 return spatialResponseAnalytic (m_idFilter, m_bw, x, param);
642 SignalFilter::haveAnalyticSpatial (FilterID filterID)
644 bool haveAnalytic = false;
647 case FILTER_BANDLIMIT:
648 case FILTER_TRIANGLE:
650 case FILTER_G_HAMMING:
651 case FILTER_ABS_BANDLIMIT:
652 case FILTER_ABS_COSINE:
653 case FILTER_ABS_G_HAMMING:
662 return (haveAnalytic);
666 SignalFilter::spatialResponseAnalytic (FilterID filterID, double bw, double x, double param)
669 double u = TWOPI * x;
672 double b2 = TWOPI / bw;
675 case FILTER_BANDLIMIT:
676 q = bw * sinc(u * w, 1.0);
678 case FILTER_TRIANGLE:
679 temp = sinc (u * w, 1.0);
680 q = bw * temp * temp;
683 q = sinc(b-u,w) + sinc(b+u,w);
685 case FILTER_G_HAMMING:
686 q = 2 * param * sin(u*w)/u + (1-param) * (sinc(b2-u, w) + sinc(b2+u, w));
688 case FILTER_ABS_BANDLIMIT:
689 q = 2 * integral_abscos (u, w);
691 case FILTER_ABS_COSINE:
692 q = integral_abscos(b-u,w) + integral_abscos(b+u,w);
694 case FILTER_ABS_G_HAMMING:
695 q = 2 * param * integral_abscos(u,w) +
696 (1-param)*(integral_abscos(u-b2,w)+integral_abscos(u+b2,w));
700 q = 4. / (PI * bw * bw);
702 q = fabs ((2 / bw) * sin (u * w)) * sinc (u * w, 1.) * sinc (u * w, 1.);
705 if (fabs (x) < bw / 2)
710 case FILTER_ABS_SINC:
712 sys_error (ERR_WARNING, "Analytic filter type %d not implemented [filter_spatial_response_analytic]", filterID);
722 * sinc Return sin(x)/x function
726 * double v sinc value
730 * v = sin(x * mult) / x;
735 * integral_abscos Returns integral of u*cos(u)
738 * q = integral_abscos (u, w)
739 * double q Integral value
740 * double u Integration variable
741 * double w Upper integration boundary
744 * Returns the value of integral of u*cos(u)*dV for V = 0 to w
748 SignalFilter::integral_abscos (double u, double w)
750 return (fabs (u) > F_EPSILON
751 ? (cos(u * w) - 1) / (u * u) + w / u * sin (u * w)
757 * convolve Discrete convolution of two functions
760 * r = convolve (f1, f2, dx, n, np, func_type)
761 * double r Convolved result
762 * double f1[], f2[] Functions to be convolved
763 * double dx Difference between successive x values
764 * int n Array index to center convolution about
765 * int np Number of points in f1 array
766 * int func_type EVEN or ODD or EVEN_AND_ODD function f2
769 * f1 is the projection data, its indices range from 0 to np - 1.
770 * The index for f2, the filter, ranges from -(np-1) to (np-1).
771 * There are 3 ways to handle the negative vertices of f2:
772 * 1. If we know f2 is an EVEN function, then f2[-n] = f2[n].
773 * All filters used in reconstruction are even.
774 * 2. If we know f2 is an ODD function, then f2[-n] = -f2[n]
775 * 3. If f2 is both ODD AND EVEN, then we must store the value of f2
776 * for negative indices. Since f2 must range from -(np-1) to (np-1),
777 * if we add (np - 1) to f2's array index, then f2's index will
778 * range from 0 to 2 * (np - 1), and the origin, x = 0, will be
779 * stored at f2[np-1].
783 SignalFilter::convolve (const double func[], const double dx, const int n, const int np) const
787 #if UNOPTIMIZED_CONVOLUTION
788 for (int i = 0; i < np; i++)
789 sum += func[i] * m_vecFilter[n - i + (np - 1)];
791 double* f2 = m_vecFilter + n + (np - 1);
792 for (int i = 0; i < np; i++)
793 sum += *func++ * *f2--;
801 SignalFilter::convolve (const float func[], const double dx, const int n, const int np) const
805 #if UNOPTIMIZED_CONVOLUTION
806 for (int i = 0; i < np; i++)
807 sum += func[i] * m_vecFilter[n - i + (np - 1)];
809 double* f2 = m_vecFilter + n + (np - 1);
810 for (int i = 0; i < np; i++)
811 sum += *func++ * *f2--;
819 SignalFilter::finiteFourierTransform (const double input[], complex<double> output[], const int n, int direction)
826 double angleIncrement = direction * 2 * PI / n;
827 for (int i = 0; i < n; i++) {
830 for (int j = 0; j < n; j++) {
831 double angle = i * j * angleIncrement;
832 sumReal += input[j] * cos(angle);
833 sumImag += input[j] * sin(angle);
839 output[i] = complex<double> (sumReal, sumImag);
845 SignalFilter::finiteFourierTransform (const complex<double> input[], complex<double> output[], const int n, int direction)
852 double angleIncrement = direction * 2 * PI / n;
853 for (int i = 0; i < n; i++) {
854 complex<double> sum (0,0);
855 for (int j = 0; j < n; j++) {
856 double angle = i * j * angleIncrement;
857 complex<double> exponentTerm (cos(angle), sin(angle));
858 sum += input[j] * exponentTerm;
868 SignalFilter::finiteFourierTransform (const complex<double> input[], double output[], const int n, int direction)
875 double angleIncrement = direction * 2 * PI / n;
876 for (int i = 0; i < n; i++) {
878 for (int j = 0; j < n; j++) {
879 double angle = i * j * angleIncrement;
880 sumReal += input[j].real() * cos(angle) - input[j].imag() * sin(angle);
890 SignalFilter::finiteFourierTransform (const double input[], complex<double> output[], int direction) const
897 for (int i = 0; i < m_nFilterPoints; i++) {
898 double sumReal = 0, sumImag = 0;
899 for (int j = 0; j < m_nFilterPoints; j++) {
900 int tableIndex = i * j;
902 sumReal += input[j] * m_vecFourierCosTable[tableIndex];
903 sumImag += input[j] * m_vecFourierSinTable[tableIndex];
905 sumReal += input[j] * m_vecFourierCosTable[tableIndex];
906 sumImag -= input[j] * m_vecFourierSinTable[tableIndex];
910 sumReal /= m_nFilterPoints;
911 sumImag /= m_nFilterPoints;
913 output[i] = complex<double> (sumReal, sumImag);
917 // (a+bi) * (c + di) = (ac - bd) + (ad + bc)i
919 SignalFilter::finiteFourierTransform (const complex<double> input[], complex<double> output[], int direction) const
926 for (int i = 0; i < m_nFilterPoints; i++) {
927 double sumReal = 0, sumImag = 0;
928 for (int j = 0; j < m_nFilterPoints; j++) {
929 int tableIndex = i * j;
931 sumReal += input[j].real() * m_vecFourierCosTable[tableIndex]
932 - input[j].imag() * m_vecFourierSinTable[tableIndex];
933 sumImag += input[j].real() * m_vecFourierSinTable[tableIndex]
934 + input[j].imag() * m_vecFourierCosTable[tableIndex];
936 sumReal += input[j].real() * m_vecFourierCosTable[tableIndex]
937 - input[j].imag() * -m_vecFourierSinTable[tableIndex];
938 sumImag += input[j].real() * -m_vecFourierSinTable[tableIndex]
939 + input[j].imag() * m_vecFourierCosTable[tableIndex];
943 sumReal /= m_nFilterPoints;
944 sumImag /= m_nFilterPoints;
946 output[i] = complex<double> (sumReal, sumImag);
951 SignalFilter::finiteFourierTransform (const complex<double> input[], double output[], int direction) const
958 for (int i = 0; i < m_nFilterPoints; i++) {
960 for (int j = 0; j < m_nFilterPoints; j++) {
961 int tableIndex = i * j;
963 sumReal += input[j].real() * m_vecFourierCosTable[tableIndex]
964 - input[j].imag() * m_vecFourierSinTable[tableIndex];
966 sumReal += input[j].real() * m_vecFourierCosTable[tableIndex]
967 - input[j].imag() * -m_vecFourierSinTable[tableIndex];
971 sumReal /= m_nFilterPoints;