简体   繁体   English

如何获得音频文件中特定时间戳的特定频率?

[英]How can I get the specific frequency at a specific timestamp in an audio file?

Hi I am currently using Librosa for an audio project I am working on, and I was wondering how can I get the amplitude of a frequency at a specific time-frame in an audio file. 嗨,我目前正在将Librosa用于正在处理的音频项目,我想知道如何在音频文件中的特定时间范围内获得频率的幅度。 I don't know if it is straightforward, but I have looked online and can't find anything. 我不知道它是否简单明了,但我在网上看了却找不到任何东西。 I know that you can produce a spectrogram, but how can you get the information suchas the amplitude of aa frequency at a given timestamp? 我知道您可以产生一个频谱图,但是如何获得给定时间戳记下的诸如频率的幅度之类的信息呢?

EDIT: I meant the amplitude at a timestamp. 编辑:我的意思是在一个时间戳振幅。

The spectrogram is a discrete time-frequency representation. 频谱图是离散的时频表示。 In librosa the frequency bins are along the first axis, and time along the second axis. 在librosa中,频率点沿第一轴,而时间沿第二轴。 The frequency bins depend on the number of FFTs chosen, and the time bins depend on the hop length. 频率仓取决于所选择的FFT数量,而时间仓取决于跳跃长度。

Below example shows how to get the amplitude at a given location in the spectrogram, and the associated time and frequency of that location. 下例显示了如何获取频谱图中给定位置的振幅以及该位置的相关时间和频率。

import librosa
import numpy

filename = librosa.util.example_audio_file()
y, sr = librosa.load(filename)
n_fft = 1024
hop_length = 512

spec = numpy.abs(librosa.core.stft(y, n_fft=n_fft, hop_length=hop_length))
freqs = librosa.core.fft_frequencies(n_fft=n_fft)
times = librosa.core.frames_to_time(spec[0], sr=sr, n_fft=n_fft, hop_length=hop_length)

print('spectrogram size', spec.shape)

fft_bin = 14
time_idx = 1000

print('freq (Hz)', freqs[fft_bin])
print('time (s)', times[time_idx])
print('amplitude', spec[fft_bin, time_idx])

Similarly you can go from frequency and time to an index in the spectrogram. 同样,您可以从频率和时间转到频谱图中的索引。 But since it has been discretized you always have to round to the closest index. 但是,由于它已经离散化,因此您始终必须舍入到最接近的索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM