如何使用Python从50Hz开始到10KHz移除鸣叫信号中的静态噪声

huangapple go评论116阅读模式
英文:

How to remove the stationary noise from the chirp signal start from 50Hz to 10KHz using python

问题

我们有一个从50Hz开始并上升到10KHz的鸣叫信号,系统输出中混入了静态噪音,希望去除与鸣叫混合的静态噪音,保留原始鸣叫信号(数据),欢迎使用Python提出任何建议。

用Python实现对鸣叫信号(50Hz到10KHz)的滤波/静态噪音降低方法。

英文:

we have chirp signal starts from 50Hz and it goes up to 10Hkz with stationary noise mixed in the system output and would like to remove the stationary noise mixed with chirp and to retain original chirp signal(data) and any suggestions would be welcome using python, thanks.

filter/stationary noise reduction approach for the chirp signal (50hz to 10Khz) using python.

答案1

得分: 1

以下是我在您的代码中建议的内容的翻译:

  1. 这里是一些代码演示了我在评论中建议的内容
  2. import scipy.signal
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. t = np.linspace(0, 5, 44100*5)
  6. w = scipy.signal.chirp(t, f0=50, f1=10000, t1=5, method='linear')
  7. mu, sigma = 0, .1
  8. s = np.random.normal(mu, sigma, len(t))
  9. sig = w + s
  10. n = 0
  11. win_len = 512
  12. hop = 64
  13. win = np.hanning(win_len)
  14. denoised = np.zeros([len(t)])
  15. while n < len(t):
  16. if n + win_len > len(t):
  17. # 如果样本不足,就用零填充
  18. input = np.zeros([win_len])
  19. end_pt = len(t) - n - 1
  20. for i in range(0, end_pt):
  21. input[i] += sig[n + i]
  22. else:
  23. input = sig[n:n + win_len]
  24. input = input * win # 对信号进行窗口处理
  25. dft = np.fft.fft(input)
  26. mags = abs(dft)
  27. ft_idx = 0
  28. # 将仅包含噪声的频率分量置零
  29. zero = np.zeros(1, dtype=complex)
  30. for m in mags:
  31. if m < 40:
  32. dft[ft_idx] = zero[0]
  33. ft_idx += 1
  34. output = win * np.real(np.fft.ifft(dft)) / (win_len / hop) # 由于输入是实数,所以FFT的逆变换也是实数
  35. # 我们必须小心地对逆变换也进行窗口处理,以便重叠添加平滑
  36. # 注意不要溢出
  37. if n + win_len > len(t):
  38. end_pt = len(t) - n - 1
  39. for i in range(0, end_pt):
  40. denoised[n + i] += output[i]
  41. else:
  42. denoised[n:n + win_len] += output
  43. n += hop
  44. plt.plot(t[0:5000], sig[0:5000])
  45. plt.plot(t[0:5000], denoised[0:5000])
  46. plt.show()

输出:
如何使用Python从50Hz开始到10KHz移除鸣叫信号中的静态噪声

显然,这并不完美。首先,我们保留的信号既受到衰减(由于幅度谱中的粗暴阈值处理)又具有波动峰值幅度(随机噪声的副产品,我们保留了不进行衰减的频率分量中的噪声)。需要更多工作来消除这些效应。

  1. <details>
  2. <summary>英文:</summary>
  3. Here is some code that demonstrates what I suggested in my comment:
  4. import scipy.signal
  5. import numpy as np
  6. import matplotlib.pyplot as plt
  7. t = np.linspace(0,5, 44100*5)
  8. w = scipy.signal.chirp(t, f0=50, f1=10000, t1=5, method=&#39;linear&#39;)
  9. mu, sigma = 0, .1
  10. s = np.random.normal(mu, sigma,len(t))
  11. sig = w+s
  12. n = 0
  13. win_len = 512
  14. hop = 64
  15. win = np.hanning(win_len)
  16. denoised = np.zeros([len(t)])
  17. while n&lt;len(t):
  18. if n+win_len &gt; len(t):
  19. # zero pad if we run out of samples
  20. input = np.zeros([win_len])
  21. end_pt = len(t)-n-1
  22. for i in range(0,end_pt):
  23. input[i]+=sig[n+i]
  24. else:
  25. input = sig[n:n+win_len]
  26. input = input * win # window the signal
  27. dft = np.fft.fft(input)
  28. mags = abs(dft)
  29. ft_idx = 0
  30. # zero out the bins that are only noise
  31. zero = np.zeros(1, dtype=complex)
  32. for m in mags:
  33. if m&lt;40:
  34. dft[ft_idx] = zero[0]
  35. ft_idx +=1
  36. output = win*np.real(np.fft.ifft(dft))/(win_len/hop)# since the input is real,
  37. # the idft of the fft will also be real
  38. # we must take care to also window the idft so
  39. # that the overlap add is smooth
  40. # take care not to overflow
  41. if n+win_len &gt; len(t):
  42. end_pt = len(t)-n-1
  43. for i in range(0,end_pt):
  44. denoised[n+i]+=output[i]
  45. else:
  46. denoised[n:n+win_len] += output
  47. n+=hop
  48. plt.plot(t[0:5000], sig[0:5000])
  49. plt.plot(t[0:5000], denoised[0:5000])
  50. plt.show()
  51. Output:
  52. [![enter image description here][1]][1]
  53. Obviously it is not perfect. For one thing, the signal we keep is both attenuated (due to the brute force thresholding in the magnitude spedtrum) and the peak amplitudes are warbly (a byproduct of the random noise that we keep in the bins we do not attenuate). More work would be needed to get rid of those effects.
  54. [1]: https://i.stack.imgur.com/1VDAE.png
  55. </details>

huangapple
  • 本文由 发表于 2023年6月29日 04:44:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76576597.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定