# library(ts) n_512 x.sim_harmonic.proc(w=pi*c(1/10,1/17),A=c(5,7),t=n-1,plot=F) x_x.sim$x+rnorm(n,sd=25) ts.plot(x) x.pgram_pgram(x) plot(x.pgram$freq,x.pgram$pgram,type="l") # freq = w/(2*pi) # w1 = pi/10 ; freq = 1/20 # w2 = pi/17 ; freq = 1/34 abline(v=1/34,lty=2) abline(v=1/20,lty=2) # estimate the amplitudes by least squares w1_pi/10 ; w2_pi/17 time_0:(n-1) y_cbind(cos(w1*time),sin(w1*time),cos(w2*time),sin(w2*time)) x.fit_lsfit(y,x,intercept=F) x.fit$coef # ts plot of residuals ts.plot(x.fit$resid) # periodograms of residuals x.resid.pgram_pgram(x.fit$resid) plot(x.resid.pgram$freq,x.resid.pgram$pgram,type="l") # fitted sinusoidal trend and periodogram plots of fit x.hat_x-x.fit$resid x.hat.pgram_pgram(x.hat) plot(x.hat.pgram$freq,x.hat.pgram$pgram,type="l") plot(x.pgram$pgram,x.hat.pgram$pgram) #round(x.pgram$pgram-x.hat.pgram$pgram,2) ts.plot(x.pgram$pgram-x.hat.pgram$pgram) ts.plot(cbind(x.pgram$pgram,x.hat.pgram$pgram),gpars=list(lty=1:2)) #*************** f.hat.plt.eg1_function(x.pgram,m=3){ txt_paste( c("Daniell Smooth, m = ",m),collapse="") k.daniell_kernel("daniell",m=m) f.hat_kernapply(x.pgram$pgram/(2*pi),k.daniell) freq_x.pgram$freq[m:(length(x.pgram$freq)-1-m)] f.ci.up_f.hat*(1+2*sqrt(var.exp/(2*m+1))) f.ci.low_f.hat*(1-2*sqrt(var.exp/(2*m+1))) ylim_c(0, max(f.hat,f.ci.up)) plot(freq,f.hat,type="l",main=txt,ylim=ylim) lines(freq,f.ci.up,lty=2) lines(freq,f.ci.low,lty=2) } m_10 f.hat.plt.eg1(x.pgram,m=m)