梅爾倒譜系數(shù)(MFCC)實(shí)現(xiàn)
本文實(shí)例為大家分享了梅爾倒譜系數(shù)實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下
""" @author: zoutai @file: mymfcc.py @time: 2018/03/26 @description: """ from matplotlib.colors import BoundaryNorm import librosa import librosa.display import numpy import scipy.io.wavfile from scipy.fftpack import dct import matplotlib.pyplot as plt import numpy as np # 第一步-讀取音頻,畫(huà)出時(shí)域圖(采樣率-幅度) sample_rate, signal = scipy.io.wavfile.read('OSR_us_000_0010_8k.wav') # File assumed to be in the same directory signal = signal[0:int(3.5 * sample_rate)] # plot the wave time = np.arange(0,len(signal))*(1.0 / sample_rate) # plt.plot(time,signal) plt.xlabel("Time(s)") plt.ylabel("Amplitude") plt.title("Signal in the Time Domain ") plt.grid('on')#標(biāo)尺,on:有,off:無(wú)。 # 第二步-預(yù)加重 # 消除高頻信號(hào)。因?yàn)楦哳l信號(hào)往往都是相似的, # 通過(guò)前后時(shí)間相減,就可以近乎抹去高頻信號(hào),留下低頻信號(hào)。 # 原理:y(t)=x(t)−αx(t−1) pre_emphasis = 0.97 emphasized_signal = numpy.append(signal[0], signal[1:] - pre_emphasis * signal[:-1]) time = np.arange(0,len(emphasized_signal))*(1.0 / sample_rate) # plt.plot(time,emphasized_signal) # plt.xlabel("Time(s)") # plt.ylabel("Amplitude") # plt.title("Signal in the Time Domain after Pre-Emphasis") # plt.grid('on')#標(biāo)尺,on:有,off:無(wú)。 # 第三步、取幀,用幀表示 frame_size = 0.025 # 幀長(zhǎng) frame_stride = 0.01 # 步長(zhǎng) # frame_length-一幀對(duì)應(yīng)的采樣數(shù), frame_step-一個(gè)步長(zhǎng)對(duì)應(yīng)的采樣數(shù) frame_length, frame_step = frame_size * sample_rate, frame_stride * sample_rate # Convert from seconds to samples signal_length = len(emphasized_signal) # 總的采樣數(shù) frame_length = int(round(frame_length)) frame_step = int(round(frame_step)) # 總幀數(shù) num_frames = int(numpy.ceil(float(numpy.abs(signal_length - frame_length)) / frame_step)) # Make sure that we have at least 1 frame pad_signal_length = num_frames * frame_step + frame_length z = numpy.zeros((pad_signal_length - signal_length)) pad_signal = numpy.append(emphasized_signal, z) # Pad Signal to make sure that all frames have equal number of samples without truncating any samples from the original signal # Construct an array by repeating A(200) the number of times given by reps(348). # 這個(gè)寫(xiě)法太妙了。目的:用矩陣來(lái)表示幀的次數(shù),348*200,348-總的幀數(shù),200-每一幀的采樣數(shù) # 第一幀采樣為0、1、2...200;第二幀為80、81、81...280..依次類(lèi)推 indices = numpy.tile(numpy.arange(0, frame_length), (num_frames, 1)) + numpy.tile(numpy.arange(0, num_frames * frame_step, frame_step), (frame_length, 1)).T frames = pad_signal[indices.astype(numpy.int32, copy=False)] # Copy of the array indices # frame:348*200,橫坐標(biāo)348為幀數(shù),即時(shí)間;縱坐標(biāo)200為一幀的200毫秒時(shí)間,內(nèi)部數(shù)值代表信號(hào)幅度 # plt.matshow(frames, cmap='hot') # plt.colorbar() # plt.figure() # plt.pcolormesh(frames) # 第四步、加漢明窗 # 傅里葉變換默認(rèn)操作的時(shí)間段內(nèi)前后端點(diǎn)是連續(xù)的,即整個(gè)時(shí)間段剛好是一個(gè)周期, # 但是,顯示卻不是這樣的。所以,當(dāng)這種情況出現(xiàn)時(shí),仍然采用FFT操作時(shí), # 就會(huì)將單一頻率周期信號(hào)認(rèn)作成多個(gè)不同的頻率信號(hào)的疊加,而不是原始頻率,這樣就差生了頻譜泄漏問(wèn)題 frames *= numpy.hamming(frame_length) # 相乘,和卷積類(lèi)似 # # frames *= 0.54 - 0.46 * numpy.cos((2 * numpy.pi * n) / (frame_length - 1)) # Explicit Implementation ** # plt.pcolormesh(frames) # 第五步-傅里葉變換頻譜和能量譜 # _raw_fft掃窗重疊,將348*200,擴(kuò)展成348*512 NFFT = 512 mag_frames = numpy.absolute(numpy.fft.rfft(frames, NFFT)) # Magnitude of the FFT pow_frames = ((1.0 / NFFT) * ((mag_frames) ** 2)) # Power Spectrum # plt.pcolormesh(mag_frames) # # plt.pcolormesh(pow_frames) # 第六步,F(xiàn)ilter Banks濾波器組 # 公式:m=2595*log10(1+f/700);f=700(10^(m/2595)−1) nfilt = 40 #窗的數(shù)目 low_freq_mel = 0 high_freq_mel = (2595 * numpy.log10(1 + (sample_rate / 2) / 700)) # Convert Hz to Mel mel_points = numpy.linspace(low_freq_mel, high_freq_mel, nfilt + 2) # Equally spaced in Mel scale hz_points = (700 * (10**(mel_points / 2595) - 1)) # Convert Mel to Hz bin = numpy.floor((NFFT + 1) * hz_points / sample_rate) fbank = numpy.zeros((nfilt, int(numpy.floor(NFFT / 2 + 1)))) for m in range(1, nfilt + 1): f_m_minus = int(bin[m - 1]) # left f_m = int(bin[m]) # center f_m_plus = int(bin[m + 1]) # right for k in range(f_m_minus, f_m): fbank[m - 1, k] = (k - bin[m - 1]) / (bin[m] - bin[m - 1]) for k in range(f_m, f_m_plus): fbank[m - 1, k] = (bin[m + 1] - k) / (bin[m + 1] - bin[m]) filter_banks = numpy.dot(pow_frames, fbank.T) filter_banks = numpy.where(filter_banks == 0, numpy.finfo(float).eps, filter_banks) # Numerical Stability filter_banks = 20 * numpy.log10(filter_banks) # dB;348*26 # plt.subplot(111) # plt.pcolormesh(filter_banks.T) # plt.grid('on') # plt.ylabel('Frequency [Hz]') # plt.xlabel('Time [sec]') # plt.show() # # 第七步,梅爾頻譜倒譜系數(shù)-MFCCs num_ceps = 12 #取12個(gè)系數(shù) cep_lifter=22 #倒譜的升個(gè)數(shù)?? mfcc = dct(filter_banks, type=2, axis=1, norm='ortho')[:, 1 : (num_ceps + 1)] # Keep 2-13 (nframes, ncoeff) = mfcc.shape n = numpy.arange(ncoeff) lift = 1 + (cep_lifter / 2) * numpy.sin(numpy.pi * n / cep_lifter) mfcc *= lift #* # plt.pcolormesh(mfcc.T) # plt.ylabel('Frequency [Hz]') # plt.xlabel('Time [sec]') # 第八步,均值化優(yōu)化 # to balance the spectrum and improve the Signal-to-Noise (SNR), we can simply subtract the mean of each coefficient from all frames. filter_banks -= (numpy.mean(filter_banks, axis=0) + 1e-8) mfcc -= (numpy.mean(mfcc, axis=0) + 1e-8) # plt.subplot(111) # plt.pcolormesh(mfcc.T) # plt.ylabel('Frequency [Hz]') # plt.xlabel('Time [sec]') # plt.show() # 直接頻譜分析 # plot the wave # plt.specgram(signal,Fs = sample_rate, scale_by_freq = True, sides = 'default') # plt.ylabel('Frequency(Hz)') # plt.xlabel('Time(s)') # plt.show() plt.figure(figsize=(10, 4)) mfccs = librosa.feature.melspectrogram(signal,sr=8000,n_fft=512,n_mels=40) librosa.display.specshow(mfccs, x_axis='time') plt.colorbar() plt.title('MFCC') plt.tight_layout() plt.show()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Python設(shè)計(jì)模式編程中觀察者模式與策略模式的運(yùn)用
這篇文章主要介紹了Python設(shè)計(jì)模式編程中觀察者模式與策略模式的運(yùn)用,觀察者模式和策略模式都可以歸類(lèi)為結(jié)構(gòu)型的設(shè)計(jì)模式,需要的朋友可以參考下2016-03-03python網(wǎng)絡(luò)編程之多線(xiàn)程同時(shí)接受和發(fā)送
這篇文章主要為大家詳細(xì)介紹了python網(wǎng)絡(luò)編程之多線(xiàn)程同時(shí)接受和發(fā)送,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09Python中__new__和__init__的區(qū)別與聯(lián)系
這篇文章主要介紹了Python中__new__和__init__的區(qū)別與聯(lián)系,需要的朋友可以參考下2021-05-05基于python的圖片修復(fù)程序(實(shí)現(xiàn)水印去除)
這篇文章主要給大家介紹了關(guān)于python圖片修復(fù)程序的相關(guān)資料,可以用于實(shí)現(xiàn)圖片中水印去除,主要利用的是OpenCV這個(gè)框架實(shí)現(xiàn)的,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧2018-06-06python實(shí)現(xiàn)將漢字保存成文本的方法
今天小編就為大家分享一篇python實(shí)現(xiàn)將漢字保存成文本的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-11-11Opencv圖像添加椒鹽噪聲、高斯濾波去除噪聲原理以及手寫(xiě)Python代碼實(shí)現(xiàn)方法
椒鹽噪聲的特征非常明顯,為圖像上有黑色和白色的點(diǎn),下面這篇文章主要給大家介紹了關(guān)于Opencv圖像添加椒鹽噪聲、高斯濾波去除噪聲原理以及手寫(xiě)Python代碼實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09詳解pytest+Allure搭建方法以及生成報(bào)告常用操作
本文主要介紹了詳解pytest+Allure搭建方法以及生成報(bào)告常用操作,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09對(duì)Pytorch 中的contiguous理解說(shuō)明
這篇文章主要介紹了對(duì)Pytorch 中的contiguous理解說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03Python動(dòng)態(tài)創(chuàng)建類(lèi)實(shí)例詳解
這篇文章主要為大家介紹了Python動(dòng)態(tài)創(chuàng)建類(lèi)實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12