r135: *** 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.12 2000/07/05 17:59:26 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 #if HAVE_FFTW
374     fftw_complex in[m_nFilterPoints], out[m_nFilterPoints];
375     for (int i = 0; i < m_nSignalPoints; i++) {
376       in[i].re = input[i];
377       in[i].im = 0;
378     }
379     for (int i = m_nSignalPoints; i < m_nFilterPoints; i++) {
380       in[i].re = in[i].im = 0;      // ZeroPad
381     }
382     fftw_one(m_planForward, in, out);
383     for (int i = 0; i < m_nFilterPoints; i++) {
384       out[i].re = m_vecFilter[i] * out[i].re;
385       out[i].im = m_vecFilter[i] * out[i].im;
386     }
387     fftw_one(m_planBackward, out, in);
388     for (int i = 0; i < m_nSignalPoints; i++) 
389       output[i] = in[i].re;
390   }
391 #endif
392 }
393
394 double
395 SignalFilter::response (double x)
396 {
397   double response = 0;
398
399   if (m_idDomain == DOMAIN_SPATIAL)
400     response = spatialResponse (m_idFilter, m_bw, x, m_filterParam, m_numIntegral);
401   else if (m_idDomain == DOMAIN_FREQUENCY)
402     response = frequencyResponse (m_idFilter, m_bw, x, m_filterParam);
403
404   return (response);
405 }
406
407
408 double 
409 SignalFilter::spatialResponse (FilterID filterID, double bw, double x, double param, int nIntegral = 0)
410 {
411   if (nIntegral == 0)
412     return spatialResponseAnalytic (filterID, bw, x, param);
413   else
414     return spatialResponseCalc (filterID, bw, x, param, nIntegral);
415 }
416
417 /* NAME
418  *   filter_spatial_response_calc       Calculate filter by discrete inverse fourier
419  *                                      transform of filters's frequency
420  *                                      response
421  *
422  * SYNOPSIS
423  *   y = filter_spatial_response_calc (filt_type, x, m_bw, param, n)
424  *   double y                   Filter's response in spatial domain
425  *   int filt_type              Type of filter (definitions in ct.h)
426  *   double x                   Spatial position to evaluate filter
427  *   double m_bw                        Bandwidth of window
428  *   double param               General parameter for various filters
429  *   int n                      Number of points to calculate integrations
430  */
431
432 double 
433 SignalFilter::spatialResponseCalc (double x, double param, int nIntegral) const
434 {
435   return (spatialResponseCalc (m_idFilter, m_bw, x, param, nIntegral));
436 }
437
438 double 
439 SignalFilter::spatialResponseCalc (FilterID filterID, double bw, double x, double param, int n)
440 {
441   double zmin, zmax;
442
443   if (filterID == FILTER_TRIANGLE) {
444     zmin = 0;
445     zmax = bw;
446   } else {
447     zmin = 0;
448     zmax = bw / 2;
449   }
450   double zinc = (zmax - zmin) / (n - 1);
451
452   double z = zmin;
453   double q [n];
454   for (int i = 0; i < n; i++, z += zinc)
455     q[i] = frequencyResponse (filterID, bw, z, param) * cos (TWOPI * z * x);
456   
457   double y = 2 * integrateSimpson (zmin, zmax, q, n);
458   
459   return (y);
460 }
461
462
463 /* NAME
464  *    filter_frequency_response                 Return filter frequency response
465  *
466  * SYNOPSIS
467  *    h = filter_frequency_response (filt_type, u, m_bw, param)
468  *    double h                  Filters frequency response at u
469  *    int filt_type             Type of filter
470  *    double u                  Frequency to evaluate filter at
471  *    double m_bw                       Bandwidth of filter
472  *    double param              General input parameter for various filters
473  */
474
475 double 
476 SignalFilter::frequencyResponse (double u, double param) const
477 {
478   return frequencyResponse (m_idFilter, m_bw, u, param);
479 }
480
481
482 double 
483 SignalFilter::frequencyResponse (FilterID filterID, double bw, double u, double param)
484 {
485   double q;
486   double au = fabs (u);
487
488   switch (filterID) {
489   case FILTER_BANDLIMIT:
490     if (au >= bw / 2)
491       q = 0.;
492     else
493       q = 1;
494     break;
495   case FILTER_ABS_BANDLIMIT:
496     if (au >= bw / 2)
497       q = 0.;
498     else
499       q = au;
500     break;
501   case FILTER_TRIANGLE:
502     if (au >= bw)
503       q = 0;
504     else
505       q = 1 - au / bw;
506     break;
507   case FILTER_COSINE:
508     if (au >= bw / 2)
509       q = 0;
510     else
511       q = cos(PI * u / bw);
512     break;
513   case FILTER_ABS_COSINE:
514     if (au >= bw / 2)
515       q = 0;
516     else
517       q = au * cos(PI * u / bw);
518     break;
519   case FILTER_SINC:
520     q = bw * sinc (PI * bw * u, 1.);
521     break;
522   case FILTER_ABS_SINC:
523     q = au * bw * sinc (PI * bw * u, 1.);
524     break;
525   case FILTER_G_HAMMING:
526     if (au >= bw / 2)
527       q = 0;
528     else
529       q = param + (1 - param) * cos (TWOPI * u / bw);
530     break;
531   case FILTER_ABS_G_HAMMING:
532     if (au >= bw / 2)
533       q = 0;
534     else
535       q = au * (param + (1 - param) * cos(TWOPI * u / bw));
536     break;
537   default:
538     q = 0;
539     sys_error (ERR_WARNING, "Frequency response for filter %d not implemented [filter_frequency_response]", filterID);
540     break;
541   }
542   return (q);
543 }
544
545
546
547 /* NAME
548  *   filter_spatial_response_analytic                   Calculate filter by analytic inverse fourier
549  *                              transform of filters's frequency
550  *                              response
551  *
552  * SYNOPSIS
553  *   y = filter_spatial_response_analytic (filt_type, x, m_bw, param)
554  *   double y                   Filter's response in spatial domain
555  *   int filt_type              Type of filter (definitions in ct.h)
556  *   double x                   Spatial position to evaluate filter
557  *   double m_bw                        Bandwidth of window
558  *   double param               General parameter for various filters
559  */
560
561 double 
562 SignalFilter::spatialResponseAnalytic (double x, double param) const
563 {
564   return spatialResponseAnalytic (m_idFilter, m_bw, x, param);
565 }
566
567 double 
568 SignalFilter::spatialResponseAnalytic (FilterID filterID, double bw, double x, double param)
569 {
570   double q, temp;
571   double u = TWOPI * x;
572   double w = bw / 2;
573   double b = PI / bw;
574   double b2 = TWOPI / bw;
575
576   switch (filterID) {
577   case FILTER_BANDLIMIT:
578     q = bw * sinc(u * w, 1.0);
579     break;
580   case FILTER_TRIANGLE:
581     temp = sinc (u * w, 1.0);
582     q = bw * temp * temp;
583     break;
584   case FILTER_COSINE:
585     q = sinc(b-u,w) + sinc(b+u,w);
586     break;
587   case FILTER_G_HAMMING:
588     q = 2 * param * sin(u*w)/u + (1-param) * (sinc(b2-u, w) + sinc(b2+u, w));
589     break;
590   case FILTER_ABS_BANDLIMIT:
591     q = 2 * integral_abscos (u, w);
592     break;
593   case FILTER_ABS_COSINE:
594     q = integral_abscos(b-u,w) + integral_abscos(b+u,w);
595     break;
596   case FILTER_ABS_G_HAMMING:
597     q = 2 * param * integral_abscos(u,w) +
598       (1-param)*(integral_abscos(u-b2,w)+integral_abscos(u+b2,w));
599     break;
600   case FILTER_SHEPP:
601     if (fabs (u) < 1E-7)
602       q = 4. / (PI * bw * bw);
603     else
604       q = fabs ((2 / bw) * sin (u * w)) * sinc (u * w, 1.) * sinc (u * w, 1.);
605     break;
606   case FILTER_SINC:
607     if (fabs (x) < bw / 2)
608       q = 1.;
609     else
610       q = 0.;
611     break;
612   case FILTER_ABS_SINC:
613   default:
614     sys_error (ERR_WARNING, "Analytic filter type %d not implemented [filter_spatial_response_analytic]", filterID);
615     q = 0;
616     break;
617   }
618   
619   return (q);
620 }
621
622
623 /* NAME
624  *   sinc                       Return sin(x)/x function
625  *
626  * SYNOPSIS
627  *   v = sinc (x, mult)
628  *   double v                   sinc value
629  *   double x, mult
630  *
631  * DESCRIPTION
632  *   v = sin(x * mult) / x;
633  */
634
635
636 /* NAME
637  *   integral_abscos                    Returns integral of u*cos(u)
638  *
639  * SYNOPSIS
640  *   q = integral_abscos (u, w)
641  *   double q                   Integral value
642  *   double u                   Integration variable
643  *   double w                   Upper integration boundary
644  *
645  * DESCRIPTION
646  *   Returns the value of integral of u*cos(u)*dV for V = 0 to w
647  */
648
649 double 
650 SignalFilter::integral_abscos (double u, double w)
651 {
652   return (fabs (u) > F_EPSILON 
653      ? (cos(u * w) - 1) / (u * u) + w / u * sin (u * w) 
654      : (w * w / 2));
655 }
656
657
658 /* NAME
659  *    convolve                  Discrete convolution of two functions
660  *
661  * SYNOPSIS
662  *    r = convolve (f1, f2, dx, n, np, func_type)
663  *    double r                  Convolved result
664  *    double f1[], f2[]         Functions to be convolved
665  *    double dx                 Difference between successive x values
666  *    int n                     Array index to center convolution about
667  *    int np                    Number of points in f1 array
668  *    int func_type             EVEN or ODD or EVEN_AND_ODD function f2
669  *
670  * NOTES
671  *    f1 is the projection data, its indices range from 0 to np - 1.
672  *    The index for f2, the filter, ranges from -(np-1) to (np-1).
673  *    There are 3 ways to handle the negative vertices of f2:
674  *      1. If we know f2 is an EVEN function, then f2[-n] = f2[n].
675  *         All filters used in reconstruction are even.
676  *      2. If we know f2 is an ODD function, then f2[-n] = -f2[n] 
677  *      3. If f2 is both ODD AND EVEN, then we must store the value of f2
678  *         for negative indices.  Since f2 must range from -(np-1) to (np-1),
679  *         if we add (np - 1) to f2's array index, then f2's index will
680  *         range from 0 to 2 * (np - 1), and the origin, x = 0, will be
681  *         stored at f2[np-1].
682  */
683
684 double 
685 SignalFilter::convolve (const double func[], const double dx, const int n, const int np) const
686 {
687   double sum = 0.0;
688
689 #if UNOPTIMIZED_CONVOLUTION
690   for (int i = 0; i < np; i++)
691     sum += func[i] * m_vecFilter[n - i + (np - 1)];
692 #else
693   double* f2 = m_vecFilter + n + (np - 1);
694   for (int i = 0; i < np; i++)
695     sum += *func++ * *f2--;
696 #endif
697
698   return (sum * dx);
699 }
700
701
702 double 
703 SignalFilter::convolve (const float func[], const double dx, const int n, const int np) const
704 {
705   double sum = 0.0;
706
707 #if UNOPTIMIZED_CONVOLUTION
708 for (int i = 0; i < np; i++)
709   sum += func[i] * m_vecFilter[n - i + (np - 1)];
710 #else
711 double* f2 = m_vecFilter + n + (np - 1);
712 for (int i = 0; i < np; i++)
713   sum += *func++ * *f2--;
714 #endif
715
716   return (sum * dx);
717 }
718
719
720 void
721 SignalFilter::finiteFourierTransform (const float input[], complex<double> output[], const int n, int direction)
722 {
723   if (direction < 0)
724     direction = -1;
725   else 
726     direction = 1;
727     
728   double angleIncrement = 2 * PI / n;
729   for (int i = 0; i < n; i++) {
730     double sumReal = 0;
731     double sumImag = 0;
732     for (int j = 0; j < n; j++) {
733       double angle = i * j * angleIncrement * direction;
734       sumReal += input[j] * cos(angle);
735       sumImag += input[j] * sin(angle);
736     }
737     if (direction < 0) {
738       sumReal /= n;
739       sumImag /= n;
740     }
741     output[i] = complex<double> (sumReal, sumImag);
742   }
743 }
744
745
746 void
747 SignalFilter::finiteFourierTransform (const complex<double> input[], complex<double> output[], const int n, int direction)
748 {
749   if (direction < 0)
750     direction = -1;
751   else 
752     direction = 1;
753     
754   double angleIncrement = 2 * PI / n;
755   for (int i = 0; i < n; i++) {
756     complex<double> sum (0,0);
757     for (int j = 0; j < n; j++) {
758       double angle = i * j * angleIncrement * direction;
759       complex<double> exponentTerm (cos(angle), sin(angle));
760       sum += input[j] * exponentTerm;
761     }
762     if (direction < 0) {
763       sum /= n;
764     }
765     output[i] = sum;
766   }
767 }
768
769 void
770 SignalFilter::finiteFourierTransform (const float input[], complex<double> output[], int direction) const
771 {
772   if (direction < 0)
773     direction = -1;
774   else 
775     direction = 1;
776     
777   for (int i = 0; i < m_nSignalPoints; i++) {
778     double sumReal = 0, sumImag = 0;
779     for (int j = 0; j < m_nSignalPoints; j++) {
780       int tableIndex = i * j;
781       if (direction > 0) {
782         sumReal += input[i] * m_vecFourierCosTable[tableIndex];
783         sumImag += input[i] * m_vecFourierSinTable[tableIndex];
784       } else {
785         sumReal += input[i] * m_vecFourierCosTable[tableIndex];
786         sumImag -= input[i] * m_vecFourierSinTable[tableIndex];
787       }
788     }
789     if (direction < 0) {
790       sumReal /= m_nSignalPoints;
791       sumImag /= m_nSignalPoints;
792     }
793     output[i] = complex<double> (sumReal, sumImag);
794   }
795 }
796
797 // (a+bi) * (c + di) = (ac - db) + (bc + da)i
798 #if 0
799 void
800 SignalFilter::finiteFourierTransform (const complex<double> input[], complex<double> output[], int direction) const
801 {
802   if (direction < 0)
803     direction = -1;
804   else 
805     direction = 1;
806     
807   for (int i = 0; i < m_nSignalPoints; i++) {
808     double sumReal = 0, sumImag = 0;
809     for (int j = 0; j < m_nSignalPoints; j++) {
810       int tableIndex = i * j;
811       if (direction > 0) {
812         sumReal += input[i] * m_vecFourierCosTable[tableIndex];
813         sumImag += input[i] * m_vecFourierSinTable[tableIndex];
814       } else {
815         sumReal += input[i] * m_vecFourierCosTable[tableIndex];
816         sumImag -= input[i] * m_vecFourierSinTable[tableIndex];
817       }
818     }
819     if (direction > 0) {
820       sumReal /= m_nSignalPoints;
821       sumImag /= m_nSignalPoints;
822     }
823     output[i] = complex<double> (sumReal, sumImag);
824   }
825 }
826 #endif
827
828 void 
829 SignalFilter::dotProduct (const double v1[], const complex<double> v2[], complex<double> output[], const int n)
830 {
831     for (int i = 0; i < n; i++)
832         output[i] = v1[i] * v2[i];
833 }