-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio_analysis.py
154 lines (119 loc) · 4.49 KB
/
audio_analysis.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import matplotlib.pyplot as plt
import os
import numpy as np
import scipy.optimize as optimize
import librosa
from modules import audio, analyse, norms, utils
from modules.csv_writer import CSVWriter
from modules.label_timer import LabelTimer
print(
"Make sure that the output folder is not a subdirectory of the audio folder. At most, they can be the same folder."
)
AUDIO_FOLDER = input("Enter the path of the audio folder: ")
OUTPUT_FOLDER = input("Enter the path of the output folder: ")
utils.ensure_dir(OUTPUT_FOLDER)
results_path = os.path.join(OUTPUT_FOLDER, "results.csv")
results_csv = CSVWriter(
results_path,
["id", "norm_id", "duration", "delta_duration", "intensity", "delta_intensity"],
)
LABELS_FILE_NAME = "labels.txt"
for dir_name in utils.list_subdirectories(AUDIO_FOLDER):
dir_path = os.path.join(AUDIO_FOLDER, dir_name)
labels_file_path = os.path.join(dir_path, LABELS_FILE_NAME)
timer = LabelTimer(labels_file_path)
references = []
reference_folder_path = os.path.join(dir_path, "reference")
for file_name in os.listdir(reference_folder_path):
file_path = os.path.join(reference_folder_path, file_name)
y, sr = librosa.load(file_path, sr=None)
frequencies, spectrum = audio.get_spectrum(y, sr)
references.append((frequencies, spectrum))
t_values = []
y_values = []
y_deltas = []
effect_folder = os.path.join(dir_path, "effect")
for file_name in os.listdir(effect_folder):
t_values.append(timer(file_name))
file_path = os.path.join(effect_folder, file_name)
y, sr = librosa.load(file_path, sr=None)
frequencies, spectrum = audio.get_spectrum(y, sr)
distance, delta_distance = analyse.get_distance(
norms.l1_log, spectrum, references, frequencies
)
y_values.append(distance)
y_deltas.append(delta_distance)
plot_title = analyse.format_plot_title(dir_name)
plt.figure(figsize=(12, 10))
plt.scatter(t_values, y_values, label="Norm: L1 with log x axis")
max_y = np.max(y_values)
min_y = np.min(y_values)
plt.ylim(top=max_y + 5, bottom=min_y - 5)
plt.xlim(left=0)
current_output_folder = os.path.join(OUTPUT_FOLDER, dir_name)
utils.ensure_dir(current_output_folder)
data_csv_path = os.path.join(current_output_folder, f"{dir_name}.csv")
data_csv = CSVWriter(data_csv_path, ["time", "distance", "delta_distance"])
for index in range(len(t_values)):
data_csv.addline(
{
"time": t_values[index],
"distance": y_values[index],
"delta_distance": y_deltas[index],
}
)
data_csv.write()
log_file_path = os.path.join(current_output_folder, f"{dir_name}_log.txt")
try:
(par_a, par_b, par_c), pcov = optimize.curve_fit(
analyse.exponential, t_values, y_values, sigma=y_deltas, p0=[20, -1 / 10, 5]
)
delta_a, delta_b, delta_c = np.sqrt(np.diag(pcov))
results_csv.addline(
{
"id": dir_name,
"norm_id": "l1_log",
"duration": -1 / par_b,
"delta_duration": delta_b / par_b**2,
"intensity": par_a,
"delta_intensity": delta_a,
}
)
t0 = 0
tf = np.max(t_values)
t_values = np.linspace(t0, tf, 10000)
y_values = analyse.exponential(t_values, par_a, par_b, par_c)
plt.plot(
t_values,
y_values,
"r",
label="Fit",
)
errors = analyse.get_error(t_values, par_a, par_b, delta_a, delta_b, delta_c)
plt.fill_between(
t_values,
y_values - errors,
y_values + errors,
color="r",
alpha=0.2,
label="Error",
)
with open(log_file_path, "w") as log_file:
log_file.write(f"Parameter a: {par_a} +- {delta_a}\n")
log_file.write(f"Parameter b: {par_b} +- {delta_b}\n")
log_file.write(f"Parameter c: {par_c} +- {delta_c}\n")
except:
with open(log_file_path, "w") as log_file:
log_file.write(f"Fit failed")
plt.legend()
plt.xlabel("Time (s)")
plt.ylabel("Distance (Hz dB)")
plt.title(plot_title)
plt.grid(True)
plt.margins(x=0)
fig_path = os.path.join(OUTPUT_FOLDER, dir_name, f"{dir_name}.pdf")
plt.savefig(fig_path)
plt.close()
print(f"Done {plot_title}")
results_csv.write()
print("Done writing results")