避免在R中的移动窗口中循环

时间:2018-08-09 13:54:42

标签: r loops moving-average spectrum

我有一个用于频谱计算的示例def REE_Group_Plot(dfs, label): # combine all dataframes into 1 all_dfs = pd.concat(dfs) # the rest of the code to plot the graph REE_Group_Plot(dfs=[df, df2, df3, df4], label = “ABC”) 脚本。我将信号分成几个块,并对每个块进行计算。

R

此处spect=function(x,samplingfrequency=1,blocksize=2^12) { T=length(x) blocks=trunc(T/blocksize) localfreq=c() ; localspec=c() for(i in 1:blocks){localfreq[[i]]=c() ; localspec[[i]]=c() } for(i in 1:blocks) { time=c( (1+(i-1)*blocksize) : (i*blocksize) ) localspectrum=spectrum(x[time],plot=FALSE) localfreq[[i]]=localspectrum$freq localspec[[i]]=localspectrum$spec } averagespec=rep(0,(blocksize/2)) for(freq in 1:(blocksize/2)) { for(block in 1:blocks) { averagespec[freq]=(averagespec[freq]+localspec[[block]][freq]) } averagespec[freq]=averagespec[freq]/blocks } par(mar=c(5.1,5.1,2.5,1.5)) plot(c(1:(blocksize/2))/(blocksize/2)*samplingfrequency/2,averagespec,log="xy",t="l",xlab="frequency [Hz]",ylab="average spectrum [a.u.]",cex.lab=1.8,cex.axis=1.8) abline(v=(samplingfrequency/2),col=2) abline(v=(1/blocksize*samplingfrequency),col=4) } 是您的时间序列输入。我不直接使用x中的spectrum函数,因为结果太吵了。我想知道我是否可以以某种方式避免脚本中的R循环?

0 个答案:

没有答案