r128: *** 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.7 2000/07/03 11:02:06 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 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 signalIncrement, 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, signalIncrement, n, param, m_idDomain, numIntegral);
74 }
75
76 SignalFilter::SignalFilter (const FilterID filterID, const FilterMethodID filterMethodID, double bw, double signalIncrement, int n, double param, const DomainID domainID, int numIntegral = 0)
77 {
78   init (filterID, filterMethodID, bw, signalIncrement, 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_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 n, 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_nameFilter = convertFilterIDToName (m_idFilter);
119   m_nameDomain = convertDomainIDToName (m_idDomain);
120   m_nameFilterMethod = convertFilterMethodIDToName (m_idFilterMethod);
121   m_fail = false;
122   m_nSignalPoints = n;
123   m_nFilterPoints = 2 * m_nSignalPoints - 1;
124
125   m_signalInc = signalIncrement;
126   m_filterMin = -signalIncrement * (m_nSignalPoints - 1);
127   m_filterMax = signalIncrement * (m_nSignalPoints - 1);
128   m_filterInc = (m_filterMax - m_filterMin) / (m_nFilterPoints - 1);
129   m_numIntegral = numint;
130   m_filterParam = param;  
131   m_vecFilter = new double[ m_nFilterPoints ];
132   if (m_idFilterMethod == FILTER_METHOD_FOURIER) {
133     int nFourier = n * n + 1;
134     double angleIncrement = (2. * PI) / n;
135     m_vecFourierCosTable = new double[ nFourier ];
136     m_vecFourierSinTable = new double[ nFourier ];
137     for (int i = 0; i < nFourier; i++) {
138       m_vecFourierCosTable[i] = cos (angleIncrement * i);
139       m_vecFourierSinTable[i] = sin (angleIncrement * i);
140     }
141   }
142
143   if (m_idFilter == FILTER_SHEPP) {
144     double a = 2 * m_bw;
145     double c = - 4. / (a * a);
146     int center = (m_nFilterPoints - 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_filterMin, i = 0; i < m_nFilterPoints; x += m_filterInc, 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_filterMin, i = 0; i < m_nFilterPoints; x += m_filterInc, 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 float input[], double output[]) const
309 {
310   if (m_idFilterMethod == FILTER_METHOD_CONVOLUTION) {
311     for (int i = 0; i < m_nSignalPoints; i++)
312       output[i] = convolve (input, m_signalInc, i, m_nSignalPoints);
313   } else if (m_idFilterMethod == FILTER_METHOD_FOURIER) {
314     complex<double> fftSignal[m_nSignalPoints];
315     complex<double> complexOutput;
316     finiteFourierTransform (input, fftSignal, 1);
317     //    finiteFourierTransform (fftSignal, complexOutput, -1);
318     //    for (int i = 0; i < m_nSignalPoints; i++)
319     //      output[i] = complexOutput[i].hypot();
320   }
321 }
322
323 double
324 SignalFilter::response (double x)
325 {
326   double response = 0;
327
328   if (m_idDomain == DOMAIN_SPATIAL)
329     response = spatialResponse (m_idFilter, m_bw, x, m_filterParam, m_numIntegral);
330   else if (m_idDomain == DOMAIN_FREQUENCY)
331     response = frequencyResponse (m_idFilter, m_bw, x, m_filterParam);
332
333   return (response);
334 }
335
336
337 double 
338 SignalFilter::spatialResponse (FilterID filterID, double bw, double x, double param, int nIntegral = 0)
339 {
340   if (nIntegral == 0)
341     return spatialResponseAnalytic (filterID, bw, x, param);
342   else
343     return spatialResponseCalc (filterID, bw, x, param, nIntegral);
344 }
345
346 /* NAME
347  *   filter_spatial_response_calc       Calculate filter by discrete inverse fourier
348  *                                      transform of filters's frequency
349  *                                      response
350  *
351  * SYNOPSIS
352  *   y = filter_spatial_response_calc (filt_type, x, m_bw, param, n)
353  *   double y                   Filter's response in spatial domain
354  *   int filt_type              Type of filter (definitions in ct.h)
355  *   double x                   Spatial position to evaluate filter
356  *   double m_bw                        Bandwidth of window
357  *   double param               General parameter for various filters
358  *   int n                      Number of points to calculate integrations
359  */
360
361 double 
362 SignalFilter::spatialResponseCalc (double x, double param, int nIntegral) const
363 {
364   return (spatialResponseCalc (m_idFilter, m_bw, x, param, nIntegral));
365 }
366
367 double 
368 SignalFilter::spatialResponseCalc (FilterID filterID, double bw, double x, double param, int n)
369 {
370   double zmin, zmax;
371
372   if (filterID == FILTER_TRIANGLE) {
373     zmin = 0;
374     zmax = bw;
375   } else {
376     zmin = 0;
377     zmax = bw / 2;
378   }
379   double zinc = (zmax - zmin) / (n - 1);
380
381   double z = zmin;
382   double q [n];
383   for (int i = 0; i < n; i++, z += zinc)
384     q[i] = frequencyResponse (filterID, bw, z, param) * cos (TWOPI * z * x);
385   
386   double y = 2 * integrateSimpson (zmin, zmax, q, n);
387   
388   return (y);
389 }
390
391
392 /* NAME
393  *    filter_frequency_response                 Return filter frequency response
394  *
395  * SYNOPSIS
396  *    h = filter_frequency_response (filt_type, u, m_bw, param)
397  *    double h                  Filters frequency response at u
398  *    int filt_type             Type of filter
399  *    double u                  Frequency to evaluate filter at
400  *    double m_bw                       Bandwidth of filter
401  *    double param              General input parameter for various filters
402  */
403
404 double 
405 SignalFilter::frequencyResponse (double u, double param) const
406 {
407   return frequencyResponse (m_idFilter, m_bw, u, param);
408 }
409
410
411 double 
412 SignalFilter::frequencyResponse (FilterID filterID, double bw, double u, double param)
413 {
414   double q;
415   double au = fabs (u);
416
417   switch (filterID) {
418   case FILTER_BANDLIMIT:
419     if (au >= bw / 2)
420       q = 0.;
421     else
422       q = 1;
423     break;
424   case FILTER_ABS_BANDLIMIT:
425     if (au >= bw / 2)
426       q = 0.;
427     else
428       q = au;
429     break;
430   case FILTER_TRIANGLE:
431     if (au >= bw)
432       q = 0;
433     else
434       q = 1 - au / bw;
435     break;
436   case FILTER_COSINE:
437     if (au >= bw / 2)
438       q = 0;
439     else
440       q = cos(PI * u / bw);
441     break;
442   case FILTER_ABS_COSINE:
443     if (au >= bw / 2)
444       q = 0;
445     else
446       q = au * cos(PI * u / bw);
447     break;
448   case FILTER_SINC:
449     q = bw * sinc (PI * bw * u, 1.);
450     break;
451   case FILTER_ABS_SINC:
452     q = au * bw * sinc (PI * bw * u, 1.);
453     break;
454   case FILTER_G_HAMMING:
455     if (au >= bw / 2)
456       q = 0;
457     else
458       q = param + (1 - param) * cos (TWOPI * u / bw);
459     break;
460   case FILTER_ABS_G_HAMMING:
461     if (au >= bw / 2)
462       q = 0;
463     else
464       q = au * (param + (1 - param) * cos(TWOPI * u / bw));
465     break;
466   default:
467     q = 0;
468     sys_error (ERR_WARNING, "Frequency response for filter %d not implemented [filter_frequency_response]", filterID);
469     break;
470   }
471   return (q);
472 }
473
474
475
476 /* NAME
477  *   filter_spatial_response_analytic                   Calculate filter by analytic inverse fourier
478  *                              transform of filters's frequency
479  *                              response
480  *
481  * SYNOPSIS
482  *   y = filter_spatial_response_analytic (filt_type, x, m_bw, param)
483  *   double y                   Filter's response in spatial domain
484  *   int filt_type              Type of filter (definitions in ct.h)
485  *   double x                   Spatial position to evaluate filter
486  *   double m_bw                        Bandwidth of window
487  *   double param               General parameter for various filters
488  */
489
490 double 
491 SignalFilter::spatialResponseAnalytic (double x, double param) const
492 {
493   return spatialResponseAnalytic (m_idFilter, m_bw, x, param);
494 }
495
496 double 
497 SignalFilter::spatialResponseAnalytic (FilterID filterID, double bw, double x, double param)
498 {
499   double q, temp;
500   double u = TWOPI * x;
501   double w = bw / 2;
502   double b = PI / bw;
503   double b2 = TWOPI / bw;
504
505   switch (filterID) {
506   case FILTER_BANDLIMIT:
507     q = bw * sinc(u * w, 1.0);
508     break;
509   case FILTER_TRIANGLE:
510     temp = sinc (u * w, 1.0);
511     q = bw * temp * temp;
512     break;
513   case FILTER_COSINE:
514     q = sinc(b-u,w) + sinc(b+u,w);
515     break;
516   case FILTER_G_HAMMING:
517     q = 2 * param * sin(u*w)/u + (1-param) * (sinc(b2-u, w) + sinc(b2+u, w));
518     break;
519   case FILTER_ABS_BANDLIMIT:
520     q = 2 * integral_abscos (u, w);
521     break;
522   case FILTER_ABS_COSINE:
523     q = integral_abscos(b-u,w) + integral_abscos(b+u,w);
524     break;
525   case FILTER_ABS_G_HAMMING:
526     q = 2 * param * integral_abscos(u,w) +
527       (1-param)*(integral_abscos(u-b2,w)+integral_abscos(u+b2,w));
528     break;
529   case FILTER_SHEPP:
530     if (fabs (u) < 1E-7)
531       q = 4. / (PI * bw * bw);
532     else
533       q = fabs ((2 / bw) * sin (u * w)) * sinc (u * w, 1.) * sinc (u * w, 1.);
534     break;
535   case FILTER_SINC:
536     if (fabs (x) < bw / 2)
537       q = 1.;
538     else
539       q = 0.;
540     break;
541   case FILTER_ABS_SINC:
542   default:
543     sys_error (ERR_WARNING, "Analytic filter type %d not implemented [filter_spatial_response_analytic]", filterID);
544     q = 0;
545     break;
546   }
547   
548   return (q);
549 }
550
551
552 /* NAME
553  *   sinc                       Return sin(x)/x function
554  *
555  * SYNOPSIS
556  *   v = sinc (x, mult)
557  *   double v                   sinc value
558  *   double x, mult
559  *
560  * DESCRIPTION
561  *   v = sin(x * mult) / x;
562  */
563
564
565 /* NAME
566  *   integral_abscos                    Returns integral of u*cos(u)
567  *
568  * SYNOPSIS
569  *   q = integral_abscos (u, w)
570  *   double q                   Integral value
571  *   double u                   Integration variable
572  *   double w                   Upper integration boundary
573  *
574  * DESCRIPTION
575  *   Returns the value of integral of u*cos(u)*dV for V = 0 to w
576  */
577
578 double 
579 SignalFilter::integral_abscos (double u, double w)
580 {
581   return (fabs (u) > F_EPSILON 
582      ? (cos(u * w) - 1) / (u * u) + w / u * sin (u * w) 
583      : (w * w / 2));
584 }
585
586
587 /* NAME
588  *    convolve                  Discrete convolution of two functions
589  *
590  * SYNOPSIS
591  *    r = convolve (f1, f2, dx, n, np, func_type)
592  *    double r                  Convolved result
593  *    double f1[], f2[]         Functions to be convolved
594  *    double dx                 Difference between successive x values
595  *    int n                     Array index to center convolution about
596  *    int np                    Number of points in f1 array
597  *    int func_type             EVEN or ODD or EVEN_AND_ODD function f2
598  *
599  * NOTES
600  *    f1 is the projection data, its indices range from 0 to np - 1.
601  *    The index for f2, the filter, ranges from -(np-1) to (np-1).
602  *    There are 3 ways to handle the negative vertices of f2:
603  *      1. If we know f2 is an EVEN function, then f2[-n] = f2[n].
604  *         All filters used in reconstruction are even.
605  *      2. If we know f2 is an ODD function, then f2[-n] = -f2[n] 
606  *      3. If f2 is both ODD AND EVEN, then we must store the value of f2
607  *         for negative indices.  Since f2 must range from -(np-1) to (np-1),
608  *         if we add (np - 1) to f2's array index, then f2's index will
609  *         range from 0 to 2 * (np - 1), and the origin, x = 0, will be
610  *         stored at f2[np-1].
611  */
612
613 double 
614 SignalFilter::convolve (const double func[], const double dx, const int n, const int np) const
615 {
616   double sum = 0.0;
617
618 #if UNOPTIMIZED_CONVOLUTION
619   for (int i = 0; i < np; i++)
620     sum += func[i] * m_vecFilter[n - i + (np - 1)];
621 #else
622   double* f2 = m_vecFilter + n + (np - 1);
623   for (int i = 0; i < np; i++)
624     sum += *func++ * *f2--;
625 #endif
626
627   return (sum * dx);
628 }
629
630
631 double 
632 SignalFilter::convolve (const float func[], const double dx, const int n, const int np) const
633 {
634   double sum = 0.0;
635
636 #if UNOPTIMIZED_CONVOLUTION
637 for (int i = 0; i < np; i++)
638   sum += func[i] * m_vecFilter[n - i + (np - 1)];
639 #else
640 double* f2 = m_vecFilter + n + (np - 1);
641 for (int i = 0; i < np; i++)
642   sum += *func++ * *f2--;
643 #endif
644
645   return (sum * dx);
646 }
647
648
649 void
650 SignalFilter::finiteFourierTransform (const float input[], complex<double> output[], const int n, int direction)
651 {
652   if (direction < 0)
653     direction = -1;
654   else 
655     direction = 1;
656     
657   double angleIncrement = 2 * PI / n;
658   for (int i = 0; i < n; i++) {
659     double sumReal = 0;
660     double sumImag = 0;
661     for (int j = 0; j < n; j++) {
662       double angle = i * j * angleIncrement * direction;
663       sumReal += input[i] * cos(angle);
664       sumImag += input[i] * sin(angle);
665     }
666     if (direction > 0) {
667       sumReal /= n;
668       sumImag /= n;
669     }
670     output[i] = complex<double> (sumReal, sumImag);
671   }
672 }
673
674 void
675 SignalFilter::finiteFourierTransform (const float input[], complex<double> output[], int direction) const
676 {
677   if (direction < 0)
678     direction = -1;
679   else 
680     direction = 1;
681     
682   double angleIncrement = 2 * PI / m_nSignalPoints;
683   for (int i = 0; i < m_nSignalPoints; i++) {
684     double sumReal = 0, sumImag = 0;
685     for (int j = 0; j < m_nSignalPoints; j++) {
686       int tableIndex = i * j;
687       if (direction > 0) {
688         sumReal += input[i] * m_vecFourierCosTable[tableIndex];
689         sumImag += input[i] * m_vecFourierSinTable[tableIndex];
690       } else {
691         sumReal += input[i] * m_vecFourierCosTable[tableIndex];
692         sumImag -= input[i] * m_vecFourierSinTable[tableIndex];
693       }
694     }
695     if (direction > 0) {
696       sumReal /= m_nSignalPoints;
697       sumImag /= m_nSignalPoints;
698     }
699     output[i] = complex<double> (sumReal, sumImag);
700   }
701 }