Created
April 30, 2020 19:58
-
-
Save dlebauer/997b89a886f7370a560399958989782e to your computer and use it in GitHub Desktop.
Exercise from math circle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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