我有成千上万的录音,用于我正在构建的应用程序。最近我注意到一些录音有一个奇怪的回声。
录音采用 .wav 格式,我正在使用 python 来处理它们。
我看到很多人试图取消回声的问题,但我只需要找到这些文件。
是否有工具或代码可以用来查找这些文件(无需取消回显)。
我试图编写一些代码来取消回声,看看这是否有助于我理解文件何时有回声,但它不起作用。结果文件只是噪音,所以我猜我的算法是错误的。
def nlms(u, d, M, step, eps=0.001, leak=0, initCoeffs=None, N=None, returnCoeffs=False):
# Initialization
if N is None:
N = len(u)-M+1
if initCoeffs is None:
initCoeffs = np.zeros(M)
y = np.zeros(N) # Filter output
e = np.zeros(N) # Error signal
w = initCoeffs # Initial filter coeffs
leakstep = (1 - step*leak)
if returnCoeffs:
W = np.zeros((N, M)) # Matrix to hold coeffs for each iteration
# Perform filtering
for n in xrange(N):
x = np.flipud(u[n:n+M]) # Slice to get view of M latest datapoints
y[n] = np.dot(x, w)
e[n] = d[n+M-1] - y[n]
normFactor = 1./(np.dot(x, x) + eps)
w = leakstep * w + step * normFactor * x * e[n]
y[n] = np.dot(x, w)
if returnCoeffs:
W[n] = w
if returnCoeffs:
w = W
return y, e, w
def CancelEcho(file_path):
np.seterr(all='raise')
audio_file = wave.open(file_path, 'r')
audio_params = audio_file.getparams()
new_frames = []
u = 'a'
while u != " ":
data = audio_file.readframes(1024)
u = np.fromstring(data, np.int16)
u = np.float64(u)
if len(u) ==0:
break
# Generate received signal d(n) using randomly chosen coefficients
coeffs = np.concatenate(([0.8], np.zeros(8), [-0.7], np.zeros(9),
[0.5], np.zeros(11), [-0.3], np.zeros(3),
[0.1], np.zeros(20), [-0.05]))
coeffs.dtype = np.int16
d = np.convolve(u, coeffs)
# Add background noise
v = np.random.randn(len(d)) * np.sqrt(5000)
d += v
# Apply adaptive filter
M = 100 # Number of filter taps in adaptive filter
step = 0.1 # Step size
y, e, w = nlms(u, d, M, step, returnCoeffs=True)
new_frames.extend(y)
audio_file.close()
audio_file = wave.open(out_file, 'w')
audio_file.setparams(audio_params)
audio_file.writeframes(y.astype(np.int16).tostring())
audio_file.close()