X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;ds=sidebyside;f=libctsim%2Ffilter.cpp;h=1c9e95bc37c03e372147f3349ea6dea4b75fcad7;hb=e4c1f7f8eb87558c3abf3bf1d20732361f425351;hp=30d47185e3cd08120a9c66c826d6946afb105f71;hpb=99dd1d6ed10db1f669a5fe6af71225a50fc0ddfb;p=ctsim.git diff --git a/libctsim/filter.cpp b/libctsim/filter.cpp index 30d4718..1c9e95b 100644 --- a/libctsim/filter.cpp +++ b/libctsim/filter.cpp @@ -1,5 +1,5 @@ /***************************************************************************** -** FILE IDENTIFICATION +** File IDENTIFICATION ** ** Name: filter.cpp ** Purpose: Routines for signal-procesing filters @@ -9,7 +9,7 @@ ** This is part of the CTSim program ** Copyright (C) 1983-2000 Kevin Rosenberg ** -** $Id: filter.cpp,v 1.1 2000/06/19 02:59:34 kevin Exp $ +** $Id: filter.cpp,v 1.18 2000/07/15 08:36:13 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 @@ -28,60 +28,465 @@ #include "ct.h" +int SignalFilter::N_INTEGRAL=500; //static member + /* NAME - * filter_generate Generate a filter + * SignalFilter::SignalFilter Construct a signal * * SYNOPSIS - * f = filter_generate (filt_type, bw, xmin, xmax, n, param, domain, analytic) - * double f Generated filter vector - * int filt_type Type of filter wanted - * double bw Bandwidth of filter - * double xmin, xmax Filter limits - * int n Number of points in filter - * double param General input parameter to filters - * int domain FREQ 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 + * f = SignalFilter (filt_type, bw, filterMin, filterMax, n, param, domain, analytic) + * double f Generated filter vector + * int filt_type Type of filter wanted + * double bw Bandwidth of filter + * double filterMin, filterMax Filter limits + * int nSignalPoints Number of points in signal + * double param General input parameter to filters + * int domain FREQUENCY or SPATIAL domain wanted */ -double * -filter_generate (const FilterType filt_type, double bw, double xmin, double xmax, int n, double param, const DomainType domain, int numint) +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_vecFourierSinTable = NULL; + m_idFilter = convertFilterNameToID (filterName); + if (m_idFilter == FILTER_INVALID) { + m_fail = true; + m_failMessage = "Invalid Filter name "; + m_failMessage += filterName; + return; + } + m_idFilterMethod = convertFilterMethodNameToID (filterMethodName); + if (m_idFilterMethod == FILTER_METHOD_INVALID) { + m_fail = true; + m_failMessage = "Invalid filter method name "; + m_failMessage += filterMethodName; + return; + } + m_idDomain = convertDomainNameToID (domainName); + if (m_idDomain == DOMAIN_INVALID) { + m_fail = true; + m_failMessage = "Invalid domain name "; + m_failMessage += domainName; + return; + } + 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 zeropad = 0, int preinterpolationFactor = 1) +{ + init (filterID, filterMethodID, bw, signalIncrement, nSignalPoints, param, domainID, zeropad, preinterpolationFactor); +} + +SignalFilter::SignalFilter (const char* filterName, const char* domainName, double bw, double param) +{ + m_bw = bw; + m_nSignalPoints = 0; + m_nFilterPoints = 0; + m_vecFilter = NULL; + m_vecFourierCosTable = NULL; + m_vecFourierSinTable = NULL; + m_filterParam = param; + m_idFilter = convertFilterNameToID (filterName); + if (m_idFilter == FILTER_INVALID) { + m_fail = true; + m_failMessage = "Invalid Filter name "; + m_failMessage += filterName; + return; + } + m_idDomain = convertDomainNameToID (domainName); + if (m_idDomain == DOMAIN_INVALID) { + m_fail = true; + m_failMessage = "Invalid domain name "; + m_failMessage += domainName; + return; + } +} + +void +SignalFilter::init (const FilterID filterID, const FilterMethodID filterMethodID, double bw, double signalIncrement, int nSignalPoints, double filterParam, const DomainID domainID, int zeropad, int preinterpolationFactor) { - double *f = new double [n]; - double xinc = (xmax - xmin) / (n - 1); - - if (filt_type == FILTER_SHEPP) { - double a = 2 * bw; - double c = - 4. / (a * a); - int center = (n - 1) / 2; - int sidelen = center; - f[center] = 4. / (a * a); + m_bw = bw; + m_idFilter = filterID; + m_idDomain = domainID; + m_idFilterMethod = filterMethodID; + if (m_idFilter == FILTER_INVALID || m_idDomain == DOMAIN_INVALID || m_idFilterMethod == FILTER_METHOD_INVALID) { + m_fail = true; + return; + } + m_traceLevel = TRACE_NONE; + m_nameFilter = convertFilterIDToName (m_idFilter); + m_nameDomain = convertDomainIDToName (m_idDomain); + m_nameFilterMethod = convertFilterMethodIDToName (m_idFilterMethod); + m_fail = false; + m_nSignalPoints = nSignalPoints; + m_signalInc = signalIncrement; + 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_zeropad > 0) { + double logBase2 = log(m_nSignalPoints) / log(2); + int nextPowerOf2 = static_cast(floor(logBase2)); + if (logBase2 != floor(logBase2)) + nextPowerOf2++; + nextPowerOf2 += (m_zeropad - 1); + m_nFilterPoints = 1 << nextPowerOf2; + 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(i) / halfFilter/ (2. * m_signalInc); + for (int i = 1; i <= halfFilter; i++) + m_vecFilter[m_nFilterPoints - i] = static_cast(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 + 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_vecFilter = new double[ m_nFilterPoints ]; - for (int i = 1; i <= sidelen; i++ ) - f [center + i] = f [center - i] = c / (4 * (i * i) - 1); - } else if (domain == D_FREQ) { - double x; - int i; - for (x = xmin, i = 0; i < n; x += xinc, i++) - f[i] = filter_frequency_response (filt_type, x, bw, param); - } else if (domain == D_SPATIAL) { - double x; - int i; - for (x = xmin, i = 0; i < n; x += xinc, i++) - if (numint == 0) - f[i] = filter_spatial_response_analytic (filt_type, x, bw, param); - else - f[i] = filter_spatial_response_calc (filt_type, x, bw, param, numint); - } else { - sys_error (ERR_WARNING, "Illegal domain %d [filt_generate]", domain); - return (NULL); + if (m_idFilter == FILTER_SHEPP) { + double a = 2 * m_bw; + double c = - 4. / (a * a); + int center = (m_nFilterPoints - 1) / 2; + int sidelen = center; + m_vecFilter[center] = 4. / (a * a); + + for (int i = 1; i <= sidelen; i++ ) + m_vecFilter [center + i] = m_vecFilter [center - i] = c / (4 * (i * i) - 1); + } else if (m_idDomain == DOMAIN_FREQUENCY) { + double x; + int i; + for (x = m_filterMin, i = 0; i < m_nFilterPoints; x += m_filterInc, i++) + 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 (haveAnalyticSpatial(m_idFilter)) + m_vecFilter[i] = spatialResponseAnalytic (x, m_filterParam); + else + m_vecFilter[i] = spatialResponseCalc (x, m_filterParam); + } else { + m_failMessage = "Illegal domain name "; + m_failMessage += m_idDomain; + m_fail = true; + } } - - return (f); } +SignalFilter::~SignalFilter (void) +{ + delete [] m_vecFilter; + delete [] m_vecFourierSinTable; + delete [] m_vecFourierCosTable; + +#if HAVE_FFTW + 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 +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; + + 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; + + return (name); +} + +const SignalFilter::FilterMethodID +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_FOURIER_TABLE_STR) == 0) + fmID = FILTER_METHOD_FOURIER_TABLE; + else if (strcasecmp (filterMethodName, FILTER_METHOD_FFT_STR) == 0) + fmID = FILTER_METHOD_FFT; +#if HAVE_FFTW + else if (strcasecmp (filterMethodName, FILTER_METHOD_FFTW_STR) == 0) + fmID = FILTER_METHOD_FFTW; + else if (strcasecmp (filterMethodName, FILTER_METHOD_RFFTW_STR) == 0) + fmID = FILTER_METHOD_RFFTW; +#endif + + 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_FOURIER_TABLE) + return (FILTER_METHOD_FOURIER_TABLE_STR); + else if (fmID == FILTER_METHOD_FFT) + return (FILTER_METHOD_FFT_STR); +#if HAVE_FFTW + else if (fmID == FILTER_METHOD_FFTW) + return (FILTER_METHOD_FFTW_STR); + else if (fmID == FILTER_METHOD_RFFTW) + return (FILTER_METHOD_RFFTW_STR); +#endif + + return (name); +} + +const SignalFilter::DomainID +SignalFilter::convertDomainNameToID (const char* const domainName) +{ + DomainID dID = DOMAIN_INVALID; + + if (strcasecmp (domainName, DOMAIN_SPATIAL_STR) == 0) + dID = DOMAIN_SPATIAL; + else if (strcasecmp (domainName, DOMAIN_FREQUENCY_STR) == 0) + dID = DOMAIN_FREQUENCY; + + return (dID); +} + +const char * +SignalFilter::convertDomainIDToName (const DomainID domain) +{ + const char *name = ""; + + if (domain == DOMAIN_SPATIAL) + return (DOMAIN_SPATIAL_STR); + else if (domain == DOMAIN_FREQUENCY) + return (DOMAIN_FREQUENCY_STR); + + return (name); +} + +void +SignalFilter::filterSignal (const float input[], double output[]) const +{ + if (m_idFilterMethod == FILTER_METHOD_CONVOLUTION) { + for (int i = 0; i < m_nSignalPoints; i++) + output[i] = convolve (input, m_signalInc, i, m_nSignalPoints); + } else if (m_idFilterMethod == FILTER_METHOD_FOURIER) { + 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 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] = 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 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] = 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 +SignalFilter::response (double x) +{ + double response = 0; + + if (m_idDomain == DOMAIN_SPATIAL) + response = spatialResponse (m_idFilter, m_bw, x, m_filterParam); + else if (m_idDomain == DOMAIN_FREQUENCY) + response = frequencyResponse (m_idFilter, m_bw, x, m_filterParam); + + return (response); +} + + +double +SignalFilter::spatialResponse (FilterID filterID, double bw, double x, double param) +{ + if (haveAnalyticSpatial(filterID)) + return spatialResponseAnalytic (filterID, bw, x, param); + else + return spatialResponseCalc (filterID, bw, x, param, N_INTEGRAL); +} /* NAME * filter_spatial_response_calc Calculate filter by discrete inverse fourier @@ -89,21 +494,27 @@ filter_generate (const FilterType filt_type, double bw, double xmin, double xmax * response * * SYNOPSIS - * y = filter_spatial_response_calc (filt_type, x, bw, param, n) + * y = filter_spatial_response_calc (filt_type, x, m_bw, param, n) * double y Filter's response in spatial domain * int filt_type Type of filter (definitions in ct.h) * double x Spatial position to evaluate filter - * double bw Bandwidth of window + * double m_bw Bandwidth of window * double param General parameter for various filters * int n Number of points to calculate integrations */ double -filter_spatial_response_calc (int filt_type, double x, double bw, double param, int n) +SignalFilter::spatialResponseCalc (double x, double param) const +{ + return (spatialResponseCalc (m_idFilter, m_bw, x, param, N_INTEGRAL)); +} + +double +SignalFilter::spatialResponseCalc (FilterID filterID, double bw, double x, double param, int n) { double zmin, zmax; - if (filt_type == FILTER_TRIANGLE) { + if (filterID == FILTER_TRIANGLE) { zmin = 0; zmax = bw; } else { @@ -115,7 +526,7 @@ filter_spatial_response_calc (int filt_type, double x, double bw, double param, double z = zmin; double q [n]; for (int i = 0; i < n; i++, z += zinc) - q[i] = filter_frequency_response (filt_type, z, bw, param) * cos (TWOPI * z * x); + q[i] = frequencyResponse (filterID, bw, z, param) * cos (TWOPI * z * x); double y = 2 * integrateSimpson (zmin, zmax, q, n); @@ -127,21 +538,28 @@ filter_spatial_response_calc (int filt_type, double x, double bw, double param, * filter_frequency_response Return filter frequency response * * SYNOPSIS - * h = filter_frequency_response (filt_type, u, bw, param) + * h = filter_frequency_response (filt_type, u, m_bw, param) * double h Filters frequency response at u * int filt_type Type of filter * double u Frequency to evaluate filter at - * double bw Bandwidth of filter + * double m_bw Bandwidth of filter * double param General input parameter for various filters */ double -filter_frequency_response (int filt_type, double u, double bw, double param) +SignalFilter::frequencyResponse (double u, double param) const +{ + return frequencyResponse (m_idFilter, m_bw, u, param); +} + + +double +SignalFilter::frequencyResponse (FilterID filterID, double bw, double u, double param) { double q; double au = fabs (u); - switch (filt_type) { + switch (filterID) { case FILTER_BANDLIMIT: if (au >= bw / 2) q = 0.; @@ -192,9 +610,7 @@ filter_frequency_response (int filt_type, double u, double bw, double param) break; default: q = 0; - sys_error (ERR_WARNING, - "Frequency response for filter %d not implemented [filter_frequency_response]", - filt_type); + sys_error (ERR_WARNING, "Frequency response for filter %d not implemented [filter_frequency_response]", filterID); break; } return (q); @@ -208,16 +624,46 @@ filter_frequency_response (int filt_type, double u, double bw, double param) * response * * SYNOPSIS - * y = filter_spatial_response_analytic (filt_type, x, bw, param) + * y = filter_spatial_response_analytic (filt_type, x, m_bw, param) * double y Filter's response in spatial domain * int filt_type Type of filter (definitions in ct.h) * double x Spatial position to evaluate filter - * double bw Bandwidth of window + * double m_bw Bandwidth of window * double param General parameter for various filters */ double -filter_spatial_response_analytic (int filt_type, double x, double bw, double param) +SignalFilter::spatialResponseAnalytic (double x, double param) const +{ + return spatialResponseAnalytic (m_idFilter, m_bw, x, param); +} + +const bool +SignalFilter::haveAnalyticSpatial (FilterID 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) { double q, temp; double u = TWOPI * x; @@ -225,7 +671,7 @@ filter_spatial_response_analytic (int filt_type, double x, double bw, double par double b = PI / bw; double b2 = TWOPI / bw; - switch (filt_type) { + switch (filterID) { case FILTER_BANDLIMIT: q = bw * sinc(u * w, 1.0); break; @@ -237,8 +683,7 @@ filter_spatial_response_analytic (int filt_type, double x, double bw, double par q = sinc(b-u,w) + sinc(b+u,w); break; case FILTER_G_HAMMING: - q = 2 * param * sin(u*w)/u + (1-param) * - (sinc(b2-u, w) + sinc(b2+u, w)); + q = 2 * param * sin(u*w)/u + (1-param) * (sinc(b2-u, w) + sinc(b2+u, w)); break; case FILTER_ABS_BANDLIMIT: q = 2 * integral_abscos (u, w); @@ -264,9 +709,7 @@ filter_spatial_response_analytic (int filt_type, double x, double bw, double par break; case FILTER_ABS_SINC: default: - sys_error (ERR_WARNING, - "Analytic filter type %d not implemented [filter_spatial_response_analytic]", - filt_type); + sys_error (ERR_WARNING, "Analytic filter type %d not implemented [filter_spatial_response_analytic]", filterID); q = 0; break; } @@ -287,12 +730,6 @@ filter_spatial_response_analytic (int filt_type, double x, double bw, double par * v = sin(x * mult) / x; */ -double -sinc (double x, double mult) -{ - return (fabs(x) > F_EPSILON ? (sin (x * mult) / x) : 1.0); -} - /* NAME * integral_abscos Returns integral of u*cos(u) @@ -308,12 +745,233 @@ sinc (double x, double mult) */ double -integral_abscos (double u, double w) +SignalFilter::integral_abscos (double u, double w) { - if (fabs (u) > F_EPSILON) - return (cos(u * w) - 1) / (u * u) + w / u * sin (u * w); - else - return (w * w / 2); + return (fabs (u) > F_EPSILON + ? (cos(u * w) - 1) / (u * u) + w / u * sin (u * w) + : (w * w / 2)); +} + + +/* 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 +SignalFilter::convolve (const double func[], const double dx, const int n, const int np) const +{ + double sum = 0.0; + +#if UNOPTIMIZED_CONVOLUTION + for (int i = 0; i < np; i++) + sum += func[i] * m_vecFilter[n - i + (np - 1)]; +#else + double* f2 = m_vecFilter + n + (np - 1); + for (int i = 0; i < np; i++) + sum += *func++ * *f2--; +#endif + + return (sum * dx); +} + + +double +SignalFilter::convolve (const float func[], const double dx, const int n, const int np) const +{ + double sum = 0.0; + +#if UNOPTIMIZED_CONVOLUTION +for (int i = 0; i < np; i++) + sum += func[i] * m_vecFilter[n - i + (np - 1)]; +#else +double* f2 = m_vecFilter + n + (np - 1); +for (int i = 0; i < np; i++) + sum += *func++ * *f2--; +#endif + + return (sum * dx); +} + + +void +SignalFilter::finiteFourierTransform (const double input[], complex output[], const int n, int direction) +{ + if (direction < 0) + direction = -1; + else + direction = 1; + + 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; + sumReal += input[j] * cos(angle); + sumImag += input[j] * sin(angle); + } + if (direction < 0) { + sumReal /= n; + sumImag /= n; + } + output[i] = complex (sumReal, sumImag); + } +} + + +void +SignalFilter::finiteFourierTransform (const complex input[], complex output[], const int n, int direction) +{ + if (direction < 0) + direction = -1; + else + direction = 1; + + double angleIncrement = direction * 2 * PI / n; + for (int i = 0; i < n; i++) { + complex sum (0,0); + for (int j = 0; j < n; j++) { + double angle = i * j * angleIncrement; + complex exponentTerm (cos(angle), sin(angle)); + sum += input[j] * exponentTerm; + } + if (direction < 0) { + sum /= n; + } + output[i] = sum; + } +} + +void +SignalFilter::finiteFourierTransform (const complex input[], double output[], const int n, int direction) +{ + if (direction < 0) + direction = -1; + else + direction = 1; + + 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 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_nFilterPoints; j++) { + int tableIndex = i * j; + if (direction > 0) { + sumReal += input[j] * m_vecFourierCosTable[tableIndex]; + sumImag += input[j] * m_vecFourierSinTable[tableIndex]; + } else { + sumReal += input[j] * m_vecFourierCosTable[tableIndex]; + sumImag -= input[j] * m_vecFourierSinTable[tableIndex]; + } + } + if (direction < 0) { + sumReal /= m_nFilterPoints; + sumImag /= m_nFilterPoints; + } + output[i] = complex (sumReal, sumImag); + } +} + +// (a+bi) * (c + di) = (ac - bd) + (ad + bc)i +void +SignalFilter::finiteFourierTransform (const complex input[], complex 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_nFilterPoints; j++) { + int tableIndex = i * j; + if (direction > 0) { + 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[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_nFilterPoints; + sumImag /= m_nFilterPoints; + } + output[i] = complex (sumReal, sumImag); + } +} + +void +SignalFilter::finiteFourierTransform (const complex input[], double output[], int direction) const +{ + 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; + } }