if (require("ggplot2")) {
### Generating an artificial audio for the example
## For this example we'll generate a sweep in a noisy soundscape
library(tuneR)
library(ggplot2)
# Define parameters for the artificial audio
samprate <- 12050
dur <- 60
n <- samprate * dur
# White noise
set.seed(413)
noise <- rnorm(n)
# Linear fade-out envelope
fade <- seq(1, 0, length.out = n)
# Apply fade
signal <- noise * fade
# Create Wave object
wave <- Wave(
left = signal,
samp.rate = samprate,
bit = 16
)
# Running singleSat() on the artificial audio
time <- 10
sat <- activity(wave, timeBin = time)
# Now we can plot the results
satDim <- dim(sat)
numericTime <- seq(0, dur, by = time)
labels <- paste0(numericTime[-length(numericTime)], "-", numericTime[-1], "s")
satDF <- data.frame(BIN = rep(paste0("BIN", seq(satDim[2])), each = satDim[1]),
WIN = rep(seq(satDim[1]), satDim[2]),
ACT = factor(c(sat), levels = c(0,1)))
ggplot(satDF, aes(x = BIN, y = WIN, fill = ACT)) +
geom_tile() +
theme_bw() +
scale_fill_manual(values = c("white", "black")) +
scale_y_continuous(expand = c(0,0)) +
scale_x_discrete(expand = c(0,0), labels = labels) +
labs(x = "Time Bin", y = "Spectral Window") +
guides(fill = guide_legend(title = "Activity"))
}
Run the code above in your browser using DataLab