Skip to content

Commit 4adbc27

Browse files
committed
Numerical evaluation of Fourier transform of Daubechies scaling functions. [ci skip]
1 parent e2c989c commit 4adbc27

5 files changed

+291
-0
lines changed

doc/sf/daubechies.qbk

+17
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,23 @@ The 2 vanishing moment scaling function.
127127
[$../graphs/daubechies_8_scaling.svg]
128128
The 8 vanishing moment scaling function.
129129

130+
Boost.Math also provides numerical evaluation of the Fourier transform of these functions.
131+
This is useful in sparse recovery problems where the measurements are taken in the Fourier basis.
132+
The usage is exhibited below:
133+
134+
#include <boost/math/special_functions/fourier_transform_daubechies_scaling.hpp>
135+
using boost::math::fourier_transform_daubechies_scaling;
136+
// Evaluate the Fourier transform of the 4-vanishing moment Daubechies scaling function at ω=1.8:
137+
std::complex<float> hat_phi = fourier_transform_daubechies_scaling<float, 4>(1.8f);
138+
139+
The Fourier transform convention is unitary with the sign of i being given in Daubechies Ten Lectures.
140+
In particular, this means that `fourier_transform_daubechies_scaling<float, p>(0.0)` returns 1/sqrt(2π).
141+
142+
The implementation computes an infinite product of trigonometric polynomials as can be found from recursive application of the identity 𝓕[φ](ω) = m(ω/2)𝓕[φ](ω/2).
143+
This is neither particularly fast nor accurate, but there appears to be no literature on this extremely useful topic, and hence the naive method must suffice.
144+
145+
146+
130147
[heading References]
131148

