本文目錄一覽:
- 1、PYTHON語言如何取到聲音的頻率(其他語言也可行)
- 2、急!!!!如何通過python製作一個簡單的錄音機,錄製自己的聲音採用8k採樣,16位量化編碼,觀察其數值?
- 3、怎麼使用Python中Pandas庫Resample,實現重採樣,完成線性插值
PYTHON語言如何取到聲音的頻率(其他語言也可行)
先得到時域信號,然後做傅立葉變換,得到頻譜。
感覺題主可能對python比較熟悉?那就別換語言了。稍微百度谷歌以下肯定能找到python的傅立葉變換的庫。
急!!!!如何通過python製作一個簡單的錄音機,錄製自己的聲音採用8k採樣,16位量化編碼,觀察其數值?
#我可以幫你寫一段代碼,能夠錄音形成wav文件,不過要分析錄音文件的波形,你可以另外找#工具,比如cooledit,也很方便。
from sys import byteorder
from array import array
from struct import pack
import pyaudio
import wave
THRESHOLD = 500
CHUNK_SIZE = 1024
FORMAT = pyaudio.paInt16
RATE = 44100
def is_silent(snd_data):
“Returns ‘True’ if below the ‘silent’ threshold”
return max(snd_data) THRESHOLD
def normalize(snd_data):
“Average the volume out”
MAXIMUM = 16384
times = float(MAXIMUM)/max(abs(i) for i in snd_data)
r = array(‘h’)
for i in snd_data:
r.append(int(i*times))
return r
def trim(snd_data):
“Trim the blank spots at the start and end”
def _trim(snd_data):
snd_started = False
r = array(‘h’)
for i in snd_data:
if not snd_started and abs(i)THRESHOLD:
snd_started = True
r.append(i)
elif snd_started:
r.append(i)
return r
# Trim to the left
snd_data = _trim(snd_data)
# Trim to the right
snd_data.reverse()
snd_data = _trim(snd_data)
snd_data.reverse()
return snd_data
def add_silence(snd_data, seconds):
“Add silence to the start and end of ‘snd_data’ of length ‘seconds’ (float)”
r = array(‘h’, [0 for i in xrange(int(seconds*RATE))])
r.extend(snd_data)
r.extend([0 for i in xrange(int(seconds*RATE))])
return r
def record():
“””
Record a word or words from the microphone and
return the data as an array of signed shorts.
Normalizes the audio, trims silence from the
start and end, and pads with 0.5 seconds of
blank sound to make sure VLC et al can play
it without getting chopped off.
“””
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT, channels=1, rate=RATE,
input=True, output=True,
frames_per_buffer=CHUNK_SIZE)
num_silent = 0
snd_started = False
r = array(‘h’)
while 1:
# little endian, signed short
snd_data = array(‘h’, stream.read(CHUNK_SIZE))
if byteorder == ‘big’:
snd_data.byteswap()
r.extend(snd_data)
silent = is_silent(snd_data)
if silent and snd_started:
num_silent += 1
elif not silent and not snd_started:
snd_started = True
if snd_started and num_silent 30:
break
sample_width = p.get_sample_size(FORMAT)
stream.stop_stream()
stream.close()
p.terminate()
r = normalize(r)
r = trim(r)
r = add_silence(r, 0.5)
return sample_width, r
def record_to_file(path):
“Records from the microphone and outputs the resulting data to ‘path'”
sample_width, data = record()
data = pack(” + (‘h’*len(data)), *data)
wf = wave.open(path, ‘wb’)
wf.setnchannels(1)
wf.setsampwidth(sample_width)
wf.setframerate(RATE)
wf.writeframes(data)
wf.close()
if __name__ == ‘__main__’:
print(“please speak a word into the microphone”)
record_to_file(‘demo.wav’)
print(“done – result written to demo.wav”)
怎麼使用Python中Pandas庫Resample,實現重採樣,完成線性插值
#python中的pandas庫主要有DataFrame和Series類(面向對象的的語言更願意叫類) DataFrame也就是
#數據框(主要是借鑒R裏面的data.frame),Series也就是序列 ,pandas底層是c寫的 性能很棒,有大神
#做過測試 處理億級別的數據沒問題,起性能可以跟同等配置的sas媲美
#DataFrame索引 df.loc是標籤選取操作,df.iloc是位置切片操作
print(df[[‘row_names’,’Rape’]])
df[‘行標籤’]
df.loc[行標籤,列標籤]
print(df.loc[0:2,[‘Rape’,’Murder’]])
df.iloc[行位置,列位置]
df.iloc[1,1]#選取第二行,第二列的值,返回的為單個值
df.iloc[0,2],:]#選取第一行及第三行的數據
df.iloc[0:2,:]#選取第一行到第三行(不包含)的數據
df.iloc[:,1]#選取所有記錄的第一列的值,返回的為一個Series
df.iloc[1,:]#選取第一行數據,返回的為一個Series
print(df.ix[1,1]) # 更廣義的切片方式是使用.ix,它自動根據你給到的索引類型判斷是使用位置還是標籤進行切片
print(df.ix[0:2])
#DataFrame根據條件選取子集 類似於sas裏面if、where ,R裏面的subset之類的函數
df[df.Murder13]
df[(df.Murder10)(df.Rape30)]
df[df.sex==u’男’]
#重命名 相當於sas裏面的rename R軟件中reshape包的中的rename
df.rename(columns={‘A’:’A_rename’})
df.rename(index={1:’other’})
#刪除列 相當於sas中的drop R軟件中的test[‘col’]-null
df.drop([‘a’,’b’],axis=1) or del df[[‘a’,’b’]]
#排序 相當於sas裏面的sort R軟件裏面的df[order(x),]
df.sort(columns=’C’) #行排序 y軸上
df.sort(axis=1) #各個列之間位置排序 x軸上
#數據描述 相當於sas中proc menas R軟件裏面的summary
df.describe()
#生成新的一列 跟R裏面有點類似
df[‘new_columns’]=df[‘columns’]
df.insert(1,’new_columns’,df[‘B’]) #效率最高
df.join(Series(df[‘columns’],name=’new_columns’))
#列上面的追加 相當於sas中的append R裏面cbind()
df.append(df1,ignore_index=True)
pd.concat([df,df1],ignore_index=True)
#最經典的join 跟sas和R裏面的merge類似 跟sql裏面的各種join對照
merge()
#刪除重行 跟sas裏面nodukey R裏面的which(!duplicated(df[])類似
df.drop_duplicated()
#獲取最大值 最小值的位置 有點類似矩陣裏面的方法
df.idxmin(axis=0 ) df.idxmax(axis=1) 0和1有什麼不同 自己摸索去
#讀取外部數據跟sas的proc import R裏面的read.csv等類似
read_excel() read_csv() read_hdf5() 等
與之相反的是df.to_excel() df.to_ecv()
#缺失值處理 個人覺得pandas中缺失值處理比sas和R方便多了
df.fillna(9999) #用9999填充
#鏈接數據庫 不多說 pandas裏面主要用 MySQLdb
import MySQLdb
conn=MySQLdb.connect(host=”localhost”,user=”root”,passwd=””,db=”mysql”,use_unicode=True,charset=”utf8″)
read_sql() #很經典
#寫數據進數據庫
df.to_sql(‘hbase_visit’,con, flavor=”mysql”, if_exists=’replace’, index=False)
#groupby 跟sas裏面的中的by R軟件中dplyr包中的group_by sql裏面的group by功能是一樣的 這裡不多說
#求啞變量
dumiper=pd.get_dummies(df[‘key’])
df[‘key’].join(dumpier)
#透視表 和交叉表 跟sas裏面的proc freq步類似 R裏面的aggrate和cast函數類似
pd.pivot_table()
pd.crosstab()
#聚合函數經常跟group by一起組合用
df.groupby(‘sex’).agg({‘height’:[‘mean’,’sum’],’weight’:[‘count’,’min’]})
#數據查詢過濾
test.query(“0.2
將STK_ID中的值過濾出來
stk_list = [‘600809′,’600141′,’600329’]中的全部記錄過濾出來,命令是:rpt[rpt[‘STK_ID’].isin(stk_list)].
將dataframe中,某列進行清洗的命令
刪除換行符:misc[‘product_desc’] = misc[‘product_desc’].str.replace(‘\n’, ”)
刪除字符串前後空格:df[“Make”] = df[“Make”].map(str.strip)
如果用模糊匹配的話,命令是:
rpt[rpt[‘STK_ID’].str.contains(r’^600[0-9]{3}$’)]
對dataframe中元素,進行類型轉換
df[‘2nd’] = df[‘2nd’].str.replace(‘,’,”).astype(int) df[‘CTR’] = df[‘CTR’].str.replace(‘%’,”).astype(np.float64)
#時間變換 主要依賴於datemie 和time兩個包
#其他的一些技巧
df2[df2[‘A’].map(lambda x:x.startswith(’61’))] #篩選出以61開頭的數據
df2[“Author”].str.replace(“.+”, “”).head() #replace(“.+”, “”)表示將字符串中以」」開頭;以」」結束的任意子串替換為空字符串
commits = df2[“Name”].head(15)
print commits.unique(), len(commits.unique()) #獲的NAME的不同個數,類似於sql裏面count(distinct name)
#pandas中最核心 最經典的函數apply map applymap
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/230305.html