python 语音数据进行分帧

# -*- coding: utf-8 -*-  
import numpy as np
import wave
import os
import librosa
from scipy.io import wavfile

def enframe(signal, nw, inc, winc):
    '''
    将语音信号转化为帧
    :param signal: 原始信号
    :param nw: 每一帧的长度,自定义的参数
    :param inc: 相邻帧的间隔,自定义的参数
    :return:
    '''
    signal_length = len(signal)  # 信号总长度
    if signal_length <= nw:      # 如果信号长度小于一帧的长度,则帧数定义为1
        nf = 1                    # nf表示帧数量
    else:
        nf = int(np.ceil((1.0 * signal_length - nw + inc) / inc))  # 处理后,所有帧的数量,不要最后一帧
    pad_length = int((nf - 1) * inc + nw)                          # 所有帧加起来总的平铺后的长度
    pad_signal = np.pad(signal, (0, pad_length - signal_length), 'constant')     # 0填充最后一帧
    indices = np.tile(np.arange(0, nw), (nf, 1)) + np.tile(np.arange(0, nf*inc, inc), (nw, 1)).T  # 每帧的索引
    indices = np.array(indices, dtype=np.int32)                    
    frames = pad_signal[indices]                    # 得到帧信号, 用索引拿数据
    if winc is not None:
        win=np.tile(winfunc,(nf,1))                    # winc为窗函数
    	return frames*win                                 # 返回加窗的信号
    return frames

a = np.arange(0,100)
print(a)
nw = 10
inc = 2
frames = enframe(a, nw, inc)
print("-" * 10)
print(frames)

# 结果
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
 48 49]
----------
[[ 0  1  2  3  4  5  6  7  8  9]
 [ 2  3  4  5  6  7  8  9 10 11]
 [ 4  5  6  7  8  9 10 11 12 13]
 [ 6  7  8  9 10 11 12 13 14 15]
 [ 8  9 10 11 12 13 14 15 16 17]
 [10 11 12 13 14 15 16 17 18 19]
 [12 13 14 15 16 17 18 19 20 21]
 [14 15 16 17 18 19 20 21 22 23]
 [16 17 18 19 20 21 22 23 24 25]
 [18 19 20 21 22 23 24 25 26 27]
 [20 21 22 23 24 25 26 27 28 29]
 [22 23 24 25 26 27 28 29 30 31]
 [24 25 26 27 28 29 30 31 32 33]
 [26 27 28 29 30 31 32 33 34 35]
 [28 29 30 31 32 33 34 35 36 37]
 [30 31 32 33 34 35 36 37 38 39]
 [32 33 34 35 36 37 38 39 40 41]
 [34 35 36 37 38 39 40 41 42 43]
 [36 37 38 39 40 41 42 43 44 45]
 [38 39 40 41 42 43 44 45 46 47]
 [40 41 42 43 44 45 46 47 48 49]]

你可能感兴趣的:(python)