132149
* Daubechies, Ingrid. ['Ten Lectures on Wavelets.] Vol. 61. Siam, 1992.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <boost/math/filters/daubechies.hpp>
2+
#include <boost/math/tools/polynomial.hpp>
3+
#include <boost/multiprecision/cpp_bin_float.hpp>
4+
#include <boost/math/constants/constants.hpp>
5+
6+
using std::pow;
7+
using boost::multiprecision::cpp_bin_float_100;
8+
using boost::math::filters::daubechies_scaling_filter;
9+
using boost::math::tools::polynomial;
10+
using boost::math::constants::half;
11+
using boost::math::constants::root_two;
12+
13+
template<typename Real, size_t N>
14+
std::vector<Real> get_constants() {
15+
auto h = daubechies_scaling_filter<cpp_bin_float_100, N>();
16+
auto p = polynomial<cpp_bin_float_100>(h.begin(), h.end());
17+
18+
auto q = polynomial({half<cpp_bin_float_100>(), half<cpp_bin_float_100>()});
19+
q = pow(q, N);
20+
auto l = p/q;
21+
return l.data();
22+
}
23+
24+
template<typename Real>
25+
void print_constants(std::vector<Real> const & l) {
26+
std::cout << std::setprecision(std::numeric_limits<Real>::digits10 -10);
27+
std::cout << "constexpr const std::array<Real, " << l.size() << ">{";
28+
for (size_t i = 0; i < l.size() - 1; ++i) {
29+
std::cout << "BOOST_MATH_BIG_CONSTANT(Real, std::numeric_limits<Real>::digits, " << l[i]/root_two<Real>() << "), ";
30+
}
31+
std::cout << l.back()/root_two<Real>() << "};";
32+
}
33+
34+
int main() {
35+
auto constants = get_constants<cpp_bin_float_100, 3>();
36+
print_constants(constants);
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright Nick Thompson, Matt Borland, 2023
3+
* Use, modification and distribution are subject to the
4+
* Boost Software License, Version 1.0. (See accompanying file
5+
* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
*/
7+
8+
#ifndef BOOST_MATH_SPECIAL_FOURIER_TRANSFORM_DAUBECHIES_SCALING_HPP
9+
#define BOOST_MATH_SPECIAL_FOURIER_TRANSFORM_DAUBECHIES_SCALING_HPP
10+
#include <cmath>
11+
#include <complex>
12+
#include <iostream>
13+
#include <array>
14+
#include <limits>
15+
#include <boost/math/constants/constants.hpp>
16+
#include <boost/math/tools/big_constant.hpp>
17+
18+
namespace boost::math {
19+
20+
namespace detail {
21+
22+
// See the Table 6.2 of Daubechies, Ten Lectures on Wavelets.
23+
// These constants are precisely those divided by 1/sqrt(2), because otherwise we'd immediately just have to divide through by 1/sqrt(2):
24+
template<typename Real, size_t N>
25+
constexpr const std::array<Real, N> ft_daubechies_scaling_polynomial_coefficients() {
26+
if constexpr (N==3) {
27+
return std::array<Real, 3>{BOOST_MATH_BIG_CONSTANT(Real, std::numeric_limits<Real>::digits, 1.88186883113665472301331643028468183320710177910151845853383427363197699204347143889269703), BOOST_MATH_BIG_CONSTANT(Real, std::numeric_limits<Real>::digits, -1.08113883008418966599944677221635926685977756966260841342875242639629721931484516409937898), 0.199269998947534942986130341931677433652675790561089954894918152764320227250084833874126086};
28+
}
29+
else {
30+
throw std::domain_error("Not yet implemented.");
31+
}
32+
}
33+
}
34+
35+
/*
36+
* Given an angular frequency ω, computes a numerical approximation to 𝓕[𝜙](ω),
37+
* where 𝜙 is the Daubechies scaling function.
38+
* N.B.: This is *slow*; take ~352ns to recover double precision on M1.
39+
* The goal of this is to have *something*, rather than nothing.
40+
* and fast evaluation of these function seems to me to be a research project.
41+
* In any case, this is an infinite product of trigonometric polynomials.
42+
* See Daubechies, 10 Lectures on Wavelets, equation 5.1.17, 5.1.18.
43+
* This uses the factorization of m₀ shown in Corollary 5.5.4 in Ten Lectures and using equation 5.5.5.
44+
* See more discusion near equation 6.1.1.
45+
*/
46+
47+
template<class Real, int p>
48+
std::complex<Real> fourier_transform_daubechies_scaling(Real omega) {
49+
static_assert(p==3, "Only 3 vanishing moments have been implemented as we're currently experimenting with algorithms, not bulletproofing.");
50+
// This arg promotion is kinda sad, but IMO the accuracy is not good enough in float precision using this method.
51+
// Requesting a better algorithm!
52+
// N.B.: I'm currently commenting this out because right now I'm *only* focusing on the performance, and this is only for accuracy:
53+
//if constexpr (std::is_same_v<Real, float>) {
54+
// return static_cast<std::complex<float>>(fourier_transform_daubechies_scaling<double, p>(static_cast<double>(omega)));
55+
//}
56+
using std::sqrt;
57+
using std::abs;
58+
using std::norm;
59+
using std::pow;
60+
using std::exp;
61+
using boost::math::constants::one_div_root_two_pi;
62+
auto const lxi = detail::ft_daubechies_scaling_polynomial_coefficients<Real, p>();
63+
auto xi = -omega/2;
64+
std::complex<Real> phi{one_div_root_two_pi<Real>(), 0};
65+
std::complex<Real> L{std::numeric_limits<Real>::quiet_NaN(), std::numeric_limits<Real>::quiet_NaN()};
66+
std::complex<Real> prefactor{Real(1), Real(0)};
67+
do {
68+
std::complex<Real> arg{0, xi};
69+
auto z = exp(arg);
70+
// Horner's method for each term in the infinite product:
71+
int64_t n = lxi.size() - 1;
72+
L = std::complex<Real>(lxi.back(), Real(0));
73+
for (int64_t i = n - 1; i >= 0; --i) {
74+
// I have tried replacing this complex multiplication with a Kahan difference of products to improve precision, but no joy:
75+
L = z*L + lxi[i];
76+
}
77+
phi *= L;
78+
prefactor *= (Real(1) + z)/Real(2);
79+
xi /= 2;
80+
} while (abs(xi) > std::numeric_limits<Real>::epsilon());
81+
return phi*static_cast<std::complex<Real>>(pow(prefactor, p));
82+
}
83+
84+
template<class Real, int p>
85+
std::complex<Real> fourier_transform_daubechies_wavelet(Real omega) {
86+
// See Daubechies, 10 Lectures on Wavelets, page 135, unlabelled equation just after 5.1.31:
87+
// 𝓕[ψ](ω) = exp(iω/2)conj(m0(ω/2 + π))𝓕[𝜙](ω)
88+
throw std::domain_error("Not yet implemented!");
89+
}
90+
91+
}
92+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// (C) Copyright Nick Thompson 2023.
2+
// Use, modification and distribution are subject to the
3+
// Boost Software License, Version 1.0. (See accompanying file
4+
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5+
6+
#include <random>
7+
#include <array>
8+
#include <vector>
9+
#include <iostream>
10+
#include <benchmark/benchmark.h>
11+
#include <boost/math/special_functions/fourier_transform_daubechies_scaling.hpp>
12+
13+
using boost::math::fourier_transform_daubechies_scaling;
14+
15+
template<class Real>
16+
void FourierTransformDaubechiesScaling(benchmark::State& state)
17+
{
18+
std::random_device rd;
19+
auto seed = rd();
20+
std::mt19937_64 mt(seed);
21+
std::uniform_real_distribution<Real> unif(0, 10);
22+
23+
for (auto _ : state)
24+
{
25+
benchmark::DoNotOptimize(fourier_transform_daubechies_scaling<Real, 3>(unif(mt)));
26+
}
27+
}
28+
29+
BENCHMARK_TEMPLATE(FourierTransformDaubechiesScaling, float);
30+
BENCHMARK_TEMPLATE(FourierTransformDaubechiesScaling, double);
31+
//BENCHMARK_TEMPLATE(FourierTransformDaubechiesScaling, long double);
32+
33+
BENCHMARK_MAIN();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Copyright Nick Thompson, 2023
3+
* Use, modification and distribution are subject to the
4+
* Boost Software License, Version 1.0. (See accompanying file
5+
* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
*/
7+
8+
#include "math_unit_test.hpp"
9+
#include <numeric>
10+
#include <utility>
11+
#include <iomanip>
12+
#include <iostream>
13+
#include <random>
14+
#include <boost/math/tools/condition_numbers.hpp>
15+
#include <boost/math/constants/constants.hpp>
16+
#include <boost/math/quadrature/trapezoidal.hpp>
17+
#include <boost/math/special_functions/daubechies_scaling.hpp>
18+
#include <boost/math/special_functions/fourier_transform_daubechies_scaling.hpp>
19+
20+
#ifdef BOOST_HAS_FLOAT128
21+
#include <boost/multiprecision/float128.hpp>
22+
using boost::multiprecision::float128;
23+
#endif
24+
25+
using boost::math::fourier_transform_daubechies_scaling;
26+
using boost::math::tools::summation_condition_number;
27+
using boost::math::constants::two_pi;
28+
using boost::math::constants::one_div_root_two_pi;
29+
using boost::math::quadrature::trapezoidal;
30+
// 𝓕[φ](-ω) = 𝓕[φ](ω)*
31+
template<typename Real, int p>
32+
void test_evaluation_symmetry() {
33+
auto phi = fourier_transform_daubechies_scaling<Real, p>(0.0);
34+
CHECK_ULP_CLOSE(one_div_root_two_pi<Real>(), phi.real(), 3);
35+
CHECK_ULP_CLOSE(static_cast<Real>(0), phi.imag(), 3);
36+
37+
Real domega = Real(1)/128;
38+
for (Real omega = domega; omega < 10; omega += domega) {
39+
auto phi1 = fourier_transform_daubechies_scaling<Real, p>(-omega);
40+
auto phi2 = fourier_transform_daubechies_scaling<Real, p>(omega);
41+
CHECK_ULP_CLOSE(phi1.real(), phi2.real(), 3);
42+
CHECK_ULP_CLOSE(phi1.imag(), -phi2.imag(), 3);
43+
}
44+
45+
for (Real omega = 10; omega < std::numeric_limits<double>::max(); omega *= 10) {
46+
auto phi1 = fourier_transform_daubechies_scaling<Real, p>(-omega);
47+
auto phi2 = fourier_transform_daubechies_scaling<Real, p>(omega);
48+
CHECK_ULP_CLOSE(phi1.real(), phi2.real(), 3);
49+
CHECK_ULP_CLOSE(phi1.imag(), -phi2.imag(), 3);
50+
}
51+
}
52+
53+
template<int p>
54+
void test_quadrature() {
55+
auto phi = boost::math::daubechies_scaling<double, p>();
56+
auto [tmin, tmax] = phi.support();
57+
double domega = 1/128.0;
58+
for (double omega = domega; omega < 10; omega += domega) {
59+
// I suspect the quadrature is less accurate than special function evaluation, so this is just a sanity check:
60+
auto f = [&](double t) {
61+
return phi(t)*std::exp(std::complex<double>(0, -omega*t))*one_div_root_two_pi<double>();
62+
};
63+
auto expected = trapezoidal(f, tmin, tmax, 2*std::numeric_limits<double>::epsilon());
64+
auto computed = fourier_transform_daubechies_scaling<float, p>(static_cast<float>(omega));
65+
CHECK_MOLLIFIED_CLOSE(static_cast<float>(expected.real()), computed.real(), 1e-9);
66+
CHECK_MOLLIFIED_CLOSE(static_cast<float>(expected.imag()), computed.imag(), 1e-9);
67+
}
68+
}
69+
70+
// Tests Daubechies "Ten Lectures on Wavelets", equation 5.1.19:
71+
template<typename Real, int p>
72+
void test_ten_lectures_eq_5_1_19() {
73+
Real domega = Real(1)/Real(16);
74+
for (Real omega = 0; omega < 1; omega += domega) {
75+
Real term = std::norm(fourier_transform_daubechies_scaling<Real, p>(omega));
76+
auto sum = summation_condition_number<Real>(term);
77+
int64_t l = 1;
78+
while (l < 50 && term > 2*std::numeric_limits<Real>::epsilon()) {
79+
Real tpl = std::norm(fourier_transform_daubechies_scaling<Real, p>(omega + two_pi<Real>()*l));
80+
Real tml = std::norm(fourier_transform_daubechies_scaling<Real, p>(omega - two_pi<Real>()*l));
81+
82+
sum += tpl;
83+
sum += tml;
84+
Real term = tpl + tml;
85+
++l;
86+
}
87+
CHECK_ULP_CLOSE(1/two_pi<Real>(), sum.sum(), 13);
88+
}
89+
}
90+
91+
int main()
92+
{
93+
test_evaluation_symmetry<float, 2>();
94+
test_evaluation_symmetry<float, 6>();
95+
test_evaluation_symmetry<float, 8>();
96+
test_evaluation_symmetry<float, 16>();
97+
98+
test_quadrature<17>();
99+
test_quadrature<18>();
100+
101+
test_ten_lectures_eq_5_1_19<float, 2>();
102+
test_ten_lectures_eq_5_1_19<float, 3>();
103+
test_ten_lectures_eq_5_1_19<float, 4>();
104+
test_ten_lectures_eq_5_1_19<float, 5>();
105+
test_ten_lectures_eq_5_1_19<float, 6>();
106+
test_ten_lectures_eq_5_1_19<float, 7>();
107+
test_ten_lectures_eq_5_1_19<float, 8>();
108+
test_ten_lectures_eq_5_1_19<float, 9>();
109+
test_ten_lectures_eq_5_1_19<float, 10>();
110+
111+
return boost::math::test::report_errors();
112+
}

0 commit comments

Comments
 (0)