r117: *** 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.3 2000/06/22 10:17:28 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 filter
41  *   double param       General input parameter to filters
42  *   int domain         FREQ 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, double bw, double xmin, double xmax, int n, double param, const char* domainName, int numint)
48 {
49   m_idFilter = convertFilterNameToID (filterName);
50   m_idDomain = convertDomainNameToID (domainName);
51   init (m_idFilter, bw, xmin, xmax, n, param, m_idDomain, numint);
52 }
53
54 SignalFilter::SignalFilter (const FilterID filterID, double bw, double xmin, double xmax, int n, double param, const DomainID domainID, int numint)
55 {
56   init (filterID, bw, xmin, xmax, n, param, domainID, numint);
57 }
58
59 void
60 SignalFilter::init (const FilterID filterID, double bw, double xmin, double xmax, int n, double param, const DomainID domainID, int numint)
61 {
62   m_bw = bw;
63   m_idFilter = filterID;
64   m_idDomain = domainID;
65   if (m_idFilter == FILTER_INVALID || m_idDomain == DOMAIN_INVALID) {
66     m_fail = true;
67     return;
68   }
69   m_nameFilter = convertFilterIDToName (m_idFilter);
70   m_nameDomain = convertDomainIDToName (m_idDomain);
71   m_fail = false;
72   m_nPoints = n;
73   m_xmin = xmin;
74   m_xmax = xmax;
75   m_vecFilter = new double[n];
76
77   double xinc = (m_xmax - m_xmin) / (m_nPoints - 1);
78
79   if (m_idFilter == FILTER_SHEPP) {
80     double a = 2 * m_bw;
81     double c = - 4. / (a * a);
82     int center = (m_nPoints - 1) / 2;
83     int sidelen = center;
84     m_vecFilter[center] = 4. / (a * a);
85
86     for (int i = 1; i <= sidelen; i++ )
87       m_vecFilter [center + i] = m_vecFilter [center - i] = c / (4 * (i * i) - 1);
88   } else if (m_idDomain == DOMAIN_FREQ) {
89     double x;
90     int i;
91     for (x = m_xmin, i = 0; i < m_nPoints; x += xinc, i++)
92       m_vecFilter[i] = frequencyResponse (x, param);
93   } else if (m_idDomain == DOMAIN_SPATIAL) {
94     double x;
95     int i;
96     for (x = m_xmin, i = 0; i < m_nPoints; x += xinc, i++)
97       if (numint == 0)
98         m_vecFilter[i] = spatialResponseAnalytic (x, param);
99       else
100         m_vecFilter[i] = spatialResponseCalc (x, param, numint);
101   } else {
102       sys_error (ERR_WARNING, "Illegal domain %d [filt_generate]", m_idDomain);
103       m_fail = true;
104   }
105 }
106
107 SignalFilter::~SignalFilter (void)
108 {
109     delete m_vecFilter;
110 }
111
112
113 SignalFilter::FilterID
114 SignalFilter::convertFilterNameToID (const char *filterName)
115 {
116   FilterID filterID;
117
118   if (strcasecmp (filterName, FILTER_BANDLIMIT_STR) == 0)
119     filterID = FILTER_BANDLIMIT;
120   else if (strcasecmp (filterName, FILTER_HAMMING_STR) == 0)
121     filterID = FILTER_G_HAMMING;
122   else if (strcasecmp (filterName, FILTER_SINC_STR) == 0)
123     filterID = FILTER_SINC;
124   else if (strcasecmp (filterName, FILTER_COS_STR) == 0)
125     filterID = FILTER_COSINE;
126   else if (strcasecmp (filterName, FILTER_TRIANGLE_STR) == 0)
127     filterID = FILTER_TRIANGLE;
128   else if (strcasecmp (filterName, FILTER_ABS_BANDLIMIT_STR) == 0)
129     filterID = FILTER_ABS_BANDLIMIT;
130   else if (strcasecmp (filterName, FILTER_ABS_HAMMING_STR) == 0)
131     filterID = FILTER_ABS_G_HAMMING;
132   else if (strcasecmp (filterName, FILTER_ABS_SINC_STR) == 0)
133     filterID = FILTER_ABS_SINC;
134   else if (strcasecmp (filterName, FILTER_ABS_COS_STR) == 0)
135     filterID = FILTER_ABS_COSINE;
136   else if (strcasecmp (filterName, FILTER_SHEPP_STR) == 0)
137     filterID = FILTER_SHEPP;
138   else {
139     sys_error(ERR_WARNING, "Invalid filter type %s\n", filterName);
140     filterID = FILTER_INVALID;
141   }
142
143   return (filterID);
144 }
145
146 const char *
147 SignalFilter::convertFilterIDToName (const FilterID filterID)
148 {
149   const char *name = "";
150
151   if (filterID == FILTER_SHEPP)
152     name = FILTER_SHEPP_STR;
153   else if (filterID == FILTER_ABS_COSINE)
154     name = FILTER_ABS_COS_STR;
155   else if (filterID == FILTER_ABS_SINC)
156     name = FILTER_ABS_SINC_STR;
157   else if (filterID == FILTER_ABS_G_HAMMING)
158     name = FILTER_ABS_HAMMING_STR;
159   else if (filterID == FILTER_ABS_BANDLIMIT)
160     name = FILTER_ABS_BANDLIMIT_STR;
161   else if (filterID == FILTER_COSINE)
162     name = FILTER_COS_STR;
163   else if (filterID == FILTER_SINC)
164     name = FILTER_SINC_STR;
165   else if (filterID == FILTER_G_HAMMING)
166     name = FILTER_HAMMING_STR;
167   else if (filterID == FILTER_BANDLIMIT)
168     name = FILTER_BANDLIMIT_STR;
169   else if (filterID == FILTER_TRIANGLE)
170     name = FILTER_TRIANGLE_STR;
171             
172   return (name);
173 }
174       
175 const SignalFilter::DomainID
176 SignalFilter::convertDomainNameToID (const char* const domainName)
177 {
178   DomainID dID;
179
180   if (strcasecmp (domainName, DOMAIN_SPATIAL_STR) == 0)
181     dID = DOMAIN_SPATIAL;
182   else if (strcasecmp (domainName, DOMAIN_FREQ_STR) == 0)
183     dID = DOMAIN_FREQ;
184   else
185     dID = DOMAIN_INVALID;
186
187   return (dID);
188 }
189
190 const char *
191 SignalFilter::convertDomainIDToName (const DomainID domain)
192 {
193   const char *name = "";
194
195   if (domain == DOMAIN_SPATIAL)
196     return (DOMAIN_SPATIAL_STR);
197   else if (domain == DOMAIN_FREQ)
198     return (DOMAIN_FREQ_STR);
199
200   return (name);
201 }
202
203
204 double
205 SignalFilter::response (const char* filterName, const char* domainName, double bw, double x, double filt_param)
206 {
207   double response = 0;
208   FilterID filterID = convertFilterNameToID (filterName);
209   DomainID domainID = convertDomainNameToID (domainName);
210
211   if (domainID == DOMAIN_SPATIAL)
212     response = spatialResponseAnalytic (filterID, bw, x, filt_param);
213   else if (domainID == DOMAIN_FREQ)
214     response = frequencyResponse (filterID, bw, x, filt_param);
215
216   return (response);
217 }
218
219 /* NAME
220  *   filter_spatial_response_calc       Calculate filter by discrete inverse fourier
221  *                                      transform of filters's frequency
222  *                                      response
223  *
224  * SYNOPSIS
225  *   y = filter_spatial_response_calc (filt_type, x, m_bw, param, n)
226  *   double y                   Filter's response in spatial domain
227  *   int filt_type              Type of filter (definitions in ct.h)
228  *   double x                   Spatial position to evaluate filter
229  *   double m_bw                        Bandwidth of window
230  *   double param               General parameter for various filters
231  *   int n                      Number of points to calculate integrations
232  */
233
234 double 
235 SignalFilter::spatialResponseCalc (double x, double param, int n) const
236 {
237   return (spatialResponseCalc (m_idFilter, m_bw, x, param, n));
238 }
239
240 double 
241 SignalFilter::spatialResponseCalc (FilterID filterID, double bw, double x, double param, int n)
242 {
243   double zmin, zmax;
244
245   if (filterID == FILTER_TRIANGLE) {
246     zmin = 0;
247     zmax = bw;
248   } else {
249     zmin = 0;
250     zmax = bw / 2;
251   }
252   double zinc = (zmax - zmin) / (n - 1);
253
254   double z = zmin;
255   double q [n];
256   for (int i = 0; i < n; i++, z += zinc)
257     q[i] = frequencyResponse (filterID, bw, z, param) * cos (TWOPI * z * x);
258   
259   double y = 2 * integrateSimpson (zmin, zmax, q, n);
260   
261   return (y);
262 }
263
264
265 /* NAME
266  *    filter_frequency_response                 Return filter frequency response
267  *
268  * SYNOPSIS
269  *    h = filter_frequency_response (filt_type, u, m_bw, param)
270  *    double h                  Filters frequency response at u
271  *    int filt_type             Type of filter
272  *    double u                  Frequency to evaluate filter at
273  *    double m_bw                       Bandwidth of filter
274  *    double param              General input parameter for various filters
275  */
276
277 double 
278 SignalFilter::frequencyResponse (double u, double param) const
279 {
280   return frequencyResponse (m_idFilter, m_bw, u, param);
281 }
282
283
284 double 
285 SignalFilter::frequencyResponse (FilterID filterID, double bw, double u, double param)
286 {
287   double q;
288   double au = fabs (u);
289
290   switch (filterID) {
291   case FILTER_BANDLIMIT:
292     if (au >= bw / 2)
293       q = 0.;
294     else
295       q = 1;
296     break;
297   case FILTER_ABS_BANDLIMIT:
298     if (au >= bw / 2)
299       q = 0.;
300     else
301       q = au;
302     break;
303   case FILTER_TRIANGLE:
304     if (au >= bw)
305       q = 0;
306     else
307       q = 1 - au / bw;
308     break;
309   case FILTER_COSINE:
310     if (au >= bw / 2)
311       q = 0;
312     else
313       q = cos(PI * u / bw);
314     break;
315   case FILTER_ABS_COSINE:
316     if (au >= bw / 2)
317       q = 0;
318     else
319       q = au * cos(PI * u / bw);
320     break;
321   case FILTER_SINC:
322     q = bw * sinc (PI * bw * u, 1.);
323     break;
324   case FILTER_ABS_SINC:
325     q = au * bw * sinc (PI * bw * u, 1.);
326     break;
327   case FILTER_G_HAMMING:
328     if (au >= bw / 2)
329       q = 0;
330     else
331       q = param + (1 - param) * cos (TWOPI * u / bw);
332     break;
333   case FILTER_ABS_G_HAMMING:
334     if (au >= bw / 2)
335       q = 0;
336     else
337       q = au * (param + (1 - param) * cos(TWOPI * u / bw));
338     break;
339   default:
340     q = 0;
341     sys_error (ERR_WARNING, "Frequency response for filter %d not implemented [filter_frequency_response]", filterID);
342     break;
343   }
344   return (q);
345 }
346
347
348
349 /* NAME
350  *   filter_spatial_response_analytic                   Calculate filter by analytic inverse fourier
351  *                              transform of filters's frequency
352  *                              response
353  *
354  * SYNOPSIS
355  *   y = filter_spatial_response_analytic (filt_type, x, m_bw, param)
356  *   double y                   Filter's response in spatial domain
357  *   int filt_type              Type of filter (definitions in ct.h)
358  *   double x                   Spatial position to evaluate filter
359  *   double m_bw                        Bandwidth of window
360  *   double param               General parameter for various filters
361  */
362
363 double 
364 SignalFilter::spatialResponseAnalytic (double x, double param) const
365 {
366   return spatialResponseAnalytic (m_idFilter, m_bw, x, param);
367 }
368
369 double 
370 SignalFilter::spatialResponseAnalytic (FilterID filterID, double bw, double x, double param)
371 {
372   double q, temp;
373   double u = TWOPI * x;
374   double w = bw / 2;
375   double b = PI / bw;
376   double b2 = TWOPI / bw;
377
378   switch (filterID) {
379   case FILTER_BANDLIMIT:
380     q = bw * sinc(u * w, 1.0);
381     break;
382   case FILTER_TRIANGLE:
383     temp = sinc (u * w, 1.0);
384     q = bw * temp * temp;
385     break;
386   case FILTER_COSINE:
387     q = sinc(b-u,w) + sinc(b+u,w);
388     break;
389   case FILTER_G_HAMMING:
390     q = 2 * param * sin(u*w)/u + (1-param) * (sinc(b2-u, w) + sinc(b2+u, w));
391     break;
392   case FILTER_ABS_BANDLIMIT:
393     q = 2 * integral_abscos (u, w);
394     break;
395   case FILTER_ABS_COSINE:
396     q = integral_abscos(b-u,w) + integral_abscos(b+u,w);
397     break;
398   case FILTER_ABS_G_HAMMING:
399     q = 2 * param * integral_abscos(u,w) +
400       (1-param)*(integral_abscos(u-b2,w)+integral_abscos(u+b2,w));
401     break;
402   case FILTER_SHEPP:
403     if (fabs (u) < 1E-7)
404       q = 4. / (PI * bw * bw);
405     else
406       q = fabs ((2 / bw) * sin (u * w)) * sinc (u * w, 1.) * sinc (u * w, 1.);
407     break;
408   case FILTER_SINC:
409     if (fabs (x) < bw / 2)
410       q = 1.;
411     else
412       q = 0.;
413     break;
414   case FILTER_ABS_SINC:
415   default:
416     sys_error (ERR_WARNING, "Analytic filter type %d not implemented [filter_spatial_response_analytic]", filterID);
417     q = 0;
418     break;
419   }
420   
421   return (q);
422 }
423
424
425 /* NAME
426  *   sinc                       Return sin(x)/x function
427  *
428  * SYNOPSIS
429  *   v = sinc (x, mult)
430  *   double v                   sinc value
431  *   double x, mult
432  *
433  * DESCRIPTION
434  *   v = sin(x * mult) / x;
435  */
436
437
438 /* NAME
439  *   integral_abscos                    Returns integral of u*cos(u)
440  *
441  * SYNOPSIS
442  *   q = integral_abscos (u, w)
443  *   double q                   Integral value
444  *   double u                   Integration variable
445  *   double w                   Upper integration boundary
446  *
447  * DESCRIPTION
448  *   Returns the value of integral of u*cos(u)*dV for V = 0 to w
449  */
450
451 double 
452 SignalFilter::integral_abscos (double u, double w)
453 {
454   if (fabs (u) > F_EPSILON)
455     return (cos(u * w) - 1) / (u * u) + w / u * sin (u * w);
456   else
457     return (w * w / 2);
458 }
459
460
461 /* NAME
462  *    convolve                  Discrete convolution of two functions
463  *
464  * SYNOPSIS
465  *    r = convolve (f1, f2, dx, n, np, func_type)
466  *    double r                  Convolved result
467  *    double f1[], f2[]         Functions to be convolved
468  *    double dx                 Difference between successive x values
469  *    int n                     Array index to center convolution about
470  *    int np                    Number of points in f1 array
471  *    int func_type             EVEN or ODD or EVEN_AND_ODD function f2
472  *
473  * NOTES
474  *    f1 is the projection data, its indices range from 0 to np - 1.
475  *    The index for f2, the filter, ranges from -(np-1) to (np-1).
476  *    There are 3 ways to handle the negative vertices of f2:
477  *      1. If we know f2 is an EVEN function, then f2[-n] = f2[n].
478  *         All filters used in reconstruction are even.
479  *      2. If we know f2 is an ODD function, then f2[-n] = -f2[n] 
480  *      3. If f2 is both ODD AND EVEN, then we must store the value of f2
481  *         for negative indices.  Since f2 must range from -(np-1) to (np-1),
482  *         if we add (np - 1) to f2's array index, then f2's index will
483  *         range from 0 to 2 * (np - 1), and the origin, x = 0, will be
484  *         stored at f2[np-1].
485  */
486
487 double 
488 SignalFilter::convolve (const double func[], const double dx, const int n, const int np) const
489 {
490   double sum = 0.0;
491
492 #if UNOPTIMIZED_CONVOLUTION
493   for (int i = 0; i < np; i++)
494     sum += func[i] * m_vecFilter[n - i + (np - 1)];
495 #else
496   double* f2 = m_vecFilter + n + (np - 1);
497   for (int i = 0; i < np; i++)
498     sum += *func++ * *f2--;
499 #endif
500
501   return (sum * dx);
502 }
503
504
505 double 
506 SignalFilter::convolve (const float func[], const double dx, const int n, const int np) const
507 {
508   double sum = 0.0;
509
510 #if UNOPTIMIZED_CONVOLUTION
511 for (int i = 0; i < np; i++)
512   sum += func[i] * m_vecFilter[n - i + (np - 1)];
513 #else
514 double* f2 = m_vecFilter + n + (np - 1);
515 for (int i = 0; i < np; i++)
516   sum += *func++ * *f2--;
517 #endif
518
519   return (sum * dx);
520 }
521