Skip to content

Commit

Permalink
Issue #15
Browse files Browse the repository at this point in the history
Create the fft_plot() function, its tests, and its documentation.
  • Loading branch information
akey7 committed Dec 28, 2022
1 parent 4bc33ec commit fc06126
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 1 deletion.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ Config/testthat/parallel: true
Imports:
dplyr,
ggplot2,
magrittr
magrittr,
tidyr
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ export("%>%")
export(compute_fft)
export(cos_sum)
export(exp_decay)
export(fft_plot)
export(length_norm)
export(waveform)
export(waveform_plot)
importFrom(dplyr,filter)
importFrom(dplyr,mutate)
importFrom(dplyr,transmute)
importFrom(ggplot2,aes)
importFrom(ggplot2,geom_line)
importFrom(ggplot2,ggplot)
importFrom(ggplot2,labs)
importFrom(magrittr,"%>%")
importFrom(stats,fft)
importFrom(tidyr,pivot_longer)
importFrom(utils,tail)
58 changes: 58 additions & 0 deletions R/fft_plot.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
utils::globalVariables(c(".idx", ".value", ".mod", ".re", ".im", "component", "value"))

#' fft_plot()
#'
#' @param incoming Required. A data.frame with the incoming waveform.
#' @param show Optional. Either "half" (which shows the modulus of the FFT up to half of its values to display frequencies up to the Nyquist limit) or "everything" (which shows modulus, real, and imaginary components of all values.)
#' @param ... Optional. If specified, passed to the ggplot2 labs() function.
#'
#' @return ggplot() object of the FFT
#' @export
#' @importFrom ggplot2 ggplot
#' @importFrom ggplot2 aes
#' @importFrom ggplot2 geom_line
#' @importFrom ggplot2 labs
#' @importFrom dplyr transmute
#' @importFrom dplyr filter
#' @importFrom tidyr pivot_longer
#'
#' @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() %>%
#' fft_plot(show = "everything")
#'
#' 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() %>%
#' fft_plot(show = "half")
fft_plot <- function(incoming, show = "everything", ...) {
stopifnot("incoming must be a data.frame with an FFT." = is.data.frame(incoming))
stopifnot("show argument must be \"everything\" or \"half\"." = show %in% c("half", "everything"))

plot_data <- incoming %>%
dplyr::transmute(
.idx,
.re = Re(.value),
.im = Im(.value),
.mod = Mod(.value)
) %>%
tidyr::pivot_longer(-.idx, names_to = "component", values_to = "value")

if (show == "everything") {
result <- ggplot2::ggplot(plot_data, aes(x = .idx, y = value)) +
ggplot2::geom_point() +
ggplot2::facet_wrap(~ component, nrow = 3)
}
else {
result <- plot_data %>%
dplyr::filter(component == ".mod", .idx <= max(incoming$.idx) / 2) %>%
ggplot2::ggplot(aes(x = .idx, y = value)) +
ggplot2::geom_point()
}

result +
labs(...)
}
34 changes: 34 additions & 0 deletions man/fft_plot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions tests/testthat/test-fft_plot.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
test_that("fft_plot() expects incoming to be a data.frame", {
expect_error(fft_plot(show = "half"))
})

test_that("fft_plot() expects show to be \"half\" or \"everything\".", {
expect_error(fft_plot(data.frame(), show = "nothing"))
})

0 comments on commit fc06126

Please sign in to comment.