如何在录音结束时消除噪音?

2024-05-29 10:31:39 发布

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

我已经用python生成了音频。但它的结局是:

enter image description here

所以我想知道如何使用pythonlibrosa/pydub删除如此响亮的随机noize

换句话说,如何检测噪声开始和结束的位置。我们知道录制结束时会有杂音,所以问题是如何找到杂音的起始位置,并在python中删除杂音


Tags: 杂音音频噪声时会pydub结局noizepythonlibrosa
1条回答
网友
1楼 · 发布于 2024-05-29 10:31:39

在您的情况下,似乎一个简单的阈值将起作用。为了确保不会过早剪辑,我们将要求在截断音频之前至少k值超过阈值

import librosa
import numpy as np

def first_occ_index(w, n):
    # Borrowed from https://stackoverflow.com/questions/49693770/get-index-of-the-first-block-of-at-least-n-consecutive-false-values-in-boolean-a
    idx = np.flatnonzero(np.r_[True, w, True])
    lens = np.diff(idx) - 1
    return idx[(lens >= n).argmax()]

X, fs = librosa.load('your.audio')
threshold = 3 * X.std() # or e.g. 0.6 * X.max() - play with it
X_th = np.abs(X) < threshold
k = 20 # we require 20 consecutive values above the threshold 
idx_to_cut = first_occ_index(X_th, k)
my_audio = X[:idx_to_cut]
garbage = X[idx_to_cut:]

相关问题 更多 >

    热门问题