Skip to content

Commit

Permalink
Numerical evaluation of Fourier transform of Daubechies scaling funct…
Browse files Browse the repository at this point in the history
…ions. [ci skip]
  • Loading branch information
NAThompson committed Jan 18, 2023
1 parent e2c989c commit 5c65458
Show file tree
Hide file tree
Showing 4 changed files with 240 additions and 0 deletions.
17 changes: 17 additions & 0 deletions doc/sf/daubechies.qbk
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,23 @@ The 2 vanishing moment scaling function.
[$../graphs/daubechies_8_scaling.svg]
The 8 vanishing moment scaling function.

Boost.Math also provides numerical evaluation of the Fourier transform of these functions.
This is useful in sparse recovery problems where the measurements are taken in the Fourier basis.
The usage is exhibited below:

#include <boost/math/special_functions/fourier_transform_daubechies_scaling.hpp>
using boost::math::fourier_transform_daubechies_scaling;
// Evaluate the Fourier transform of the 4-vanishing moment Daubechies scaling function at ω=1.8:
std::complex<float> hat_phi = fourier_transform_daubechies_scaling<float, 4>(1.8f);

The Fourier transform convention is unitary with the sign of i being given in Daubechies Ten Lectures.
In particular, this means that `fourier_transform_daubechies_scaling<float, p>(0.0)` returns 1/sqrt(2π).

The implementation computes an infinite product of trigonometric polynomials as can be found from recursive application of the identity 𝓕[φ](ω) = m(ω/2)𝓕[φ](ω/2).
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.



[heading References]

* Daubechies, Ingrid. ['Ten Lectures on Wavelets.] Vol. 61. Siam, 1992.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* 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 <array>
#include <limits>
#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 std::exp;
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 = 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>>(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
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();
112 changes: 112 additions & 0 deletions test/fourier_transform_daubechies_scaling_test.cpp
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();
}

0 comments on commit 5c65458

Please sign in to comment.