Fast Continuous Wavelet Transform

Enables the usage of the fast continuous wavelet transform, originally implemented in the 'C++' library 'fCWT' by Lukas Arts. See Arts, P.A. and Van den Broek, E.L. (2022) for details.


fCWTr fCWTr website

R-CMD-check Lifecycle:experimental CRANstatus Codecov testcoverage

The R package fCWTr wraps the fCWT library, a library implementing a continuous wavelet transform with a Morlet wavelet, utilizing the power of fftw, a fast fourier transform implementation. It provides an R-like functional interface and implements common S3 methods for convenience.

See the original paper by Arts, L.P.A., van den Broek, E.L. The fast continuous wavelet transformation (fCWT) for real-time, high-quality, noise-resistant time–frequency analysis. Nat Comput Sci 2, 47–58 (2022). https://doi.org/10.1038/s43588-021-00183-z

System dependencies

  • R >= 4.1
  • fftw library with single-precision support enabled (used by fCWT)
  • Optional: a CPU/compiler supporting the AVX instruction set
  • Optional: OpenMP (and fftw built with OpenMP support)
    • On Windows, OpenMP support is disabled since rtools’ fftw is compiled without OpenMP support.
    • On Linux and MacOS the build scripts should automatically detect whether OpenMP support is available.

By default, most compiler setups do not make use of AVX to increase portability of the binary. If you are an R user that has a CPU supporting AVX and want to make use of it, you might need to manually enable compiler flags to let R know about it, and install the package from source (so that it gets compiled on your machine). One way to enable the flags is to create a file ~/.R/Makevars with the following content:

CPPFLAGS = -mavx
CXXFLAGS = -mavx

Installation

You can install the latest CRAN release of fCWTr with:

install.packages("fCWTr")

Alternatively, you can install the development version of fCWTr like so (requiring installed devtools package):

devtools::install_github("lschneiderbauer/fCWTr")

Note that the installation process might fail if the package needs to be compiled from source and system requirements are not satisfied. The error message should give you hints, however, on what’s missing on your system.

  • Common confusion: fftw is installed, but without single precision support (libfftw3f). Some distributions ship it as a separate package:
    • Debian/Ubuntu: libfftw3-dev
    • Fedora/RHEL: fftw-devel and fftw-libs-single
    • macOS: brew install fftw (or the CRAN macOS recipe fftw-s)
    • If you build fftw yourself, configure it with --enable-float. See fftw.org.

Example

This is a basic example where the continuous wavelet transform of a sample signal is calculated. The result is inspected and plotted.

library(fCWTr)

# A signal encoded in a numeric vector.
# In this example we use some superimposed sin signals.
signal <- ts_sin_superpos

output <-
  fcwt(
    signal,
    x_sample_freq = u(44, "kHz"),
    sigma = 5,
    y_sample_freq = u(1, "kHz"),
    freq_begin = u(16, "Hz"),
    freq_end = u(2100, "Hz"),
    n_freqs = 200,
    freq_scale = "linear"
  )

# The result is a numeric matrix with time and frequency dimension
dim(output)
#> [1] 137 200

# Some meta data is recorded too
output
#> _Scalogram_
#> * (Time/Frequency) dimension:  ( 137 , 200 )
#> * Sampling rate: 1 [kHz]
#> * Frequency scale: 16 [Hz] - 2100 [Hz], linear
#> * Time offset: 0 [s] 
#> * Sigma: 5
#>   o Time resolution at  16 [Hz] :  1.25 [s] 
#>   o Time resolution at  2100 [Hz] :  9.52381 [ms] 
#>   o Relative frequency resolution:  0.127324 
#> * Time/frequency matrix summary
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
#>  0.0000  0.0001  0.0011  0.1267  0.0339  2.8194    6671

The result can also be converted into a data frame:

as.data.frame(output) |>
  head(10)
#>    time_index   time      freq        value
#> 1           0 0 [ms] 2100 [Hz]           NA
#> 2           1 1 [ms] 2100 [Hz]           NA
#> 3           2 2 [ms] 2100 [Hz]           NA
#> 4           3 3 [ms] 2100 [Hz]           NA
#> 5           4 4 [ms] 2100 [Hz] 3.326482e-03
#> 6           5 5 [ms] 2100 [Hz] 1.388348e-03
#> 7           6 6 [ms] 2100 [Hz] 4.916695e-04
#> 8           7 7 [ms] 2100 [Hz] 1.552785e-04
#> 9           8 8 [ms] 2100 [Hz] 5.435228e-05
#> 10          9 9 [ms] 2100 [Hz] 3.173394e-05

We can also directly plot the resulting scalogram:

plot(output)

For long sequences, the required memory can exceed your available local memory. In this case it can be useful to reduce the time resolution of the result and process the data in batches. This can be done with fcwt_batch(). In case the batch size is not explicitly provided, some heuristics are used to determine a batch size automatically:

batch_result <-
  fcwt_batch(
    rep(ts_sin_sin, 5),
    x_sample_freq = u(44.1, "kHz"),
    y_sample_freq = u(100, "Hz"),
    freq_begin = u(100, "Hz"),
    freq_end = u(12, "kHz"),
    n_freqs = 200,
    freq_scale = "linear",
    sigma = 4
  )

plot(batch_result)

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("fCWTr")

0.4.1 by Lukas Schneiderbauer, 16 hours ago


https://lschneiderbauer.github.io/fCWTr/, https://github.com/lschneiderbauer/fCWTr


Report a bug at https://github.com/lschneiderbauer/fCWTr/issues


Browse source code at https://github.com/cran/fCWTr


Authors: Lukas Schneiderbauer [aut, cre, cph] (ORCID: , Lukas P. A. Arts [cph] (Author of the majority of the C++ code , indicated in the respective header files.) , Egon L. van den Broek [cph] (Author of the majority of the C++ code , indicated in the respective header files.)


Documentation:   PDF Manual  


GPL (>= 3) license


Imports units

Suggests ggplot2, viridis, rlang, tibble, testthat, knitr, rmarkdown

Linking to cpp11

System requirements: C++17, fftw3 library with single-precision support, i.e. libfftw3f (deb: libfftw3-dev, rpm: fftw-devel and fftw-libs-single, brew: fftw, macOS recipe: fftw-s), pkg-config (optional), OpenMP-enabled single-precision fftw3, i.e. libfftw3f_omp (optional), OpenMP (optional)


See at CRAN