audio - 如何在jupyter笔记本上将音频数据转换为傅立叶?

标签 audio jupyter-notebook fft spectrogram

我想使用courier的转换来转换mp3文件并导出为频谱图,然后我需要另存为包含我mp3的所有频率的PNG文件。如何使用jupyter笔记本来做到这一点?

最佳答案

以下大多数内容来自:http://myinspirationinformation.com/uncategorized/audio-signals-in-python/

mp3样本来自BBC鸟类歌曲网站。

我使用在Linux Mint下运行的Python 3.6在Jupyter笔记本中运行了此文件。

from IPython.display import Audio, display
import matplotlib.pyplot as plt
from numpy import fft
import numpy as np
import pydub
from scipy.fftpack import fft
from scipy.io import wavfile
import scipy
import urllib


AUDIO_URL='http://downloads.bbc.co.uk/rmhttp/radio4/science/Birdsong-Blackbird.mp3'
temp_folder = '/home/bill/data/tmp/'


urllib.request.urlretrieve(AUDIO_URL, temp_folder+'file.mp3')
#read mp3 file
mp3 = pydub.AudioSegment.from_mp3(temp_folder+"file.mp3")
#convert to wav
mp3.export(temp_folder+"file.wav", format="wav")
#read wav file
freq, audio_data = scipy.io.wavfile.read(temp_folder+"file.wav")
length = audio_data.shape[0]/freq
channels = audio_data.shape[1]
print('freq: {} length: {} channels: {}'.format(freq, length, channels))
#if stereo grab both channels
channel1 = audio_data[:,0] #left
channel2 = audio_data[:,1] #right

#create a time variable in seconds
time = np.arange(0, float(audio_data.shape[0]), 1) / freq

#plot amplitude (or loudness) over time
plt.figure(1)
plt.subplot(211)
plt.plot(time, channel1, linewidth=0.01, alpha=0.7, color='#ff7f00')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')
plt.subplot(212)
plt.plot(time, channel2, linewidth=0.01, alpha=0.7, color='#ff7f00')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')
plt.show()

fourier=fft(channel1)

n = len(channel1)
fourier = fourier[0:int(n/2)]

# scale by the number of points so that the magnitude does not depend on the length
fourier = fourier / float(n)

#calculate the frequency at each point in Hz
freq_array = np.arange(0, (n/2), 1.0) * (freq*1.0/n);

plt.plot(freq_array/1000, 10*np.log10(fourier), color='#ff7f00', linewidth=0.02)
plt.xlabel('frequency in kHz')
plt.ylabel('power in dB')
plt.savefig(temp_folder+'spectrogram.png')

关于audio - 如何在jupyter笔记本上将音频数据转换为傅立叶?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47737408/

相关文章:

matlab - 在 matlab 中使用 fft、ifft 和 fftshift

winapi - 如何捕获正在播放的音频?

ios - 在VueJS应用中使用WavesurferJS,可以播放音频,但波形从未加载到IOS Safari?

audio - 使用带有内部DAC的I2S在ESP32上播放WAV时出现问题

jupyter-notebook - 修复损坏的 Jupyter 笔记本/加载以前的版本?

ios - 从 ipad 本地主机上的 Web 服务器运行 jupyter notebook

python - 如何从字符串创建一个 numpy 数组?

android - Hybrid Cordova应用程序:Android上本地没有声音

python - 如何使用 python 在表格中显示多个 jpeg 图像?

signal-processing - 如何从音频流中检测语音