r127: *** 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.6 2000/07/02 18:21:39 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, xmin, xmax, n, param, domain, analytic)
36  *   double f           Generated filter vector
37  *   int filt_type      Type of filter wanted
38  *   double bw          Bandwidth of filter
39  *   double xmin, xmax  Filter limits
40  *   int n              Number of points in signal
41  *   double param       General input parameter to filters
42  *   int domain         FREQUENCY or SPATIAL domain wanted
43  *   int numint         Number if intervals for calculating discrete inverse fourier xform
44  *                      for spatial domain filters.  For ANALYTIC solutions, use numint = 0
45  */
46
47 SignalFilter::SignalFilter (const char* filterName, const char* filterMethodName, double bw, double signalLength, int n, 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, signalLength, n, param, m_idDomain, numIntegral);
74 }
75
76 SignalFilter::SignalFilter (const FilterID filterID, const FilterMethodID filterMethodID, double bw, double signalLength, int n, double param, const DomainID domainID, int numIntegral = 0)
77 {
78   init (filterID, filterMethodID, bw, signalLength, n, 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_nPoints = 0;
85   m_vecFilter = NULL;
86   m_vecFourierCosTable = NULL;
87   m_vecFourierSinTable = NULL;
88   m_filterParam = param;  
89   m_numIntegral = numIntegral;
90   m_idFilter = convertFilterNameToID (filterName);
91   if (m_idFilter == FILTER_INVALID) {
92     m_fail = true;
93     m_failMessage = "Invalid Filter name ";
94     m_failMessage += filterName;
95     return;
96   }
97   m_idDomain = convertDomainNameToID (domainName);
98   if (m_idDomain == DOMAIN_INVALID) {
99     m_fail = true;
100     m_failMessage = "Invalid domain name ";
101     m_failMessage += domainName;
102     return;
103   }
104 }
105
106 void
107 SignalFilter::init (const FilterID filterID, const FilterMethodID filterMethodID, double bw, double signalLength, int n, double param, const DomainID domainID, int numint)
108 {
109   m_bw = bw;
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) {
114     m_fail = true;
115     return;
116   }
117   m_nameFilter = convertFilterIDToName (m_idFilter);
118   m_nameDomain = convertDomainIDToName (m_idDomain);
119   m_nameFilterMethod = convertFilterMethodIDToName (m_idFilterMethod);
120   m_fail = false;
121   m_nSignalPoints = n;
122   m_nFilterPoints = 2 * m_nSignalPoints - 1;
123
124   m_signalLength = signalLength;
125   m_xmin = -signalLength;
126   m_xmax = signalLength;
127   m_numIntegral = numint;
128   m_filterParam = param;  
129   m_vecFilter = new double[n];
130   if (m_idFilterMethod == FILTER_METHOD_FOURIER) {
131     int nFourier = n * n + 1;
132     double angleIncrement = (2. * PI) / n;
133     m_vecFourierCosTable = new double[ nFourier ];
134     m_vecFourierSinTable = new double[ nFourier ];
135     for (int i = 0; i < nFourier; i++) {
136       m_vecFourierCosTable[i] = cos (angleIncrement * i);
137       m_vecFourierSinTable[i] = sin (angleIncrement * i);
138     }
139   }
140
141   double xinc = (m_xmax - m_xmin) / (m_nPoints - 1);
142
143   if (m_idFilter == FILTER_SHEPP) {
144     double a = 2 * m_bw;
145     double c = - 4. / (a * a);
146     int center = (m_nPoints - 1) / 2;
147     int sidelen = center;
148     m_vecFilter[center] = 4. / (a * a);
149
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) {
153     double x;
154     int i;
155     for (x = m_xmin, i = 0; i < m_nPoints; x += xinc, i++)
156       m_vecFilter[i] = frequencyResponse (x, param);
157   } else if (m_idDomain == DOMAIN_SPATIAL) {
158     double x;
159     int i;
160     for (x = m_xmin, i = 0; i < m_nPoints; x += xinc, i++)
161       if (numint == 0)
162         m_vecFilter[i] = spatialResponseAnalytic (x, param);
163       else
164         m_vecFilter[i] = spatialResponseCalc (x, param, numint);
165   } else {
166       m_failMessage = "Illegal domain name ";
167       m_failMessage += m_idDomain;
168       m_fail = true;
169   }
170 }
171
172 SignalFilter::~SignalFilter (void)
173 {
174     delete m_vecFilter;
175     delete m_vecFourierSinTable;
176     delete m_vecFourierCosTable;
177 }
178
179
180 const SignalFilter::FilterID
181 SignalFilter::convertFilterNameToID (const char *filterName)
182 {
183   FilterID filterID = FILTER_INVALID;
184
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;
205
206   return (filterID);
207 }
208
209 const char *
210 SignalFilter::convertFilterIDToName (const FilterID filterID)
211 {
212   const char *name = "";
213
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;
234             
235   return (name);
236 }
237       
238 const SignalFilter::FilterMethodID
239 SignalFilter::convertFilterMethodNameToID (const char* const filterMethodName)
240 {
241   FilterMethodID fmID = FILTER_METHOD_INVALID;
242
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;
255
256   return (fmID);
257 }
258
259 const char *
260 SignalFilter::convertFilterMethodIDToName (const FilterMethodID fmID)
261 {
262   const char *name = "";
263
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);
276
277   return (name);
278 }
279
280 const SignalFilter::DomainID
281 SignalFilter::convertDomainNameToID (const char* const domainName)
282 {
283   DomainID dID = DOMAIN_INVALID;
284
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;
289
290   return (dID);
291 }
292
293 const char *
294 SignalFilter::convertDomainIDToName (const DomainID domain)
295 {
296   const char *name = "";
297
298   if (domain == DOMAIN_SPATIAL)
299     return (DOMAIN_SPATIAL_STR);
300   else if (domain == DOMAIN_FREQUENCY)
301     return (DOMAIN_FREQUENCY_STR);
302
303   return (name);
304 }
305
306
307 void
308 SignalFilter::filterSignal (const double input[], double output[], double dx, const int n) const
309 {
310   if (m_idFilterMethod == FILTER_METHOD_CONVOLUTION) {
311     for (int i = 0; i < n; i++)
312       output[i] = convolve (input, dx, i, n);
313   } else if (m_idFilterMethod == FILTER_METHOD_FOURIER) {
314     complex<double> fftSignal[n];
315     complex<double> complexOutput;
316     finiteFourierTransform (input, fftSignal, 1);
317     finiteFourierTransform (fftSignal, complexOutput, -1);
318     for (int i = 0; i < n; i++)
319       output[i] = complexOutput[i].mag();
320   }
321 }
322
323 void
324 SignalFilter::filterSignal (const float input[], double output[], double dx, const int n) const
325 {
326   if (m_idFilterMethod == FILTER_METHOD_CONVOLUTION) {
327     for (int i = 0; i < n; i++)
328       output[i] = convolve (input, dx, i, n);
329   }
330 }
331
332
333 double
334 SignalFilter::response (double x)
335 {
336   double response = 0;
337
338   if (m_idDomain == DOMAIN_SPATIAL)
339     response = spatialResponse (m_idFilter, m_bw, x, m_filterParam, m_numIntegral);
340   else if (m_idDomain == DOMAIN_FREQUENCY)
341     response = frequencyResponse (m_idFilter, m_bw, x, m_filterParam);
342
343   return (response);
344 }
345
346
347 double 
348 SignalFilter::spatialResponse (FilterID filterID, double bw, double x, double param, int nIntegral = 0)
349 {
350   if (nIntegral == 0)
351     return spatialResponseAnalytic (filterID, bw, x, param);
352   else
353     return spatialResponseCalc (filterID, bw, x, param, nIntegral);
354 }
355
356 /* NAME
357  *   filter_spatial_response_calc       Calculate filter by discrete inverse fourier
358  *                                      transform of filters's frequency
359  *                                      response
360  *
361  * SYNOPSIS
362  *   y = filter_spatial_response_calc (filt_type, x, m_bw, param, n)
363  *   double y                   Filter's response in spatial domain
364  *   int filt_type              Type of filter (definitions in ct.h)
365  *   double x                   Spatial position to evaluate filter
366  *   double m_bw                        Bandwidth of window
367  *   double param               General parameter for various filters
368  *   int n                      Number of points to calculate integrations
369  */
370
371 double 
372 SignalFilter::spatialResponseCalc (double x, double param, int nIntegral) const
373 {
374   return (spatialResponseCalc (m_idFilter, m_bw, x, param, nIntegral));
375 }
376
377 double 
378 SignalFilter::spatialResponseCalc (FilterID filterID, double bw, double x, double param, int n)
379 {
380   double zmin, zmax;
381
382   if (filterID == FILTER_TRIANGLE) {
383     zmin = 0;
384     zmax = bw;
385   } else {
386     zmin = 0;
387     zmax = bw / 2;
388   }
389   double zinc = (zmax - zmin) / (n - 1);
390
391   double z = zmin;
392   double q [n];
393   for (int i = 0; i < n; i++, z += zinc)
394     q[i] = frequencyResponse (filterID, bw, z, param) * cos (TWOPI * z * x);
395   
396   double y = 2 * integrateSimpson (zmin, zmax, q, n);
397   
398   return (y);
399 }
400
401
402 /* NAME
403  *    filter_frequency_response                 Return filter frequency response
404  *
405  * SYNOPSIS
406  *    h = filter_frequency_response (filt_type, u, m_bw, param)
407  *    double h                  Filters frequency response at u
408  *    int filt_type             Type of filter
409  *    double u                  Frequency to evaluate filter at
410  *    double m_bw                       Bandwidth of filter
411  *    double param              General input parameter for various filters
412  */
413
414 double 
415 SignalFilter::frequencyResponse (double u, double param) const
416 {
417   return frequencyResponse (m_idFilter, m_bw, u, param);
418 }
419
420
421 double 
422 SignalFilter::frequencyResponse (FilterID filterID, double bw, double u, double param)
423 {
424   double q;
425   double au = fabs (u);
426
427   switch (filterID) {
428   case FILTER_BANDLIMIT:
429     if (au >= bw / 2)
430       q = 0.;
431     else
432       q = 1;
433     break;
434   case FILTER_ABS_BANDLIMIT:
435     if (au >= bw / 2)
436       q = 0.;
437     else
438       q = au;
439     break;
440   case FILTER_TRIANGLE:
441     if (au >= bw)
442       q = 0;
443     else
444       q = 1 - au / bw;
445     break;
446   case FILTER_COSINE:
447     if (au >= bw / 2)
448       q = 0;
449     else
450       q = cos(PI * u / bw);
451     break;
452   case FILTER_ABS_COSINE:
453     if (au >= bw / 2)
454       q = 0;
455     else
456       q = au * cos(PI * u / bw);
457     break;
458   case FILTER_SINC:
459     q = bw * sinc (PI * bw * u, 1.);
460     break;
461   case FILTER_ABS_SINC:
462     q = au * bw * sinc (PI * bw * u, 1.);
463     break;
464   case FILTER_G_HAMMING:
465     if (au >= bw / 2)
466       q = 0;
467     else
468       q = param + (1 - param) * cos (TWOPI * u / bw);
469     break;
470   case FILTER_ABS_G_HAMMING:
471     if (au >= bw / 2)
472       q = 0;
473     else
474       q = au * (param + (1 - param) * cos(TWOPI * u / bw));
475     break;
476   default:
477     q = 0;
478     sys_error (ERR_WARNING, "Frequency response for filter %d not implemented [filter_frequency_response]", filterID);
479     break;
480   }
481   return (q);
482 }
483
484
485
486 /* NAME
487  *   filter_spatial_response_analytic                   Calculate filter by analytic inverse fourier
488  *                              transform of filters's frequency
489  *                              response
490  *
491  * SYNOPSIS
492  *   y = filter_spatial_response_analytic (filt_type, x, m_bw, param)
493  *   double y                   Filter's response in spatial domain
494  *   int filt_type              Type of filter (definitions in ct.h)
495  *   double x                   Spatial position to evaluate filter
496  *   double m_bw                        Bandwidth of window
497  *   double param               General parameter for various filters
498  */
499
500 double 
501 SignalFilter::spatialResponseAnalytic (double x, double param) const
502 {
503   return spatialResponseAnalytic (m_idFilter, m_bw, x, param);
504 }
505
506 double 
507 SignalFilter::spatialResponseAnalytic (FilterID filterID, double bw, double x, double param)
508 {
509   double q, temp;
510   double u = TWOPI * x;
511   double w = bw / 2;
512   double b = PI / bw;
513   double b2 = TWOPI / bw;
514
515   switch (filterID) {
516   case FILTER_BANDLIMIT:
517     q = bw * sinc(u * w, 1.0);
518     break;
519   case FILTER_TRIANGLE:
520     temp = sinc (u * w, 1.0);
521     q = bw * temp * temp;
522     break;
523   case FILTER_COSINE:
524     q = sinc(b-u,w) + sinc(b+u,w);
525     break;
526   case FILTER_G_HAMMING:
527     q = 2 * param * sin(u*w)/u + (1-param) * (sinc(b2-u, w) + sinc(b2+u, w));
528     break;
529   case FILTER_ABS_BANDLIMIT:
530     q = 2 * integral_abscos (u, w);
531     break;
532   case FILTER_ABS_COSINE:
533     q = integral_abscos(b-u,w) + integral_abscos(b+u,w);
534     break;
535   case FILTER_ABS_G_HAMMING:
536     q = 2 * param * integral_abscos(u,w) +
537       (1-param)*(integral_abscos(u-b2,w)+integral_abscos(u+b2,w));
538     break;
539   case FILTER_SHEPP:
540     if (fabs (u) < 1E-7)
541       q = 4. / (PI * bw * bw);
542     else
543       q = fabs ((2 / bw) * sin (u * w)) * sinc (u * w, 1.) * sinc (u * w, 1.);
544     break;
545   case FILTER_SINC:
546     if (fabs (x) < bw / 2)
547       q = 1.;
548     else
549       q = 0.;
550     break;
551   case FILTER_ABS_SINC:
552   default:
553     sys_error (ERR_WARNING, "Analytic filter type %d not implemented [filter_spatial_response_analytic]", filterID);
554     q = 0;
555     break;
556   }
557   
558   return (q);
559 }
560
561
562 /* NAME
563  *   sinc                       Return sin(x)/x function
564  *
565  * SYNOPSIS
566  *   v = sinc (x, mult)
567  *   double v                   sinc value
568  *   double x, mult
569  *
570  * DESCRIPTION
571  *   v = sin(x * mult) / x;
572  */
573
574
575 /* NAME
576  *   integral_abscos                    Returns integral of u*cos(u)
577  *
578  * SYNOPSIS
579  *   q = integral_abscos (u, w)
580  *   double q                   Integral value
581  *   double u                   Integration variable
582  *   double w                   Upper integration boundary
583  *
584  * DESCRIPTION
585  *   Returns the value of integral of u*cos(u)*dV for V = 0 to w
586  */
587
588 double 
589 SignalFilter::integral_abscos (double u, double w)
590 {
591   return (fabs (u) > F_EPSILON 
592      ? (cos(u * w) - 1) / (u * u) + w / u * sin (u * w) 
593      : (w * w / 2));
594 }
595
596
597 /* NAME
598  *    convolve                  Discrete convolution of two functions
599  *
600  * SYNOPSIS
601  *    r = convolve (f1, f2, dx, n, np, func_type)
602  *    double r                  Convolved result
603  *    double f1[], f2[]         Functions to be convolved
604  *    double dx                 Difference between successive x values
605  *    int n                     Array index to center convolution about
606  *    int np                    Number of points in f1 array
607  *    int func_type             EVEN or ODD or EVEN_AND_ODD function f2
608  *
609  * NOTES
610  *    f1 is the projection data, its indices range from 0 to np - 1.
611  *    The index for f2, the filter, ranges from -(np-1) to (np-1).
612  *    There are 3 ways to handle the negative vertices of f2:
613  *      1. If we know f2 is an EVEN function, then f2[-n] = f2[n].
614  *         All filters used in reconstruction are even.
615  *      2. If we know f2 is an ODD function, then f2[-n] = -f2[n] 
616  *      3. If f2 is both ODD AND EVEN, then we must store the value of f2
617  *         for negative indices.  Since f2 must range from -(np-1) to (np-1),
618  *         if we add (np - 1) to f2's array index, then f2's index will
619  *         range from 0 to 2 * (np - 1), and the origin, x = 0, will be
620  *         stored at f2[np-1].
621  */
622
623 double 
624 SignalFilter::convolve (const double func[], const double dx, const int n, const int np) const
625 {
626   double sum = 0.0;
627
628 #if UNOPTIMIZED_CONVOLUTION
629   for (int i = 0; i < np; i++)
630     sum += func[i] * m_vecFilter[n - i + (np - 1)];
631 #else
632   double* f2 = m_vecFilter + n + (np - 1);
633   for (int i = 0; i < np; i++)
634     sum += *func++ * *f2--;
635 #endif
636
637   return (sum * dx);
638 }
639
640
641 double 
642 SignalFilter::convolve (const float func[], const double dx, const int n, const int np) const
643 {
644   double sum = 0.0;
645
646 #if UNOPTIMIZED_CONVOLUTION
647 for (int i = 0; i < np; i++)
648   sum += func[i] * m_vecFilter[n - i + (np - 1)];
649 #else
650 double* f2 = m_vecFilter + n + (np - 1);
651 for (int i = 0; i < np; i++)
652   sum += *func++ * *f2--;
653 #endif
654
655   return (sum * dx);
656 }
657
658
659 void
660 SignalFilter::finiteFourierTransform (const double input[], complex<double> output[], const int n, int direction)
661 {
662   if (direction < 0)
663     direction = -1;
664   else 
665     direction = 1;
666     
667   double angleIncrement = 2 * PI / n;
668   for (int i = 0; i < n; i++) {
669     double sumReal = 0;
670     double sumImag = 0;
671     for (int j = 0; j < n; j++) {
672       double angle = i * j * angleIncrement * direction;
673       sumReal += input[i] * cos(angle);
674       sumImag += input[i] * sin(angle);
675     }
676     if (direction > 0) {
677       sumReal /= n;
678       sumImag /= n;
679     }
680     output[i] = complex<double> (sumReal, sumImag);
681   }
682 }
683
684 void
685 SignalFilter::finiteFourierTransform (const double input[], complex<double> output[], int direction) const
686 {
687   if (direction < 0)
688     direction = -1;
689   else 
690     direction = 1;
691     
692   double angleIncrement = 2 * PI / m_nPoints;
693   for (int i = 0; i < m_nPoints; i++) {
694     double sumReal = 0, sumImag = 0;
695     for (int j = 0; j < m_nPoints; j++) {
696       int tableIndex = i * j;
697       if (direction > 0) {
698         sumReal += input[i] * m_vecFourierCosTable[tableIndex];
699         sumImag += input[i] * m_vecFourierSinTable[tableIndex];
700       } else {
701         sumReal += input[i] * m_vecFourierCosTable[tableIndex];
702         sumImag -= input[i] * m_vecFourierSinTable[tableIndex];
703       }
704     }
705     if (direction > 0) {
706       sumReal /= m_nPoints;
707       sumImag /= m_nPoints;
708     }
709     output[i] = complex<double> (sumReal, sumImag);
710   }
711 }