Skip to content

Instantly share code, notes, and snippets.

@dlebauer
Created April 30, 2020 19:58
Show Gist options
  • Save dlebauer/997b89a886f7370a560399958989782e to your computer and use it in GitHub Desktop.
Save dlebauer/997b89a886f7370a560399958989782e to your computer and use it in GitHub Desktop.
Exercise from math circle
n <- 0:20
y <- x%%7
x <- 2^n
x%%13
# https://stats.stackexchange.com/a/1214/1381
find.freq <- function(x)
{
n <- length(x)
spec <- spec.ar(c(x),plot=FALSE)
if(max(spec$spec)>10) # Arbitrary threshold chosen by trial and error.
{
period <- round(1/spec$freq[which.max(spec$spec)])
if(period==Inf) # Find next local maximum
{
j <- which(diff(spec$spec)>0)
if(length(j)>0)
{
nextmax <- j[1] + which.max(spec$spec[j[1]:500])
period <- round(1/spec$freq[nextmax])
}
else
period <- 1
}
}
else
period <- 1
return(period)
}
find.freq(x%%n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment