diff --git a/NAMESPACE b/NAMESPACE index f12ac4e..e73297a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -5,6 +5,7 @@ S3method(as.data.frame,fcwtr_scalogram) S3method(as.matrix,fcwtr_scalogram) S3method(plot,fcwtr_scalogram) S3method(print,fcwtr_scalogram) +S3method(rbind,fcwtr_scalogram) export(autoplot.fcwtr_scalogram) export(fcwt) export(fcwt_batch) diff --git a/NEWS.md b/NEWS.md index d2979b0..fafde14 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,6 @@ # fcwtr 0.2.9999 -- Add S3 methods: `print()`, `[]`, `as.matrix()`. +- Add S3 methods: `print()`, `[]`, `as.matrix()`, `rbind()`. - Include improved physical unit treatment with the 'units' package. Frequency and time parameters can now be "units" objects, created with `u()`. Allow user to adjust plot scales to use arbitrary time/freq units. diff --git a/R/fcwt_batch.R b/R/fcwt_batch.R index f242ddb..8b88d4e 100644 --- a/R/fcwt_batch.R +++ b/R/fcwt_batch.R @@ -163,7 +163,7 @@ fcwt_batch <- function(signal, sc_agg(w) } ) |> - do.call(tbind, args = _) + do.call(rbind, args = _) } batches <- function(batch_size, signal_size, dt) { diff --git a/R/fcwtr_scalogram.R b/R/fcwtr_scalogram.R index 7c606a9..6455b50 100644 --- a/R/fcwtr_scalogram.R +++ b/R/fcwtr_scalogram.R @@ -180,30 +180,50 @@ sc_agg <- function(x, wnd) { ) } -tbind <- function(..., deparse.level = 1) { +#' Combine scalograms in "time" direction +#' +#' Given two or more scalograms with identical sampling frequencies, frequency +#' scales and sigma, it can be useful to combine several into a single object +#' creating a longer time series. +#' The function errs if these conditions are not satisfied. +#' +#' The scalograms are stitched together in chronological fashion (i.e. the first +#' argument will the initial piece, etc.). +#' Time offset information is kept from the first piece. +#' +#' @param ... +#' One or more "fcwtr_scalogram" objects, generated by `fcwt()`. +#' +#' @return Returns a new time-wise combined "fcwtr_scalogram" object. +#' +#' @export +rbind.fcwtr_scalogram <- function(...) { args <- list(...) stopifnot(length(args) >= 1) lapply(args, \(arg) stopifnot(inherits(arg, "fcwtr_scalogram"))) + arg_attr_ident <- function(attr) { + vals <- + sapply(args, \(arg) attr(arg, attr)) + + all.equal(max(vals), min(vals)) + } + # check if attributes are identical, otherwise combination # does not make sense - if (length(unique(lapply(args, \(arg) round(attr(arg, "sample_freq"))))) > 1) { - stop("Sampling frequencies need to be identical.") - } - if (length(unique(lapply(args, \(arg) attr(arg, "freq_begin")))) > 1) { - stop("Frequency ranges need to be identical.") - } - if (length(unique(lapply(args, \(arg) attr(arg, "freq_end")))) > 1) { - stop("Frequency ranges need to be identical.") - } - if (length(unique(lapply(args, \(arg) attr(arg, "freq_scale")))) > 1) { - stop("Frequency scales need to be identical.") - } - if (length(unique(lapply(args, \(arg) attr(arg, "sigma")))) > 1) { - stop("Sigma parameter needs to be identical.") - } + stopifnot( + "Sampling frequencies need to be identical." = arg_attr_ident("sample_freq"), + "Frequency ranges need to be identical." = arg_attr_ident("freq_begin"), + "Frequency ranges need to be identical." = arg_attr_ident("freq_end"), + "Frequency scales need to be identical." = arg_attr_ident("freq_scale"), + "Sigma parameter needs to be identical." = arg_attr_ident("sigma") + ) - x_new <- do.call(rbind, c(lapply(args, unclass), list(deparse.level = deparse.level))) + x_new <- + do.call( + rbind, + c(lapply(args, unclass)) + ) new_fcwtr_scalogram( x_new, diff --git a/_pkgdown.yml b/_pkgdown.yml index f6bd88b..95a2c5f 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -16,6 +16,7 @@ reference: - as.data.frame.fcwtr_scalogram - as.matrix.fcwtr_scalogram - "`[.fcwtr_scalogram`" + - rbind.fcwtr_scalogram - print.fcwtr_scalogram - plot.fcwtr_scalogram - autoplot.fcwtr_scalogram diff --git a/man/rbind.fcwtr_scalogram.Rd b/man/rbind.fcwtr_scalogram.Rd new file mode 100644 index 0000000..8465634 --- /dev/null +++ b/man/rbind.fcwtr_scalogram.Rd @@ -0,0 +1,25 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/fcwtr_scalogram.R +\name{rbind.fcwtr_scalogram} +\alias{rbind.fcwtr_scalogram} +\title{Combine scalograms in "time" direction} +\usage{ +\method{rbind}{fcwtr_scalogram}(...) +} +\arguments{ +\item{...}{One or more "fcwtr_scalogram" objects, generated by \code{fcwt()}.} +} +\value{ +Returns a new time-wise combined "fcwtr_scalogram" object. +} +\description{ +Given two or more scalograms with identical sampling frequencies, frequency +scales and sigma, it can be useful to combine several into a single object +creating a longer time series. +The function errs if these conditions are not satisfied. +} +\details{ +The scalograms are stitched together in chronological fashion (i.e. the first +argument will the initial piece, etc.). +Time offset information is kept from the first piece. +} diff --git a/tests/testthat/test-fcwtr_scalogram.R b/tests/testthat/test-fcwtr_scalogram.R index 0c8a050..8c7b08e 100644 --- a/tests/testthat/test-fcwtr_scalogram.R +++ b/tests/testthat/test-fcwtr_scalogram.R @@ -1,4 +1,4 @@ -test_that("tbind() for fcwtr_scalogram", { +test_that("rbind() for fcwtr_scalogram", { first <- fcwt( ts_sin_440[1:1000], @@ -40,11 +40,11 @@ test_that("tbind() for fcwtr_scalogram", { "fcwtr_scalogram" ) expect_s3_class( - tbind(first, second), + rbind(first, second), "fcwtr_scalogram" ) expect_error( - tbind(first, third) + rbind(first, third) ) })