日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python知识:生成声波并显示

發布時間:2025/3/21 python 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python知识:生成声波并显示 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

scipy.signal.chirp — SciPy v1.8.0 Manual

1 說明?

? ? ? ? 如何自己產生一個聲波? 采用signal.chirp產生聲波,對聲波讀、存、參數設定,等聲波操作,都依靠一個叫Wav的對象完成。

2 讀文件Wave_read Objects

Wave_read Objects對象是由open()返回,Wave_read Objects內部函數是:

  • Wave_read.close()

如果流被wave打開,請關閉它,并使實例不可用。這在對象集合中自動調用。

  • Wave_read.getnchannels()

返回音頻通道數(1個用于單聲道 mono,2個用于立體聲 stereo)。

  • Wave_read.getsampwidth()

返回以字節為單位的樣本寬度。

  • Wave_read.getframerate()

返回采樣頻率。

  • Wave_read.getnframes()

返回音頻幀數。

  • 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頭將自動更新,以反映實際寫入的幀數。對于不可見流,寫入第一幀數據時,nframes值必須準確。通過調用setnframes()或setparams(),在調用close()之前寫入的幀數,然后使用writeframesraw()寫入幀數據,或者通過調用writeframes()寫入所有要寫入的幀數據,可以獲得準確的nframes值。在后一種情況下,writeframes()將計算數據中的幀數,并在寫入幀數據之前相應地設置nframes。

  • Wave_write.close()

確保nframes是正確的,如果文件是由wave打開的,關閉該文件。此方法在對象集合中調用。如果輸出流不可查找且nframes與實際寫入的幀數不匹配,則會引發異常。

  • Wave_write.setnchannels(n)

設置通道

  • Wave_write.setsampwidth(n)

設置采樣寬度?n?bytes.

  • Wave_write.setframerate(n)

設置幀的速率.?在版本3.2中更改:此方法的非整數輸入被四舍五入到最接近的整數。

  • Wave_write.setnframes(n)

將幀數設置為n。如果實際寫入的幀數不同,這將在以后更改(如果輸出流不可查找,則此更新嘗試將引發錯誤)。

  • Wave_write.setcomptype(type,?name)

設置壓縮類型和描述。目前,僅支持壓縮類型NONE,這意味著不支持壓縮。

  • Wave_write.setparams(tuple)

獲取屬性的元組,元組應該是(nchannels、sampwidth、framerate、nframes、comptype、compname),其值對set*()方法有效。設置所有參數。

  • Wave_write.tell()

返回文件中的當前位置,與Wave_read的免責聲明相同。告訴()并揮手閱讀。setpos()方法。

  • Wave_write.writeframesraw(data)

編寫音頻幀,但不更正nframes。在版本3.4中更改:現在接受任何類似對象的字節。

  • Wave_write.writeframes(data)

編寫音頻幀并確保nframes是正確的。如果輸出流不可查找,并且寫入數據后寫入的幀總數與之前為nframes設置的值不匹配,則會引發錯誤。

Changed in version 3.4:?Any?bytes-like object?is now accepted.

4 signal.chirp函數

函數說明:

Frequency-swept名叫余弦信號發生器。

在下文中,‘Hz’應解釋為“每單位循環數”;此處不要求單位為一秒。重要的區別是旋轉單位是周期,而不是弧度。同樣,t可以表示空間而不是時間。

函數格式:

scipy.signal.chirp(t, f0, t1, f1, method='linear', phi=0, vertex_zero=True)

參數:

tarray_like??

評估波形的時間。

f0float

在時間t = 0處的頻率(例如Hz)。

t1float

指定f1的時間。

f1float

在時間t1處的波形頻率(例如Hz)。

method{‘linear’, ‘quadratic’, ‘logarithmic’, ‘hyperbolic’}, 可選參數

頻率掃描的種類。如果未給出,則假定為線性。有關更多詳細信息,請參見下面的注釋。

phifloat, 可選參數

相位偏移,以度為單位。默認值為0。

vertex_zerobool, 可選參數

僅當方法為‘quadratic’時才使用此參數。它確定拋物線的頂點(即頻率的圖表)是在t = 0還是在t = t1。

返回值:

yndarray

包含在t處以請求的time-varying頻率評估的信號的numpy數組。更準確地說,函數返回cos(phase?+?(pi/180)*phi)其中相是整數(從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()

總結

以上是生活随笔為你收集整理的Python知识:生成声波并显示的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。