语音质量指标Python(PESQ、STOI、SegSNR、LLR、WSS、CD、LSD、CSIG、CBAK、COVL)


在做语音增强的时候,论文中会提到一些语音质量的评价指标,无从下手,于是自己摸索了两天时间搞出了其计算的python版本。

1.安装对应的依赖库

#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
@Time    : 2020/9/13 16:53
@Author  : kingback
@Site    :
@File    : SEPM.py
@Software: PyCharm
'''


'''
the install order is :
因为一些函数包需要一些依赖,所以建议大家按照如下步骤安装操作


    0. All the package I use the commond "python setup.py install" to install to our machine.
    Maybe we also need to install other packages like :   cython ,numba   ,scipy  ,matplotlib ,numpy  ...
    0.说明,以下所有的安装包最好都再对应的github链接下载其源码,然后cmd进入到其对应的文件见,执行python setup.py install命令来进行安装

    1. install the gammatone from : https://github.com/detly/gammatone
    1. 第一步,先安装这个包,下载地址如上,下载好后进入文件夹执行命令python setup.py install

    2.install the SRMRpy from : https://github.com/jfsantos/SRMRpy
    2.这是需要安装的第二个包,因为这些包都是有依赖关系的,所以建议大家还是按照这个顺序来

    3.install the python-pesq from  :   https://github.com/vBaiCai/python-pesq
    3.这是第三个包

    4.install the pystoi from   :   https://github.com/mpariente/pystoi
    4.这是第四个

    5.install the pyseqm from   :   https://github.com/schmiph2/pysepm
    5.这是第五个

