diff --git a/NAMESPACE b/NAMESPACE index 94d013d..ea35b4e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,6 +1,7 @@ # Generated by roxygen2: do not edit by hand export("%>%") +export(compute_fft) export(cos_sum) export(exp_decay) export(length_norm) @@ -12,4 +13,5 @@ importFrom(ggplot2,geom_line) importFrom(ggplot2,ggplot) importFrom(ggplot2,labs) importFrom(magrittr,"%>%") +importFrom(stats,fft) importFrom(utils,tail) diff --git a/R/compute_fft.R b/R/compute_fft.R new file mode 100644 index 0000000..e642512 --- /dev/null +++ b/R/compute_fft.R @@ -0,0 +1,25 @@ +#' compute_fft() +#' +#' Computes the FFT of the given waveform. +#' +#' @param incoming data.frame containing the incoming waveform for the FFT. +#' +#' @return A data.frame with the following columns: .values with the values from the FFT, and .idx with the indexes for these values (this makes it easier to plot with ggplot later). +#' @export +#' @importFrom stats fft +#' +#' @examples +#' waveform(duration_s = 1.0, sr = 10) %>% +#' cos_sum(freqs = c(2.0, 3.0), amplitudes = c(0.5, 1.0)) %>% +#' length_norm() %>% +#' compute_fft() +compute_fft <- function(incoming) { + stopifnot("incoming must be a data.frame with a waveform." = is.data.frame(incoming)) + + .value <- stats::fft(incoming$.value) + + data.frame( + .idx = seq_along(.value), + .value = .value + ) +} diff --git a/man/compute_fft.Rd b/man/compute_fft.Rd new file mode 100644 index 0000000..a70fe7b --- /dev/null +++ b/man/compute_fft.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/compute_fft.R +\name{compute_fft} +\alias{compute_fft} +\title{compute_fft()} +\usage{ +compute_fft(incoming) +} +\arguments{ +\item{incoming}{data.frame containing the incoming waveform for the FFT.} +} +\value{ +A data.frame with the following columns: .values with the values from the FFT, and .idx with the indexes for these values (this makes it easier to plot with ggplot later). +} +\description{ +Computes the FFT of the given waveform. +} +\examples{ +waveform(duration_s = 1.0, sr = 10) \%>\% + cos_sum(freqs = c(2.0, 3.0), amplitudes = c(0.5, 1.0)) \%>\% + length_norm() \%>\% + compute_fft() +} diff --git a/tests/testthat/test-compute_fft.R b/tests/testthat/test-compute_fft.R new file mode 100644 index 0000000..48bd868 --- /dev/null +++ b/tests/testthat/test-compute_fft.R @@ -0,0 +1,21 @@ +test_that("compute_fft() expects a data.frame", { + expect_error(compute_fft(1.0)) +}) + +test_that("compute_fft() computes an FFT of the sum of two cosines", { + wv <- waveform(duration_s = 1.0, sr = 10) %>% + cos_sum(freqs = c(2.0, 3.0), amplitudes = c(0.5, 1.0)) %>% + length_norm() + + actual <- compute_fft(wv)$.value + + expected <- c( + -4.857226e-17+0.000000e+00i, 0.000000e+00-8.326673e-17i, + 7.725425e-02+2.377641e-01i, -1.545085e-01+4.755283e-01i, + -5.551115e-17+5.551115e-17i, -8.326673e-17+2.775558e-17i, + -5.551115e-17-5.551115e-17i, -1.545085e-01-4.755283e-01i, + 7.725425e-02-2.377641e-01i, 0.000000e+00+5.551115e-17i + ) + + expect_equal(expected, actual, tolerance = 1e-3) +})