如何从音频信号计算响度?

2024-10-03 15:30:48 发布

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

我有一个音频信号,我想从中检测出响亮的时刻

我的问题是,我不确定下面给出的算法/代码是否正确

我在许多帖子上读到,“响度”的概念很复杂,取决于个人。我还读到,它可以用光谱图、A加权和RMS来近似。我是音频处理的新手,但根据我所读的内容,我编写了以下算法:

  • 使用STFT计算光谱图
  • 将其转换为dB
  • 应用A加权
  • 计算有效值

我使用Librosa编写的相应代码是:

# Load the input audio
y, sr = librosa.load(path, sr=22050)

# Compute the spectrogram (magnitude)
n_fft = 2048
hop_length = 1024
spec_mag = abs(librosa.stft(y, n_fft=n_fft, hop_length=hop_length))

# Convert the spectrogram into dB
spec_db = librosa.amplitude_to_db(spec_mag)

# Compute A-weighting values
freqs = librosa.fft_frequencies(sr=sr, n_fft=n_fft)
a_weights = librosa.A_weighting(freqs)
a_weights = np.expand_dims(a_weights, axis=1)

# Apply the A-weghting to the spectrogram in dB
spec_dba = spec_db + a_weights

# Compute the "loudness" value
loudness = librosa.feature.rms(S=librosa.db_to_amplitude(spec_dba))

我进展顺利吗? 这个算法正确吗? 我使用Librosa正确吗

谢谢你的帮助


Tags: thetofft算法db音频lengthcompute