当前位置: 首页 > article >正文

声音响度、声压级计权(A B C)实现

 声压 sound pressure

声压就是大气压受到声波扰动后产生的变化,即为大气压强的余压,它相当于在大气压强上的叠加一个声波扰动引起的压强变化。由于声压的测量比较容易实现,通过声压的测量也可以间接求得质点速度等其它物理量,所以声学中常用这个物理量来描述声波

我们知道大气压强单位 1Pa = 1 pascal = 1N/m

实际计算可以参考http://www.sengpielaudio.com/calculator-soundlevel.htm

由于人对不同的声音频段 听感大小不一致,所以要对声音进行计权处理

如下实现 A  B   C 计权的实现,计权的实现参考标准,用于逼近实际的等响度曲线

 

ISO 226-2003标准

A、B、C三种计权网络特性,分别对应于倒置的40、70、100Phon等响曲线(1000Hz归一化到0dB),其作用是分别反应人耳对低、中、高声压级的响度感觉。A计权被证实是人耳对声压级主观反应的极好校正。对由A计权测量的声级称为A声级,记作LPA 或dB(A)。近来B计权、C计权已很少采用。

  • A计权:40Phon等响曲线的翻转,模拟55dB以下低强度噪声特性。

  • B计权:70Phon等响曲线的翻转,模拟55~85dB中等强度噪声特性。

  • C计权:100Phon等响曲线的翻转,模拟高强度噪声特性。

  • D计权:专用于飞机噪声的测量。


target_folder='audio/'
audio_targets = '.wav'
spl_folder = '/c_audio'
from  librosa import load
from  os import  listdir,path
from scipy.signal import lfilter,bilinear
from numpy import pi, convolve,log10,sqrt,sum,power
from csv import writer

def a_weighting_coeffs_design(sample_rate):
    """Returns b and a coeff of a A-weighting filter.
    Parameters
    ----------
    sample_rate : scalar
        Sample rate of the signals that well be filtered.
    Returns
    -------
    b, a : ndarray
        Filter coefficients for a digital weighting filter.
    Examples
    --------
    >>> b, a = a_weighting_coeff_design(sample_rate)
    To Filter a signal use scipy lfilter:
    >>> from scipy.signal import lfilter
    >>> y = lfilter(b, a, x)
    See Also
    --------
    b_weighting_coeffs_design : B-Weighting coefficients.
    c_weighting_coeffs_design : C-Weighting coefficients.
    weight_signal : Apply a weighting filter to a signal.
    scipy.lfilter : Filtering signal with `b` and `a` coefficients.
    """

    f1 = 20.598997
    f2 = 107.65265
    f3 = 737.86223
    f4 = 12194.217
    A1000 = 1.9997
    numerators = [(2 * pi * f4)**2 * (10**(A1000 / 20.0)), 0., 0., 0., 0.]
    denominators = convolve([1., +4 * pi * f4, (2 * pi * f4)**2],
                            [1., +4 * pi * f1, (2 * pi * f1)**2])
    denominators = convolve(convolve(denominators, [1., 2 * pi * f3]),
                            [1., 2 * pi * f2])
    return bilinear(numerators, denominators, sample_rate)


def b_weighting_coeffs_design(sample_rate):
    """Returns `b` and `a` coeff of a B-weighting filter.
    B-Weighting is no longer described in DIN61672.
    Parameters
    ----------
    sample_rate : scalar
        Sample rate of the signals that well be filtered.
    Returns
    -------
    b, a : ndarray
        Filter coefficients for a digital weighting filter.
    Examples
    --------
    >>> b, a = b_weighting_coeff_design(sample_rate)
    To Filter a signal use :function: scipy.lfilter:
    >>> from scipy.signal import lfilter
    >>> y = lfilter(b, a, x)
    See Also
    --------
    a_weighting_coeffs_design : A-Weighting coefficients.
    c_weighting_coeffs_design : C-Weighting coefficients.
    weight_signal : Apply a weighting filter to a signal.
    """

    f1 = 20.598997
    f2 = 158.5
    f4 = 12194.217
    B1000 = 0.17
    numerators = [(2 * pi * f4)**2 * (10**(B1000 / 20)), 0, 0, 0]
    denominators = convolve([1, +4 * pi * f4, (2 * pi * f4)**2],
                            [1, +4 * pi * f1, (2 * pi * f1)**2])
    denominators = convolve(denominators, [1, 2 * pi * f2])
    return bilinear(numerators, denominators, sample_rate)


