在语音分析,合成,转换中,第一步往往是提取语音特征参数。
利用机器学习方法进行上述语音任务,常用到梅尔频谱。
本文介绍从音频文件提取梅尔频谱,和从梅尔频谱变成音频波形。

从音频波形提取Mel频谱:

对音频信号预加重、分帧和加窗
对每帧信号进行短时傅立叶变换STFT,得到短时幅度谱
短时幅度谱通过Mel滤波器组得到Mel频谱
从Mel频谱重建音频波形

Mel频谱转换成幅度谱
griffin_lim声码器算法重建波形
去加重
声码器有很多种,比如world,straight等,但是griffin_lim是特殊的,它不需要相位信息就可以重频谱重建波形,实际上它根据帧之间的关系估计相位信息。和成的音频质量也较高,代码也比较简单。
音频波形 到 mel-spectrogram

sr = 24000 # Sample rate.
n_fft = 2048 # fft points (samples)
frame_shift = 0.0125 # seconds
frame_length = 0.05 # seconds
hop_length = int(sr*frame_shift) # samples.
win_length = int(sr*frame_length) # samples.
n_mels = 512 # Number of Mel banks to generate
power = 1.2 # Exponent for amplifying the predicted magnitude
n_iter = 100 # Number of inversion iterations
preemphasis = .97 # or None
max_db = 100
ref_db = 20
top_db = 15
1
2
3
4
5
6
7
8
9
10
11
12
13
def get_spectrograms(fpath):
'''Returns normalized log(melspectrogram) and log(magnitude) from `sound_file`.
Args:
sound_file: A string. The full path of a sound file.

Returns:
mel: A 2d array of shape (T, n_mels) <- Transposed
mag: A 2d array of shape (T, 1+n_fft/2) <- Transposed
'''
# Loading sound file
y, sr = librosa.load(fpath, sr=sr)

# Trimming
y, _ = librosa.effects.trim(y, top_db=top_db)

# Preemphasis
y = np.append(y[0], y[1:] - preemphasis * y[:-1])

# stft
linear = librosa.stft(y=y,
n_fft=n_fft,
hop_length=hop_length,
win_length=win_length)

# magnitude spectrogram
mag = np.abs(linear) # (1+n_fft//2, T)

# mel spectrogram
mel_basis = librosa.filters.mel(sr, n_fft, n_mels) # (n_mels, 1+n_fft//2)
mel = np.dot(mel_basis, mag) # (n_mels, t)

# to decibel
mel = 20 * np.log10(np.maximum(1e-5, mel))
mag = 20 * np.log10(np.maximum(1e-5, mag))

# normalize
mel = np.clip((mel - ref_db + max_db) / max_db, 1e-8, 1)
mag = np.clip((mag - ref_db + max_db) / max_db, 1e-8, 1)

# Transpose
mel = mel.T.astype(np.float32) # (T, n_mels)
mag = mag.T.astype(np.float32) # (T, 1+n_fft//2)

return mel, mag

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
mel-spectrogram 到 音频波形

def melspectrogram2wav(mel):
'''# Generate wave file from spectrogram'''
# transpose
mel = mel.T

# de-noramlize
mel = (np.clip(mel, 0, 1) * max_db) - max_db + ref_db

# to amplitude
mel = np.power(10.0, mel * 0.05)
m = _mel_to_linear_matrix(sr, n_fft, n_mels)
mag = np.dot(m, mel)

# wav reconstruction
wav = griffin_lim(mag)

# de-preemphasis
wav = signal.lfilter([1], [1, -preemphasis], wav)

# trim
wav, _ = librosa.effects.trim(wav)

return wav.astype(np.float32)

def spectrogram2wav(mag):
'''# Generate wave file from spectrogram'''
# transpose
mag = mag.T

# de-noramlize
mag = (np.clip(mag, 0, 1) * max_db) - max_db + ref_db

# to amplitude
mag = np.power(10.0, mag * 0.05)

# wav reconstruction
wav = griffin_lim(mag)

# de-preemphasis
wav = signal.lfilter([1], [1, -preemphasis], wav)

# trim
wav, _ = librosa.effects.trim(wav)

return wav.astype(np.float32)
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
几个辅助函数:

def _mel_to_linear_matrix(sr, n_fft, n_mels):
m = librosa.filters.mel(sr, n_fft, n_mels)
m_t = np.transpose(m)
p = np.matmul(m, m_t)
d = [1.0 / x if np.abs(x) > 1.0e-8 else x for x in np.sum(p, axis=0)]
return np.matmul(m_t, np.diag(d))

def griffin_lim(spectrogram):
'''Applies Griffin-Lim's raw.
'''
X_best = copy.deepcopy(spectrogram)
for i in range(n_iter):
X_t = invert_spectrogram(X_best)
est = librosa.stft(X_t, n_fft, hop_length, win_length=win_length)
phase = est / np.maximum(1e-8, np.abs(est))
X_best = spectrogram * phase
X_t = invert_spectrogram(X_best)
y = np.real(X_t)

return y

def invert_spectrogram(spectrogram):
'''
spectrogram: [f, t]
'''
return librosa.istft(spectrogram, hop_length, win_length=win_length, window="hann")

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
预加重:
语音信号的平均功率谱受声门激励和口鼻辐射影响,高频端约在800HZ以上按6dB/倍频程衰落,预加重的目的是提升高频成分,使信号频谱平坦化,以便于频谱分析或声道参数分析.
---------------------

