-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsp20_periodicity_finder.py
More file actions
40 lines (31 loc) · 1.11 KB
/
sp20_periodicity_finder.py
File metadata and controls
40 lines (31 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""
==========================
Crude periodicity finding
==========================
Discover the periods in evolution of animal populations
(:download:`E:/CODE/PYTHON/2021/data-done-tutorial-basic-matplot-numpy-panda-scipy-tkinter/populations.txt`)
"""
import numpy as np
from scipy import fftpack
import matplotlib.pyplot as plt
data = np.loadtxt('E:/CODE/PYTHON/2021/data-done-tutorial-basic-matplot-numpy-panda-scipy-tkinter/sp20_populations.txt')
years = data[:, 0]
populations = data[:, 1:]
# Plot the data
plt.figure()
plt.plot(years, populations * 1e-3)
plt.xlabel('Year')
plt.ylabel('Population number ($\cdot10^3$)')
plt.legend(['hare', 'lynx', 'carrot'], loc=1)
# Plot its periods
ft_populations = fftpack.fft(populations, axis=0)
frequencies = fftpack.fftfreq(populations.shape[0], years[1] - years[0])
periods = 1 / frequencies
plt.figure()
plt.plot(periods, abs(ft_populations) * 1e-3, 'o')
plt.xlim(0, 22)
plt.xlabel('Period')
plt.ylabel('Power ($\cdot10^3$)')
plt.show()
# There's probably a period of around 10 years (obvious from the plot),
# but for this crude a method, there's not enough data to say much more.