-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdtft.py
More file actions
30 lines (26 loc) · 925 Bytes
/
dtft.py
File metadata and controls
30 lines (26 loc) · 925 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
import numpy as np
import statsmodels.tsa.stattools as st
import scipy.io.wavfile as wav
import matplotlib.pyplot as plt
def my_dtft(sample: np.ndarray, f: np.ndarray, fs: int) -> np.ndarray:
ns = np.arange(0, sample.size, 1)
f_tmp = f
exp_power = (ns * (-1j) * 2 * np.pi) / fs
exp_power = np.outer(f_tmp, exp_power)
e_arr = np.exp(exp_power)
res = np.dot(e_arr, sample)
return np.abs(res)
if __name__ == '__main__':
fs, x = wav.read('record.wav')
x = x.astype(np.float32)
start_point = 100
frequencies = np.arange(40, 501, 1)
spec = my_dtft(x, frequencies, fs)
max_ind = np.argmax(spec[:start_point]) + start_point
f_tone = frequencies[max_ind]
print(f'Основной тон {f_tone}')
plt.plot(frequencies, spec, label="Спектр сигнала")
plt.xlabel("Частота")
plt.ylabel("Амплитуда")
plt.grid(True)
plt.show()