一、时域处理

1.1 librosa.zero_crossings

https://librosa.org/doc/latest/generated/librosa.zero_crossings.html

1.1.1 语法与参数

librosa.zero_crossings(y, *, threshold=1e-10, ref_magnitude=None, pad=True, zero_pos=True, axis=- 1)[source]

找到信号y 的过零点:满足sign(y[i]) != sign(y[j]) 的索引i。

如果y是多维的,则沿指定的axis 计算过零。

>参数
	y:np.ndarray
		输入数组

	threshold:float > 0 or None
		如果指定,则将-threshold <= y <= threshold其中的值剪裁为 0。

	ref_magnitude:float > 0 or callable
		如果是数字,则阈值相对于ref_magnitude 缩放。
		如果可调用,则阈值相对于 ref_magnitude(np.abs(y)) 缩放。

	pad:boolean
		如果True,则y[0]认为是有效的过零。

	zero_pos:boolean
		如果True然后值 0 被解释为具有正号。
		如果False,则 0、-1 和 +1 都有不同的符号。

	axis:int
		沿其计算过零的轴。

>返回值
	zero_crossings:np.ndarray [shape=y.shape, dtype=boolean]
		y沿选定轴的过零指标数组。

笔记
此函数缓存在 20 级。

1.1.2 示例:计算过零点

import librosa
import scipy
import numpy as np
import matplotlib.pyplot as plt

# 创建一个时间序列
y = np.sin(np.linspace(0, 4 * 2 * np.pi, 20))
print(y)
"""
[ 0.00000000e+00  9.69400266e-01  4.75947393e-01 -7.35723911e-01
 -8.37166478e-01  3.24699469e-01  9.96584493e-01  1.64594590e-01
 -9.15773327e-01 -6.14212713e-01  6.14212713e-01  9.15773327e-01
 -1.64594590e-01 -9.96584493e-01 -3.24699469e-01  8.37166478e-01
  7.35723911e-01 -4.75947393e-01 -9.69400266e-01 -9.79717439e-16]
"""
print(y.shape)
"""(20,)"""

# 计算过零点
z = librosa.zero_crossings(y)
print(z)
"""
[ True False False  True False  True False False  True False  True False
  True False False  True False  True False  True]
"""
print(z.shape)
"""(20,)"""

# 将y堆在过零指示器上
y_ind = librosa.util.stack([y, z], axis=-1)
print(y_ind)
"""
[[ 0.00000000e+00  1.00000000e+00]
 [ 9.69400266e-01  0.00000000e+00]
 [ 4.75947393e-01  0.00000000e+00]
 [-7.35723911e-01  1.00000000e+00]
 [-8.37166478e-01  0.00000000e+00]
 [ 3.24699469e-01  1.00000000e+00]
 [ 9.96584493e-01  0.00000000e+00]
 [ 1.64594590e-01  0.00000000e+00]
 [-9.15773327e-01  1.00000000e+00]
 [-6.14212713e-01  0.00000000e+00]
 [ 6.14212713e-01  1.00000000e+00]
 [ 9.15773327e-01  0.00000000e+00]
 [-1.64594590e-01  1.00000000e+00]
 [-9.96584493e-01  0.00000000e+00]
 [-3.24699469e-01  0.00000000e+00]
 [ 8.37166478e-01  1.00000000e+00]
 [ 7.35723911e-01  0.00000000e+00]
 [-4.75947393e-01  1.00000000e+00]
 [-9.69400266e-01  0.00000000e+00]
 [-9.79717439e-16  1.00000000e+00]]
"""
print(y_ind.shape)
"""(20, 2)"""

# 求过零点的指数
z_ind = np.nonzero(z)
print(z_ind)
"""
(array([ 0,  3,  5,  8, 10, 12, 15, 17, 19], dtype=int64),)
"""
print(len(z_ind), len(z_ind[0]))
"""1 9"""

1.2 librosa.mu_compress

https://librosa.org/doc/latest/generated/librosa.mu_compress.html

1.2.1 语法与参数

librosa.mu_compress(x, *, mu=255, quantize=True)[source]

律压缩
给定一个 -1 <= x <= 1输入信号,mu-law 压缩计算如下:

sign(x) * ln(1 + mu * abs(x)) /  ln(1 + mu)
>参数
	x:np.ndarray with values in [-1, +1]
		要压缩的输入信号

	mu:positive number
		压缩参数。形如2**n - 1 的值 (例如,15、31、63 等)是最常见的。

	quantize:bool
		如果True,则将压缩值量化为 1 + mu不同的整数值。
		如果False,则应用 mu-law 压缩而不进行量化。

