r187: *** empty log message ***
[ctsim.git] / libctsim / procsignal.cpp
index 70d759cbacc471f96ab39b3d8fb3ea3dd990e378..a9ab2c7956053165a89eff72eea2ad5ec813a5e5 100644 (file)
@@ -9,7 +9,7 @@
 **  This is part of the CTSim program
 **  Copyright (C) 1983-2000 Kevin Rosenberg
 **
-**  $Id: procsignal.cpp,v 1.2 2000/08/22 07:02:48 kevin Exp $
+**  $Id: procsignal.cpp,v 1.6 2000/09/02 05:10:39 kevin Exp $
 **
 **  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
@@ -77,7 +77,7 @@ const int ProcessSignal::s_iFilterGenerationCount = sizeof(s_aszFilterGeneration
 // CLASS IDENTIFICATION
 //   ProcessSignal
 //
-ProcessSignal::ProcessSignal (const char* szFilterName, const char* szFilterMethodName, double dBandwidth, double dSignalIncrement, int nSignalPoints, double dFilterParam, const char* szDomainName, const char* szFilterGenerationName, int iZeropad = 0, int iPreinterpolationFactor = 1, int iTraceLevel = TRACE_NONE)
+ProcessSignal::ProcessSignal (const char* szFilterName, const char* szFilterMethodName, double dBandwidth, double dSignalIncrement, int nSignalPoints, double dFilterParam, const char* szDomainName, const char* szFilterGenerationName, int iZeropad, int iPreinterpolationFactor, int iTraceLevel, int iGeometry, double dFocalLength, SGP* pSGP)
     : m_adFourierCosTable(NULL), m_adFourierSinTable(NULL), m_adFilter(NULL), m_fail(false)
 {
   m_idFilterMethod = convertFilterMethodNameToID (szFilterMethodName);
@@ -109,17 +109,20 @@ ProcessSignal::ProcessSignal (const char* szFilterName, const char* szFilterMeth
     return;
   }
 
-  init (m_idFilter, m_idFilterMethod, dBandwidth, dSignalIncrement, nSignalPoints, dFilterParam, m_idDomain, m_idFilterGeneration, iZeropad, iPreinterpolationFactor, iTraceLevel);
+  init (m_idFilter, m_idFilterMethod, dBandwidth, dSignalIncrement, nSignalPoints, dFilterParam, m_idDomain, m_idFilterGeneration, iZeropad, iPreinterpolationFactor, iTraceLevel, iGeometry, dFocalLength, pSGP);
 }
 
 
 void
-ProcessSignal::init (const int idFilter, const int idFilterMethod, double dBandwidth, double dSignalIncrement, int nSignalPoints, double dFilterParam, const int idDomain, const int idFilterGeneration, const int iZeropad, const int iPreinterpolationFactor, int iTraceLevel)
+ProcessSignal::init (const int idFilter, const int idFilterMethod, double dBandwidth, double dSignalIncrement, int nSignalPoints, double dFilterParam, const int idDomain, const int idFilterGeneration, const int iZeropad, const int iPreinterpolationFactor, int iTraceLevel, int iGeometry, double dFocalLength, SGP* pSGP)
 {
   m_idFilter = idFilter;
   m_idDomain = idDomain;
   m_idFilterMethod = idFilterMethod;
   m_idFilterGeneration = idFilterGeneration;
+  m_idGeometry = iGeometry;
+  m_dFocalLength = dFocalLength;
+
   if (m_idFilter == SignalFilter::FILTER_INVALID || m_idDomain == SignalFilter::DOMAIN_INVALID || m_idFilterMethod == FILTER_METHOD_INVALID || m_idFilterGeneration == FILTER_GENERATION_INVALID) {
     m_fail = true;
     return;
@@ -134,6 +137,13 @@ ProcessSignal::init (const int idFilter, const int idFilterMethod, double dBandw
   m_iZeropad = iZeropad;
   m_iPreinterpolationFactor = iPreinterpolationFactor;
 
+  // scale signalInc/BW to signalInc/2 to adjust for imaginary detector
+  // through origin of phantom, see Kak-Slaney Fig 3.22, for Collinear
+  if (m_idGeometry == Scanner::GEOMETRY_EQUILINEAR) {
+    m_dSignalInc /= 2;
+    m_dBandwidth *= 2;
+  }
+
   if (m_idFilterMethod == FILTER_METHOD_FFT) {
 #if HAVE_FFTW
     m_idFilterMethod = FILTER_METHOD_RFFTW;
@@ -168,62 +178,62 @@ ProcessSignal::init (const int idFilter, const int idFilterMethod, double dBandw
        m_adFilter = new double[ m_nFilterPoints ];
        double adFrequencyFilter [m_nFilterPoints];
        filter.copyFilterData (adFrequencyFilter, 0, m_nFilterPoints);
-       if (m_traceLevel >= TRACE_PLOT) {
-         SGPDriver sgpDriver ("Frequency Filter: Natural Order");
-         SGP sgp (sgpDriver);
-         EZPlot ezplot (sgp);
-
-         ezplot.ezset ("title Filter Response: Natural Order");
-         ezplot.addCurve (adFrequencyFilter, m_nFilterPoints);
-         ezplot.plot();
-         cio_put_str ("Press any key to continue");
-         cio_kb_getc ();
+       EZPlot* pEZPlot = NULL;
+       if (pSGP && m_traceLevel >= Trace::TRACE_PLOT) {
+         pEZPlot = new EZPlot (*pSGP);
+         pEZPlot->ezset ("title Filter Response: Natural Order");
+         pEZPlot->ezset ("ylength 0.25");
+         pEZPlot->addCurve (adFrequencyFilter, m_nFilterPoints);
+         pEZPlot->plot();
        }
            
        shuffleNaturalToFourierOrder (adFrequencyFilter, m_nFilterPoints);
-       if (m_traceLevel >= TRACE_PLOT) {
-         SGPDriver sgpDriver ("Frequency Filter: Fourier Order");
-         SGP sgp (sgpDriver);
-         EZPlot ezplot (sgp);
-
-         ezplot.ezset ("title Filter Response: Fourier Order");
-         ezplot.addCurve (adFrequencyFilter, m_nFilterPoints);
-         ezplot.plot();
-         cio_put_str ("Press any key to continue");
-         cio_kb_getc ();
+       if (pEZPlot && m_traceLevel >= Trace::TRACE_PLOT) {
+         pEZPlot->ezset ("title Filter Response: Fourier Order");
+         pEZPlot->ezset ("ylength 0.25");
+         pEZPlot->ezset ("yporigin 0.25");
+         pEZPlot->addCurve (adFrequencyFilter, m_nFilterPoints);
+         pEZPlot->plot();
        }
        ProcessSignal::finiteFourierTransform (adFrequencyFilter, m_adFilter, m_nFilterPoints, -1);
-       if (m_traceLevel >= TRACE_PLOT) {
-         SGPDriver sgpDriver ("Inverse Fourier Frequency: Fourier Order");
-         SGP sgp (sgpDriver);
-         EZPlot ezplot (sgp);
-
-         ezplot.ezset ("title Inverse Fourier Frequency: Fourier Order");
-         ezplot.addCurve (m_adFilter, m_nFilterPoints);
-         ezplot.plot();
-         cio_put_str ("Press any key to continue");
-         cio_kb_getc ();
+       if (pEZPlot && m_traceLevel >= Trace::TRACE_PLOT) {
+         pEZPlot->ezset ("title Inverse Fourier Frequency: Fourier Order");
+         pEZPlot->ezset ("ylength 0.25");
+         pEZPlot->ezset ("yporigin 0.50");
+         pEZPlot->addCurve (m_adFilter, m_nFilterPoints);
+         pEZPlot->plot();
        }
        shuffleFourierToNaturalOrder (m_adFilter, m_nFilterPoints);
-       if (m_traceLevel >= TRACE_PLOT) {
-         SGPDriver sgpDriver ("Inverse Fourier Frequency: Natural Order");
-         SGP sgp (sgpDriver);
-         EZPlot ezplot (sgp);
-
-         ezplot.ezset ("title Inverse Fourier Frequency: Natural Order");
-         ezplot.addCurve (m_adFilter, m_nFilterPoints);
-         ezplot.plot();
-         cio_put_str ("Press any key to continue");
-         cio_kb_getc ();
+       if (pEZPlot && m_traceLevel >= Trace::TRACE_PLOT) {
+         pEZPlot->ezset ("title Inverse Fourier Frequency: Natural Order");
+         pEZPlot->ezset ("ylength 0.25");
+         pEZPlot->ezset ("yporigin 0.75");
+         pEZPlot->addCurve (m_adFilter, m_nFilterPoints);
+         pEZPlot->plot();
+         delete pEZPlot;
        }
        for (int i = 0; i < m_nFilterPoints; i++) {
            m_adFilter[i] /= m_dSignalInc;
        }
     }
-  } 
+    if (m_idGeometry == Scanner::GEOMETRY_EQUILINEAR) {
+       for (int i = 0; i < m_nFilterPoints; i++)
+           m_adFilter[i] *= 0.5;
+    } else if (m_idGeometry == Scanner::GEOMETRY_EQUIANGULAR) {
+       for (int i = 0; i < m_nFilterPoints; i++) {
+         int iDetFromZero = i - ((m_nFilterPoints - 1) / 2);
+         double sinScale = sin (iDetFromZero * m_dSignalInc);
+         if (fabs(sinScale) < 1E-7)
+             sinScale = 1;
+         else
+             sinScale = (iDetFromZero * m_dSignalInc) / sinScale;
+         double dScale = 0.5 * sinScale * sinScale;
+         m_adFilter[i] *= dScale;
+       }
+    } // if (geometry)
+  } // if (spatial filtering)
 
-  // Frequency-based filtering
-  else if (m_bFrequencyFiltering) {
+  else if (m_bFrequencyFiltering) {  // Frequency-based filtering
 
     if (m_idFilterGeneration == FILTER_GENERATION_DIRECT) {
       // calculate number of filter points with zeropadding
@@ -235,8 +245,10 @@ ProcessSignal::init (const int idFilter, const int idFilterMethod, double dBandw
          nextPowerOf2++;
        nextPowerOf2 += (m_iZeropad - 1);
        m_nFilterPoints = 1 << nextPowerOf2;
-       if (m_traceLevel >= TRACE_TEXT)
-       cout << "nFilterPoints = " << m_nFilterPoints << endl;
+#ifdef DEBUG
+       if (m_traceLevel >= Trace::TRACE_CONSOLE)
+         cout << "nFilterPoints = " << m_nFilterPoints << endl;
+#endif
       }
       m_nOutputPoints = m_nFilterPoints * m_iPreinterpolationFactor;
 
@@ -254,29 +266,40 @@ ProcessSignal::init (const int idFilter, const int idFilterMethod, double dBandw
       SignalFilter filter (m_idFilter, m_dFilterMin, m_dFilterMax, m_nFilterPoints, m_dBandwidth, m_dFilterParam, SignalFilter::DOMAIN_FREQUENCY);
       m_adFilter = new double [m_nFilterPoints];
       filter.copyFilterData (m_adFilter, 0, m_nFilterPoints);
-      if (m_traceLevel >= TRACE_PLOT) {
-       SGPDriver sgpDriver ("Frequency Filter: Natural Order");
-       SGP sgp (sgpDriver);
-       EZPlot ezplot (sgp);
-       
-       ezplot.ezset ("title Filter Filter: Natural Order");
-       ezplot.addCurve (m_adFilter, m_nFilterPoints);
-         ezplot.plot();
-         cio_put_str ("Press any key to continue");
-         cio_kb_getc ();
+
+      if (m_idGeometry == Scanner::GEOMETRY_EQUILINEAR) {
+       for (int i = 0; i < m_nFilterPoints; i++)
+         m_adFilter[i] *= 0.5;
+      } else if (m_idGeometry == Scanner::GEOMETRY_EQUIANGULAR) {
+       for (int i = 0; i < m_nFilterPoints; i++) {
+         int iDetFromZero = i - ((m_nFilterPoints - 1) / 2);
+         double sinScale = sin (iDetFromZero * m_dSignalInc);
+         if (fabs(sinScale) < 1E-7)
+           sinScale = 1;
+         else
+           sinScale = (iDetFromZero * m_dSignalInc) / sinScale;
+         double dScale = 0.5 * sinScale * sinScale;
+         //      m_adFilter[i] *= dScale;
+       }
+      }
+      EZPlot* pEZPlot = NULL;
+      if (pSGP && m_traceLevel >= Trace::TRACE_PLOT) {
+       pEZPlot = new EZPlot (*pSGP);
+       pEZPlot->ezset ("title Filter Filter: Natural Order");
+       pEZPlot->ezset ("ylength 0.50");
+       pEZPlot->ezset ("yporigin 0.00");
+       pEZPlot->addCurve (m_adFilter, m_nFilterPoints);
+       pEZPlot->plot();
       }
       shuffleNaturalToFourierOrder (m_adFilter, m_nFilterPoints);
-       if (m_traceLevel >= TRACE_PLOT) {
-         SGPDriver sgpDriver ("Frequency Filter: Fourier Order");
-         SGP sgp (sgpDriver);
-         EZPlot ezplot (sgp);
-
-         ezplot.ezset ("title Filter Filter: Fourier Order");
-         ezplot.addCurve (m_adFilter, m_nFilterPoints);
-         ezplot.plot();
-         cio_put_str ("Press any key to continue");
-         cio_kb_getc ();
-       }
+      if (pEZPlot && m_traceLevel >= Trace::TRACE_PLOT) {
+       pEZPlot->ezset ("title Filter Filter: Fourier Order");
+       pEZPlot->ezset ("ylength 0.50");
+       pEZPlot->ezset ("yporigin 0.50");
+       pEZPlot->addCurve (m_adFilter, m_nFilterPoints);
+       pEZPlot->plot();
+       delete pEZPlot;
+      }
     } else if (m_idFilterGeneration == FILTER_GENERATION_INVERSE_FOURIER) {
       // calculate number of filter points with zeropadding
       int nSpatialPoints = 2 * (m_nSignalPoints - 1) + 1;
@@ -293,21 +316,37 @@ ProcessSignal::init (const int idFilter, const int idFilterMethod, double dBandw
        m_nFilterPoints = 1 << nextPowerOf2;
       }
       m_nOutputPoints = m_nFilterPoints * m_iPreinterpolationFactor;
-      if (m_traceLevel >= TRACE_TEXT)
+#ifdef DEBUG
+      if (m_traceLevel >= Trace::TRACE_CONSOLE)
        cout << "nFilterPoints = " << m_nFilterPoints << endl;
+#endif
       double adSpatialFilter [m_nFilterPoints];
       SignalFilter filter (m_idFilter, m_dFilterMin, m_dFilterMax, nSpatialPoints, m_dBandwidth, m_dFilterParam, SignalFilter::DOMAIN_SPATIAL);
       filter.copyFilterData (adSpatialFilter, 0, nSpatialPoints);
-      if (m_traceLevel >= TRACE_PLOT) {
-       SGPDriver sgpDriver ("Spatial Filter: Natural Order");
-       SGP sgp (sgpDriver);
-       EZPlot ezplot (sgp);
-       
-       ezplot.ezset ("title Spatial Filter: Natural Order");
-       ezplot.addCurve (adSpatialFilter, nSpatialPoints);
-       ezplot.plot();
-       cio_put_str ("Press any key to continue");
-       cio_kb_getc ();
+      EZPlot* pEZPlot = NULL;
+      if (pSGP && m_traceLevel >= Trace::TRACE_PLOT) {
+       pEZPlot = new EZPlot (*pSGP);
+       pEZPlot->ezset ("title Spatial Filter: Natural Order");
+       pEZPlot->ezset ("ylength 0.50");
+       pEZPlot->ezset ("yporigin 0.00");
+       pEZPlot->addCurve (adSpatialFilter, nSpatialPoints);
+       pEZPlot->plot();
+       delete pEZPlot;
+      }
+      if (m_idGeometry == Scanner::GEOMETRY_EQUILINEAR) {
+       for (int i = 0; i < m_nFilterPoints; i++)
+         adSpatialFilter[i] *= 0.5;
+      } else if (m_idGeometry == Scanner::GEOMETRY_EQUIANGULAR) {
+       for (int i = 0; i < m_nFilterPoints; i++) {
+         int iDetFromZero = i - ((m_nFilterPoints - 1) / 2);
+         double sinScale = sin (iDetFromZero * m_dSignalInc);
+         if (fabs(sinScale) < 1E-7)
+           sinScale = 1;
+         else
+           sinScale = (iDetFromZero * m_dSignalInc) / sinScale;
+         double dScale = 0.5 * sinScale * sinScale;
+         adSpatialFilter[i] *= dScale;
+       }
       }
       for (int i = nSpatialPoints; i < m_nFilterPoints; i++)
        adSpatialFilter[i] = 0;
@@ -317,16 +356,13 @@ ProcessSignal::init (const int idFilter, const int idFilterMethod, double dBandw
       finiteFourierTransform (adSpatialFilter, acInverseFilter, m_nFilterPoints, 1);
       for (int i = 0; i < m_nFilterPoints; i++)
        m_adFilter[i] = abs(acInverseFilter[i]) * m_dSignalInc;
-      if (m_traceLevel >= TRACE_PLOT) {
-       SGPDriver sgpDriver ("Spatial Filter: Inverse");
-       SGP sgp (sgpDriver);
-       EZPlot ezplot (sgp);
-       
-       ezplot.ezset ("title Spatial Filter: Inverse");
-       ezplot.addCurve (m_adFilter, m_nFilterPoints);
-       ezplot.plot();
-       cio_put_str ("Press any key to continue");
-       cio_kb_getc ();
+      if (pEZPlot && m_traceLevel >= Trace::TRACE_PLOT) {
+       pEZPlot->ezset ("title Spatial Filter: Inverse");
+       pEZPlot->ezset ("ylength 0.50");
+       pEZPlot->ezset ("yporigin 0.50");
+       pEZPlot->addCurve (m_adFilter, m_nFilterPoints);
+       pEZPlot->plot();
+       delete pEZPlot;
       }
     }
   }
@@ -468,11 +504,26 @@ ProcessSignal::convertFilterGenerationIDToTitle (const int fgID)
 }
 
 void
-ProcessSignal::filterSignal (const float input[], double output[]) const
+ProcessSignal::filterSignal (const float constInput[], double output[]) const
 {
+  double input [m_nSignalPoints];
+  for (int i = 0; i < m_nSignalPoints; i++)
+    input[i] = constInput[i];
+
+  if (m_idGeometry == Scanner::GEOMETRY_EQUILINEAR) {
+    for (int i = 0; i < m_nSignalPoints; i++) {
+      int iDetFromCenter = i - (m_nSignalPoints / 2);
+      input[i] *= m_dFocalLength / sqrt (m_dFocalLength * m_dFocalLength + iDetFromCenter * iDetFromCenter * m_dSignalInc * m_dSignalInc);
+    }
+  } else if (m_idGeometry == Scanner::GEOMETRY_EQUIANGULAR) {
+    for (int i = 0; i < m_nSignalPoints; i++) {
+      int iDetFromCenter = i - (m_nSignalPoints / 2);
+      input[i] *= m_dFocalLength * cos (iDetFromCenter * m_dSignalInc);
+    }
+  }
   if (m_idFilterMethod == FILTER_METHOD_CONVOLUTION) {
-    for (int i = 0; i < m_nSignalPoints; i++)
-      output[i] = convolve (input, m_dSignalInc, i, m_nSignalPoints);
+      for (int i = 0; i < m_nSignalPoints; i++)
+       output[i] = convolve (input, m_dSignalInc, i, m_nSignalPoints);
   } else if (m_idFilterMethod == FILTER_METHOD_FOURIER) {
     double inputSignal[m_nFilterPoints];
     for (int i = 0; i < m_nSignalPoints; i++)