Skip to content

Commit

Permalink
Merge pull request #7 from akey7/issue_002_cos_sum
Browse files Browse the repository at this point in the history
Issues 002 and 003
  • Loading branch information
akey7 authored Dec 27, 2022
2 parents 528de6d + 104681b commit db08f3a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
13 changes: 8 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
Package: fftpipe
Type: Package
Title: What the Package Does (Title Case)
Title: A Pipeable Interface to Base R's fft() Function
Version: 0.1.0
Author: Who wrote it
Maintainer: The package maintainer <[email protected]>
Description: More about what it does (maybe more than one line)
Use four spaces when indenting paragraphs within the Description.
Author: Alicia M. F. Key
Maintainer: Alicia M. F. Key <[email protected]>
Description: fftpipe wraps dataframes, piping, and the tidyverse around base R's fft() function.
fftpipe's style and interface are inspired by tidyverse and tidymodels. This package contains functions for generating, transforming, and plotting waveforms with operations around the fft() function.
License: What license is it under?
Encoding: UTF-8
LazyData: true
Suggests:
testthat (>= 3.0.0)
Config/testthat/edition: 3
RoxygenNote: 7.2.3
Config/testthat/parallel: true
Imports:
dplyr
26 changes: 26 additions & 0 deletions R/cos_sum.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
cos_sum <- function(incoming, freqs, amplitudes = NULL, phases = NULL) {
stopifnot("incoming waveform must a data.frame." = is.data.frame(incoming))
stopifnot("freqs must be a vector of doubles or a single double." = is.numeric(freqs))

if (is.null(amplitudes)) {
amplitudes <- rep(1.0, length(freqs))
}

if (is.null(phases)) {
phases <- rep(0.0, length(freqs))
}

sr <- length(incoming$.sec) / tail(incoming$.sec, n = 1)

waveform_matrix <- matrix(nrow = length(freqs) + 1, ncol = nrow(incoming))
waveform_matrix[1, ] <- incoming$.value

for (i in seq_along(freqs)) {
freq <- freqs[i]
amplitude <- amplitudes[i]
phase <- phases[i]
waveform_matrix[i + 1,] <- amplitude * cos(2 * pi * freq * incoming$.sample / sr)
}

dplyr::mutate(incoming, .value = colSums(waveform_matrix))
}
11 changes: 11 additions & 0 deletions tests/testthat/test-cos_sum.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
test_that("cos_sum() stops when invoked bad arguments.", {
expect_error(cos_sum(5, 5))
})

test_that("cos_sum() creates the sum of a single cosine.", {
result <- fftpipe::waveform(duration_s = 1.0, sr = 10) %>%
cos_sum(1)

.value <- c(0.809017, 0.309017, -0.309017, -0.809017, -1.000000, -0.809017, -0.309017, 0.309017, 0.809017, 1.000000)
expect_identical(round(.value, 5), round(result$.value, 5))
})

0 comments on commit db08f3a

Please sign in to comment.