找出音响中是否有回声

2024-10-03 02:40:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我有成千上万的录音,我用在我正在开发的应用程序中。 最近我注意到有些录音有奇怪的回音。在

录音是.wav格式的,我用python来处理它们。在

我看到很多问题,其中pepole试图取消回声,但我只需要找到那些文件。在

是否有工具或代码可以用来查找这些文件(不需要取消回音)。在

我试图编写一些代码来取消回显,看看这是否有助于我理解文件何时有echo,但它不起作用。 结果文件只是噪音,所以我猜我的算法是错误的。在

    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()

Tags: 文件nonelenifstepnpzerosfilter
1条回答
网友
1楼 · 发布于 2024-10-03 02:40:10

一种想法是从文件中提取一部分,然后将其移动到文件的其余部分,找到一个信号转换成另一个信号所需的倍增因子。在

代码归属: https://docs.python.org/2/library/audioop.html

这可能会起作用:

def echocancel(outputdata, inputdata):
  pos = audioop.findmax(outputdata, 800)    # one tenth second
  out_test = outputdata[pos*2:]
  in_test = inputdata[pos*2:]
  ipos, factor = audioop.findfit(in_test, out_test)
  # Optional (for better cancellation):
  # factor = audioop.findfactor(in_test[ipos*2:ipos*2+len(out_test)],
  #              out_test)
  return factor

系数越接近1.0,就越有可能出现回声

相关问题 更多 >