r99: *** empty log message ***
[ctsim.git] / libctsim / convolve.cpp
diff --git a/libctsim/convolve.cpp b/libctsim/convolve.cpp
new file mode 100644 (file)
index 0000000..52abd94
--- /dev/null
@@ -0,0 +1,84 @@
+/*****************************************************************************
+** FILE IDENTIFICATION
+**
+**     Name:           Convolve.c              Convolution functions
+**     Date Started:   13 Mar 86
+**
+**  This is part of the CTSim program
+**  Copyright (C) 1983-2000 Kevin Rosenberg
+**
+**  This program is free software; you can redistribute it and/or modify
+**  it under the terms of the GNU General Public License (version 2) as
+**  published by the Free Software Foundation.
+**
+**  This program is distributed in the hope that it will be useful,
+**  but WITHOUT ANY WARRANTY; without even the implied warranty of
+**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+**  GNU General Public License for more details.
+**
+**  You should have received a copy of the GNU General Public License
+**  along with this program; if not, write to the Free Software
+**  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+******************************************************************************/
+
+#include "ct.h"
+
+
+/* NAME
+ *    convolve                 Discrete convolution of two functions
+ *
+ * SYNOPSIS
+ *    r = convolve (f1, f2, dx, n, np, func_type)
+ *    double r                 Convolved result
+ *    double f1[], f2[]                Functions to be convolved
+ *    double dx                        Difference between successive x values
+ *    int n                    Array index to center convolution about
+ *    int np                   Number of points in f1 array
+ *    int func_type            EVEN or ODD or EVEN_AND_ODD function f2
+ *
+ * NOTES
+ *    f1 is the projection data, its indices range from 0 to np - 1.
+ *    The index for f2, the filter, ranges from -(np-1) to (np-1).
+ *    There are 3 ways to handle the negative vertices of f2:
+ *     1. If we know f2 is an EVEN function, then f2[-n] = f2[n].
+ *        All filters used in reconstruction are even.
+ *      2. If we know f2 is an ODD function, then f2[-n] = -f2[n] 
+ *      3. If f2 is both ODD AND EVEN, then we must store the value of f2
+ *        for negative indices.  Since f2 must range from -(np-1) to (np-1),
+ *        if we add (np - 1) to f2's array index, then f2's index will
+ *        range from 0 to 2 * (np - 1), and the origin, x = 0, will be
+ *        stored at f2[np-1].
+ */
+
+double 
+convolve (const double f1[], const double f2[], const double dx, const int n, const int np, const FunctionSymmetry func_type)
+{
+  double sum = 0.0;
+
+  if (func_type == FUNC_BOTH) {
+#if UNOPTIMIZED_CONVOLUTION
+    for (int i = 0; i < np; i++)
+      sum += f1[i] * f2[n - i + (np - 1)];
+#else
+    f2 += n + (np - 1);
+    for (int i = 0; i < np; i++)
+      sum += *f1++ * *f2--;
+#endif
+  }  else if (func_type == FUNC_EVEN) {
+    for (int i = 0; i < np; i++) {
+      int k = abs (n - i);
+      sum += f1[i] * f2[k];
+    }
+  } else if (func_type == FUNC_ODD) {
+    for (int i = 0; i < np; i++) {
+      int k = n - i;
+      if (k < 0)
+       sum -= f1[i] * f2[k];
+      else
+       sum += f1[i] * f2[k];
+    }
+  } else
+    sys_error (ERR_WARNING, "Illegal function type %d [convolve]", func_type);
+
+  return (sum * dx);
+}