-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
74 lines (57 loc) · 1.96 KB
/
main.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from fft import fft
from multiply_poly import multiply_poly
from sound import create_sample, read_mp3, convert_audio_to_spectrogram_data, convert_spectrogram_data_to_img, c_spec, c_fft
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
# def example_mult_poly():
# poly1 = [-3, 4, 8]
# poly2 = [-1, -2, 5]
# print("Multiplying (" + str_poly(poly1) + ") * (" + str_poly(poly2) + ")")
# res = multiply_poly(poly1, poly2)
# print(str_poly_i(res))
# # list of coeffs to string
# def str_poly(poly):
# s = []
# for (i, coeff) in enumerate(poly):
# if coeff != 0:
# s.append(str(coeff) + "x^" + str(i))
# return " + ".join(s)
# # list of imaginary coeffs to string
# def str_poly_i(poly):
# s = []
# for (i, coeff) in enumerate(poly):
# if coeff.real != 0:
# s.append(str(coeff.real) + "x^" + str(i))
# return " + ".join(s)
def example_fft_sound():
sample = create_sample(30,1,5,1024)
sample2 = create_sample(5,1,1,1024)
t_data = [pt[0] for pt in sample]
a_data = [pt1[1] + pt2[1] for pt1,pt2 in zip(sample,sample2)]
fig, (ax1, ax2) = plt.subplots(2,1)
ax1.plot(t_data, a_data)
a_data = np.array(a_data)
res = [x for x in c_fft(a_data)]
res = res[:len(res)//2]
ax2.plot(range(len(res)), res)
plt.show()
def example_fft_file():
y, sr = read_mp3("music/beep.wav")
t_data = [x / sr for x in range(len(y))]
t_data = t_data[sr:sr+sr]
a_data = y[sr:sr+sr]
a_data = np.array(a_data)
fig, (ax1, ax2) = plt.subplots(2,1)
ax1.plot(t_data, a_data)
res = [x for x in c_fft(a_data)]
res = res[:len(res)//2]
ax2.plot(range(len(res)), res)
plt.show()
def example_spectrogram():
y, sr = read_mp3("music/chlorine.mp3")
# data = convert_audio_to_spectrogram_data(y, sr)
bits = 14
data = c_spec(y, len(y), sr, bits)
convert_spectrogram_data_to_img(data, sr, bits)
example_spectrogram()