栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Python中的音频频率

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python中的音频频率

下面的此功能可查找频谱。我还提供了一个正弦信号和一个WAV文件示例应用程序。这是出于教育目的;您也可以使用易于使用的matplotlib.pyplot.magnitude_spectrum(请参见下文)。

from scipy import fft, arangeimport numpy as npimport matplotlib.pyplot as pltfrom scipy.io import wavfileimport osdef frequency_sepectrum(x, sf):    """    Derive frequency spectrum of a signal from time domain    :param x: signal in the time domain    :param sf: sampling frequency    :returns frequencies and their content distribution    """    x = x - np.average(x)  # zero-centering    n = len(x)    print(n)    k = arange(n)    tarr = n / float(sf)    frqarr = k / float(tarr)  # two sides frequency range    frqarr = frqarr[range(n // 2)]  # one side frequency range    x = fft(x) / n  # fft computing and normalization    x = x[range(n // 2)]    return frqarr, abs(x)# Sine sample with a frequency of 1hz and add some noisesr = 32  # sampling ratey = np.linspace(0, 2*np.pi, sr)y = np.tile(np.sin(y), 5)y += np.random.normal(0, 1, y.shape)t = np.arange(len(y)) / float(sr)plt.subplot(2, 1, 1)plt.plot(t, y)plt.xlabel('t')plt.ylabel('y')frq, X = frequency_sepectrum(y, sr)plt.subplot(2, 1, 2)plt.plot(frq, X, 'b')plt.xlabel('Freq (Hz)')plt.ylabel('|X(freq)|')plt.tight_layout()# wav sample from https://freewavesamples.com/files/Alesis-Sanctuary-QCard-Crickets.wavhere_path = os.path.dirname(os.path.realpath(__file__))wav_file_name = 'Alesis-Sanctuary-QCard-Crickets.wav'wave_file_path = os.path.join(here_path, wav_file_name)sr, signal = wavfile.read(wave_file_path)y = signal[:, 0]  # use the first channel (or take their average, alternatively)t = np.arange(len(y)) / float(sr)plt.figure()plt.subplot(2, 1, 1)plt.plot(t, y)plt.xlabel('t')plt.ylabel('y')frq, X = frequency_sepectrum(y, sr)plt.subplot(2, 1, 2)plt.plot(frq, X, 'b')plt.xlabel('Freq (Hz)')plt.ylabel('|X(freq)|')plt.tight_layout()plt.show()

您还可以参考SciPy的Fourier变换和Matplotlib的幅度谱绘图页面以获取更多信息和功能。

magspec = plt.magnitude_spectrum(y, sr)  # returns a tuple with the frequencies and associated magnitudes


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/624891.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号