### Simulate MA(3) data and plot. R library(ts) # simulate white Gaussian noise and 3pt MA # two graphs one on top of other win.graph() x <- rnorm(250, mean=0, sd=1) x.fil <- filter(x, c(1,1,1)/3) par(mfrow=c(2,1)) ts.plot(x) ts.plot(x.fil) # plot of data with MA superimposed win.graph() par(mfrow=c(1,1)) t <- 1:length(x) plot(t,x) lines(x.fil) # Random walk simulation win.graph() par(mfrow=c(2,2)) w <- rnorm(250, mean=0, sd=1) x <- cumsum(w) ts.plot(x, gpars=list(main="Random Walk")) acf(x) xd <- diff(x) ts.plot(xd,gpars=list(main="Differenced")) acf(xd) # Cosine signal plus noise win.graph() par(mfrow=c(2,2)) t <- 1:200 x <- sqrt(2)*cos(2*pi*t/50) w <- rnorm(200,0,1) ts.plot(x, gpars=list(main="Sinusoid (one cylce every 50 points)")) ts.plot(x+w, gpars=list(main="SNR=1")) ts.plot(x+2*w, gpars=list(main="SNR=1/4")) ts.plot(x+4*w, gpars=list(main="SNR=1/16")) # NOTES: # SNR signal-to-noise ratio = var(signal)/var(noise) # Here var(x)=sum(x^2)/n, in this case n=200 # and sum[cos(2*pi*nu*t)^2]=n/2 except when nu=0 or n/2. # Here nu=1/50 so in this example # var(signal)/var(noise) = 1/1, 1/4 and 1/16. # check the sample values var(x)/var(w) var(x)/var(2*w) var(x)/var(4*w) # modulated signal + noise w <- rnorm(200,0,1) t <- 1:100 y <- cos(2*pi*t/10) e <- exp(-t/50) s <- c(rep(0,100),10*y*e ) x <- s+w par(mfrow=c(2,1)) ts.plot(s, gpars=list(main="signal")) ts.plot(x, gpars=list(main="signal+noise"))