def c_weighting_coeffs_design(sample_rate):
    """Returns b and a coeff of a C-weighting filter.
    Parameters
    ----------
    sample_rate : scalar
        Sample rate of the signals that well be filtered.
    Returns
    -------
    b, a : ndarray
        Filter coefficients for a digital weighting filter.
    Examples
    --------
    b, a = c_weighting_coeffs_design(sample_rate)
    To Filter a signal use scipy lfilter:
    from scipy.signal import lfilter
    y = lfilter(b, a, x)
    See Also
    --------
    a_weighting_coeffs_design : A-Weighting coefficients.
    b_weighting_coeffs_design : B-Weighting coefficients.
    weight_signal : Apply a weighting filter to a signal.
    """

    f1 = 20.598997
    f4 = 12194.217
    C1000 = 0.0619
    numerators = [(2 * pi * f4)**2 * (10**(C1000 / 20)), 0, 0]
    denominators = convolve([1, +4 * pi * f4, (2 * pi * f4)**2],
                            [1, +4 * pi * f1, (2 * pi * f1)**2])
    return bilinear(numerators, denominators, sample_rate)
    
def SPLCal(x):
    Leng = len(x)
    pa = sqrt(sum(power(x, 2))/Leng)
    p0 = 2e-5
    spl = 20 * log10(pa / p0)
    return spl    



def preprocess_spl(name,spl):
    """Main logic for SPL weighting"""
    n = 1
    ##at = find_recordings(target_folder, audio_targets)
    at =listdir(target_folder)
    for f in at:
        filename = path.join(target_folder, f)
        x, Fs = load(filename)
        b, a = c_weighting_coeffs_design(Fs)
        y = lfilter(b, a, x)
        out = SPLCal(y)
        spl.append(out)
        name.append(f[:-4])
        print(filename[6:-4]+"     spl:"+str(out))
        '''print("--- Preprocessing SPLs: " + str(round(n / len(at) * 100, 2)) +
              "% done. ---\t\t",
              end='\r\r\r\n\n')'''
        n += 1    
if __name__ == '__main__':
    name =[]
    spl= []
    preprocess_spl(name,spl)
    header =['name', 'spl(dbc)']
    with open('save.csv', 'w') as file:
        # 2. Create a CSV writer
        mywrite = writer(file)
        # 3. Write data to the file
        mywrite.writerow(header)
        tmp = zip(name,spl)
        mywrite.writerows(tmp)
        file.close()


http://www.kler.cn/a/145643.html

相关文章:

  • 计算机网络 (55)流失存储音频/视频
  • postgresql15的停止
  • 【玩转全栈】---基于YOLO8的图片、视频目标检测
  • GPT 结束语设计 以nanogpt为例
  • 炸场硅谷,大模型“蒸汽机”迎来“瓦特时刻”
  • elementUI Table组件实现表头吸顶效果
  • 高品质MP3音频解码语音芯片WT2003Hx的特征优势与应用场景
  • WebSocket了解
  • 论文公式和代码对应
  • C语言数据类型和变量
  • 机器学习/sklearn笔记:MeanShift
  • SkyWalking全景解析:从原理到实现的分布式追踪之旅
  • DY点赞、搜索功能测试用例设计
  • 【刷题笔记】接雨水||暴力通过||符合思维方式
  • JC/T 456-2015 陶瓷马赛克检测
  • 【单调栈】子数组的最小值之和
  • Presto+Alluxio数据平台实战
  • IDM(Internet Download Manager)PC版提升下载速度与效率的利器
  • uniapp+vue基于Android的校园二手跳蚤市场的设计与实现 微信小程序
  • 18.天气小案例
  • 电子学会C/C++编程等级考试2021年06月(三级)真题解析
  • SELinux零知识学习三十、SELinux策略语言之角色和用户(1)
  • 常见遍历方法 for循环、forEach、map、filter、find、findIndex、some、every
  • 1 Python实现23种计模式
  • qt双击treeview节点之后,完成编辑,获取完成编辑得信号
  • C++变量、函数、类的声明和定义