音频滤波器

1.高斯滤波器

scipy.ndimage.gaussian_filter1d — SciPy v1.8.0 Manual

对含噪数据进行平滑处理 - MATLAB smoothdata- MathWorks 中国

from scipy.signal import gaussian

def gaussian_filter(M, std, sym=True):
    """
    Return a Gaussian window.
    """
    return gaussian(M, std, sym)

2.均值滤波器

3.中值滤波器

4.卡尔曼滤波器

###################################################################################
# based on https://stackoverflow.com/questions/13901997/kalman-2d-filter-in-python
###################################################################################

def kalman_xy(x,
              P,
              measurement,
              R,
              motion=np.matrix('0. 0. 0. 0.').T,
              Q=np.matrix(np.eye(4))):
    """
    Args:
        x: initial state 4-tuple of location and velocity: (x0, x1, x0_dot, x1_dot)
        P: initial uncertainty convariance matrix
        measurement: observed position
        R: measurement noise
        motion: external motion added to state vector x
        Q: motion noise (same shape as P)
    """
    return kalman(x,
                  P,
                  measurement,
                  R,
                  motion,
                  Q,
                  F=np.matrix('''
                      1. 0. 1. 0.;
                      0. 1. 0. 1.;
                      0. 0. 1. 0.;
                      0. 0. 0. 1.
                      '''),
                  H=np.matrix('''
                      1. 0. 0. 0.;
                      0. 1. 0. 0.'''))


def kalman(x, P, measurement, R, motion, Q, F, H):
    """
    Args:
        x: initial state
        P: initial uncertainty convariance matrix
        measurement: observed position (same shape as H*x)
        R: measurement noise (same shape as H)
        motion: external motion added to state vector x
        Q: motion noise (same shape as P)
        F: next state function: x_prime = F*x
        H: measurement function: position = H*x

    Returns:
        the updated and predicted new values for (x, P)

    See also http://en.wikipedia.org/wiki/Kalman_filter

    This version of kalman can be applied to many different situations by
    appropriately defining F and H.
    """
    # UPDATE x, P based on measurement m
    # distance between measured and current position-belief
    y = np.matrix(measurement).T - H * x
    S = H * P * H.T + R  # residual convariance
    K = P * H.T * S.I  # Kalman gain
    x = x + K * y
    I = np.matrix(np.eye(F.shape[0]))  # identity matrix
    P = (I - K * H) * P

    # PREDICT x, P based on motion
    x = F * x + motion
    P = F * P * F.T + Q

    return x, P

5.rasta滤波器

def rasta_filter(x):
    """
    % y = rastafilt(x)
    %
    % rows of x = critical bands, cols of x = frame
    % same for y but after filtering
    %
    % default filter is single pole at 0.94
    """
    numer = np.arange(-2, 3)
    numer = (-1 * numer) / np.sum(numer * numer)
    denom = np.array([1, -0.94])

    zi = signal.lfilter_zi(numer, 1)
    y = np.zeros((x.shape))

    for i in range(x.shape[0]):
        y1, zi = signal.lfilter(numer, 1, x[i, 0:4], axis=0, zi=zi * x[i, 0])
        y1 = y1 * 0
        y2, _ = signal.lfilter(numer, denom, x[i, 4:x.shape[1]], axis=0, zi=zi)
        y[i, :] = np.append(y1, y2)
    return y

6.sobel滤波器

from scipy.ndimage import sobel

def sobel_filter(sig, axis=-1, mode="reflect", cval=0.0):
    """
    Calculate a Sobel filter.
    """
    return sobel(sig, axis, mode, cval)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值