-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Numerical evaluation of Fourier transform of Daubechies scaling funct…
…ions. [ci skip]
- Loading branch information
1 parent
e2c989c
commit c4b0791
Showing
4 changed files
with
237 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
include/boost/math/special_functions/fourier_transform_daubechies_scaling.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright Nick Thompson, 2023 | ||
* Use, modification and distribution are subject to the | ||
* Boost Software License, Version 1.0. (See accompanying file | ||
* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
*/ | ||
|
||
#ifndef BOOST_MATH_SPECIAL_FOURIER_TRANSFORM_DAUBECHIES_SCALING_HPP | ||
#define BOOST_MATH_SPECIAL_FOURIER_TRANSFORM_DAUBECHIES_SCALING_HPP | ||
#include <cmath> | ||
#include <complex> | ||
#include <iostream> | ||
#include <boost/math/constants/constants.hpp> | ||
#include <boost/math/filters/daubechies.hpp> | ||
|
||
/* | ||
* Given an angular frequency ω, computes a numerical approximation to 𝓕[𝜙](ω), | ||
* where 𝜙 is the Daubechies scaling function. | ||
* N.B.: This is *slow*; take ~352ns to recover double precision on M1. | ||
* The goal of this is to have *something*, rather than nothing. | ||
* and fast evaluation of these function seems to me to be a research project. | ||
* In any case, this is an infinite product of trigonometric polynomials. | ||
* See Daubechies, 10 Lectures on Wavelets, equation 5.1.17, 5.1.18. | ||
* This uses the factorization of m₀ shown in Corollary 5.5.4 in Ten Lectures and using equation 5.5.5. | ||
* See more discusion near equation 6.1.1. | ||
*/ | ||
namespace boost::math { | ||
|
||
template<class Real, int p> | ||
std::complex<Real> fourier_transform_daubechies_scaling(Real omega) { | ||
static_assert(p==3, "Only 3 vanishing moments have been implemented as we're currently experimenting with algorithms, not bulletproofing."); | ||
// This arg promotion is kinda sad, but IMO the accuracy is not good enough in float precision using this method. | ||
// Requesting a better algorithm! | ||
// N.B.: I'm currently commenting this out because right now I'm *only* focusing on the performance, and this is only for accuracy: | ||
//if constexpr (std::is_same_v<Real, float>) { | ||
// return static_cast<std::complex<float>>(fourier_transform_daubechies_scaling<double, p>(static_cast<double>(omega))); | ||
//} | ||
using std::sqrt; | ||
using std::abs; | ||
using std::norm; | ||
using std::pow; | ||
using boost::math::constants::one_div_root_two_pi; | ||
// See the Table 6.2 of Daubechies, Ten Lectures on Wavelets. | ||
// I'll implement more accurate tables once we know this method works! | ||
const std::array<Real, 3> lxi{static_cast<Real>(2.6613644236)/sqrt(Real(2)), static_cast<Real>(-1.52896119631)/sqrt(Real(2)), static_cast<Real>(0.281810335086)/sqrt(Real(2))}; | ||
auto xi = -omega/2; | ||
std::complex<Real> phi{one_div_root_two_pi<Real>(), 0}; | ||
std::complex<Real> L{std::numeric_limits<Real>::quiet_NaN(), std::numeric_limits<Real>::quiet_NaN()}; | ||
std::complex<Real> prefactor{Real(1), Real(0)}; | ||
do { | ||
std::complex<Real> arg{0, xi}; | ||
auto z = std::exp(arg); | ||
// Horner's method for each term in the infinite product: | ||
int64_t n = lxi.size() - 1; | ||
L = std::complex<Real>(lxi.back(), Real(0)); | ||
for (int64_t i = n - 1; i >= 0; --i) { | ||
// I have tried replacing this complex multiplication with a Kahan difference of products to improve precision, but no joy: | ||
L = z*L + lxi[i]; | ||
} | ||
phi *= L; | ||
prefactor *= (Real(1) + z)/Real(2); | ||
xi /= 2; | ||
} while (abs(xi) > std::numeric_limits<Real>::epsilon()); | ||
return phi*static_cast<std::complex<Real>>(std::pow(prefactor, p)); | ||
} | ||
|
||
template<class Real, int p> | ||
std::complex<Real> fourier_transform_daubechies_wavelet(Real omega) { | ||
// See Daubechies, 10 Lectures on Wavelets, page 135, unlabelled equation just after 5.1.31: | ||
// 𝓕[ψ](ω) = exp(iω/2)conj(m0(ω/2 + π))𝓕[𝜙](ω) | ||
throw std::domain_error("Not yet implemented!"); | ||
} | ||
|
||
} | ||
#endif |
33 changes: 33 additions & 0 deletions
33
reporting/performance/fourier_transform_daubechies_scaling_performance.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// (C) Copyright Nick Thompson 2023. | ||
// Use, modification and distribution are subject to the | ||
// Boost Software License, Version 1.0. (See accompanying file | ||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
|
||
#include <random> | ||
#include <array> | ||
#include <vector> | ||
#include <iostream> | ||
#include <benchmark/benchmark.h> | ||
#include <boost/math/special_functions/fourier_transform_daubechies_scaling.hpp> | ||
|
||
using boost::math::fourier_transform_daubechies_scaling; | ||
|
||
template<class Real> | ||
void FourierTransformDaubechiesScaling(benchmark::State& state) | ||
{ | ||
std::random_device rd; | ||
auto seed = rd(); | ||
std::mt19937_64 mt(seed); | ||
std::uniform_real_distribution<Real> unif(0, 10); | ||
|
||
for (auto _ : state) | ||
{ | ||
benchmark::DoNotOptimize(fourier_transform_daubechies_scaling<Real, 3>(unif(mt))); | ||
} | ||
} | ||
|
||
BENCHMARK_TEMPLATE(FourierTransformDaubechiesScaling, float); | ||
BENCHMARK_TEMPLATE(FourierTransformDaubechiesScaling, double); | ||
//BENCHMARK_TEMPLATE(FourierTransformDaubechiesScaling, long double); | ||
|
||
BENCHMARK_MAIN(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* Copyright Nick Thompson, 2023 | ||
* Use, modification and distribution are subject to the | ||
* Boost Software License, Version 1.0. (See accompanying file | ||
* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
*/ | ||
|
||
#include "math_unit_test.hpp" | ||
#include <numeric> | ||
#include <utility> | ||
#include <iomanip> | ||
#include <iostream> | ||
#include <random> | ||
#include <boost/math/tools/condition_numbers.hpp> | ||
#include <boost/math/constants/constants.hpp> | ||
#include <boost/math/quadrature/trapezoidal.hpp> | ||
#include <boost/math/special_functions/daubechies_scaling.hpp> | ||
#include <boost/math/special_functions/fourier_transform_daubechies_scaling.hpp> | ||
|
||
#ifdef BOOST_HAS_FLOAT128 | ||
#include <boost/multiprecision/float128.hpp> | ||
using boost::multiprecision::float128; | ||
#endif | ||
|
||
using boost::math::fourier_transform_daubechies_scaling; | ||
using boost::math::tools::summation_condition_number; | ||
using boost::math::constants::two_pi; | ||
using boost::math::constants::one_div_root_two_pi; | ||
using boost::math::quadrature::trapezoidal; | ||
// 𝓕[φ](-ω) = 𝓕[φ](ω)* | ||
template<typename Real, int p> | ||
void test_evaluation_symmetry() { | ||
auto phi = fourier_transform_daubechies_scaling<Real, p>(0.0); | ||
CHECK_ULP_CLOSE(one_div_root_two_pi<Real>(), phi.real(), 3); | ||
CHECK_ULP_CLOSE(static_cast<Real>(0), phi.imag(), 3); | ||
|
||
Real domega = Real(1)/128; | ||
for (Real omega = domega; omega < 10; omega += domega) { | ||
auto phi1 = fourier_transform_daubechies_scaling<Real, p>(-omega); | ||
auto phi2 = fourier_transform_daubechies_scaling<Real, p>(omega); | ||
CHECK_ULP_CLOSE(phi1.real(), phi2.real(), 3); | ||
CHECK_ULP_CLOSE(phi1.imag(), -phi2.imag(), 3); | ||
} | ||
|
||
for (Real omega = 10; omega < std::numeric_limits<double>::max(); omega *= 10) { | ||
auto phi1 = fourier_transform_daubechies_scaling<Real, p>(-omega); | ||
auto phi2 = fourier_transform_daubechies_scaling<Real, p>(omega); | ||
CHECK_ULP_CLOSE(phi1.real(), phi2.real(), 3); | ||
CHECK_ULP_CLOSE(phi1.imag(), -phi2.imag(), 3); | ||
} | ||
} | ||
|
||
template<int p> | ||
void test_quadrature() { | ||
auto phi = boost::math::daubechies_scaling<double, p>(); | ||
auto [tmin, tmax] = phi.support(); | ||
double domega = 1/128.0; | ||
for (double omega = domega; omega < 10; omega += domega) { | ||
// I suspect the quadrature is less accurate than special function evaluation, so this is just a sanity check: | ||
auto f = [&](double t) { | ||
return phi(t)*std::exp(std::complex<double>(0, -omega*t))*one_div_root_two_pi<double>(); | ||
}; | ||
auto expected = trapezoidal(f, tmin, tmax, 2*std::numeric_limits<double>::epsilon()); | ||
auto computed = fourier_transform_daubechies_scaling<float, p>(static_cast<float>(omega)); | ||
CHECK_MOLLIFIED_CLOSE(static_cast<float>(expected.real()), computed.real(), 1e-9); | ||
CHECK_MOLLIFIED_CLOSE(static_cast<float>(expected.imag()), computed.imag(), 1e-9); | ||
} | ||
} | ||
|
||
// Tests Daubechies "Ten Lectures on Wavelets", equation 5.1.19: | ||
template<typename Real, int p> | ||
void test_ten_lectures_eq_5_1_19() { | ||
Real domega = Real(1)/Real(16); | ||
for (Real omega = 0; omega < 1; omega += domega) { | ||
Real term = std::norm(fourier_transform_daubechies_scaling<Real, p>(omega)); | ||
auto sum = summation_condition_number<Real>(term); | ||
int64_t l = 1; | ||
while (l < 50 && term > 2*std::numeric_limits<Real>::epsilon()) { | ||
Real tpl = std::norm(fourier_transform_daubechies_scaling<Real, p>(omega + two_pi<Real>()*l)); | ||
Real tml = std::norm(fourier_transform_daubechies_scaling<Real, p>(omega - two_pi<Real>()*l)); | ||
|
||
sum += tpl; | ||
sum += tml; | ||
Real term = tpl + tml; | ||
++l; | ||
} | ||
CHECK_ULP_CLOSE(1/two_pi<Real>(), sum.sum(), 13); | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
test_evaluation_symmetry<float, 2>(); | ||
test_evaluation_symmetry<float, 6>(); | ||
test_evaluation_symmetry<float, 8>(); | ||
test_evaluation_symmetry<float, 16>(); | ||
|
||
test_quadrature<17>(); | ||
test_quadrature<18>(); | ||
|
||
test_ten_lectures_eq_5_1_19<float, 2>(); | ||
test_ten_lectures_eq_5_1_19<float, 3>(); | ||
test_ten_lectures_eq_5_1_19<float, 4>(); | ||
test_ten_lectures_eq_5_1_19<float, 5>(); | ||
test_ten_lectures_eq_5_1_19<float, 6>(); | ||
test_ten_lectures_eq_5_1_19<float, 7>(); | ||
test_ten_lectures_eq_5_1_19<float, 8>(); | ||
test_ten_lectures_eq_5_1_19<float, 9>(); | ||
test_ten_lectures_eq_5_1_19<float, 10>(); | ||
|
||
return boost::math::test::report_errors(); | ||
} |