| Title: | Fast Continuous Wavelet Transform |
|---|---|
| Description: | 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) <doi:10.1038/s43588-021-00183-z> for details. The package includes simple helpers such as a plotting function. |
| Authors: | Lukas Schneiderbauer [aut, cre, cph]
|
| Maintainer: | Lukas Schneiderbauer <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 0.2.1 |
| Built: | 2026-11-13 11:39:11 UTC |
| Source: | https://github.com/cran/fCWTr |
Internally, the scalogram resulting from fcwt() is represented by
a numeric matrix. This method coerces this matrix into a reasonable
data frame. Note that this conversion has a significant run time cost.
## S3 method for class 'fcwtr_scalogram' as.data.frame(x, ...)## S3 method for class 'fcwtr_scalogram' as.data.frame(x, ...)
x |
An object resulting from |
... |
additional arguments to be passed to or from methods. |
A data.frame() object representing the scalogram data with four columns:
An integer index uniquely identifying time slices.
The time difference to the first time slice in physical units.
The time unit is the inverse of the frequency unit chosen by the user
for the sample_freq argument of fcwt().
The frequency in the same units as the sample_freq argument
of fcwt().
The fCWT result for the particular time-frequency combination.
fcwt( sin((1:5000) * 2 * pi * 440 / 44100), sample_freq = 44100, n_freqs = 10 ) |> as.data.frame() |> head()fcwt( sin((1:5000) * 2 * pi * 440 / 44100), sample_freq = 44100, n_freqs = 10 ) |> as.data.frame() |> head()
The core function of this package making use of the fCWT library. It processes an input signal in form of a real valued numeric vector interpreted as an evenly spaced time series and returns the absolute values of a spectogram, i.e. a graph with a time and a frequency dimension.
fcwt( signal, sample_freq, n_freqs, freq_begin = 2 * sample_freq/length(signal), freq_end = sample_freq/2, sigma = 1, remove_coi = TRUE, n_threads = 2L )fcwt( signal, sample_freq, n_freqs, freq_begin = 2 * sample_freq/length(signal), freq_end = sample_freq/2, sigma = 1, remove_coi = TRUE, n_threads = 2L )
signal |
Real-valued time series. The time steps are assumed to be evenly spaced. |
sample_freq |
Sampling rate of input time series. This number primarily establishes a connection to physical units which is used in other frequency definitions as well as the units of the output data. |
n_freqs |
Number of frequency bins generated by the CWT. The frequencies are linearly distributed. Computation time increases when raising the number of frequency bins. |
freq_begin, freq_end
|
Optionally specifies the frequency range |
sigma |
Sets a dimensionless parameter modifying the wavelet spread which in the
time-domain is roughly given by |
remove_coi |
Boundary effects can result in nonphysical artifacts. If |
n_threads |
Number of threads used by the computation, if supported by your platform. Defaults to 2 threads (to accomodate CRAN requirements). |
The wavelet used in this calculation is the so called Morlet wavelet,
a sinusoidal wave modulated by a Gaussian whose spread is controlled by
the argument sigma.
See the original paper 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). doi:10.1038/s43588-021-00183-z
The spectogram, a numeric real-valued matrix with dimensions
dim = c(length(signal), n_freqs).
This matrix is wrapped into a S3-class fcwtr_scalogram so that plotting and
coercion functions can be used conveniently.
ts_sin_440 <- sin((1:5000) * 2 * pi * 440 / 44100) res <- fcwt( ts_sin_440, sample_freq = 44100, freq_begin = 50, freq_end = 1000, n_freqs = 10, sigma = 5 )ts_sin_440 <- sin((1:5000) * 2 * pi * 440 / 44100) res <- fcwt( ts_sin_440, sample_freq = 44100, freq_begin = 50, freq_end = 1000, n_freqs = 10, sigma = 5 )
Performs a fast continuous wavelet transform on long sequences by sequentially processing junks of the input signal and keeping only low-resolution output data to preserve memory. This is only useful for very long signals whose output does not fit into memory as a whole. It should not be used on short signals since boundary artefacts are discarded (and those potentially dominate for short sequences).
fcwt_batch( signal, sample_freq, n_freqs, time_resolution, freq_begin = 2 * sample_freq/length(signal), freq_end = sample_freq/2, sigma = 1, max_batch_size = ceiling(4 * 10^9/(n_freqs * 4)), n_threads = 2L, progress_bar = FALSE )fcwt_batch( signal, sample_freq, n_freqs, time_resolution, freq_begin = 2 * sample_freq/length(signal), freq_end = sample_freq/2, sigma = 1, max_batch_size = ceiling(4 * 10^9/(n_freqs * 4)), n_threads = 2L, progress_bar = FALSE )
signal |
Real-valued time series. The time steps are assumed to be evenly spaced. |
sample_freq |
Sampling rate of input time series. This number primarily establishes a connection to physical units which is used in other frequency definitions as well as the units of the output data. |
n_freqs |
Number of frequency bins generated by the CWT. The frequencies are linearly distributed. Computation time increases when raising the number of frequency bins. |
time_resolution |
The time resolution in inverse units of |
freq_begin, freq_end
|
Optionally specifies the frequency range |
sigma |
Sets a dimensionless parameter modifying the wavelet spread which in the
time-domain is roughly given by |
max_batch_size |
The maximal batch size that is used for splitting up the input sequence. This limits the maximal memory that is used. Defaults to roughly 4GB. The actual batch size is optimized for use with FFTW. |
n_threads |
Number of threads used by the computation, if supported by your platform. Defaults to 2 threads (to accomodate CRAN requirements). |
progress_bar |
Monitoring progress can sometimes be useful when performing time consuming
operations. Setting |
In case of input sequences that exceed the a certain size, the output
sequence will not fit into the local memory and the fcwt cannot be
performed in one run.
For instance, in case of processing a song of 10 minutes length (assuming
a sampling rate of 44100 Hz), the size of the output vector is
10 * 60 seconds * 44100 Hz * nfreqs * 4 bytes,
which for e.g. nfreqs = 200, equals ~ 21 GB, hence
nowadays already at the limit of the hardware of a modern personal computer.
In cases where the required output time-resolution is smaller than the time
resolution of the input signal, one can perform the fcwt() and reduce the
output size by averaging.
(The input signal time resolution can in general not be reduced since
high-frequency information would get lost.)
This function splits up the input sequence into batches, processes each batch separately, reduces the time resolution, and adds the outputs together.
Attention: Boundary artefacts are removed, so some high frequency information
at the beginning and the end of the sequence is lost. (The amount depends on
the minimal frequency captured min_freq.)
The spectogram, a numeric real-valued matrix with dimensions roughly
dim ~ c(length(signal) * time_resolution * sample_freq, n_freqs).
The exact length of the output depends on boundary effect details.
This matrix is wrapped into a S3-class fcwtr_scalogram so that plotting and
coercion functions can be used conveniently.
res <- fcwt_batch( ts_sin_sin, sample_freq = 44100, freq_begin = 100, freq_end = 11000, n_freqs = 10, sigma = 10, max_batch_size = 50000, time_resolution = 0.001 )res <- fcwt_batch( ts_sin_sin, sample_freq = 44100, freq_begin = 100, freq_end = 11000, n_freqs = 10, sigma = 10, max_batch_size = 50000, time_resolution = 0.001 )
Plots the scalogram resulting from fcwt().
Requires ggplot2.
## S3 method for class 'fcwtr_scalogram' plot(x, n = 1000, ...)## S3 method for class 'fcwtr_scalogram' plot(x, n = 1000, ...)
x |
An object resulting from |
n |
The plotting function reduces the time resolution by averaging
to generate a reasonable graphics format. |
... |
other arguments passed to specific methods |
No return value, called for side effects.
ts_sin_440 <- sin((1:4410) * 2 * pi * 440 / 44100) res <- fcwt( ts_sin_440, sample_freq = 44100, freq_begin = 50, freq_end = 1000, n_freqs = 10, sigma = 5 ) plot(res)ts_sin_440 <- sin((1:4410) * 2 * pi * 440 / 44100) res <- fcwt( ts_sin_440, sample_freq = 44100, freq_begin = 50, freq_end = 1000, n_freqs = 10, sigma = 5 ) plot(res)
Assuming a sample rate of 44100 Hz, the sine wave's frequency is 440 Hz.
ts_sin_440ts_sin_440
A numeric vector containing a time series signal.
The frequency itself is changing in a sinusoidal fashion.
ts_sin_sints_sin_sin
A numeric vector containing a time series signal.
Assuming a sample rate of 44100 Hz, the superimposed signals' frequencies are 440 Hz, 880 Hz, 100 Hz, 500 Hz, 1200 Hz and 50 Hz.
ts_sin_superposts_sin_superpos
A numeric vector containing a time series signal.