【发布时间】:2020-02-22 08:41:58
【问题描述】:
我目前正在尝试处理语音信号。我已经在 -1 和 1 之间缩放了一个向量。我已经设法做到了,但请查看我获得的向量图。我需要将该信号以 0 为中心,以获得将提供给神经网络的特征向量。略低于0。
如何在 -1 和 1 之间缩放这个向量并保持居中在 0 ?
原始信号: enter image description here
缩放信号: enter image description here
代码和归一化函数:
samplerate, data = wavfile.read('avarii.wav')
times = np.arange(len(data))/float(samplerate)
print(times)
print(len(data))
print(samplerate)
# Make the plot
# You can tweak the figsize (width, height) in inches
#unit_vector = norm(data[:,1])
samples = data[:,1].tolist()
normalized_vector = norm(samples,-1,1)
#print(norm_vect)
plt.figure(figsize=(30, 4))
plt.plot(times,normalized_vector)
# plt.fill_between(times, data[:,0], data[:,1], color='k')
plt.xlim(times[0], times[-1])
plt.xlabel('time (s)')
plt.ylabel('amplitude')
# You can set the format by changing the extension
# like .pdf, .svg, .eps
plt.savefig('plot.png', dpi=100)
plt.show()
def norm(vector,a,b):
#normalized_vector = []
#standard_deviation = stdev(vector)
max_value = max(vector)
min_value = min(vector)
#average = sum(vector)/len(vector)
print(max_value)
print(min_value)
print(average)
for value in vector:
#value = value - average
norm_value = a + ((value - min_value)*(b-a))/(max_value - min_value)
normalized_vector.append(norm_value)
# for value in vector:
# normalized_vector.append((value - average)/standard_deviation)
# for value in vector:
# normalized_vector.append((value - min_value) / (max_value-min_value))
return normalized_vector
总之,我想将这些样本的值限制在 [-1,1] 之间并以 0 为中心。我该怎么做?
【问题讨论】:
-
减去平均值然后除以最大值就可以了。 (或者更确切地说是信号绝对值的最大值)。它应该由 numpy 操作完成,而不是循环
标签: python signals signal-processing