如何解释 mp3/wav 文件的 matplotlib 图中的各种颜色
How to interpret various colors in matplotlib plot of a mp3/wav file
我是 python 新手和音频分析新手。如果这不是这个问题的正确位置,请指出正确的位置。
我有一个只有静音的 mp3 音频文件。
使用 sox 转换为 .wav
sox input.mp3 output.wav
from scipy.io.wavfile import read
import matplotlib.pyplot as plt
(fs,x)=read('/home/vivek/Documents/VivekProjects/Silence/silence.wav')
##plt.rcParams['agg.path.chunksize'] = 5000 # for preventing overflow error.
fs
x.size/float(fs)
plt.plot(x)
生成此图像的对象:
我也用了这个问题的答案:How to plot a wav file
from scipy.io.wavfile import read
import matplotlib.pyplot as plt
# read audio samples
from scipy.io.wavfile import read
import matplotlib.pyplot as plt
# read audio samples
input_data = read("/home/vivek/Documents/VivekProjects/Silence/silence.wav")
audio = input_data[1]
# plot the first 1024 samples
plt.plot(audio)
# label the axes
plt.ylabel("Amplitude")
plt.xlabel("Time")
# set the title
plt.title("Sample Wav")
# display the plot
plt.show()
生成这张图片的人:
问题:
我想知道如何解释图表中的不同颜色条(蓝绿色、黄色)。如果你听这个文件,它只是寂静,我希望看到的只是一条扁平线(如果有的话)。
我的 mp3 文件可以从 here 下载。
可以找到sox转换的wav文件here.
尽管文件是无声的,但 dropbox 也在生成波形。我似乎无法弄清楚为什么。
首先,在绘图之前始终检查数据的形状。
x.shape
## (3479040, 2)
所以这里的 2 意味着你的 .wav 文件中有两个通道,默认情况下 matplotlib 以不同的颜色绘制它们。在这种情况下,您需要按行对矩阵进行切片。
import matplotlib.pyplot as plt
ind = int(fs * 0.5) ## plot first 500ms
### plot as time series
plt.plot(x[:ind,:])
plt.figure()
#### Visualise distribution
plt.hist(x[:ind,0],bins = 10)
plt.gca().set_yscale('log')
#####
print x.min(),x.max()
#### -3 3
从图中可以看出,信号的绝对值非常低 (-3,3)。根据 .wav 文件的编码(整数或浮点数),它会被转换为振幅(但振幅可能非常低,这就是它没有声音的原因)。
我自己不熟悉具体的编码方式。但此页面可能会有所帮助:http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html
- For all formats other than PCM, the Format chunk must have an extended portion. The extension can be of zero length, but the size
field (with value 0) must be present.
- For float data, full scale is 1. The bits/sample would normally be 32 or 64.
- For the log-PCM formats (µ-law and A-law), the Rev. 3 documentation indicates that the bits/sample field (wBitsPerSample)
should be set to 8 bits.
- The non-PCM formats must have a fact chunk.
PS:如果你想开始一些更高级的音频分析,请检查这个workshop,我发现它非常实用,尤其是能量部分和 FFT 部分。
我怀疑您的 silence.mp3
文件中的音频非常低(低于人类听力),因为即使我以最大扬声器声音播放也听不到。
所以,我遇到了从 here
mp3
绘制音频
首先我们将 mp3
音频转换为 wav
。由于父文件是立体声,转换后的 wav
文件也是立体声。为了证明有音频,我们只需要单声道。
一旦我们有了单通道 wav
音频,我们就可以简单地根据 time
索引绘制 frequency
,并使用 dB
功率级别的颜色条。
import scipy.io.wavfile
from pydub import AudioSegment
import matplotlib.pyplot as plt
import numpy as np
from numpy import fft as fft
#read mp3 file
mp3 = AudioSegment.from_mp3("silence.mp3")
#convert to wav
mp3.export("silence.wav", format="wav")
#read wav file
rate,audData=scipy.io.wavfile.read("silence.wav")
#if stereo grab both channels
channel1=audData[:,0] #left
#channel2=audData[:,1] #right channel, we dont need here
#create a time variable in seconds
time = np.arange(0, float(audData.shape[0]), 1) / rate
#Plot spectrogram of frequency vs time
plt.figure(1, figsize=(8,6))
plt.subplot(211)
Pxx, freqs, bins, im = plt.specgram(channel1, Fs=rate, NFFT=1024, cmap=plt.get_cmap('autumn_r'))
cbar=plt.colorbar(im)
plt.xlabel('Time (s)')
plt.ylabel('Frequency (Hz)')
cbar.set_label('Intensity dB')
plt.show()
正如您在图像中看到的那样,silence.mp3
确实包含可能具有 -30 到 -45 dB 功率级别的音频。
我是 python 新手和音频分析新手。如果这不是这个问题的正确位置,请指出正确的位置。
我有一个只有静音的 mp3 音频文件。
使用 sox 转换为 .wav
sox input.mp3 output.wav
from scipy.io.wavfile import read
import matplotlib.pyplot as plt
(fs,x)=read('/home/vivek/Documents/VivekProjects/Silence/silence.wav')
##plt.rcParams['agg.path.chunksize'] = 5000 # for preventing overflow error.
fs
x.size/float(fs)
plt.plot(x)
生成此图像的对象:
我也用了这个问题的答案:How to plot a wav file
from scipy.io.wavfile import read
import matplotlib.pyplot as plt
# read audio samples
from scipy.io.wavfile import read
import matplotlib.pyplot as plt
# read audio samples
input_data = read("/home/vivek/Documents/VivekProjects/Silence/silence.wav")
audio = input_data[1]
# plot the first 1024 samples
plt.plot(audio)
# label the axes
plt.ylabel("Amplitude")
plt.xlabel("Time")
# set the title
plt.title("Sample Wav")
# display the plot
plt.show()
生成这张图片的人:
问题: 我想知道如何解释图表中的不同颜色条(蓝绿色、黄色)。如果你听这个文件,它只是寂静,我希望看到的只是一条扁平线(如果有的话)。
我的 mp3 文件可以从 here 下载。
可以找到sox转换的wav文件here.
尽管文件是无声的,但 dropbox 也在生成波形。我似乎无法弄清楚为什么。
首先,在绘图之前始终检查数据的形状。
x.shape
## (3479040, 2)
所以这里的 2 意味着你的 .wav 文件中有两个通道,默认情况下 matplotlib 以不同的颜色绘制它们。在这种情况下,您需要按行对矩阵进行切片。
import matplotlib.pyplot as plt
ind = int(fs * 0.5) ## plot first 500ms
### plot as time series
plt.plot(x[:ind,:])
plt.figure()
#### Visualise distribution
plt.hist(x[:ind,0],bins = 10)
plt.gca().set_yscale('log')
#####
print x.min(),x.max()
#### -3 3
从图中可以看出,信号的绝对值非常低 (-3,3)。根据 .wav 文件的编码(整数或浮点数),它会被转换为振幅(但振幅可能非常低,这就是它没有声音的原因)。
我自己不熟悉具体的编码方式。但此页面可能会有所帮助:http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html
- For all formats other than PCM, the Format chunk must have an extended portion. The extension can be of zero length, but the size field (with value 0) must be present.
- For float data, full scale is 1. The bits/sample would normally be 32 or 64.
- For the log-PCM formats (µ-law and A-law), the Rev. 3 documentation indicates that the bits/sample field (wBitsPerSample) should be set to 8 bits.
- The non-PCM formats must have a fact chunk.
PS:如果你想开始一些更高级的音频分析,请检查这个workshop,我发现它非常实用,尤其是能量部分和 FFT 部分。
我怀疑您的 silence.mp3
文件中的音频非常低(低于人类听力),因为即使我以最大扬声器声音播放也听不到。
所以,我遇到了从 here
mp3
绘制音频
首先我们将 mp3
音频转换为 wav
。由于父文件是立体声,转换后的 wav
文件也是立体声。为了证明有音频,我们只需要单声道。
一旦我们有了单通道 wav
音频,我们就可以简单地根据 time
索引绘制 frequency
,并使用 dB
功率级别的颜色条。
import scipy.io.wavfile
from pydub import AudioSegment
import matplotlib.pyplot as plt
import numpy as np
from numpy import fft as fft
#read mp3 file
mp3 = AudioSegment.from_mp3("silence.mp3")
#convert to wav
mp3.export("silence.wav", format="wav")
#read wav file
rate,audData=scipy.io.wavfile.read("silence.wav")
#if stereo grab both channels
channel1=audData[:,0] #left
#channel2=audData[:,1] #right channel, we dont need here
#create a time variable in seconds
time = np.arange(0, float(audData.shape[0]), 1) / rate
#Plot spectrogram of frequency vs time
plt.figure(1, figsize=(8,6))
plt.subplot(211)
Pxx, freqs, bins, im = plt.specgram(channel1, Fs=rate, NFFT=1024, cmap=plt.get_cmap('autumn_r'))
cbar=plt.colorbar(im)
plt.xlabel('Time (s)')
plt.ylabel('Frequency (Hz)')
cbar.set_label('Intensity dB')
plt.show()
正如您在图像中看到的那样,silence.mp3
确实包含可能具有 -30 到 -45 dB 功率级别的音频。