【问题标题】:Evenly-spaced 1/freq for R function spec.pgram()R 函数 spec.pgram() 的均匀间隔 1/freq
【发布时间】:2016-01-19 22:30:09
【问题描述】:

目标是绘制两个时间序列之间的相干性(即相对于频率的相关系数)。如何让 x 轴上的 1/freq(即周期)均匀分布?

t <- 0:99
ts1 <- ts(2*cos((2*pi)/24*t))
ts2 <- ts(2*cos((2*pi)/48*t))
ts12 <- ts.intersect(ts1, ts2)

Coh <- spec.pgram(ts12, spans=3)
plot(Coh$freq, Coh$coh, type='l')
plot(1/Coh$freq, Coh$coh, type='l')  # how to get 1/freq to be evenly-space?

我试图修改函数 spec.pgram() 但没有成功。更具体地说,我替换了以下行:

freq <- seq.int(from = xfreq/N, by = xfreq/N, length.out = Nspec)

与:

freq.tmp <- seq.int(from = xfreq/N, by = xfreq/N, length.out = Nspec)
freq <- rev(1/seq(from=1/max(freq.tmp), to=1/min(freq.tmp), length.out=Nspec))  

还有其他人的运气更好吗?谢谢

【问题讨论】:

    标签: r spectrum


    【解决方案1】:

    您的意思是您只想用句点而不是频率重新标记 x 轴吗?这将保持 x 轴上值的间距,但代价是 x 值的非线性缩放。例如(使用ggplot2):

    library(ggplot2)
    
    dat = as.data.frame(Coh[c("freq","spec","coh","phase")])
    
    ggplot(dat, aes(freq, coh)) + 
      geom_line() +
      geom_point() +
      scale_x_continuous(breaks=dat$freq[seq(1,nrow(dat),3)], 
                         minor_breaks=dat$freq,
                         labels=round(1/dat$freq[seq(1,nrow(dat),3)],1)) +
      labs(x="Period")
    

    您还可以将 x 值标签设置为整数周期:

    breaks = c(1:10,15,25,50,100)
    
    ggplot(dat, aes(freq, coh)) + 
      geom_line() +
      geom_point() +
      scale_x_continuous(breaks=1/breaks,
                         minor_breaks = 1/(breaks[-1] - 0.5 * diff(breaks)),
                         labels=breaks) +
      labs(x="Period")
    

    【讨论】:

    • 干得好eipi10,我认为这是一个正确的解决方案!干杯
    • 虽然拥有线性缩放或“均匀间隔”功能会更好
    • 嗯,线性缩放是你直接绘制 1/freq 时得到的。但这正是你试图避免的。
    • 我所做的修改返回均匀间隔的周期(通过指定不均匀间隔的频率),但输出(连贯性,作为周期的函数)看起来不正确(看起来不就像你的情节的拉伸版本)。我怀疑缺少光谱理论的某些东西
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-22
    • 2016-12-26
    • 1970-01-01
    • 2022-08-03
    • 2014-07-28
    • 1970-01-01
    • 2020-10-26
    相关资源
    最近更新 更多