** This is part of the CTSim program
** Copyright (C) 1983-2000 Kevin Rosenberg
**
-** $Id: filter.cpp,v 1.12 2000/07/05 17:59:26 kevin Exp $
+** $Id: filter.cpp,v 1.20 2000/07/22 15:45:33 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
#include "ct.h"
+int SignalFilter::N_INTEGRAL=500; //static member
+
+const int SignalFilter::FILTER_INVALID = -1 ;
+const int SignalFilter::FILTER_BANDLIMIT = 0;
+const int SignalFilter::FILTER_SINC = 1;
+const int SignalFilter::FILTER_G_HAMMING = 2;
+const int SignalFilter::FILTER_COSINE = 3;
+const int SignalFilter::FILTER_TRIANGLE = 4;
+const int SignalFilter::FILTER_ABS_BANDLIMIT = 5; // filter times |x = |
+const int SignalFilter::FILTER_ABS_SINC = 6;
+const int SignalFilter::FILTER_ABS_G_HAMMING = 7;
+const int SignalFilter::FILTER_ABS_COSINE = 8;
+const int SignalFilter::FILTER_SHEPP = 9;
+
+const char* SignalFilter::s_aszFilterName[] = {
+ {"bandlimit"},
+ {"sinc"},
+ {"hamming"},
+ {"cosine"},
+ {"triangle"},
+ {"abs_bandlimit"},
+ {"abs_sinc"},
+ {"abs_hamming"},
+ {"abs_cosine"},
+ {"shepp"},
+};
+
+const char* SignalFilter::s_aszFilterTitle[] = {
+ {"Bandlimit"},
+ {"Sinc"},
+ {"Hamming"},
+ {"Cosine"},
+ {"Triangle"},
+ {"Abs(w) * Bandlimit"},
+ {"Abs(w) * Sinc"},
+ {"Abs(w) * Hamming"},
+ {"Abs(w) * Cosine"},
+ {"Shepp"},
+};
+
+const int SignalFilter::s_iFilterCount = sizeof(s_aszFilterName) / sizeof(const char*);
+
+const int SignalFilter::FILTER_METHOD_INVALID = -1;
+const int SignalFilter::FILTER_METHOD_CONVOLUTION = 0;
+const int SignalFilter::FILTER_METHOD_FOURIER = 1;
+const int SignalFilter::FILTER_METHOD_FOURIER_TABLE = 2;
+const int SignalFilter::FILTER_METHOD_FFT = 3;
+#if HAVE_FFTW
+const int SignalFilter::FILTER_METHOD_FFTW = 4;
+const int SignalFilter::FILTER_METHOD_RFFTW =5 ;
+#endif
+
+const char* SignalFilter::s_aszFilterMethodName[] = {
+ {"convolution"},
+ {"fourier"},
+ {"fouier_table"},
+ {"fft"},
+#if HAVE_FFTW
+ {"fftw"},
+ {"rfftw"},
+#endif
+};
+
+const char* SignalFilter::s_aszFilterMethodTitle[] = {
+ {"Convolution"},
+ {"Direct Fourier"},
+ {"Fouier Trigometric Table Lookout"},
+ {"FFT"},
+#if HAVE_FFTW
+ {"FFTW"},
+ {"Real/Half-Complex FFTW"},
+#endif
+};
+
+const int SignalFilter::s_iFilterMethodCount = sizeof(s_aszFilterMethodName) / sizeof(const char*);
+
+
+const int SignalFilter::DOMAIN_INVALID = -1;
+const int SignalFilter::DOMAIN_FREQUENCY = 0;
+const int SignalFilter::DOMAIN_SPATIAL = 1;
+
+const char* SignalFilter::s_aszDomainName[] = {
+ {"frequency"},
+ {"spatial"},
+};
+
+const char* SignalFilter::s_aszDomainTitle[] = {
+ {"Frequency"},
+ {"Spatial"},
+};
+
+const int SignalFilter::s_iDomainCount = sizeof(s_aszDomainName) / sizeof(const char*);
+
/* NAME
* SignalFilter::SignalFilter Construct a signal
* int nSignalPoints Number of points in signal
* double param General input parameter to filters
* int domain FREQUENCY or SPATIAL domain wanted
- * int numint Number if intervals for calculating discrete inverse fourier xform
- * for spatial domain filters. For ANALYTIC solutions, use numint = 0
*/
-SignalFilter::SignalFilter (const char* filterName, const char* filterMethodName, double bw, double signalIncrement, int nSignalPoints, double param, const char* domainName, int numIntegral = 0)
+SignalFilter::SignalFilter (const char* filterName, const char* filterMethodName, double bw, double signalIncrement, int nSignalPoints, double param, const char* domainName, int zeropad = 0, int preinterpolationFactor = 1)
{
m_vecFilter = NULL;
m_vecFourierCosTable = NULL;
m_failMessage += domainName;
return;
}
- init (m_idFilter, m_idFilterMethod, bw, signalIncrement, nSignalPoints, param, m_idDomain, numIntegral);
+ init (m_idFilter, m_idFilterMethod, bw, signalIncrement, nSignalPoints, param, m_idDomain, zeropad, preinterpolationFactor);
}
-SignalFilter::SignalFilter (const FilterID filterID, const FilterMethodID filterMethodID, double bw, double signalIncrement, int nSignalPoints, double param, const DomainID domainID, int numIntegral = 0)
+SignalFilter::SignalFilter (const int filterID, const int filterMethodID, double bw, double signalIncrement, int nSignalPoints, double param, const int domainID, int zeropad = 0, int preinterpolationFactor = 1)
{
- init (filterID, filterMethodID, bw, signalIncrement, nSignalPoints, param, domainID, numIntegral);
+ init (filterID, filterMethodID, bw, signalIncrement, nSignalPoints, param, domainID, zeropad, preinterpolationFactor);
}
-SignalFilter::SignalFilter (const char* filterName, const char* domainName, double bw, double param, int numIntegral = 0)
+SignalFilter::SignalFilter (const char* filterName, const char* domainName, double bw, double param)
{
m_bw = bw;
m_nSignalPoints = 0;
m_vecFourierCosTable = NULL;
m_vecFourierSinTable = NULL;
m_filterParam = param;
- m_numIntegral = numIntegral;
m_idFilter = convertFilterNameToID (filterName);
if (m_idFilter == FILTER_INVALID) {
m_fail = true;
}
void
-SignalFilter::init (const FilterID filterID, const FilterMethodID filterMethodID, double bw, double signalIncrement, int nSignalPoints, double param, const DomainID domainID, int numint)
+SignalFilter::init (const int filterID, const int filterMethodID, double bw, double signalIncrement, int nSignalPoints, double filterParam, const int domainID, int zeropad, int preinterpolationFactor)
{
m_bw = bw;
m_idFilter = filterID;
m_fail = false;
m_nSignalPoints = nSignalPoints;
m_signalInc = signalIncrement;
- m_filterParam = param;
-
- if (m_idFilterMethod == FILTER_METHOD_FOURIER) {
- int nFourier = m_nSignalPoints * m_nSignalPoints + 1;
- double angleIncrement = (2. * PI) / m_nSignalPoints;
- m_vecFourierCosTable = new double[ nFourier ];
- m_vecFourierSinTable = new double[ nFourier ];
- for (int i = 0; i < nFourier; i++) {
- m_vecFourierCosTable[i] = cos (angleIncrement * i);
- m_vecFourierSinTable[i] = sin (angleIncrement * i);
- }
- m_nFilterPoints = m_nSignalPoints;
- m_filterMin = -1. / (2 * m_signalInc);
- m_filterMax = 1. / (2 * m_signalInc);
- m_filterInc = (m_filterMax - m_filterMin) / m_nFilterPoints;
- m_vecFilter = new double [m_nFilterPoints];
- int halfFilter = m_nFilterPoints / 2;
- for (int i = 0; i < halfFilter; i++)
- m_vecFilter[i] = static_cast<double>(i) / (halfFilter - 1) / (2 * m_signalInc);
- for (int i = 0; i < halfFilter; i++)
- m_vecFilter[m_nFilterPoints - i - 1] = static_cast<double>(i+1) / (halfFilter - 1) / (2 * m_signalInc);
- if (halfFilter % 2) // odd
- m_vecFilter[halfFilter] = 1 / (2 * m_signalInc);
- } else if (m_idFilterMethod == FILTER_METHOD_FFT || m_idFilterMethod == FILTER_METHOD_FFT_ZEROPAD_2 || m_idFilterMethod == FILTER_METHOD_FFT_ZEROPAD_4) {
+ m_filterParam = filterParam;
+ m_zeropad = zeropad;
+ m_preinterpolationFactor = preinterpolationFactor;
+
+ m_vecFourierCosTable = NULL;
+ m_vecFourierSinTable = NULL;
+ m_vecFilter = NULL;
+
+ if (m_idFilterMethod == FILTER_METHOD_FFT) {
+#if HAVE_FFTW
+ m_idFilterMethod = FILTER_METHOD_RFFTW;
+#else
+ m_fail = true;
+ m_failMessage = "FFT not yet implemented";
+ return;
+#endif
+ }
+
+ if (m_idFilterMethod == FILTER_METHOD_FOURIER || m_idFilterMethod == FILTER_METHOD_FOURIER_TABLE || m_idFilterMethod == FILTER_METHOD_FFT
+#if HAVE_FFTW
+ || m_idFilterMethod == FILTER_METHOD_FFTW || m_idFilterMethod == FILTER_METHOD_RFFTW
+#endif
+ ) {
m_nFilterPoints = m_nSignalPoints;
- if (m_idFilterMethod == FILTER_METHOD_FFT_ZEROPAD_2 || m_idFilterMethod == FILTER_METHOD_FFT_ZEROPAD_4) {
+ if (m_zeropad > 0) {
double logBase2 = log(m_nSignalPoints) / log(2);
- int nextPowerOf2 = static_cast<int>(floor(logBase2)) + 1;
- if (m_idFilterMethod == FILTER_METHOD_FFT_ZEROPAD_4)
- nextPowerOf2++;
+ int nextPowerOf2 = static_cast<int>(floor(logBase2));
if (logBase2 != floor(logBase2))
nextPowerOf2++;
+ nextPowerOf2 += (m_zeropad - 1);
m_nFilterPoints = 1 << nextPowerOf2;
- cout << "nFilterPoints = " << m_nFilterPoints << endl;
+ if (m_traceLevel >= TRACE_TEXT)
+ cout << "nFilterPoints = " << m_nFilterPoints << endl;
}
+ m_nOutputPoints = m_nFilterPoints * m_preinterpolationFactor;
m_filterMin = -1. / (2 * m_signalInc);
m_filterMax = 1. / (2 * m_signalInc);
m_filterInc = (m_filterMax - m_filterMin) / m_nFilterPoints;
m_vecFilter = new double [m_nFilterPoints];
int halfFilter = m_nFilterPoints / 2;
- for (int i = 0; i < halfFilter; i++)
- m_vecFilter[i] = static_cast<double>(i) / (halfFilter - 1) / (2 * m_signalInc) / m_nSignalPoints;
- for (int i = 0; i < halfFilter; i++)
- m_vecFilter[m_nFilterPoints - i - 1] = static_cast<double>(i+1) / (halfFilter - 1) / (2 * m_signalInc) / m_nSignalPoints;
- if (halfFilter % 2) // odd
- m_vecFilter[halfFilter] = 1 / (2 * m_signalInc) / m_nSignalPoints;
+ for (int i = 0; i <= halfFilter; i++)
+ m_vecFilter[i] = static_cast<double>(i) / halfFilter/ (2. * m_signalInc);
+ for (int i = 1; i <= halfFilter; i++)
+ m_vecFilter[m_nFilterPoints - i] = static_cast<double>(i) / halfFilter / (2. * m_signalInc);
+ }
+
+ // precalculate sin and cosine tables for fourier transform
+ if (m_idFilterMethod == FILTER_METHOD_FOURIER_TABLE) {
+ int nFourier = max(m_nFilterPoints,m_nOutputPoints) * max(m_nFilterPoints, m_nOutputPoints) + 1;
+ double angleIncrement = (2. * PI) / m_nFilterPoints;
+ m_vecFourierCosTable = new double[ nFourier ];
+ m_vecFourierSinTable = new double[ nFourier ];
+ double angle = 0;
+ for (int i = 0; i < nFourier; i++) {
+ m_vecFourierCosTable[i] = cos (angle);
+ m_vecFourierSinTable[i] = sin (angle);
+ angle += angleIncrement;
+ }
+ }
#if HAVE_FFTW
- m_planForward = fftw_create_plan (m_nFilterPoints, FFTW_FORWARD, FFTW_ESTIMATE);
- m_planBackward = fftw_create_plan (m_nFilterPoints, FFTW_BACKWARD, FFTW_ESTIMATE);
-#endif
+ if (m_idFilterMethod == FILTER_METHOD_FFTW || m_idFilterMethod == FILTER_METHOD_RFFTW) {
+ for (int i = 0; i < m_nFilterPoints; i++) //fftw uses unnormalized fft
+ m_vecFilter[i] /= m_nFilterPoints;
+ }
+
+ if (m_idFilterMethod == FILTER_METHOD_RFFTW) {
+ m_realPlanForward = rfftw_create_plan (m_nFilterPoints, FFTW_REAL_TO_COMPLEX, FFTW_ESTIMATE);
+ m_realPlanBackward = rfftw_create_plan (m_nOutputPoints, FFTW_COMPLEX_TO_REAL, FFTW_ESTIMATE);
+ m_vecRealFftInput = new fftw_real [ m_nFilterPoints ];
+ m_vecRealFftSignal = new fftw_real [ m_nOutputPoints ];
+ for (int i = 0; i < m_nFilterPoints; i++)
+ m_vecRealFftInput[i] = 0;
+ } else if (m_idFilterMethod == FILTER_METHOD_FFTW) {
+ m_complexPlanForward = fftw_create_plan (m_nFilterPoints, FFTW_FORWARD, FFTW_ESTIMATE);
+ m_complexPlanBackward = fftw_create_plan (m_nOutputPoints, FFTW_BACKWARD, FFTW_ESTIMATE);
+ m_vecComplexFftInput = new fftw_complex [ m_nFilterPoints ];
+ m_vecComplexFftSignal = new fftw_complex [ m_nOutputPoints ];
+ for (int i = 0; i < m_nFilterPoints; i++)
+ m_vecComplexFftInput[i].re = m_vecComplexFftInput[i].im = 0;
+ for (int i = 0; i < m_nOutputPoints; i++)
+ m_vecComplexFftSignal[i].re = m_vecComplexFftSignal[i].im = 0;
}
+#endif
if (m_idFilterMethod == FILTER_METHOD_CONVOLUTION) {
m_nFilterPoints = 2 * m_nSignalPoints - 1;
m_filterMin = -m_signalInc * (m_nSignalPoints - 1);
m_filterMax = m_signalInc * (m_nSignalPoints - 1);
m_filterInc = (m_filterMax - m_filterMin) / (m_nFilterPoints - 1);
- m_numIntegral = numint;
m_vecFilter = new double[ m_nFilterPoints ];
if (m_idFilter == FILTER_SHEPP) {
double x;
int i;
for (x = m_filterMin, i = 0; i < m_nFilterPoints; x += m_filterInc, i++)
- m_vecFilter[i] = frequencyResponse (x, param);
+ m_vecFilter[i] = frequencyResponse (x, m_filterParam);
} else if (m_idDomain == DOMAIN_SPATIAL) {
double x;
int i;
for (x = m_filterMin, i = 0; i < m_nFilterPoints; x += m_filterInc, i++)
- if (numint == 0)
- m_vecFilter[i] = spatialResponseAnalytic (x, param);
+ if (haveAnalyticSpatial(m_idFilter))
+ m_vecFilter[i] = spatialResponseAnalytic (x, m_filterParam);
else
- m_vecFilter[i] = spatialResponseCalc (x, param, numint);
+ m_vecFilter[i] = spatialResponseCalc (x, m_filterParam);
} else {
m_failMessage = "Illegal domain name ";
m_failMessage += m_idDomain;
SignalFilter::~SignalFilter (void)
{
- delete m_vecFilter;
- delete m_vecFourierSinTable;
- delete m_vecFourierCosTable;
+ delete [] m_vecFilter;
+ delete [] m_vecFourierSinTable;
+ delete [] m_vecFourierCosTable;
+
#if HAVE_FFTW
- if (m_idFilterMethod == FILTER_METHOD_FFT) {
- fftw_destroy_plan(m_planForward);
- fftw_destroy_plan(m_planBackward);
+ if (m_idFilterMethod == FILTER_METHOD_FFTW) {
+ fftw_destroy_plan(m_complexPlanForward);
+ fftw_destroy_plan(m_complexPlanBackward);
+ delete [] m_vecComplexFftInput;
+ delete [] m_vecComplexFftSignal;
+ }
+ if (m_idFilterMethod == FILTER_METHOD_RFFTW) {
+ rfftw_destroy_plan(m_realPlanForward);
+ rfftw_destroy_plan(m_realPlanBackward);
+ delete [] m_vecRealFftInput;
+ delete [] m_vecRealFftSignal;
}
#endif
}
-const SignalFilter::FilterID
+int
SignalFilter::convertFilterNameToID (const char *filterName)
{
- FilterID filterID = FILTER_INVALID;
-
- if (strcasecmp (filterName, FILTER_BANDLIMIT_STR) == 0)
- filterID = FILTER_BANDLIMIT;
- else if (strcasecmp (filterName, FILTER_HAMMING_STR) == 0)
- filterID = FILTER_G_HAMMING;
- else if (strcasecmp (filterName, FILTER_SINC_STR) == 0)
- filterID = FILTER_SINC;
- else if (strcasecmp (filterName, FILTER_COS_STR) == 0)
- filterID = FILTER_COSINE;
- else if (strcasecmp (filterName, FILTER_TRIANGLE_STR) == 0)
- filterID = FILTER_TRIANGLE;
- else if (strcasecmp (filterName, FILTER_ABS_BANDLIMIT_STR) == 0)
- filterID = FILTER_ABS_BANDLIMIT;
- else if (strcasecmp (filterName, FILTER_ABS_HAMMING_STR) == 0)
- filterID = FILTER_ABS_G_HAMMING;
- else if (strcasecmp (filterName, FILTER_ABS_SINC_STR) == 0)
- filterID = FILTER_ABS_SINC;
- else if (strcasecmp (filterName, FILTER_ABS_COS_STR) == 0)
- filterID = FILTER_ABS_COSINE;
- else if (strcasecmp (filterName, FILTER_SHEPP_STR) == 0)
- filterID = FILTER_SHEPP;
+ int filterID = FILTER_INVALID;
+
+ for (int i = 0; i < s_iFilterCount; i++)
+ if (strcasecmp (filterName, s_aszFilterName[i]) == 0) {
+ filterID = i;
+ break;
+ }
return (filterID);
}
const char *
-SignalFilter::convertFilterIDToName (const FilterID filterID)
-{
- const char *name = "";
-
- if (filterID == FILTER_SHEPP)
- name = FILTER_SHEPP_STR;
- else if (filterID == FILTER_ABS_COSINE)
- name = FILTER_ABS_COS_STR;
- else if (filterID == FILTER_ABS_SINC)
- name = FILTER_ABS_SINC_STR;
- else if (filterID == FILTER_ABS_G_HAMMING)
- name = FILTER_ABS_HAMMING_STR;
- else if (filterID == FILTER_ABS_BANDLIMIT)
- name = FILTER_ABS_BANDLIMIT_STR;
- else if (filterID == FILTER_COSINE)
- name = FILTER_COS_STR;
- else if (filterID == FILTER_SINC)
- name = FILTER_SINC_STR;
- else if (filterID == FILTER_G_HAMMING)
- name = FILTER_HAMMING_STR;
- else if (filterID == FILTER_BANDLIMIT)
- name = FILTER_BANDLIMIT_STR;
- else if (filterID == FILTER_TRIANGLE)
- name = FILTER_TRIANGLE_STR;
-
+SignalFilter::convertFilterIDToName (const int filterID)
+{
+ static const char *name = "";
+
+ if (filterID >= 0 && filterID < s_iFilterCount)
+ return (s_aszFilterName [filterID]);
+
return (name);
}
+
+const char *
+SignalFilter::convertFilterIDToTitle (const int filterID)
+{
+ static const char *title = "";
+
+ if (filterID >= 0 && filterID < s_iFilterCount)
+ return (s_aszFilterTitle [filterID]);
+
+ return (title);
+}
-const SignalFilter::FilterMethodID
+int
SignalFilter::convertFilterMethodNameToID (const char* const filterMethodName)
{
- FilterMethodID fmID = FILTER_METHOD_INVALID;
-
- if (strcasecmp (filterMethodName, FILTER_METHOD_CONVOLUTION_STR) == 0)
- fmID = FILTER_METHOD_CONVOLUTION;
- else if (strcasecmp (filterMethodName, FILTER_METHOD_FOURIER_STR) == 0)
- fmID = FILTER_METHOD_FOURIER;
- else if (strcasecmp (filterMethodName, FILTER_METHOD_FFT_STR) == 0)
- fmID = FILTER_METHOD_FFT;
- else if (strcasecmp (filterMethodName, FILTER_METHOD_FFT_ZEROPAD_2_STR) == 0)
- fmID = FILTER_METHOD_FFT_ZEROPAD_2;
- else if (strcasecmp (filterMethodName, FILTER_METHOD_FFT_ZEROPAD_4_STR) == 0)
- fmID = FILTER_METHOD_FFT_ZEROPAD_4;
- else if (strcasecmp (filterMethodName, FILTER_METHOD_FFT_ZEROPAD_6_STR) == 0)
- fmID = FILTER_METHOD_FFT_ZEROPAD_6;
+ int fmID = FILTER_METHOD_INVALID;
+
+ for (int i = 0; i < s_iFilterMethodCount; i++)
+ if (strcasecmp (filterMethodName, s_aszFilterMethodName[i]) == 0) {
+ fmID = i;
+ break;
+ }
return (fmID);
}
const char *
-SignalFilter::convertFilterMethodIDToName (const FilterMethodID fmID)
-{
- const char *name = "";
-
- if (fmID == FILTER_METHOD_CONVOLUTION)
- return (FILTER_METHOD_CONVOLUTION_STR);
- else if (fmID == FILTER_METHOD_FOURIER)
- return (FILTER_METHOD_FOURIER_STR);
- else if (fmID == FILTER_METHOD_FFT)
- return (FILTER_METHOD_FFT_STR);
- else if (fmID == FILTER_METHOD_FFT_ZEROPAD_2)
- return (FILTER_METHOD_FFT_ZEROPAD_2_STR);
- else if (fmID == FILTER_METHOD_FFT_ZEROPAD_4)
- return (FILTER_METHOD_FFT_ZEROPAD_4_STR);
- else if (fmID == FILTER_METHOD_FFT_ZEROPAD_6)
- return (FILTER_METHOD_FFT_ZEROPAD_6_STR);
+SignalFilter::convertFilterMethodIDToName (const int fmID)
+{
+ static const char *name = "";
+
+ if (fmID >= 0 && fmID < s_iFilterMethodCount)
+ return (s_aszFilterName [fmID]);
return (name);
}
-const SignalFilter::DomainID
+const char *
+SignalFilter::convertFilterMethodIDToTitle (const int fmID)
+{
+ static const char *title = "";
+
+ if (fmID >= 0 && fmID < s_iFilterMethodCount)
+ return (s_aszFilterTitle [fmID]);
+
+ return (title);
+}
+
+int
SignalFilter::convertDomainNameToID (const char* const domainName)
{
- DomainID dID = DOMAIN_INVALID;
+ int dID = DOMAIN_INVALID;
- if (strcasecmp (domainName, DOMAIN_SPATIAL_STR) == 0)
- dID = DOMAIN_SPATIAL;
- else if (strcasecmp (domainName, DOMAIN_FREQUENCY_STR) == 0)
- dID = DOMAIN_FREQUENCY;
+ for (int i = 0; i < s_iDomainCount; i++)
+ if (strcasecmp (domainName, s_aszDomainName[i]) == 0) {
+ dID = i;
+ break;
+ }
return (dID);
}
const char *
-SignalFilter::convertDomainIDToName (const DomainID domain)
+SignalFilter::convertDomainIDToName (const int domainID)
{
- const char *name = "";
+ static const char *name = "";
- if (domain == DOMAIN_SPATIAL)
- return (DOMAIN_SPATIAL_STR);
- else if (domain == DOMAIN_FREQUENCY)
- return (DOMAIN_FREQUENCY_STR);
+ if (domainID >= 0 && domainID < s_iDomainCount)
+ return (s_aszDomainName [domainID]);
return (name);
}
+const char *
+SignalFilter::convertDomainIDToTitle (const int domainID)
+{
+ static const char *title = "";
+
+ if (domainID >= 0 && domainID < s_iDomainCount)
+ return (s_aszDomainTitle [domainID]);
+
+ return (title);
+}
void
SignalFilter::filterSignal (const float input[], double output[]) const
for (int i = 0; i < m_nSignalPoints; i++)
output[i] = convolve (input, m_signalInc, i, m_nSignalPoints);
} else if (m_idFilterMethod == FILTER_METHOD_FOURIER) {
- complex<double> fftSignal[m_nSignalPoints];
- complex<double> complexOutput[m_nSignalPoints];
- complex<double> filteredSignal[m_nSignalPoints];
- finiteFourierTransform (input, fftSignal, m_nSignalPoints, -1);
- dotProduct (m_vecFilter, fftSignal, filteredSignal, m_nSignalPoints);
- finiteFourierTransform (filteredSignal, complexOutput, m_nSignalPoints, 1);
+ double inputSignal[m_nFilterPoints];
+ for (int i = 0; i < m_nSignalPoints; i++)
+ inputSignal[i] = input[i];
+ for (int i = m_nSignalPoints; i < m_nFilterPoints; i++)
+ inputSignal[i] = 0; // zeropad
+ complex<double> fftSignal[m_nFilterPoints];
+ finiteFourierTransform (inputSignal, fftSignal, m_nFilterPoints, -1);
+ for (int i = 0; i < m_nFilterPoints; i++)
+ fftSignal[i] *= m_vecFilter[i];
+ double inverseFourier[m_nFilterPoints];
+ finiteFourierTransform (fftSignal, inverseFourier, m_nFilterPoints, 1);
for (int i = 0; i < m_nSignalPoints; i++)
- output[i] = complexOutput[i].real();
- } else if (m_idFilterMethod == FILTER_METHOD_FFT || FILTER_METHOD_FFT_ZEROPAD_2 || FILTER_METHOD_FFT_ZEROPAD_4) {
-#if HAVE_FFTW
- fftw_complex in[m_nFilterPoints], out[m_nFilterPoints];
- for (int i = 0; i < m_nSignalPoints; i++) {
- in[i].re = input[i];
- in[i].im = 0;
- }
- for (int i = m_nSignalPoints; i < m_nFilterPoints; i++) {
- in[i].re = in[i].im = 0; // ZeroPad
- }
- fftw_one(m_planForward, in, out);
- for (int i = 0; i < m_nFilterPoints; i++) {
- out[i].re = m_vecFilter[i] * out[i].re;
- out[i].im = m_vecFilter[i] * out[i].im;
- }
- fftw_one(m_planBackward, out, in);
+ output[i] = inverseFourier[i];
+ } else if (m_idFilterMethod == FILTER_METHOD_FOURIER_TABLE) {
+ double inputSignal[m_nFilterPoints];
+ for (int i = 0; i < m_nSignalPoints; i++)
+ inputSignal[i] = input[i];
+ for (int i = m_nSignalPoints; i < m_nFilterPoints; i++)
+ inputSignal[i] = 0; // zeropad
+ complex<double> fftSignal[m_nFilterPoints];
+ finiteFourierTransform (inputSignal, fftSignal, -1);
+ for (int i = 0; i < m_nFilterPoints; i++)
+ fftSignal[i] *= m_vecFilter[i];
+ double inverseFourier[m_nFilterPoints];
+ finiteFourierTransform (fftSignal, inverseFourier, 1);
for (int i = 0; i < m_nSignalPoints; i++)
- output[i] = in[i].re;
+ output[i] = inverseFourier[i];
+ }
+#if HAVE_FFTW
+ else if (m_idFilterMethod == FILTER_METHOD_RFFTW) {
+ for (int i = 0; i < m_nSignalPoints; i++)
+ m_vecRealFftInput[i] = input[i];
+
+ fftw_real fftOutput [ m_nFilterPoints ];
+ rfftw_one (m_realPlanForward, m_vecRealFftInput, fftOutput);
+ for (int i = 0; i < m_nFilterPoints; i++)
+ m_vecRealFftSignal[i] = m_vecFilter[i] * fftOutput[i];
+ for (int i = m_nFilterPoints; i < m_nOutputPoints; i++)
+ m_vecRealFftSignal[i] = 0;
+
+ fftw_real ifftOutput [ m_nOutputPoints ];
+ rfftw_one(m_realPlanBackward, m_vecRealFftSignal, ifftOutput);
+ for (int i = 0; i < m_nSignalPoints * m_preinterpolationFactor; i++)
+ output[i] = ifftOutput[i];
+ } else if (m_idFilterMethod == FILTER_METHOD_FFTW) {
+ for (int i = 0; i < m_nSignalPoints; i++)
+ m_vecComplexFftInput[i].re = input[i];
+
+ fftw_complex fftOutput [ m_nFilterPoints ];
+ fftw_one(m_complexPlanForward, m_vecComplexFftInput, fftOutput);
+ for (int i = 0; i < m_nFilterPoints; i++) {
+ m_vecComplexFftSignal[i].re = m_vecFilter[i] * fftOutput[i].re;
+ m_vecComplexFftSignal[i].im = m_vecFilter[i] * fftOutput[i].im;
+ }
+ fftw_complex ifftOutput [ m_nOutputPoints ];
+ fftw_one(m_complexPlanBackward, m_vecComplexFftSignal, ifftOutput);
+ for (int i = 0; i < m_nSignalPoints * m_preinterpolationFactor; i++)
+ output[i] = ifftOutput[i].re;
}
#endif
}
double response = 0;
if (m_idDomain == DOMAIN_SPATIAL)
- response = spatialResponse (m_idFilter, m_bw, x, m_filterParam, m_numIntegral);
+ response = spatialResponse (m_idFilter, m_bw, x, m_filterParam);
else if (m_idDomain == DOMAIN_FREQUENCY)
response = frequencyResponse (m_idFilter, m_bw, x, m_filterParam);
double
-SignalFilter::spatialResponse (FilterID filterID, double bw, double x, double param, int nIntegral = 0)
+SignalFilter::spatialResponse (int filterID, double bw, double x, double param)
{
- if (nIntegral == 0)
+ if (haveAnalyticSpatial(filterID))
return spatialResponseAnalytic (filterID, bw, x, param);
else
- return spatialResponseCalc (filterID, bw, x, param, nIntegral);
+ return spatialResponseCalc (filterID, bw, x, param, N_INTEGRAL);
}
/* NAME
*/
double
-SignalFilter::spatialResponseCalc (double x, double param, int nIntegral) const
+SignalFilter::spatialResponseCalc (double x, double param) const
{
- return (spatialResponseCalc (m_idFilter, m_bw, x, param, nIntegral));
+ return (spatialResponseCalc (m_idFilter, m_bw, x, param, N_INTEGRAL));
}
double
-SignalFilter::spatialResponseCalc (FilterID filterID, double bw, double x, double param, int n)
+SignalFilter::spatialResponseCalc (int filterID, double bw, double x, double param, int n)
{
double zmin, zmax;
double
-SignalFilter::frequencyResponse (FilterID filterID, double bw, double u, double param)
+SignalFilter::frequencyResponse (int filterID, double bw, double u, double param)
{
double q;
double au = fabs (u);
return spatialResponseAnalytic (m_idFilter, m_bw, x, param);
}
+const bool
+SignalFilter::haveAnalyticSpatial (int filterID)
+{
+ bool haveAnalytic = false;
+
+ switch (filterID) {
+ case FILTER_BANDLIMIT:
+ case FILTER_TRIANGLE:
+ case FILTER_COSINE:
+ case FILTER_G_HAMMING:
+ case FILTER_ABS_BANDLIMIT:
+ case FILTER_ABS_COSINE:
+ case FILTER_ABS_G_HAMMING:
+ case FILTER_SHEPP:
+ case FILTER_SINC:
+ haveAnalytic = true;
+ break;
+ default:
+ break;
+ }
+
+ return (haveAnalytic);
+}
+
double
-SignalFilter::spatialResponseAnalytic (FilterID filterID, double bw, double x, double param)
+SignalFilter::spatialResponseAnalytic (int filterID, double bw, double x, double param)
{
double q, temp;
double u = TWOPI * x;
void
-SignalFilter::finiteFourierTransform (const float input[], complex<double> output[], const int n, int direction)
+SignalFilter::finiteFourierTransform (const double input[], complex<double> output[], const int n, int direction)
{
if (direction < 0)
direction = -1;
else
direction = 1;
- double angleIncrement = 2 * PI / n;
+ double angleIncrement = direction * 2 * PI / n;
for (int i = 0; i < n; i++) {
double sumReal = 0;
double sumImag = 0;
for (int j = 0; j < n; j++) {
- double angle = i * j * angleIncrement * direction;
+ double angle = i * j * angleIncrement;
sumReal += input[j] * cos(angle);
sumImag += input[j] * sin(angle);
}
else
direction = 1;
- double angleIncrement = 2 * PI / n;
+ double angleIncrement = direction * 2 * PI / n;
for (int i = 0; i < n; i++) {
complex<double> sum (0,0);
for (int j = 0; j < n; j++) {
- double angle = i * j * angleIncrement * direction;
+ double angle = i * j * angleIncrement;
complex<double> exponentTerm (cos(angle), sin(angle));
sum += input[j] * exponentTerm;
}
}
void
-SignalFilter::finiteFourierTransform (const float input[], complex<double> output[], int direction) const
+SignalFilter::finiteFourierTransform (const complex<double> input[], double output[], const int n, int direction)
{
if (direction < 0)
direction = -1;
else
direction = 1;
- for (int i = 0; i < m_nSignalPoints; i++) {
+ double angleIncrement = direction * 2 * PI / n;
+ for (int i = 0; i < n; i++) {
+ double sumReal = 0;
+ for (int j = 0; j < n; j++) {
+ double angle = i * j * angleIncrement;
+ sumReal += input[j].real() * cos(angle) - input[j].imag() * sin(angle);
+ }
+ if (direction < 0) {
+ sumReal /= n;
+ }
+ output[i] = sumReal;
+ }
+}
+
+void
+SignalFilter::finiteFourierTransform (const double input[], complex<double> output[], int direction) const
+{
+ if (direction < 0)
+ direction = -1;
+ else
+ direction = 1;
+
+ for (int i = 0; i < m_nFilterPoints; i++) {
double sumReal = 0, sumImag = 0;
- for (int j = 0; j < m_nSignalPoints; j++) {
+ for (int j = 0; j < m_nFilterPoints; j++) {
int tableIndex = i * j;
if (direction > 0) {
- sumReal += input[i] * m_vecFourierCosTable[tableIndex];
- sumImag += input[i] * m_vecFourierSinTable[tableIndex];
+ sumReal += input[j] * m_vecFourierCosTable[tableIndex];
+ sumImag += input[j] * m_vecFourierSinTable[tableIndex];
} else {
- sumReal += input[i] * m_vecFourierCosTable[tableIndex];
- sumImag -= input[i] * m_vecFourierSinTable[tableIndex];
+ sumReal += input[j] * m_vecFourierCosTable[tableIndex];
+ sumImag -= input[j] * m_vecFourierSinTable[tableIndex];
}
}
if (direction < 0) {
- sumReal /= m_nSignalPoints;
- sumImag /= m_nSignalPoints;
+ sumReal /= m_nFilterPoints;
+ sumImag /= m_nFilterPoints;
}
output[i] = complex<double> (sumReal, sumImag);
}
}
-// (a+bi) * (c + di) = (ac - db) + (bc + da)i
-#if 0
+// (a+bi) * (c + di) = (ac - bd) + (ad + bc)i
void
SignalFilter::finiteFourierTransform (const complex<double> input[], complex<double> output[], int direction) const
{
else
direction = 1;
- for (int i = 0; i < m_nSignalPoints; i++) {
+ for (int i = 0; i < m_nFilterPoints; i++) {
double sumReal = 0, sumImag = 0;
- for (int j = 0; j < m_nSignalPoints; j++) {
+ for (int j = 0; j < m_nFilterPoints; j++) {
int tableIndex = i * j;
if (direction > 0) {
- sumReal += input[i] * m_vecFourierCosTable[tableIndex];
- sumImag += input[i] * m_vecFourierSinTable[tableIndex];
+ sumReal += input[j].real() * m_vecFourierCosTable[tableIndex]
+ - input[j].imag() * m_vecFourierSinTable[tableIndex];
+ sumImag += input[j].real() * m_vecFourierSinTable[tableIndex]
+ + input[j].imag() * m_vecFourierCosTable[tableIndex];
} else {
- sumReal += input[i] * m_vecFourierCosTable[tableIndex];
- sumImag -= input[i] * m_vecFourierSinTable[tableIndex];
+ sumReal += input[j].real() * m_vecFourierCosTable[tableIndex]
+ - input[j].imag() * -m_vecFourierSinTable[tableIndex];
+ sumImag += input[j].real() * -m_vecFourierSinTable[tableIndex]
+ + input[j].imag() * m_vecFourierCosTable[tableIndex];
}
}
- if (direction > 0) {
- sumReal /= m_nSignalPoints;
- sumImag /= m_nSignalPoints;
+ if (direction < 0) {
+ sumReal /= m_nFilterPoints;
+ sumImag /= m_nFilterPoints;
}
output[i] = complex<double> (sumReal, sumImag);
}
}
-#endif
-void
-SignalFilter::dotProduct (const double v1[], const complex<double> v2[], complex<double> output[], const int n)
+void
+SignalFilter::finiteFourierTransform (const complex<double> input[], double output[], int direction) const
{
- for (int i = 0; i < n; i++)
- output[i] = v1[i] * v2[i];
+ if (direction < 0)
+ direction = -1;
+ else
+ direction = 1;
+
+ for (int i = 0; i < m_nFilterPoints; i++) {
+ double sumReal = 0;
+ for (int j = 0; j < m_nFilterPoints; j++) {
+ int tableIndex = i * j;
+ if (direction > 0) {
+ sumReal += input[j].real() * m_vecFourierCosTable[tableIndex]
+ - input[j].imag() * m_vecFourierSinTable[tableIndex];
+ } else {
+ sumReal += input[j].real() * m_vecFourierCosTable[tableIndex]
+ - input[j].imag() * -m_vecFourierSinTable[tableIndex];
+ }
+ }
+ if (direction < 0) {
+ sumReal /= m_nFilterPoints;
+ }
+ output[i] = sumReal;
+ }
}
+
+