https://github.com/lschneiderbauer/fcwtr
An R package wrapping fCWT, a library implementing a continuous wavelet transform, utilizing the power of fftw.
https://github.com/lschneiderbauer/fcwtr
cwt fft fftw r r-package signal-processing time-series wavelet-transform wrapper
Last synced: 5 months ago
JSON representation
An R package wrapping fCWT, a library implementing a continuous wavelet transform, utilizing the power of fftw.
- Host: GitHub
- URL: https://github.com/lschneiderbauer/fcwtr
- Owner: lschneiderbauer
- License: gpl-3.0
- Created: 2022-11-19T22:45:23.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2025-01-17T23:16:59.000Z (about 1 year ago)
- Last Synced: 2025-01-17T23:17:53.725Z (about 1 year ago)
- Topics: cwt, fft, fftw, r, r-package, signal-processing, time-series, wavelet-transform, wrapper
- Language: R
- Homepage: https://lschneiderbauer.github.io/fCWTr/
- Size: 11.6 MB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.Rmd
- Changelog: NEWS.md
- License: LICENSE.md
Awesome Lists containing this project
README
---
output: github_document
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%",
dpi = 300,
fig.width = 8
)
```
[](https://github.com/lschneiderbauer/fCWTr/actions/workflows/R-CMD-check.yaml) [](https://lifecycle.r-lib.org/articles/stages.html#experimental) [](https://CRAN.R-project.org/package=fCWTr) [](https://app.codecov.io/gh/lschneiderbauer/fcwtr?branch=master)
The R package fCWTr wraps the [fCWT library](https://github.com/fastlib/fCWT), a library implementing a [continuous wavelet transform](https://en.wikipedia.org/wiki/Continuous_wavelet_transform) with a Morlet wavelet, utilizing the power of [fftw](https://www.fftw.org/), 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).
## System dependencies
- R \>= 4.1
- [fftw](https://www.fftw.org/) library with [single-precision support](https://www.fftw.org/faq/section2.html#singleprec) enabled (used by [fCWT](https://github.com/fastlib/fCWT))
- Optional: a CPU/compiler supporting the [AVX](https://en.wikipedia.org/wiki/Advanced_Vector_Extensions) instruction set
- Optional: OpenMP (and fftw built with [OpenMP support](https://www.fftw.org/doc/Usage-of-Multi_002dthreaded-FFTW.html))
- 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:
``` bash
CPPFLAGS = -mavx
CXXFLAGS = -mavx
```
## Installation
You can install the latest CRAN release of fCWTr with:
``` r
install.packages("fCWTr")
```
Alternatively, you can install the development version of fCWTr like so (requiring installed [devtools](https://devtools.r-lib.org/) package):
``` r
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 compiled without single precision support. Please consult [fftw.org](https://www.fftw.org) for help.
## Example
This is a basic example where the continuous wavelet transform of a sample signal is calculated. The result is inspected and plotted.
```{r example}
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.1, "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)
# Some meta data is recorded too
output
```
The result can also be converted into a data frame:
```{r example_df}
as.data.frame(output) |>
head(10)
```
We can also directly plot the resulting scalogram:
```{r example_plot}
plot(output, time_unit = "ms")
```
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:
```{r example_long_df, results='hide'}
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)
```