梅尔频谱(mel-spectrogram)提取,griffin_lim声码器【python代码分析】的更多相关文章

  1. Python代码分析工具

    Python代码分析工具:PyChecker.Pylint - CSDN博客 https://blog.csdn.net/permike/article/details/51026156

  2. Python代码分析工具之dis模块

    转自:http://hi.baidu.com/tinyweb/item/923d012e8146d00872863ec0  ,格式调整过. 代码分析不是一个新的话题,代码分析重要性的判断比较主观,不同 ...

  3. 正则提取关键字符-python代码实现

    原文地址:http://www.bugingcode.com/blog/python_re_extraction_key.html 关于python的正则使用在以前的文章中 http://www.bu ...

  4. 60行python代码分析2018互联网大事件

    2018年是改革开放四十周年,也是互联网发展的重要一年.经历了区块链,人工智能潮的互联网行业逐渐迎来了冬天.这一年里有无数的事件发生着,正好学了python数据处理相关,那么就用python对18年的 ...

  5. 转载:量化投资中常用python代码分析(一)

    pandas的IO 量化投资逃不过数据处理,数据处理逃不过数据的读取和存储.一般,最常用的交易数据存储格式是csv,但是csv有一个很大的缺点,就是无论如何,存储起来都是一个文本的格式,例如日期‘20 ...

  6. 如何使用 Pylint 来规范 Python 代码风格

    如何使用 Pylint 来规范 Python 代码风格 转载自https://www.ibm.com/developerworks/cn/linux/l-cn-pylint/   Pylint 是什么 ...

  7. python代码检查工具pylint 让你的python更规范

    1.pylint是什么? Pylint 是一个 Python 代码分析工具,它分析 Python 代码中的错误,查找不符合代码风格标准(Pylint 默认使用的代码风格是 PEP 8,具体信息,请参阅 ...

  8. 利用这10个工具,你可以写出更好的Python代码

    我每天都使用这些实用程序来使我的Python代码可显示. 它们是免费且易于使用的. 编写漂亮的Python比看起来难. 作为发布工作流程的一部分,我使用以下工具使代码可显示并消除可避免的错误. 很多人 ...

  9. 语音识别之梅尔频谱倒数MFCC(Mel Frequency Cepstrum Coefficient)

    语音识别之梅尔频谱倒数MFCC(Mel Frequency Cepstrum Coefficient) 原理 梅尔频率倒谱系数:一定程度上模拟了人耳对语音的处理特点 预加重:在语音信号中,高频部分的能 ...

随机推荐

  1. 【Unity 系统知识】 各种路径

    一.Assets下的Resources(Unity系统文件夹) :路径 Application.dataPath/Resources 可以使用Resources.Load("文件名字,注:不 ...

  2. poj2377Bad Cowtractors (最小生成树变形之——最大生成树)

    题目链接:http://poj.org/problem?id=2377 Description Bessie has been hired to build a cheap internet netw ...

  3. Angular 输入中的禁止特定输入值--Validator 与 Directive 实现

    1 前言 最近在项目中涉及表单的情况下,需要对用户输入进行过滤,比如填写用户名的时候不可以使用空格或者特殊符号,这里有几个解决方法: 使用 Angular 的正则同步验证器 使用 RxJS对输入的值进 ...

  4. DS静态查找- 顺序-二分-索引

    静态查找 静态表是只执行查找操作,而不执行插入.删除等操作的表. 现在常说的有五大查找方法:顺序查找.分块查找.索引查找.树查找.哈希查找. 后两种之前写过了二叉查找树和哈希表,现在回顾前面三种,它们 ...

  5. 移动端web整理 移动端问题总结,移动web遇到的那些坑

    meta基础知识 H5页面窗口自动调整到设备宽度,并禁止用户缩放页面 忽略将页面中的数字识别为电话号码 忽略Android平台中对邮箱地址的识别 当网站添加到主屏幕快速启动方式,可隐藏地址栏,仅针对i ...

  6. js提交map类型参数

    方式一:使用 xmlHttpRequest 对象发送数据 function sendMapPost(map, url){ var xmlHttpRequest = createXMLHttpReque ...

  7. spring中bean的构造函数,Autowired(Value)注入与@PostConstruct调用顺序

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/yyysylvia/article/deta ...

  8. vue2.0在IE11无法打开的解决办法

    npm 安装bebel-polyfill npm install --save-dev babel-polyfill 在webpack.base.conf.js文件中将 module.exports ...

  9. CentOS下安装中文man 手册

    为了方便使用man,安装中文手册,具体如下: 版本:CentOS release 6.6 (Final) 中文包:http://pkgs.fedoraproject.org/repo/pkgs/man ...

  10. 关于TVS、ESD、稳压二极管、压敏电阻

    一.稳压管和TVS管的工作原理 稳压二极管(又叫齐纳二极管),是一种直到临界反向击穿电压前都具有很高电阻的半导体器件,在这临界击穿点上,反向电阻降低到一个很小的数值,在这个低阻区中电流增加而电压则保持 ...