### Simulate MA(3) data and plot. Splus # simulate white Gaussian noise and 3pt MA # two graphs one on top of other x <- rnorm(250, mean=0, sd=1) x.fil <- filter(x, c(1,1,1)/3) par(mfrow=c(2,1)) tsplot(x) ts.plot(x.fil) # plot of data with MA superimposed par(mfrow=c(1,1)) t <- 1:length(x) plot(t,x) lines(x.fil) # AR(2) simulation ar2.sim <- arima.sim(list(order=c(2,0,0), ar = c(1,-.9)), n=250) par(mfrow=c(2,1)) tsplot(ar2.sim, main="AR2 Simulation") acf(ar2.sim) # Random walk simulation par(mfrow=c(2,2)) w <- rnorm(250, mean=0, sd=1) x <- cumsum(w) tsplot(x, main="Random Walk") acf(x) xd <- diff(x) tsplot(xd,main="Differenced") acf(xd) # Cosine signal plus noise par(mfrow=c(2,2)) t <- 1:200 x <- sqrt(2)*cos(2*pi*t/50) w <- rnorm(200,0,1) tsplot(x, main="Sinusoid (one cylce every 50 points)") tsplot(x+w, main="SNR=1") tsplot(x+2*w, main="SNR=1/4") tsplot(x+4*w, 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)) tsplot(s, main="signal") tsplot(x, main="signal+noise")