NA 锯齿波信号
NA Sawthooth signal
如何在 R 中表示(绘图和数字)锯齿信号来自:
y <- c(NA,NA,NA,NA,1,NA,NA,NA,1,NA,NA,NA,NA,NA,1,NA,NA,NA,NA,1,NA)
其中1代表在y
中锯齿波达到峰值的时间点(显然是1)。请注意,峰之间的距离是不相等的。
我考虑过使用插值,但也许没有必要。
谢谢,
您可以像这样创建一个下降数字序列:
peaks <- c(0, which(!is.na(y)), length(y))
drop <- -1/max(diff(peaks))
df <- do.call(rbind, lapply(diff(peaks), function(x) {
data.frame(x = c(0, rep(1, x)),
y = c(1, seq(1 + drop, by = drop, length.out = x)))
}))
df$x <- cumsum(df$x)
结果如下:
plot(df$x, df$y, type = "l")
或者如果你想花哨...
library(ggplot2)
ggplot(df, aes(x, y)) +
geom_line(col = "deepskyblue4", size = 1.5) +
theme_bw()
由 reprex package (v0.3.0)
于 2020-09-18 创建
如何在 R 中表示(绘图和数字)锯齿信号来自:
y <- c(NA,NA,NA,NA,1,NA,NA,NA,1,NA,NA,NA,NA,NA,1,NA,NA,NA,NA,1,NA)
其中1代表在y
中锯齿波达到峰值的时间点(显然是1)。请注意,峰之间的距离是不相等的。
我考虑过使用插值,但也许没有必要。
谢谢,
您可以像这样创建一个下降数字序列:
peaks <- c(0, which(!is.na(y)), length(y))
drop <- -1/max(diff(peaks))
df <- do.call(rbind, lapply(diff(peaks), function(x) {
data.frame(x = c(0, rep(1, x)),
y = c(1, seq(1 + drop, by = drop, length.out = x)))
}))
df$x <- cumsum(df$x)
结果如下:
plot(df$x, df$y, type = "l")
或者如果你想花哨...
library(ggplot2)
ggplot(df, aes(x, y)) +
geom_line(col = "deepskyblue4", size = 1.5) +
theme_bw()
由 reprex package (v0.3.0)
于 2020-09-18 创建