总结些已使用过scipy的基本函数的用法
1from scipy.io import wavfile
2# read wav file
3filename = 'input.wav'
4rate,data = wavfile.read(filename)
5# write wav file
6filename = 'output.wav'
7wavfile.write(filename)
wavfile.read()是scipy用来读取wav音频文件的函数。
Parameters:
filename:str:读取的wav文件名
Return:
rate: int:采样率,每秒取样点的个数
data:numpy array:从文件中读到的数据
wavfile.write()是scipy用于保存数据为wav格式的函数。
Parameters:
filename:str:要保存的文件名
rate:rate:要保存的采样率
data:numpy array:需要保存的数据
parameters:
func:被积函数
a:float:积分下限
b:float:积分上限
Return:
y:float:从a到b函数的定积分
abserr:float:估测的绝对误差(An estimate of absolute error in the result)
1def sa(x):
2 return np.sin(x)/x
3def si(x):
4 s = []
5 for i in x:
6 #防止被积函数分母出现0
7 if i == 0:
8 i = 1e-6
9 s.append(quad(sa,1e-6,i)[0])
10 #return y,abserr
11 return s
12#plot sa函数与si函数的图像
13x = np.linspace(-10,10,100)
14f1 = sa(x)
15f2 = si(x)
16plt.plot(x,f1,label='si(x)')
17plt.plot(x,f2,label='sa(x)')
18plt.xlabel('t(s)')
19plt.ylabel('function')
20plt.legend()
21plt.show()
脉冲函数:
1def pulse1(x):
2 if x<=0:
3 result = 0
4 elif x>=1:
5 result = 0
6 else :
7 result = x
8 return result
9t = np.linspace(0,2.5,50)
10#求卷积积分
11ulist = []
12for i in t:
13 f = lambda x:pulse1(x)*pulse1(i-x)
14 # quad产生0到i的等分矩阵X,设间距为t,求出此时f(X)的值,结果为t*f(X)的和
15 ulist.append(quad(f,0,i)[0])