1、下載與安裝scipy
scipy下載連結: http://www.scipy.org/Download#head-0dfc04e10313d2e70988c6cb3bef7a9e09860c8f
同時可以下載說明文檔連結http://docs.scipy.org/doc/
2、wav檔案寫操作
3、signal.chirp函數使用說明
4、點擊按鈕實現訊號產生,點擊按鈕實現訊號顯示
5、Python代碼
#!usr/bin/env python#code=utf-8from Tkinter import *import waveimport numpy as npimport scipy.signal as signalimport matplotlib.pyplot as pltimport syssys.setrecursionlimit(1000000)#define the params of wavechannels = 1sampwidth = 2framerate = 9600file_name = 'sweep.wav'frequency_begin = 1frequency_end = 100#define the time of wavetime = 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.tostring())f.close()def my_button(root,label_text,button_text,button_func): '''''function of creat label and button''' #label details label = Label(root) label['text'] = label_text label.pack() #label details button = Button(root) button['text'] = button_text button['command'] = button_func button.pack()def read_wave_data(file_path): #open a wave file, and return a Wave_read object f = wave.open(file_path,"rb") #read the wave's format infomation,and return a tuple params = f.getparams() #get the info nchannels, sampwidth, framerate, nframes = params[:4] #Reads and returns nframes of audio, as a string of bytes. str_data = f.readframes(nframes) #close the stream f.close() #turn the wave's data to array wave_data = np.fromstring(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()