'''

如果大家链接github速度较慢,可以访问那五个安装包,提取码:r809

2.环境搭建

环境搭建的话,博主是使用conda新创了个python3.6的虚拟环境,然后再搭建的这个语音质量指标环境,以下是搭建过程及步骤:

1.创建新环境 conda create -n tp python=3.6
2.激活环境 activate tp
3.安装必备包(因为下边要安装的包需要这些依赖,如果使用pip源的话就很慢,所以在这里提前下载下来,这三个包必须提前安装哈)
pip install matplotlib -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install scipy -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install numba -i https://pypi.tuna.tsinghua.edu.cn/simple
4. 进入到gammatone_master里边执行python setup.py install命令
5. 安装成功,如下图所示

在这里插入图片描述

6. 紧接着同样的步骤安装SRMRpy,记得切换到tp环境哦

在这里插入图片描述

然后执行python setup.py install进行安装,出现以下界面就是安装成功了

在这里插入图片描述

7.接下来安装pesq,和上边的步骤一样,在对应的文件夹下进入cmd命令行,然后切换到tp虚拟环境,执行命令python setup.py install

在这里插入图片描述

8.和上边的操作一样,安装第四个包pystoi

在这里插入图片描述

9.步骤同上,安装第五个包pysepm

在这里插入图片描述

3.代码部分,计算语音的质量指标

因为新环境并没有soundfile包(用于读取语音文件)和tqdm包(用于显示进度),所以想要以下代码,还需要安装这俩包,执行以下命令

pip install soundfile tqdm

以下就是安装成功的结果啦
在这里插入图片描述
都安装完以后,就可以测试语音的质量指标啦,以下就是全部的代码:

import pysepm
import os
import numpy as np
import soundfile as sf
from tqdm import tqdm

clean_wavs = 'clean_testset_wav_16k/'#这个路径,大家根据自己的需求进行修改
denoised_wavs = 'cleaned_testset_wav_16k_48700/'#同理进行修改


def file_name(file_dir):
    L = []
    for root, dirs, files in os.walk(file_dir):
        for file in files:
            if os.path.splitext(file)[1] == '.wav':
                L.append(os.path.join(root, file))
    return L


# get wav_lists
clean_lists = file_name(clean_wavs)
denoised_lists = file_name(denoised_wavs)
# Package files
zipped = zip(clean_lists, denoised_lists)

scores = []
CSIGs = []
CBAKs = []
COVLs = []

for (clean_wav, denoised_wav) in tqdm(zipped, 'the progressing ...'):
    # Gain speech parameters
    ref, sr0 = sf.read(clean_wav)
    deg, sr1 = sf.read(denoised_wav)



    '''
    # Method 1: SNRseg (分段信噪比)
        # from pysepm Call SNRseg to calculate its metrics
        # in this case we can choose our frame length =0.03*1000=30 ms , and the overlap =30 ms *0.75 =22.5 ms
        # 得分越高,质量越好
    '''
    # score = pysepm.SNRseg(ref, deg, sr0)



    '''
    # Method 2: llr (对数似然比测度)
        # 
        # 得分越高,质量越好
    '''
    # score = pysepm.llr(ref, deg, sr0)



    '''
    # Method 3: WSS (加权谱倾斜测度)
        # 
        # 得分越低,质量越好
    '''
    score = pysepm.wss(ref, deg, sr0)


    '''
    # Method 4: STOI (可短时客观可懂)
        # 
        # the score from 0-1 . The higher the score, the better the performance.
        #得分范围0~1,得分越高,质量越好
    '''
    # score = pysepm.stoi(ref, deg, sr0)


    '''
    # Method 5: PESQ
        # when I try this commond , I faced some troubles   , so finally I gave up this commond,
        # use the PESQ.py to instead
        得分范围从 -0.5~ 4.5,得分越高,效果越好
    '''
    NaN,score = pysepm.pesq(ref,deg,sr0)



    '''
    # Method 6: CD (Cepstrum Distance)
        # 
        # 数值越高,得分越高.
    '''
    # score = pysepm.cepstrum_distance(ref, deg, sr0)


    '''
       # Method 7: LSD (对数谱距离)
            # This method I use the LSD.py to calculate the distance 
            # 数值越小,得分越高      
    '''


    '''
    Method 1 - 7 use this score to print
    '''
    # score append to scores
    scores.append(score)


    '''
    # Method 8: Composite
        # In this method , It comes some errors, if you want to solve the error ,  see the step 8 in this file.
        # CSIG , CBAK , COVL all from 1 - 5 , The higher the score, the better the performance.
    '''
    # CSIG,CBAK,COVL = pysepm.composite(ref, deg, sr0)
    # CSIGs.append(CSIG)
    # CBAKs.append(CBAK)
    # COVLs.append(COVL)


# print(scores)
print('The average SegSNR evaluation is :   ', sum(scores) / len(scores))
# calculate the standard deviation & variance of the scores
print('The standard deviation is :   ', np.std(scores))
print('The variance is :   ', np.var(scores))


# print(CSIGs,CBAKs,COVLs)
# print('The average CSIG evaluation is :   ', sum(CSIGs) / len(CSIGs))
# print('The average CBAK evaluation is :   ', sum(CBAKs) / len(CBAKs))
# print('The average COVL evaluation is :   ', sum(COVLs) / len(COVLs))

4.可能出现的问题及解决措施(应该不会出现)

4.1 错误 ModuleNotFoundError: No module named ‘pesq’

 6. if  you have finished the last step  ,don't forget to change the next step's setup.py .
    This is done so that you do not have to repeatedly download the installed files.
    And there's also the possibility of an error.
   如果你在程序运行过程中出现以下问题,可以参考以下解决方案。

7.  如果你的代码在执行过程中遇到以下问题,可以参照此教程,
       "
        File "/home/king/anaconda3/envs/tf_gpu/lib/python3.6/site-packages/pysepm-0.1-py3.6.egg/pysepm/__init__.py", line 4, in <module>
        File "/home/king/anaconda3/envs/tf_gpu/lib/python3.6/site-packages/pysepm-0.1-py3.6.egg/pysepm/qualityMeasures.py", line 4, in <module>
        ModuleNotFoundError: No module named 'pesq'
        "
		找到你的环境中的这个pysepm/qualityMeasures.py文件,当然路径肯定和我这不一样哈
        you can go to your file path "/home/king/anaconda3/envs/tf_gpu/lib/python3.6/site-packages/pysepm-0.1-py3.6.egg/pysepm/qualityMeasures.py"

		在源码中找到如下命令:
            #import pesq as pypesq # https://github.com/ludlows/python-pesq
        替换为 :
            import pypesq

4.2 测量语音的PESQ指标和CBark,CSIG,COVL时可能遇到的问题及解决措施

    9.  if you run the method pesq & Composite , it may be come some errors because we haven't introduced PESQ packages properly yet.
    10.如果你在测量语音的pesq指标,和Composite 指标(即CBark,CSIG,COVL) ,如果出现问题,建议使用如下解决方案
    	打开以下文件
        you can go to your file path "/home/king/anaconda3/envs/tf_gpu/lib/python3.6/site-packages/pysepm-0.1-py3.6.egg/pysepm/qualityMeasures.py"1) 操作1 #在文件开头添加以下命令
            "from pypesq import pesq as pq"
            
        (2).操作2 ,替换347-359行的pesq函数
            "
                def pesq(clean_speech, processed_speech, fs):
                    if fs == 8000:
                        mos_lqo = pq(clean_speech,processed_speech, fs)
                        pesq_mos = 46607/14945 - (2000*np.log(1/(mos_lqo/4 - 999/4000) - 1))/2989#0.999 + ( 4.999-0.999 ) / ( 1+np.exp(-1.4945*pesq_mos+4.6607) )
                    elif fs == 16000:
                         mos_lqo = pq(clean_speech,processed_speech, fs)
                        pesq_mos = np.NaN
                    else:
                        raise ValueError('fs must be either 8 kHz or 16 kHz')
                    return pesq_mos,mos_lqo
            "
        (3).保存并且退出

5.一劳永逸的方法解决各种错误(简单粗暴)

博主在近期重新搞了搞这个,其实如果重新搭建环境并且按照 第二部分第三部分环境搭建的话是没有出现问题的,且经过博主本人再三检测,以上所有音频指标均可正常检测,如果大家在使用过程中出现错误,为方便大家,我将该conda虚拟环境传输到百度云网盘,以提供给大家使用,下载完成后,将tp环境放置到conda安装路径下的envs文件夹下,之后选择虚拟环境,就可以直接在这个虚拟环境上进行操作啦。百度云链接,提取码:ggek

6.测试数据集

本博客中用于检测的音频数据,我也一并上传至百度网盘,大家也可拿来进行测试数据集链接,提取码:ybjp

7.参考资料

  1. https://github.com/schmiph2/pysepm/blob/master/examples/examplesForCalculatingMeasures.ipynb (How to use the packages of SEPM)
  2. https://zhuanlan.zhihu.com/p/190146707 (音频质量客观评价指标)

8.修改说明

时间操作
2021年4月21日首发
2022年5月8日重新编辑博客并上传网盘
  • 61
    点赞
  • 163
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 30
    评论
评论 30
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王延凯的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值