r133: *** empty log message ***
[ctsim.git] / libctsim / filter.cpp
1 /*****************************************************************************
2 ** File IDENTIFICATION
3 ** 
4 **     Name:                   filter.cpp
5 **     Purpose:                Routines for signal-procesing filters
6 **     Progammer:              Kevin Rosenberg
7 **     Date Started:           Aug 1984
8 **
9 **  This is part of the CTSim program
10 **  Copyright (C) 1983-2000 Kevin Rosenberg
11 **
12 **  $Id: filter.cpp,v 1.11 2000/07/05 17:24:33 kevin Exp $
13 **
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.
17 **
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.
22 **
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 ******************************************************************************/
27
28 #include "ct.h"
29
30
31 /* NAME
32  *   SignalFilter::SignalFilter     Construct a signal
33  *
34  * SYNOPSIS
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 nSignalPoints  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
45  */
46
47 SignalFilter::SignalFilter (const char* filterName, const char* filterMethodName, double bw, double signalIncrement, int nSignalPoints, double param, const char* domainName, int numIntegral = 0)
48 {
49   m_vecFilter = NULL;
50   m_vecFourierCosTable = NULL;
51   m_vecFourierSinTable = NULL;
52   m_idFilter = convertFilterNameToID (filterName);
53   if (m_idFilter == FILTER_INVALID) {
54     m_fail = true;
55     m_failMessage = "Invalid Filter name ";
56     m_failMessage += filterName;
57     return;
58   }
59   m_idFilterMethod = convertFilterMethodNameToID (filterMethodName);
60   if (m_idFilterMethod == FILTER_METHOD_INVALID) {
61     m_fail = true;
62     m_failMessage = "Invalid filter method name ";
63     m_failMessage += filterMethodName;
64     return;
65   }
66   m_idDomain = convertDomainNameToID (domainName);
67   if (m_idDomain == DOMAIN_INVALID) {
68     m_fail = true;
69     m_failMessage = "Invalid domain name ";
70     m_failMessage += domainName;
71     return;
72   }
73   init (m_idFilter, m_idFilterMethod, bw, signalIncrement, nSignalPoints, param, m_idDomain, numIntegral);
74 }
75
76 SignalFilter::SignalFilter (const FilterID filterID, const FilterMethodID filterMethodID, double bw, double signalIncrement, int nSignalPoints, double param, const DomainID domainID, int numIntegral = 0)
77 {
78   init (filterID, filterMethodID, bw, signalIncrement, nSignalPoints, param, domainID, numIntegral);
79 }
80
81 SignalFilter::SignalFilter (const char* filterName, const char* domainName, double bw, double param, int numIntegral = 0)
82 {
83   m_bw = bw;
84   m_nSignalPoints = 0;
85   m_nFilterPoints = 0;
86   m_vecFilter = NULL;
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) {
93     m_fail = true;
94     m_failMessage = "Invalid Filter name ";
95     m_failMessage += filterName;
96     return;
97   }
98   m_idDomain = convertDomainNameToID (domainName);
99   if (m_idDomain == DOMAIN_INVALID) {
100     m_fail = true;
101     m_failMessage = "Invalid domain name ";
102     m_failMessage += domainName;
103     return;
104   }
105 }
106
107 void
108 SignalFilter::init (const FilterID filterID, const FilterMethodID filterMethodID, double bw, double signalIncrement, int nSignalPoints, double param, const DomainID domainID, int numint)
109 {
110   m_bw = bw;
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) {
115     m_fail = true;
116     return;
117   }
118   m_traceLevel = TRACE_NONE;
119   m_nameFilter = convertFilterIDToName (m_idFilter);
120   m_nameDomain = convertDomainIDToName (m_idDomain);
121   m_nameFilterMethod = convertFilterMethodIDToName (m_idFilterMethod);
122   m_fail = false;
123   m_nSignalPoints = nSignalPoints;
124   m_signalInc = signalIncrement;
125   m_filterParam = param;  
126   
127   if (m_idFilterMethod == FILTER_METHOD_FOURIER) {
128     int nFourier = m_nSignalPoints * m_nSignalPoints + 1;
129     double angleIncrement = (2. * PI) / m_nSignalPoints;
130     m_vecFourierCosTable = new double[ nFourier ];
131     m_vecFourierSinTable = new double[ nFourier ];
132     for (int i = 0; i < nFourier; i++) {
133       m_vecFourierCosTable[i] = cos (angleIncrement * i);
134       m_vecFourierSinTable[i] = sin (angleIncrement * i);
135     }
136     m_nFilterPoints = m_nSignalPoints;
137     m_filterMin = -1. / (2 * m_signalInc);
138     m_filterMax = 1. / (2 * m_signalInc);
139     m_filterInc = (m_filterMax - m_filterMin) / m_nFilterPoints;
140     m_vecFilter = new double [m_nFilterPoints];
141     int halfFilter = m_nFilterPoints / 2;
142     for (int i = 0; i < halfFilter; i++) 
143         m_vecFilter[i] = static_cast<double>(i) / (halfFilter - 1) / (2 * m_signalInc);
144     for (int i = 0; i < halfFilter; i++)
145         m_vecFilter[m_nFilterPoints - i - 1] = static_cast<double>(i+1) / (halfFilter - 1) / (2 * m_signalInc);
146     if (halfFilter % 2) // odd
147       m_vecFilter[halfFilter] = 1 / (2 * m_signalInc);
148   } else if (m_idFilterMethod == FILTER_METHOD_FFT || m_idFilterMethod == FILTER_METHOD_FFT_ZEROPAD_2 || m_idFilterMethod == FILTER_METHOD_FFT_ZEROPAD_4) {
149     m_nFilterPoints = m_nSignalPoints;
150     if (m_idFilterMethod == FILTER_METHOD_FFT_ZEROPAD_2 || m_idFilterMethod == FILTER_METHOD_FFT_ZEROPAD_4) {
151       double logBase2 = log(m_nSignalPoints) / log(2);
152       int nextPowerOf2 = static_cast<int>(floor(logBase2)) + 1;
153       if (m_idFilterMethod == FILTER_METHOD_FFT_ZEROPAD_4)
154         nextPowerOf2++;
155       if (logBase2 != floor(logBase2))
156         nextPowerOf2++;
157       m_nFilterPoints = 1 << nextPowerOf2;
158       cout << "nFilterPoints = " << m_nFilterPoints << endl;
159     }
160     m_filterMin = -1. / (2 * m_signalInc);
161     m_filterMax = 1. / (2 * m_signalInc);
162     m_filterInc = (m_filterMax - m_filterMin) / m_nFilterPoints;
163     m_vecFilter = new double [m_nFilterPoints];
164     int halfFilter = m_nFilterPoints / 2;
165     for (int i = 0; i < halfFilter; i++) 
166         m_vecFilter[i] = static_cast<double>(i) / (halfFilter - 1) /  (2 * m_signalInc) / m_nSignalPoints;
167     for (int i = 0; i < halfFilter; i++)
168         m_vecFilter[m_nFilterPoints - i - 1] = static_cast<double>(i+1) / (halfFilter - 1) /  (2 * m_signalInc) / m_nSignalPoints;
169     if (halfFilter % 2) // odd
170       m_vecFilter[halfFilter] = 1 / (2 * m_signalInc) / m_nSignalPoints;
171
172 #if HAVE_FFTW
173     m_planForward = fftw_create_plan (m_nFilterPoints, FFTW_FORWARD, FFTW_ESTIMATE);
174     m_planBackward = fftw_create_plan (m_nFilterPoints, FFTW_BACKWARD, FFTW_ESTIMATE);
175 #endif
176   }
177
178  if (m_idFilterMethod == FILTER_METHOD_CONVOLUTION) {
179     m_nFilterPoints = 2 * m_nSignalPoints - 1;
180     m_filterMin = -m_signalInc * (m_nSignalPoints - 1);
181     m_filterMax = m_signalInc * (m_nSignalPoints - 1);
182     m_filterInc = (m_filterMax - m_filterMin) / (m_nFilterPoints - 1);
183     m_numIntegral = numint;
184     m_vecFilter = new double[ m_nFilterPoints ];
185     
186     if (m_idFilter == FILTER_SHEPP) {
187       double a = 2 * m_bw;
188       double c = - 4. / (a * a);
189       int center = (m_nFilterPoints - 1) / 2;
190       int sidelen = center;
191       m_vecFilter[center] = 4. / (a * a);
192       
193       for (int i = 1; i <= sidelen; i++ )
194         m_vecFilter [center + i] = m_vecFilter [center - i] = c / (4 * (i * i) - 1);
195     } else if (m_idDomain == DOMAIN_FREQUENCY) {
196       double x;
197       int i;
198       for (x = m_filterMin, i = 0; i < m_nFilterPoints; x += m_filterInc, i++)
199         m_vecFilter[i] = frequencyResponse (x, param);
200     } else if (m_idDomain == DOMAIN_SPATIAL) {
201       double x;
202       int i;
203       for (x = m_filterMin, i = 0; i < m_nFilterPoints; x += m_filterInc, i++)
204         if (numint == 0)
205           m_vecFilter[i] = spatialResponseAnalytic (x, param);
206         else
207           m_vecFilter[i] = spatialResponseCalc (x, param, numint);
208     } else {
209       m_failMessage = "Illegal domain name ";
210       m_failMessage += m_idDomain;
211       m_fail = true;
212     }
213   }
214 }
215
216 SignalFilter::~SignalFilter (void)
217 {
218     delete m_vecFilter;
219     delete m_vecFourierSinTable;
220     delete m_vecFourierCosTable;
221 #if HAVE_FFTW
222     if (m_idFilterMethod == FILTER_METHOD_FFT) {
223         fftw_destroy_plan(m_planForward);
224         fftw_destroy_plan(m_planBackward);
225     }
226 #endif
227 }
228
229
230 const SignalFilter::FilterID
231 SignalFilter::convertFilterNameToID (const char *filterName)
232 {
233   FilterID filterID = FILTER_INVALID;
234
235   if (strcasecmp (filterName, FILTER_BANDLIMIT_STR) == 0)
236     filterID = FILTER_BANDLIMIT;
237   else if (strcasecmp (filterName, FILTER_HAMMING_STR) == 0)
238     filterID = FILTER_G_HAMMING;
239   else if (strcasecmp (filterName, FILTER_SINC_STR) == 0)
240     filterID = FILTER_SINC;
241   else if (strcasecmp (filterName, FILTER_COS_STR) == 0)
242     filterID = FILTER_COSINE;
243   else if (strcasecmp (filterName, FILTER_TRIANGLE_STR) == 0)
244     filterID = FILTER_TRIANGLE;
245   else if (strcasecmp (filterName, FILTER_ABS_BANDLIMIT_STR) == 0)
246     filterID = FILTER_ABS_BANDLIMIT;
247   else if (strcasecmp (filterName, FILTER_ABS_HAMMING_STR) == 0)
248     filterID = FILTER_ABS_G_HAMMING;
249   else if (strcasecmp (filterName, FILTER_ABS_SINC_STR) == 0)
250     filterID = FILTER_ABS_SINC;
251   else if (strcasecmp (filterName, FILTER_ABS_COS_STR) == 0)
252     filterID = FILTER_ABS_COSINE;
253   else if (strcasecmp (filterName, FILTER_SHEPP_STR) == 0)
254     filterID = FILTER_SHEPP;
255
256   return (filterID);
257 }
258
259 const char *
260 SignalFilter::convertFilterIDToName (const FilterID filterID)
261 {
262   const char *name = "";
263
264   if (filterID == FILTER_SHEPP)
265     name = FILTER_SHEPP_STR;
266   else if (filterID == FILTER_ABS_COSINE)
267     name = FILTER_ABS_COS_STR;
268   else if (filterID == FILTER_ABS_SINC)
269     name = FILTER_ABS_SINC_STR;
270   else if (filterID == FILTER_ABS_G_HAMMING)
271     name = FILTER_ABS_HAMMING_STR;
272   else if (filterID == FILTER_ABS_BANDLIMIT)
273     name = FILTER_ABS_BANDLIMIT_STR;
274   else if (filterID == FILTER_COSINE)
275     name = FILTER_COS_STR;
276   else if (filterID == FILTER_SINC)
277     name = FILTER_SINC_STR;
278   else if (filterID == FILTER_G_HAMMING)
279     name = FILTER_HAMMING_STR;
280   else if (filterID == FILTER_BANDLIMIT)
281     name = FILTER_BANDLIMIT_STR;
282   else if (filterID == FILTER_TRIANGLE)
283     name = FILTER_TRIANGLE_STR;
284             
285   return (name);
286 }
287       
288 const SignalFilter::FilterMethodID
289 SignalFilter::convertFilterMethodNameToID (const char* const filterMethodName)
290 {
291   FilterMethodID fmID = FILTER_METHOD_INVALID;
292
293   if (strcasecmp (filterMethodName, FILTER_METHOD_CONVOLUTION_STR) == 0)
294     fmID = FILTER_METHOD_CONVOLUTION;
295   else if (strcasecmp (filterMethodName, FILTER_METHOD_FOURIER_STR) == 0)
296     fmID = FILTER_METHOD_FOURIER;
297   else if (strcasecmp (filterMethodName, FILTER_METHOD_FFT_STR) == 0)
298     fmID = FILTER_METHOD_FFT;
299   else if (strcasecmp (filterMethodName, FILTER_METHOD_FFT_ZEROPAD_2_STR) == 0)
300     fmID = FILTER_METHOD_FFT_ZEROPAD_2;
301   else if (strcasecmp (filterMethodName, FILTER_METHOD_FFT_ZEROPAD_4_STR) == 0)
302     fmID = FILTER_METHOD_FFT_ZEROPAD_4;
303   else if (strcasecmp (filterMethodName, FILTER_METHOD_FFT_ZEROPAD_6_STR) == 0)
304     fmID = FILTER_METHOD_FFT_ZEROPAD_6;
305
306   return (fmID);
307 }
308
309 const char *
310 SignalFilter::convertFilterMethodIDToName (const FilterMethodID fmID)
311 {
312   const char *name = "";
313
314   if (fmID == FILTER_METHOD_CONVOLUTION)
315     return (FILTER_METHOD_CONVOLUTION_STR);
316   else if (fmID == FILTER_METHOD_FOURIER)
317     return (FILTER_METHOD_FOURIER_STR);
318   else if (fmID == FILTER_METHOD_FFT)
319     return (FILTER_METHOD_FFT_STR);
320   else if (fmID == FILTER_METHOD_FFT_ZEROPAD_2)
321     return (FILTER_METHOD_FFT_ZEROPAD_2_STR);
322   else if (fmID == FILTER_METHOD_FFT_ZEROPAD_4)
323     return (FILTER_METHOD_FFT_ZEROPAD_4_STR);
324   else if (fmID == FILTER_METHOD_FFT_ZEROPAD_6)
325     return (FILTER_METHOD_FFT_ZEROPAD_6_STR);
326
327   return (name);
328 }
329
330 const SignalFilter::DomainID
331 SignalFilter::convertDomainNameToID (const char* const domainName)
332 {
333   DomainID dID = DOMAIN_INVALID;
334
335   if (strcasecmp (domainName, DOMAIN_SPATIAL_STR) == 0)
336     dID = DOMAIN_SPATIAL;
337   else if (strcasecmp (domainName, DOMAIN_FREQUENCY_STR) == 0)
338     dID = DOMAIN_FREQUENCY;
339
340   return (dID);
341 }
342
343 const char *
344 SignalFilter::convertDomainIDToName (const DomainID domain)
345 {
346   const char *name = "";
347
348   if (domain == DOMAIN_SPATIAL)
349     return (DOMAIN_SPATIAL_STR);
350   else if (domain == DOMAIN_FREQUENCY)
351     return (DOMAIN_FREQUENCY_STR);
352
353   return (name);
354 }
355
356
357 void
358 SignalFilter::filterSignal (const float input[], double output[]) const
359 {
360   if (m_idFilterMethod == FILTER_METHOD_CONVOLUTION) {
361     for (int i = 0; i < m_nSignalPoints; i++)
362       output[i] = convolve (input, m_signalInc, i, m_nSignalPoints);
363   } else if (m_idFilterMethod == FILTER_METHOD_FOURIER) {
364     complex<double> fftSignal[m_nSignalPoints];
365     complex<double> complexOutput[m_nSignalPoints];
366     complex<double> filteredSignal[m_nSignalPoints];
367     finiteFourierTransform (input, fftSignal, m_nSignalPoints, -1);
368     dotProduct (m_vecFilter, fftSignal, filteredSignal, m_nSignalPoints);
369     finiteFourierTransform (filteredSignal, complexOutput, m_nSignalPoints, 1);
370     for (int i = 0; i < m_nSignalPoints; i++) 
371       output[i] = complexOutput[i].real();
372   } else if (m_idFilterMethod == FILTER_METHOD_FFT || FILTER_METHOD_FFT_ZEROPAD_2 || FILTER_METHOD_FFT_ZEROPAD_4) {
373     fftw_complex in[m_nFilterPoints], out[m_nFilterPoints];
374     for (int i = 0; i < m_nSignalPoints; i++) {
375       in[i].re = input[i];
376       in[i].im = 0;
377     }
378     for (int i = m_nSignalPoints; i < m_nFilterPoints; i++) {
379       in[i].re = in[i].im = 0;      // ZeroPad
380     }
381     fftw_one(m_planForward, in, out);
382     for (int i = 0; i < m_nFilterPoints; i++) {
383       out[i].re = m_vecFilter[i] * out[i].re;
384       out[i].im = m_vecFilter[i] * out[i].im;
385     }
386     fftw_one(m_planBackward, out, in);
387     for (int i = 0; i < m_nSignalPoints; i++) 
388       output[i] = in[i].re;
389   }
390 }
391
392 double
393 SignalFilter::response (double x)
394 {
395   double response = 0;
396
397   if (m_idDomain == DOMAIN_SPATIAL)
398     response = spatialResponse (m_idFilter, m_bw, x, m_filterParam, m_numIntegral);
399   else if (m_idDomain == DOMAIN_FREQUENCY)
400     response = frequencyResponse (m_idFilter, m_bw, x, m_filterParam);
401
402   return (response);
403 }
404
405
406 double 
407 SignalFilter::spatialResponse (FilterID filterID, double bw, double x, double param, int nIntegral = 0)
408 {
409   if (nIntegral == 0)
410     return spatialResponseAnalytic (filterID, bw, x, param);
411   else
412     return spatialResponseCalc (filterID, bw, x, param, nIntegral);
413 }
414
415 /* NAME
416  *   filter_spatial_response_calc       Calculate filter by discrete inverse fourier
417  *                                      transform of filters's frequency
418  *                                      response
419  *
420  * SYNOPSIS
421  *   y = filter_spatial_response_calc (filt_type, x, m_bw, param, n)
422  *   double y                   Filter's response in spatial domain
423  *   int filt_type              Type of filter (definitions in ct.h)
424  *   double x                   Spatial position to evaluate filter
425  *   double m_bw                        Bandwidth of window
426  *   double param               General parameter for various filters
427  *   int n                      Number of points to calculate integrations
428  */
429
430 double 
431 SignalFilter::spatialResponseCalc (double x, double param, int nIntegral) const
432 {
433   return (spatialResponseCalc (m_idFilter, m_bw, x, param, nIntegral));
434 }
435
436 double 
437 SignalFilter::spatialResponseCalc (FilterID filterID, double bw, double x, double param, int n)
438 {
439   double zmin, zmax;
440
441   if (filterID == FILTER_TRIANGLE) {
442     zmin = 0;
443     zmax = bw;
444   } else {
445     zmin = 0;
446     zmax = bw / 2;
447   }
448   double zinc = (zmax - zmin) / (n - 1);
449
450   double z = zmin;
451   double q [n];
452   for (int i = 0; i < n; i++, z += zinc)
453     q[i] = frequencyResponse (filterID, bw, z, param) * cos (TWOPI * z * x);
454   
455   double y = 2 * integrateSimpson (zmin, zmax, q, n);
456   
457   return (y);
458 }
459
460
461 /* NAME
462  *    filter_frequency_response                 Return filter frequency response
463  *
464  * SYNOPSIS
465  *    h = filter_frequency_response (filt_type, u, m_bw, param)
466  *    double h                  Filters frequency response at u
467  *    int filt_type             Type of filter
468  *    double u                  Frequency to evaluate filter at
469  *    double m_bw                       Bandwidth of filter
470  *    double param              General input parameter for various filters
471  */
472
473 double 
474 SignalFilter::frequencyResponse (double u, double param) const
475 {
476   return frequencyResponse (m_idFilter, m_bw, u, param);
477 }
478
479
480 double 
481 SignalFilter::frequencyResponse (FilterID filterID, double bw, double u, double param)
482 {
483   double q;
484   double au = fabs (u);
485
486   switch (filterID) {
487   case FILTER_BANDLIMIT:
488     if (au >= bw / 2)
489       q = 0.;
490     else
491       q = 1;
492     break;
493   case FILTER_ABS_BANDLIMIT:
494     if (au >= bw / 2)
495       q = 0.;
496     else
497       q = au;
498     break;
499   case FILTER_TRIANGLE:
500     if (au >= bw)
501       q = 0;
502     else
503       q = 1 - au / bw;
504     break;
505   case FILTER_COSINE:
506     if (au >= bw / 2)
507       q = 0;
508     else
509       q = cos(PI * u / bw);
510     break;
511   case FILTER_ABS_COSINE:
512     if (au >= bw / 2)
513       q = 0;
514     else
515       q = au * cos(PI * u / bw);
516     break;
517   case FILTER_SINC:
518     q = bw * sinc (PI * bw * u, 1.);
519     break;
520   case FILTER_ABS_SINC:
521     q = au * bw * sinc (PI * bw * u, 1.);
522     break;
523   case FILTER_G_HAMMING:
524     if (au >= bw / 2)
525       q = 0;
526     else
527       q = param + (1 - param) * cos (TWOPI * u / bw);
528     break;
529   case FILTER_ABS_G_HAMMING:
530     if (au >= bw / 2)
531       q = 0;
532     else
533       q = au * (param + (1 - param) * cos(TWOPI * u / bw));
534     break;
535   default:
536     q = 0;
537     sys_error (ERR_WARNING, "Frequency response for filter %d not implemented [filter_frequency_response]", filterID);
538     break;
539   }
540   return (q);
541 }
542
543
544
545 /* NAME
546  *   filter_spatial_response_analytic                   Calculate filter by analytic inverse fourier
547  *                              transform of filters's frequency
548  *                              response
549  *
550  * SYNOPSIS
551  *   y = filter_spatial_response_analytic (filt_type, x, m_bw, param)
552  *   double y                   Filter's response in spatial domain
553  *   int filt_type              Type of filter (definitions in ct.h)
554  *   double x                   Spatial position to evaluate filter
555  *   double m_bw                        Bandwidth of window
556  *   double param               General parameter for various filters
557  */
558
559 double 
560 SignalFilter::spatialResponseAnalytic (double x, double param) const
561 {
562   return spatialResponseAnalytic (m_idFilter, m_bw, x, param);
563 }
564
565 double 
566 SignalFilter::spatialResponseAnalytic (FilterID filterID, double bw, double x, double param)
567 {
568   double q, temp;
569   double u = TWOPI * x;
570   double w = bw / 2;
571   double b = PI / bw;
572   double b2 = TWOPI / bw;
573
574   switch (filterID) {
575   case FILTER_BANDLIMIT:
576     q = bw * sinc(u * w, 1.0);
577     break;
578   case FILTER_TRIANGLE:
579     temp = sinc (u * w, 1.0);
580     q = bw * temp * temp;
581     break;
582   case FILTER_COSINE:
583     q = sinc(b-u,w) + sinc(b+u,w);
584     break;
585   case FILTER_G_HAMMING:
586     q = 2 * param * sin(u*w)/u + (1-param) * (sinc(b2-u, w) + sinc(b2+u, w));
587     break;
588   case FILTER_ABS_BANDLIMIT:
589     q = 2 * integral_abscos (u, w);
590     break;
591   case FILTER_ABS_COSINE:
592     q = integral_abscos(b-u,w) + integral_abscos(b+u,w);
593     break;
594   case FILTER_ABS_G_HAMMING:
595     q = 2 * param * integral_abscos(u,w) +
596       (1-param)*(integral_abscos(u-b2,w)+integral_abscos(u+b2,w));
597     break;
598   case FILTER_SHEPP:
599     if (fabs (u) < 1E-7)
600       q = 4. / (PI * bw * bw);
601     else
602       q = fabs ((2 / bw) * sin (u * w)) * sinc (u * w, 1.) * sinc (u * w, 1.);
603     break;
604   case FILTER_SINC:
605     if (fabs (x) < bw / 2)
606       q = 1.;
607     else
608       q = 0.;
609     break;
610   case FILTER_ABS_SINC:
611   default:
612     sys_error (ERR_WARNING, "Analytic filter type %d not implemented [filter_spatial_response_analytic]", filterID);
613     q = 0;
614     break;
615   }
616   
617   return (q);
618 }
619
620
621 /* NAME
622  *   sinc                       Return sin(x)/x function
623  *
624  * SYNOPSIS
625  *   v = sinc (x, mult)
626  *   double v                   sinc value
627  *   double x, mult
628  *
629  * DESCRIPTION
630  *   v = sin(x * mult) / x;
631  */
632
633
634 /* NAME
635  *   integral_abscos                    Returns integral of u*cos(u)
636  *
637  * SYNOPSIS
638  *   q = integral_abscos (u, w)
639  *   double q                   Integral value
640  *   double u                   Integration variable
641  *   double w                   Upper integration boundary
642  *
643  * DESCRIPTION
644  *   Returns the value of integral of u*cos(u)*dV for V = 0 to w
645  */
646
647 double 
648 SignalFilter::integral_abscos (double u, double w)
649 {
650   return (fabs (u) > F_EPSILON 
651      ? (cos(u * w) - 1) / (u * u) + w / u * sin (u * w) 
652      : (w * w / 2));
653 }
654
655
656 /* NAME
657  *    convolve                  Discrete convolution of two functions
658  *
659  * SYNOPSIS
660  *    r = convolve (f1, f2, dx, n, np, func_type)
661  *    double r                  Convolved result
662  *    double f1[], f2[]         Functions to be convolved
663  *    double dx                 Difference between successive x values
664  *    int n                     Array index to center convolution about
665  *    int np                    Number of points in f1 array
666  *    int func_type             EVEN or ODD or EVEN_AND_ODD function f2
667  *
668  * NOTES
669  *    f1 is the projection data, its indices range from 0 to np - 1.
670  *    The index for f2, the filter, ranges from -(np-1) to (np-1).
671  *    There are 3 ways to handle the negative vertices of f2:
672  *      1. If we know f2 is an EVEN function, then f2[-n] = f2[n].
673  *         All filters used in reconstruction are even.
674  *      2. If we know f2 is an ODD function, then f2[-n] = -f2[n] 
675  *      3. If f2 is both ODD AND EVEN, then we must store the value of f2
676  *         for negative indices.  Since f2 must range from -(np-1) to (np-1),
677  *         if we add (np - 1) to f2's array index, then f2's index will
678  *         range from 0 to 2 * (np - 1), and the origin, x = 0, will be
679  *         stored at f2[np-1].
680  */
681
682 double 
683 SignalFilter::convolve (const double func[], const double dx, const int n, const int np) const
684 {
685   double sum = 0.0;
686
687 #if UNOPTIMIZED_CONVOLUTION
688   for (int i = 0; i < np; i++)
689     sum += func[i] * m_vecFilter[n - i + (np - 1)];
690 #else
691   double* f2 = m_vecFilter + n + (np - 1);
692   for (int i = 0; i < np; i++)
693     sum += *func++ * *f2--;
694 #endif
695
696   return (sum * dx);
697 }
698
699
700 double 
701 SignalFilter::convolve (const float func[], const double dx, const int n, const int np) const
702 {
703   double sum = 0.0;
704
705 #if UNOPTIMIZED_CONVOLUTION
706 for (int i = 0; i < np; i++)
707   sum += func[i] * m_vecFilter[n - i + (np - 1)];
708 #else
709 double* f2 = m_vecFilter + n + (np - 1);
710 for (int i = 0; i < np; i++)
711   sum += *func++ * *f2--;
712 #endif
713
714   return (sum * dx);
715 }
716
717
718 void
719 SignalFilter::finiteFourierTransform (const float input[], complex<double> output[], const int n, int direction)
720 {
721   if (direction < 0)
722     direction = -1;
723   else 
724     direction = 1;
725     
726   double angleIncrement = 2 * PI / n;
727   for (int i = 0; i < n; i++) {
728     double sumReal = 0;
729     double sumImag = 0;
730     for (int j = 0; j < n; j++) {
731       double angle = i * j * angleIncrement * direction;
732       sumReal += input[j] * cos(angle);
733       sumImag += input[j] * sin(angle);
734     }
735     if (direction < 0) {
736       sumReal /= n;
737       sumImag /= n;
738     }
739     output[i] = complex<double> (sumReal, sumImag);
740   }
741 }
742
743
744 void
745 SignalFilter::finiteFourierTransform (const complex<double> input[], complex<double> output[], const int n, int direction)
746 {
747   if (direction < 0)
748     direction = -1;
749   else 
750     direction = 1;
751     
752   double angleIncrement = 2 * PI / n;
753   for (int i = 0; i < n; i++) {
754     complex<double> sum (0,0);
755     for (int j = 0; j < n; j++) {
756       double angle = i * j * angleIncrement * direction;
757       complex<double> exponentTerm (cos(angle), sin(angle));
758       sum += input[j] * exponentTerm;
759     }
760     if (direction < 0) {
761       sum /= n;
762     }
763     output[i] = sum;
764   }
765 }
766
767 void
768 SignalFilter::finiteFourierTransform (const float input[], complex<double> output[], int direction) const
769 {
770   if (direction < 0)
771     direction = -1;
772   else 
773     direction = 1;
774     
775   for (int i = 0; i < m_nSignalPoints; i++) {
776     double sumReal = 0, sumImag = 0;
777     for (int j = 0; j < m_nSignalPoints; j++) {
778       int tableIndex = i * j;
779       if (direction > 0) {
780         sumReal += input[i] * m_vecFourierCosTable[tableIndex];
781         sumImag += input[i] * m_vecFourierSinTable[tableIndex];
782       } else {
783         sumReal += input[i] * m_vecFourierCosTable[tableIndex];
784         sumImag -= input[i] * m_vecFourierSinTable[tableIndex];
785       }
786     }
787     if (direction < 0) {
788       sumReal /= m_nSignalPoints;
789       sumImag /= m_nSignalPoints;
790     }
791     output[i] = complex<double> (sumReal, sumImag);
792   }
793 }
794
795 // (a+bi) * (c + di) = (ac - db) + (bc + da)i
796 #if 0
797 void
798 SignalFilter::finiteFourierTransform (const complex<double> input[], complex<double> output[], int direction) const
799 {
800   if (direction < 0)
801     direction = -1;
802   else 
803     direction = 1;
804     
805   for (int i = 0; i < m_nSignalPoints; i++) {
806     double sumReal = 0, sumImag = 0;
807     for (int j = 0; j < m_nSignalPoints; j++) {
808       int tableIndex = i * j;
809       if (direction > 0) {
810         sumReal += input[i] * m_vecFourierCosTable[tableIndex];
811         sumImag += input[i] * m_vecFourierSinTable[tableIndex];
812       } else {
813         sumReal += input[i] * m_vecFourierCosTable[tableIndex];
814         sumImag -= input[i] * m_vecFourierSinTable[tableIndex];
815       }
816     }
817     if (direction > 0) {
818       sumReal /= m_nSignalPoints;
819       sumImag /= m_nSignalPoints;
820     }
821     output[i] = complex<double> (sumReal, sumImag);
822   }
823 }
824 #endif
825
826 void 
827 SignalFilter::dotProduct (const double v1[], const complex<double> v2[], complex<double> output[], const int n)
828 {
829     for (int i = 0; i < n; i++)
830         output[i] = v1[i] * v2[i];
831 }