>返回值
	x_compressed:np.ndarray
		压缩信号。

Raises
ParameterError参数错误
If x has values outside the range [-1, +1] If mu <= 0

1.2.2 示例

import librosa
import scipy
import numpy as np
import matplotlib.pyplot as plt

# 无量化压缩
x = np.linspace(-1, 1, num=16)
print(x)
"""
[-1.         -0.86666667 -0.73333333 -0.6        -0.46666667 -0.33333333
 -0.2        -0.06666667  0.06666667  0.2         0.33333333  0.46666667
  0.6         0.73333333  0.86666667  1.        ]
"""
print(x.shape)
"""(16,)"""

y = librosa.mu_compress(x, quantize=False)
print(y)
"""
[-1.         -0.97430198 -0.94432361 -0.90834832 -0.86336132 -0.80328309
 -0.71255496 -0.52124063  0.52124063  0.71255496  0.80328309  0.86336132
  0.90834832  0.94432361  0.97430198  1.        ]
"""
print(y.shape)
"""(16,)"""


# 量化压缩
y = librosa.mu_compress(x, quantize=True)
print(y)
"""
[-128 -124 -120 -116 -110 -102  -91  -66   66   91  102  110  116  120
  124  127]
"""
print(y.shape)
"""(16,)"""


# 量化和更小范围的压缩
y = librosa.mu_compress(x, mu=15, quantize=True)
print(y)
"""
[-8 -7 -7 -6 -6 -5 -4 -2  2  4  5  6  6  7  7  7]
"""
print(y.shape)
"""(16,)"""

1.3 librosa.mu_expand

https://librosa.org/doc/latest/generated/librosa.mu_expand.html

1.3.1 语法与参数

librosa.mu_expand(x, *, mu=255.0, quantize=True)[source]

律展开
这个函数是mu_compress 的倒数。给定一个-1 <= x <= 1 mu-law 压缩信号,mu-law 展开计算如下:
This function is the inverse of mu_compress. Given a mu-law compressed signal -1 <= x <= 1, the mu-law expansion is calculated by:

sign(x) * (1 / mu) * ((1 + mu)**abs(x) - 1)
>参数
	x:np.ndarray
		压缩信号。如果quantize=True,值必须在 [-1, +1] 范围内。

	mu:positive number
		压缩参数。形如2**n - 1的值 (例如,15、31、63 等)是最常见的。

	quantize:boolean
		如果True,则假定输入被量化为1 + mu 不同的整数值。

>返回值
	x_expanded:np.ndarray with values in the range [-1, +1]
		mu-law 扩展信号。

Raises
ParameterError 参数错误
If x has values outside the range [-1, +1] and quantize=False If mu <= 0

1.3.2 示例

import librosa
import scipy
import numpy as np
import matplotlib.pyplot as plt

# 无量化压缩和扩展
x = np.linspace(-1, 1, num=16)
print(x)
"""
[-1.         -0.86666667 -0.73333333 -0.6        -0.46666667 -0.33333333
 -0.2        -0.06666667  0.06666667  0.2         0.33333333  0.46666667
  0.6         0.73333333  0.86666667  1.        ]
"""
print(x.shape)
"""(16,)"""

y = librosa.mu_compress(x, quantize=False)
print(y)
"""
[-1.         -0.97430198 -0.94432361 -0.90834832 -0.86336132 -0.80328309
 -0.71255496 -0.52124063  0.52124063  0.71255496  0.80328309  0.86336132
  0.90834832  0.94432361  0.97430198  1.        ]
"""
print(y.shape)
"""(16,)"""

# 通过量化压缩和扩展。请注意,这必然会导致量化误差,特别是对于接近 +-1 的值。
y = librosa.mu_compress(x, quantize=True)
print(y)
"""
[-128 -124 -120 -116 -110 -102  -91  -66   66   91  102  110  116  120
  124  127]
"""
print(y.shape)
"""(16,)"""

z = librosa.mu_expand(y, quantize=True)
print(z)
"""
[-1.         -0.84027248 -0.70595818 -0.59301377 -0.4563785  -0.32155973
 -0.19817918 -0.06450245  0.06450245  0.19817918  0.32155973  0.4563785
  0.59301377  0.70595818  0.84027248  0.95743702]
"""
print(z.shape)
"""(16,)"""

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