-
Notifications
You must be signed in to change notification settings - Fork 0
/
Python对音频信号进行简单的处理
50 lines (44 loc) · 1.56 KB
/
Python对音频信号进行简单的处理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import scipy.io.wavfile
import wave
import matplotlib.pyplot
import matplotlib.pylab
import urllib.request
import numpy
response = urllib.request.urlopen('http://www.nch.com.au/acm/11k16bitpcm.wav')
WAV_FILE = 'english.wav'
file = open(WAV_FILE, 'wb+')
file.write(response.read())
file.close()
wavefile = wave.open(WAV_FILE,'r')
params = wavefile.getparams()
nchannels, sample_width, framerate, numframes = params[:4]
sample_rate, data = scipy.io.wavfile.read(WAV_FILE)
matplotlib.pyplot.subplot(2,1,1)
matplotlib.pyplot.title('Original')
matplotlib.pyplot.plot(data)
newdata = data * 0.2
newdata = newdata.astype(numpy.int16)
scipy.io.wavfile.write('silent.wav', sample_rate, newdata)
matplotlib.pyplot.subplot(2,1,2)
matplotlib.pyplot.title('Quiet')
matplotlib.pyplot.plot(newdata)
matplotlib.pyplot.show()
result = matplotlib.pylab.specgram(newdata, NFFT=1024, Fs = sample_rate, noverlap=900)
#y:音频信号
#NFFT:每个进行快速傅里叶变换的数据块大小,一般取2的幂次
#Fs:采样率
#noverlap:数据块之间重叠数据点的个数
matplotlib.pylab.show()
'''
#注:Python的wave模块也可以实现相应的wav文件读取,示例代码如下:
wavefile = wave.open(WAV_FILE,'r')
params = wavefile.getparams()
nchannels, sample_width, framerate, numframes = params[:4]
#nchannels:声道数
#sample_width:采样宽度,每个采样的字节数
#framerate:采样率
#numframes:总采样数
y_data = wavefile.readframes(numframes)
y = numpy.fromstring(y_data, dtype=numpy.int16)
result = matplotlib.pylab.specgram(y, NFFT=1024, Fs = framerate, noverlap=900)
'''