博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mfcc的特征提取python 代码实现和解析
阅读量:4884 次
发布时间:2019-06-11

本文共 3053 字,大约阅读时间需要 10 分钟。

1 #!/usr/bin/python 2 # -*- coding: UTF-8 -*- 3  4 import numpy 5 import scipy.io.wavfile 6 from matplotlib import pyplot as plt 7 from scipy.fftpack import dct 8  9 sample_rate,signal=scipy.io.wavfile.read('stop.wav')10 11 print(sample_rate,len(signal))12 #读取前3.5s 的数据13 signal=signal[0:int(3.5*sample_rate)]14 print(signal)15 16 17 18 #预先处理19 pre_emphasis = 0.9720 emphasized_signal = numpy.append(signal[0], signal[1:] - pre_emphasis * signal[:-1])21 22 23 frame_size=0.02524 frame_stride=0.125 frame_length,frame_step=frame_size*sample_rate,frame_stride*sample_rate26 signal_length=len(emphasized_signal)27 frame_length=int(round(frame_length))28 frame_step=int(round(frame_step))29 num_frames=int(numpy.ceil(float(numpy.abs(signal_length-frame_length))/frame_step))30 31 32 pad_signal_length=num_frames*frame_step+frame_length33 z=numpy.zeros((pad_signal_length-signal_length))34 pad_signal=numpy.append(emphasized_signal,z)35 36 37 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)).T38 39 frames = pad_signal[numpy.mat(indices).astype(numpy.int32, copy=False)]40 41 #加上汉明窗42 frames *= numpy.hamming(frame_length)43 # frames *= 0.54 - 0.46 * numpy.cos((2 * numpy.pi * n) / (frame_length - 1))  # Explicit Implementation **44 45 #傅立叶变换和功率谱46 NFFT = 51247 mag_frames = numpy.absolute(numpy.fft.rfft(frames, NFFT))  # Magnitude of the FFT48 #print(mag_frames.shape)49 pow_frames = ((1.0 / NFFT) * ((mag_frames) ** 2))  # Power Spectrum50 51 52 53 low_freq_mel = 054 #将频率转换为Mel55 nfilt = 4056 high_freq_mel = (2595 * numpy.log10(1 + (sample_rate / 2) / 700))57 mel_points = numpy.linspace(low_freq_mel, high_freq_mel, nfilt + 2)  # Equally spaced in Mel scale58 hz_points = (700 * (10**(mel_points / 2595) - 1))  # Convert Mel to Hz59 60 bin = numpy.floor((NFFT + 1) * hz_points / sample_rate)61 62 fbank = numpy.zeros((nfilt, int(numpy.floor(NFFT / 2 + 1))))63 64 for m in range(1, nfilt + 1):65     f_m_minus = int(bin[m - 1])   # left66     f_m = int(bin[m])             # center67     f_m_plus = int(bin[m + 1])    # right68     for k in range(f_m_minus, f_m):69         fbank[m - 1, k] = (k - bin[m - 1]) / (bin[m] - bin[m - 1])70     for k in range(f_m, f_m_plus):71         fbank[m - 1, k] = (bin[m + 1] - k) / (bin[m + 1] - bin[m])72 filter_banks = numpy.dot(pow_frames, fbank.T)73 filter_banks = numpy.where(filter_banks == 0, numpy.finfo(float).eps, filter_banks)  # Numerical Stability74 filter_banks = 20 * numpy.log10(filter_banks)  # dB75 76 num_ceps = 1277 mfcc = dct(filter_banks, type=2, axis=1, norm='ortho')[:, 1 : (num_ceps + 1)]78 (nframes, ncoeff) = mfcc.shape79 80 n = numpy.arange(ncoeff)81 cep_lifter =2282 lift = 1 + (cep_lifter / 2) * numpy.sin(numpy.pi * n / cep_lifter)83 mfcc *= lift  #*84 85 #filter_banks -= (numpy.mean(filter_banks, axis=0) + 1e-8)86 mfcc -= (numpy.mean(mfcc, axis=0) + 1e-8)87 88 print(mfcc.shape)89 plt.plot(filter_banks)90 91 plt.show()

 

测试结果:

 

转载于:https://www.cnblogs.com/dylancao/p/9790707.html

你可能感兴趣的文章
2017.11.18 手把手教你学51单片机-点亮LED
查看>>
xml的创建与解析
查看>>
grep不区分大小写查找字符串方法
查看>>
linux系统灵活运用灯[android课程3]
查看>>
Android 通用Dialog中设置RecyclerView
查看>>
利用 Android Studio 和 Gradle 打包多版本APK
查看>>
Android 自定义标题栏
查看>>
Android 如何把一个 RelativeLayout或ImageView背景设为透明
查看>>
tomcat优化方向
查看>>
http
查看>>
8-1-组队赛
查看>>
codility: CountTriangles
查看>>
赛斯说
查看>>
python 中的pipe
查看>>
(SQL Analyzer services)定义链接维度
查看>>
squid
查看>>
系统开发管理、架构与设计步步谈随笔索引
查看>>
Java的时间空间复杂度详解
查看>>
有效防止SQL注入漏洞
查看>>
Linux chown命令
查看>>