-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathacf.py
More file actions
31 lines (27 loc) · 923 Bytes
/
acf.py
File metadata and controls
31 lines (27 loc) · 923 Bytes
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
import numpy as np
import statsmodels.tsa.stattools as st
import scipy.io.wavfile as wav
import matplotlib.pyplot as plt
def my_acf(sample: np.ndarray, m: int) -> np.float32:
avg = np.mean(sample)
x1 = sample[:len(sample) - m]
x2 = sample[m:]
res = np.sum((x1 - avg) * (x2 - avg))
return res / ((len(sample - m) * np.var(sample)))
if __name__ == '__main__':
fs, x = wav.read('record.wav')
x = x.astype(np.float32) / max(abs(min(x)), abs(max(x)))
my_r = my_acf(x, 5)
r = st.acf(x, True, nlags=1500)
print(my_r, r)
time = np.linspace(0, len(r), len(r))
plt.xticks(np.arange(0, len(r), step=100))
plt.xlabel("Отсчеты")
plt.ylabel("АКФ")
plt.plot(r)
plt.grid(True)
start_point = 100
max_ind = np.argmax(r[start_point:]) + start_point
f_tone = fs / max_ind
print(f'частота основного тона: {f_tone}')
plt.show()