-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluate.py
209 lines (160 loc) · 7.31 KB
/
evaluate.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import time
import numpy as np
import pandas as pd
import csv
from scipy.interpolate import interp1d
import os
import matplotlib.pyplot as plt
ROOT_DIR = "D:\\课程\\ACA\\pyACA-master\\testdata\\URMP"
from test_amdf_register import instrument_register
def read_csv(csv_dir, row_num = 0):
# 逐行读取 CSV 文件,并且只输出第一列
with open(csv_dir) as csvfile:
reader = csv.reader(csvfile)
lst = []
for row in reader:
lst.append(row[row_num]) # 获取第一列的数据
return lst
def read_txt_file(file_name, col):
# 打开txt文件
with open(file_name, 'r') as f:
# 读取txt数据
lines = f.readlines()
# 解析每一行,并且提取第col列数据
data = []
for line in lines:
# 将一行数据按照tab分割成多个字段
fields = line.strip().split('\t')
# 取出第col列数据,并转换为浮点型数值
value = float(fields[col])
data.append(value)
return data
def count_error(predicted_lst, ground_truth_lst):
err = 0
lst_len = len(predicted_lst)
for i in range(lst_len):
if ground_truth_lst[i] * 0.9715 <= predicted_lst[i] <= ground_truth_lst[i] * 1.0293:
err = err + 0
else:
err = err + 1
return err, lst_len
def main(csv_dir, txt_dir, method_row):
# csv_dir = "E:\\testdata\\URMP\\vn\\AuSep_1_vn_01_Jupiter.csv" # target x
# txt_dir = "E:\\testdata\\URMP\\vn\\F0s_1_vn_01_Jupiter.txt" # origin x & y
# csv只用读target的time值
time_lst_target = read_csv(csv_dir=csv_dir, row_num=0)
time_lst_target = time_lst_target[1:] # 去掉time
time_lst_target = [float(i) for i in time_lst_target]
time_lst_target = time_lst_target[:-2]
# txt是源值
time_lst_old = read_txt_file(txt_dir, 0)
val_lst_old = read_txt_file(txt_dir, 1)
# 做插值
'''
‘linear’:线性插值(默认值),对应于传统的拉格朗日插值方法
‘nearest’:最近邻插值,即取离要求插值点最近的已知数据点的函数值作为插值结果
‘zero’:零阶插值,即在两个已知数据点之间取函数值为0的水平线段作为插值结果
‘slinear’:一次样条插值,即所谓的“分段线性插值”
‘quadratic’:二次插值
‘cubic’:三次插值
‘previous’:向前差值(piecewise constant function)
‘next’:向后差值(piecewise constant function)
'''
f = interp1d(time_lst_old, val_lst_old, kind='nearest')
# 按照计算结果的x轴,重新采样的groundtruth
ground_truth_lst_resampled = f(time_lst_target)
# 算法算出来的结果
'''
1 SpectralAcf
2 SpectralHps
3 TimeAcf
4 TimeAmdf
5 TimeAuditory
6 TimeZeroCrossings
7 YIN
8 PYIN
'''
val_lst_target = read_csv(csv_dir=csv_dir, row_num=method_row)
val_lst_target = val_lst_target[1:] # 去掉time
val_lst_target = [float(i) for i in val_lst_target]
val_lst_target = val_lst_target[:-2]
# 进行对比
err = count_error(predicted_lst=val_lst_target, ground_truth_lst=ground_truth_lst_resampled)
return err, val_lst_target, ground_truth_lst_resampled
def error_export():
root_dir = ROOT_DIR
instrument_list = os.listdir(root_dir)
# 新建一个全0的二维数组,行数:乐器数=13,列数:方法数=8
dim_1 = [0 for index in range(8)]
data_matrix = [list(dim_1) for index in range(13)]
# 遍历乐器
for instrument_num in range(len(instrument_list)): # 外循环:instrument
instrument_lst_path = os.path.join(root_dir, instrument_list[
instrument_num]) # "D:\\课程\\ACA-Slides-2nd_edition\\URMP\\bn"
instrument_dir = os.listdir(instrument_lst_path)
instrument_dir = [file_name for file_name in instrument_dir if not file_name.endswith('.wav')]
instrument_dir_len = len(instrument_dir)
total_err = 0
total_lst_len = 0
for method_row in range(1, 9): # 1~8个方法
for i in range(instrument_dir_len // 2): # 组数
csv_dir = instrument_dir[i]
csv_dir = os.path.join(instrument_lst_path, csv_dir)
txt_dir = instrument_dir[i + instrument_dir_len // 2]
txt_dir = os.path.join(instrument_lst_path, txt_dir)
err, lst_len = main(csv_dir=csv_dir, txt_dir=txt_dir, method_row=method_row) # 内循环:method
total_err = total_err + err
total_lst_len = total_lst_len + lst_len
err_rate = total_err / total_lst_len
data_matrix[instrument_num][method_row - 1] = err_rate
print("total error is", total_err / total_lst_len)
np.save("data_matrix", data_matrix)
def add_gt_to_csv():
urmp_path = '.\\testdata\\URMP'
urmp_folder_list = [os.path.join(urmp_path, dir) for dir in os.listdir(urmp_path)]
for urmp_folder in urmp_folder_list:
file_list = [os.path.join(urmp_folder, file) for file in os.listdir(urmp_folder)]
for file_path in file_list:
if file_path.endswith('.wav'): # 是wav文件
csv_dir = file_path[:-4] + '.csv'
txt_dir = urmp_folder + "\\" + "F0s" + file_path.split("\\")[-1][5:-4] + ".txt"
_, _, ground_truth_lst_resampled = main(csv_dir=csv_dir, txt_dir=txt_dir, method_row=1)
ground_truth_lst_resampled = pd.DataFrame(ground_truth_lst_resampled, columns=['GroundTruth'])
df = pd.read_csv(csv_dir)
df = pd.concat([df, ground_truth_lst_resampled], axis=1)
print(df)
df.to_csv(csv_dir, index=False)
def paint(csv_path):
instrument = instrument = csv_path.split("\\")[-2]
f_min = instrument_register[instrument][0]
f_max = instrument_register[instrument][1]
df = pd.read_csv(csv_path)
x = df['time']
f0_SpectralAcf = df['SpectralAcf']
f0_SpectralHps = df['SpectralHps']
f0_TimeAcf = df['TimeAcf']
f0_TimeAmdf = df['TimeAmdf']
f0_TimeAuditory = df['TimeAuditory']
f0_TimeZeroCrossings = df['TimeZeroCrossings']
f0_YIN = df['YIN']
f0_PYIN = df['PYIN']
f0_GroundTruth = df['GroundTruth']
plt.figure(num=1, figsize=(12, 8))
plt.xlabel('time')
plt.ylabel('f0')
plt.plot(x, f0_SpectralAcf, label='SpectralAcf', linestyle='--')
plt.plot(x, f0_SpectralHps, label='SpectralHps', linestyle='--')
plt.plot(x, f0_TimeAcf, label='TimeAcf', linestyle='--')
plt.plot(x, f0_TimeAmdf, label='TimeAmdf', linestyle='--')
plt.plot(x, f0_TimeAuditory, label='TimeAuditory', linestyle='--')
plt.plot(x, f0_TimeZeroCrossings, label='TimeZeroCrossings', linestyle='--')
plt.plot(x, f0_YIN, label='YIN', linestyle='--')
plt.plot(x, f0_PYIN, label='PYIN', linestyle='--')
plt.plot(x, f0_GroundTruth, label='GroundTruth', color='k', linestyle='-')
# plt.yscale('log')
plt.ylim(f_min, f_max*0.8)
plt.xlim(15, 20)
plt.legend(loc="upper right")
plt.show()
if __name__ == "__main__":
paint(r"D:\课程\ACA\pyACA-master\testdata\URMP\vn\AuSep_1_vn_01_Jupiter.csv")