gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 22:31:48 24 4
gpt4 key购买 nike

我想使用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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com