-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create the fft_plot() function, its tests, and its documentation.
- Loading branch information
Showing
5 changed files
with
105 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,4 +17,5 @@ Config/testthat/parallel: true | |
Imports: | ||
dplyr, | ||
ggplot2, | ||
magrittr | ||
magrittr, | ||
tidyr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(...) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
}) |