首页 > 其他分享 >librosa.filters.mel

librosa.filters.mel

时间:2023-01-05 19:56:35浏览次数:30  
标签:... fft mels librosa weights filters mel np

@deprecate_positional_args
@cache(level=10)
def mel(
    *,
    sr,
    n_fft,
    n_mels=128,
    fmin=0.0,
    fmax=None,
    htk=False,
    norm="slaney",
    dtype=np.float32,
):
    """Create a Mel filter-bank.

    This produces a linear transformation matrix to project
    FFT bins onto Mel-frequency bins.

    Parameters
    ----------
    sr : number > 0 [scalar]
        sampling rate of the incoming signal

    n_fft : int > 0 [scalar]
        number of FFT components

    n_mels : int > 0 [scalar]
        number of Mel bands to generate

    fmin : float >= 0 [scalar]
        lowest frequency (in Hz)

    fmax : float >= 0 [scalar]
        highest frequency (in Hz).
        If `None`, use ``fmax = sr / 2.0``

    htk : bool [scalar]
        use HTK formula instead of Slaney

    norm : {None, 'slaney', or number} [scalar]
        If 'slaney', divide the triangular mel weights by the width of the mel band
        (area normalization).

        If numeric, use `librosa.util.normalize` to normalize each filter by to unit l_p norm.
        See `librosa.util.normalize` for a full description of supported norm values
        (including `+-np.inf`).

        Otherwise, leave all the triangles aiming for a peak value of 1.0

    dtype : np.dtype
        The data type of the output basis.
        By default, uses 32-bit (single-precision) floating point.

    Returns
    -------
    M : np.ndarray [shape=(n_mels, 1 + n_fft/2)]
        Mel transform matrix

    See Also
    --------
    librosa.util.normalize

    Notes
    -----
    This function caches at level 10.

    Examples
    --------
    >>> melfb = librosa.filters.mel(sr=22050, n_fft=2048)
    >>> melfb
    array([[ 0.   ,  0.016, ...,  0.   ,  0.   ],
           [ 0.   ,  0.   , ...,  0.   ,  0.   ],
           ...,
           [ 0.   ,  0.   , ...,  0.   ,  0.   ],
           [ 0.   ,  0.   , ...,  0.   ,  0.   ]])

    Clip the maximum frequency to 8KHz

    >>> librosa.filters.mel(sr=22050, n_fft=2048, fmax=8000)
    array([[ 0.  ,  0.02, ...,  0.  ,  0.  ],
           [ 0.  ,  0.  , ...,  0.  ,  0.  ],
           ...,
           [ 0.  ,  0.  , ...,  0.  ,  0.  ],
           [ 0.  ,  0.  , ...,  0.  ,  0.  ]])

    >>> import matplotlib.pyplot as plt
    >>> fig, ax = plt.subplots()
    >>> img = librosa.display.specshow(melfb, x_axis='linear', ax=ax)
    >>> ax.set(ylabel='Mel filter', title='Mel filter bank')
    >>> fig.colorbar(img, ax=ax)
    """

    if fmax is None:
        fmax = float(sr) / 2

    # Initialize the weights
    n_mels = int(n_mels)
    weights = np.zeros((n_mels, int(1 + n_fft // 2)), dtype=dtype)

    # Center freqs of each FFT bin
    fftfreqs = fft_frequencies(sr=sr, n_fft=n_fft)

    # 'Center freqs' of mel bands - uniformly spaced between limits
    mel_f = mel_frequencies(n_mels + 2, fmin=fmin, fmax=fmax, htk=htk)

    fdiff = np.diff(mel_f)
    ramps = np.subtract.outer(mel_f, fftfreqs)

    for i in range(n_mels):
        # lower and upper slopes for all bins
        lower = -ramps[i] / fdiff[i]
        upper = ramps[i + 2] / fdiff[i + 1]

        # .. then intersect them with each other and zero
        weights[i] = np.maximum(0, np.minimum(lower, upper))

    if norm == "slaney":
        # Slaney-style mel is scaled to be approx constant energy per channel
        enorm = 2.0 / (mel_f[2 : n_mels + 2] - mel_f[:n_mels])
        weights *= enorm[:, np.newaxis]
    else:
        weights = util.normalize(weights, norm=norm, axis=-1)

    # Only check weights if f_mel[0] is positive
    if not np.all((mel_f[:-2] == 0) | (weights.max(axis=1) > 0)):
        # This means we have an empty channel somewhere
        warnings.warn(
            "Empty filters detected in mel frequency basis. "
            "Some channels will produce empty responses. "
            "Try increasing your sampling rate (and fmax) or "
            "reducing n_mels.",
            stacklevel=2,
        )

    return weights

标签:...,fft,mels,librosa,weights,filters,mel,np
From: https://www.cnblogs.com/prettysky/p/17028720.html

相关文章

  • Pomelo:网易开源基于 Node.js 的游戏服务端框架
    ​​Pomelo​​是基于Node.js的高性能、分布式游戏服务器框架。它包括基础的开发框架和相关的扩展组件(库和工具包),可以帮助你省去游戏开发枯燥中的重复劳动和底层逻辑......
  • 53、过滤器filters
    1、局部过滤器filters<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metanam......
  • Tutorial on Gabor Filters
    1TheTemporal(1-D)GaborFilterGaborfilterscanserveasexcellentband-passfiltersforunidimensionnalsignals(e.g.,speech)AcomplexGaborfilterisd......
  • Thymeleaf 表达式工具类
    Thymeleaf默认提供了丰富的表达式工具类,这里列举一些常用的工具类。Objects工具类/**当obj不为空时,返回obj,否则返回default默认值*其同样适用于数组、列表或集合*/${#obje......
  • Timeline - 自定义轨道
    Playable轨道和自定义轨道的区别1)Playable轨道中,自定义PlayableBehaviour的上一级只能是Playable。自定义轨道中,上一级是我们自定义的Playable2) 自定义轨道可以直......
  • Timeline - PlayableBehavior轨道
    使用Timeline实现CanvasGroup的补间动画1) 创建轨道资源类usingUnityEngine;usingUnityEngine.Playables;[System.Serializable]publicclassCanvasGroupClip:......
  • Windows Server 2012 R2 Standard安装mellanox网卡驱动
    原因:我的系统是WindowsServer2012R2Standard首先我是想要安装mellanox网卡驱动,然后系统让我安装WindowsServer2012R2安装补丁KB2999226思路:经过网上查找资料安装......
  • Thymeleaf一篇就够了
    目录​​什么是Thymeleaf​​​​模板引擎介绍​​​​Thymeleaf介绍​​​​学习Thymeleaf必知的知识点​​​​Springboot​​​​MVC介绍​​​​动静分离​​​​第一个......
  • shiro整合thymeleaf
    1、依赖<dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-spring</artifactId><version>1.10.0</ver......
  • Thymeleaf利用layout.html文件生成页面布局框架
    1.layout.html文件生成布局<!DOCTYPEhtml><htmllang="zh-CN"xmlns:th="http://www.thymeleaf.org"xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout">......