Python知识:生成声波并显示
scipy.signal.chirp — SciPy v1.8.0 Manual
1 說明?
? ? ? ? 如何自己產(chǎn)生一個聲波? 采用signal.chirp產(chǎn)生聲波,對聲波讀、存、參數(shù)設(shè)定,等聲波操作,都依靠一個叫Wav的對象完成。
2 讀文件Wave_read Objects
Wave_read Objects對象是由open()返回,Wave_read Objects內(nèi)部函數(shù)是:
-
Wave_read.close()
如果流被wave打開,請關(guān)閉它,并使實例不可用。這在對象集合中自動調(diào)用。
-
Wave_read.getnchannels()
返回音頻通道數(shù)(1個用于單聲道 mono,2個用于立體聲 stereo)。
- Wave_read.getsampwidth()
返回以字節(jié)為單位的樣本寬度。
- Wave_read.getframerate()
返回采樣頻率。
- Wave_read.getnframes()
返回音頻幀數(shù)。
- Wave_read.getcomptype()
返回壓縮類型(“無”是唯一受支持的類型,因wav格式從不壓縮)。
- Wave_read.getcompname()
壓縮getcomptype()的人能讀格式。通常“不壓縮”平行“無”。
- Wave_read.getparams()
返回一個屬性名稱的列組:(nchannels,?sampwidth,?framerate,?nframes,?comptype,?compname), equivalent to output of the?get*()?methods.
- Wave_read.readframes(n)
讀取第n個幀的聲音采樣信號,以?bytes?格式讀入。
- Wave_read.rewind()
將文件指針倒回音頻流的開頭。
以下兩種方法是為了與aifc模塊兼容而定義的,不做任何有趣的事情。
- Wave_read.getmarkers()
Returns?None.
- Wave_read.getmark(id)
Raise an error.
The following two methods define a term “position” which is compatible between them, and is otherwise implementation dependent.
Wave_read.setpos(pos)
Set the file pointer to the specified position.
Wave_read.tell()
Return current file pointer position
3 存儲 Wave_write Objects?
????????對于可查找的輸出流,wave頭將自動更新,以反映實際寫入的幀數(shù)。對于不可見流,寫入第一幀數(shù)據(jù)時,nframes值必須準(zhǔn)確。通過調(diào)用setnframes()或setparams(),在調(diào)用close()之前寫入的幀數(shù),然后使用writeframesraw()寫入幀數(shù)據(jù),或者通過調(diào)用writeframes()寫入所有要寫入的幀數(shù)據(jù),可以獲得準(zhǔn)確的nframes值。在后一種情況下,writeframes()將計算數(shù)據(jù)中的幀數(shù),并在寫入幀數(shù)據(jù)之前相應(yīng)地設(shè)置nframes。
- Wave_write.close()
確保nframes是正確的,如果文件是由wave打開的,關(guān)閉該文件。此方法在對象集合中調(diào)用。如果輸出流不可查找且nframes與實際寫入的幀數(shù)不匹配,則會引發(fā)異常。
- Wave_write.setnchannels(n)
設(shè)置通道
- Wave_write.setsampwidth(n)
設(shè)置采樣寬度?n?bytes.
- Wave_write.setframerate(n)
設(shè)置幀的速率.?在版本3.2中更改:此方法的非整數(shù)輸入被四舍五入到最接近的整數(shù)。
- Wave_write.setnframes(n)
將幀數(shù)設(shè)置為n。如果實際寫入的幀數(shù)不同,這將在以后更改(如果輸出流不可查找,則此更新嘗試將引發(fā)錯誤)。
- Wave_write.setcomptype(type,?name)
設(shè)置壓縮類型和描述。目前,僅支持壓縮類型NONE,這意味著不支持壓縮。
- Wave_write.setparams(tuple)
獲取屬性的元組,元組應(yīng)該是(nchannels、sampwidth、framerate、nframes、comptype、compname),其值對set*()方法有效。設(shè)置所有參數(shù)。
- Wave_write.tell()
返回文件中的當(dāng)前位置,與Wave_read的免責(zé)聲明相同。告訴()并揮手閱讀。setpos()方法。
- Wave_write.writeframesraw(data)
編寫音頻幀,但不更正nframes。在版本3.4中更改:現(xiàn)在接受任何類似對象的字節(jié)。
- Wave_write.writeframes(data)
編寫音頻幀并確保nframes是正確的。如果輸出流不可查找,并且寫入數(shù)據(jù)后寫入的幀總數(shù)與之前為nframes設(shè)置的值不匹配,則會引發(fā)錯誤。
Changed in version 3.4:?Any?bytes-like object?is now accepted.
4 signal.chirp函數(shù)
函數(shù)說明:
Frequency-swept名叫余弦信號發(fā)生器。
在下文中,‘Hz’應(yīng)解釋為“每單位循環(huán)數(shù)”;此處不要求單位為一秒。重要的區(qū)別是旋轉(zhuǎn)單位是周期,而不是弧度。同樣,t可以表示空間而不是時間。
函數(shù)格式:
scipy.signal.chirp(t, f0, t1, f1, method='linear', phi=0, vertex_zero=True)參數(shù):
t:array_like??
評估波形的時間。
f0:float
在時間t = 0處的頻率(例如Hz)。
t1:float
指定f1的時間。
f1:float
在時間t1處的波形頻率(例如Hz)。
method:{‘linear’, ‘quadratic’, ‘logarithmic’, ‘hyperbolic’}, 可選參數(shù)
頻率掃描的種類。如果未給出,則假定為線性。有關(guān)更多詳細(xì)信息,請參見下面的注釋。
phi:float, 可選參數(shù)
相位偏移,以度為單位。默認(rèn)值為0。
vertex_zero:bool, 可選參數(shù)
僅當(dāng)方法為‘quadratic’時才使用此參數(shù)。它確定拋物線的頂點(即頻率的圖表)是在t = 0還是在t = t1。
返回值:
y:ndarray
包含在t處以請求的time-varying頻率評估的信號的numpy數(shù)組。更準(zhǔn)確地說,函數(shù)返回cos(phase?+?(pi/180)*phi)其中相是整數(shù)(從0到t的)2*pi*f(t)。f(t)在下面定義。
5、Python代碼
#!usr/bin/env python #code=utf-8from tkinter import * import wave import numpy as np import scipy.signal as signal import matplotlib.pyplot as plt import syssys.setrecursionlimit(1000000)#define the params of wave channels = 1 sampwidth = 2 framerate = 9600 file_name = 'sweep.wav' frequency_begin = 1 frequency_end = 100 #define the time of wave time = 1def Generate_Wav():#generate the time bart = np.arange(0,time,1.0/framerate)#generate the chirp signal from 300 to 3300Hzwave_data = signal.chirp(t, frequency_begin, time, frequency_end, method = 'linear')*1000#cast to the type of shortwave_data = wave_data.astype(np.short)#open a wav documentf = wave.open(file_name,"wb")#set wav paramsf.setnchannels(channels)f.setsampwidth(sampwidth)f.setframerate(framerate)#turn the data to stringf.writeframes(wave_data.tobytes())f.close()def my_button(root,label_text,button_text,button_func):'''''function of creat label and button'''#label detailslabel = Label(root)label['text'] = label_textlabel.pack()#label detailsbutton = Button(root)button['text'] = button_textbutton['command'] = button_funcbutton.pack()def read_wave_data(file_path):#open a wave file, and return a Wave_read objectf = wave.open(file_path,"rb")#read the wave's format infomation,and return a tupleparams = f.getparams()#get the infonchannels, sampwidth, framerate, nframes = params[:4]#Reads and returns nframes of audio, as a string of bytes.str_data = f.readframes(nframes)#close the streamf.close()#turn the wave's data to arraywave_data = np.frombuffer(str_data, dtype = np.short)time = np.arange(0, nframes) * (1.0/framerate)return wave_data, timedef Plot_Wav():wave_data, time = read_wave_data(file_name)plt.plot(time, wave_data)plt.grid(True)plt.show()def main():root = Tk()my_button(root, 'Generate a sweep wav', 'Generate', Generate_Wav)my_button(root, 'Plot the wav', 'Plot', Plot_Wav)root.mainloop()if __name__ == "__main__":main()總結(jié)
以上是生活随笔為你收集整理的Python知识:生成声波并显示的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 机器学习系列3:能量函数分析
- 下一篇: Python知识: scipy sign