data(presidents)
## Example 1: Create a data stream with three copies of president approval ratings.
## We will use several convolutions.
stream <- data.frame(
approval_orig = presidents,
approval_MA = presidents,
approval_diff1 = presidents,
.time = time(presidents)) %>%
DSD_Memory()
plot(stream, dim = 1, n = 120, method = "ts")
## apply a moving average filter to dimension 1 (using the column name) and diff to dimension 3
filteredStream <- stream %>%
DSF_Convolve(kernel = filter_MA(5), dim = "approval_orig", na.rm = TRUE) %>%
DSF_Convolve(kernel = filter_diff(1), dim = 3)
filteredStream
## resetting the filtered stream also resets the original stream
reset_stream(filteredStream)
ps <- get_points(filteredStream, n = 120)
head(ps)
year <- ps[[".time"]]
approval <- remove_info(ps)
matplot(year, approval, type = "l", ylim = c(-20, 100))
legend("topright", colnames(approval), col = 1:3, lty = 1:3, bty = "n")
## Example 2: Create a stream with a constant sine wave and apply
## a moving average, an RMS envelope and a differences
stream <- DSD_Memory(data.frame(y = sin(seq(0, 2 * pi - (2 * pi / 100) ,
length.out = 100))), loop = TRUE)
plot(stream, n = 200, method = "ts")
filteredStream <- stream %>%
DSF_Convolve(kernel = filter_MA(100), dim = 1,
replace = FALSE, name = "MA") %>%
DSF_Convolve(kernel = filter_MA(100), pre = pow2, post = sqrt, dim = 1,
replace = FALSE, name = "RMS") %>%
DSF_Convolve(kernel = filter_diff(1), dim = 1,
replace = FALSE, name = "diff1")
filteredStream
ps <- get_points(filteredStream, n = 500)
head(ps)
matplot(ps, type = "l")
legend("topright", colnames(ps), col = 1:4, lty = 1:4)
## Note that MA and RMS use a window of length 200 and are missing at the
## beginning of the stream the window is full.
## Filters: look at different filters
filter_MA(5)
filter_diff(1)
plot(filter_Hamming(20), type = "h")
plot(filter_Sinc(10, 100, width = 20), type = "h")
Run the code above in your browser using DataLab