How to detect if a repeating pattern exists

jcaine04 picture jcaine04 · Oct 24, 2014 · Viewed 8.5k times · Source

My question isn't language specific... I would probably implement this in C# or Python unless there is a specific feature of a language that helps me get what I am looking for.

Is there some sort of algorithm that anyone knows of that can help me determine if a list of numbers contains a repeating pattern?

Let's say I have a several lists of numbers...

[12, 4, 5, 7, 1, 2]
[1, 2, 3, 1, 2, 3, 1, 2, 3]
[1, 1, 1, 1, 1, 1]
[ 1, 2, 4, 12, 13, 1, 2, 4, 12, 13]

I need to detect if there is a repeating pattern in each list... For example, list 1 returns false, but and lists 2, 3, and 4 return true.

I was thinking maybe taking a count of each value that appears in the list and if val 1 == val 2 == val n... then that would do it. Any better ideas?

Answer

dvreed77 picture dvreed77 · Oct 24, 2014

You want to look at the autocorrelation of the signal. Autocorrelation basically does a convolution of the signal with itself. When a you iteratively slide one signal across another, and there is a repeating pattern, the output will resonate strongly.