Python audio processing (a) - signal waveform and spectrum

Foreword

The audio processing part of university courses "Multimedia", theoretical knowledge of what the sampling rate, such as spectrum, bloggers here will slowly explain the actual code, not a general theory of pure knowledge to put on a lot, after all, learn audio process to deal with our daily work life.

Way about audio processing, the code behind will use python language processing, as for after school can be doing, certainly we are very relationship, such as noise music to simulate a person's voice, extract audio and other important information , can be achieved through the python code, we did not talk much began to introduce us directly.

Waveform

In Python language, the reader will need thinkdsp.py waveform codes written others, for example, where we need to read an audio waveform diagram and plotted, as follows:

from matplotlib import pyplot
import thinkdsp

wave=thinkdsp.read_wave("aaa.wav")
wave.plot()
pyplot.show()

Three lines of code to obtain a waveform diagram of an audio file, and the code belong matplotlib rendering graphics package, shown in the code results as shown below:
Graph

Spectrum

Reads the incoming audio file returned by thinkdsp Wave, provides a make_spectrum, spectrum is a spectrum of meaning, we can adopt the following code to print out the spectrum of the audio file:

import thinkdsp
import thinkplot

wave=thinkdsp.read_wave("aaa.wav")
spectrum=wave.make_spectrum()
spectrum.plot()
thinkplot.show()

This code shows the results as shown below:
Spectrum

Spectrum

Spectrum provides a method to modify the spectrum of 3:

(1) low_pass: it loads a low-pass filter, that is higher than a given cutoff frequency of the frequency element is attenuated according to a certain factor, which is reduced in size.

(2) high_pass: it loads a high-pass filter element that is lower than a certain cutoff frequency are attenuated.

(3) band_stop: it lets in the frequency band between elements within the two cutoff frequency attenuated.

For example, here we look at an example, the following code:

import thinkdsp
import thinkplot

wave=thinkdsp.read_wave("aaa.wav")
spectrum=wave.make_spectrum()
spectrum.low_pass(cutoff=600,factor=0.01)
spectrum.plot()
thinkplot.show()
wave=spectrum.make_wave()
wave.write("bbb.wav")

This code will remove bright high-frequency signal, the result of the sound more depressed and dark, after low-pass filter, the spectrum obtained as follows:
Comparedthe reader can compare the difference between the above two graphs, the last two lines of code above the spectrum is converted into Wave, and then save the file to bbb.wav, the reader can try to listen to the code.

Waveform objects

thinkdsp.py里面并没有什么复杂的东西。其提供的大部分函数只是对Numpy和SciPy函数的稀薄封装。其中初始化的库包括Signal,Wave和Spectrum。给定Signal,读者就可以创建一个Wave。给定一个Wave,就可以创建一个Spectrum。Wave与Spectrum可以互相转换。

Wave对象包括3个特性:包括信号参数的Numpy数组ys;信号开始采用和取值的事件点数组ts;每单位时间的采样数framerate。时间的单位通常是秒,但也可以不是。

Wave还提供了三个只读属性:start,end和duration。如果修改了ts,这些属性也会变更。我们来看一段代码:

wave = thinkdsp.read_wave("aaa.wav")
#wave.scale(2)
#wave.shift(1)
wave.ys *= 2
wave.ts += 1
wave.write("bbb.wav")

上面的代码时将波形扩大两倍,晚一秒时间播放,注释的代码与ys,ts那两行代码是等价的,没有任何不同。

信号对象

Signal是一个父类,它向各种信号提供通用的函数,比如make_wave。子类继承了这些方法并提供evaluate,也就是在给定的时间序列内对信号取值。例如,Sinusoid是Signal的子类,定义如下:

class Sinusoid(thinkdsp.Signal):
    def __init__(self, freq=400, amp=1.0, offset=0, func=np.sin):
        thinkdsp.Signal.__init__()
        self.freq = freq
        self.amp = amp
        self.offset = offset
        self.func = func

参数的含义如下:

(1)freq:频率,其含义为每秒周期数,单位是Hz。

(2)amp:幅度。幅度的单位比较随意,通常设定为1.0,对应为传声器的最大输入或给扬声器的最大输出

(3)offset:其含义为信号周期的起始。offset的单位为弧度。

(4)func:它是一个python函数,用来指定时间点的信号求值。通常它不是np.sin就是np.cos,对应的分别是正弦信号和余弦信号。

跟很多初始化的方法一样,它也是把参数存起来以备未来使用。Signal提供了make_wave,如下所示:

def make_wave(self,duration=1,start=0,framerate=11025):
	n=round(duration*framerate)
	ts=start+np.arange(n)/framerate
	ys=self.evaluate(ts)
	return Wave(ys,ts,framerate=framerate)

start和duration分别表示开始的时间和持续的时间,单位为秒.framerate是每秒帧率(采样数)。n代表采样的数量,而ts是采样时间的Numpy数组。为了计算ys,make_wave需要引入evaluate,它是由Sinusoid提供的:

def evaluate(self,ts):
	phases=PI*self.freq*ts+self.offset
	ys=self.amp*self.func(phases)
	return ys

下面我们来逐步地解析这个函数:

(1) self.freq frequency, represents the number of cycles per second, while the various elements are in seconds ts, so their product is the period from the start time of the number actually.

(2) PI2 is a constant, [pi are also commonly used round, here 2Π. After the period multiplied by the PI2 put the converted phase. "Number of cycle time from the fact," the reader can be understood as a phase in radians, and the curvature of each cycle is 2Π.

(3) self.offset is a phase at t = 0, its role is to turn left or right to move a certain distance in a time domain.

(4) If self.func is np.sin or np.cos, which is a value between -1 and 1 for.

(5) generated after the signal range is multiplied self.amp -self.amp to self.amp.

In the mathematical sense, evaluate the following formula:

y=Acos(2πft+φ0)

Where A is the amplitude, f is the frequency, t is time, φ0 is the phase offset. The code above is actually the value of this expression, but as we shall see, this code provides a framework to deal with all signals, not just a function of three signals.

Contents of the first chapter of the introduction here, we used thinkdsp and thinkplot code can be downloaded here, download the following address: click to download

Published 114 original articles · won praise 147 · Views 1.14 million +

Guess you like

Origin blog.csdn.net/liyuanjinglyj/article/details/104701036