matlab 转 python 等价函数

关于matlabpython代码的基础规则, 参见: NumPy for MATLAB users
这里列出我在matlabpython代码过程中, 涉及到的其他matlab的等价函数, 这些函数在"NumPy for MATLAB users"中可能不存在. 本文不对matlab函数做过多介绍.
如果你觉得这篇博客,对你有帮助, 欢迎收藏订阅!

matlabpythonnote
[y,Fs] = audioread(filename)Fs, y = scipy.io.wavfile.read(filename)scipy.io.wavfile.read()返回的y值可能需要做转换.
d = bi2de(b,p,flg)d = bi2de(b,p,left_msb)flg='left-msb'时, left_msb=True;
flg='right-msb'时, left_msb=False.
参见: bi2de
CC = bwconncomp(BW,conn)CC = bwconncomp(BW,conn)参见: matlab函数(bwconncomp)的python实现
Y = circshift(A, K)Y = circshift(A, K)参见: circshift
w = conv(u,v,shape)w = numpy.convolve(u,v, shape)
[f,x] = ecdf(y)f, x = ecdf(y)参见: ecdf
[pks,locs] = findpeaks(data)locs, _ =signal.find_peaks(data)
pks = data[locs]
b = fir1(n, Wn)b = scipy.signal.firwin(n+1, Wn)
h= hanning(N)h = scipy.signal.windows.hann(N+2)[1:-1]scipy中的hann() 窗长度为N+2, 掐头去尾后, 同matlab的结果相同.
[n,Wn,beta,ftype] = kaiserord(f,a,dev)numtaps, beta = scipy.signal.kaiserord(ripple, width)
k = kurtosis(X)k = scipy.stats.kurtosis(X, fisher=False)参见: Matlab 与 Python 基于窗函数的滤波器设计对比 之 凯瑟窗
p = logncdf(x,mu,sigma)p = logncdf(x,mu,sigma)参见: logncdf
[a, g] = lpc(x, p)a = lpc(x, p)Linear Predictor Coefficients. 参见: lpc
M = movmean(A,k)M = movmean(A,k)代码见: movmean的实现
P = nextpow2(A)P = int(np.ceil(np.log2(A)))Exponent of next higher power of 2
y = pskmod(x,M,ini_phase)y = pskmod(x,M,ini_phase)参见: pskmod

bi2de

import numpy as np
def bi2de(b: np.ndarray, p: int = 2, left_msb = False):
    if left_msb == True:
        b = b[::-1]
    if b.ndim == 1:
        d = 0
        for i, bi in enumerate(b):
            d += bi * (p**i)
    else:
        d = np.zeros(b.shape[-1])
        for i, bi in enumerate(b):
            d += bi * (p**i)
    return d

circshift

import numpy as np
def circshift(A, K):
    return np.hstack((A[-K:], A[:-K]))

ecdf

matlab中的ecdf功能不同, matlab中deecdf返回的f(累计概率)为线性递增, 这里的ecdf返回的x为线性递增.

import numpy as np
from statsmodels.distributions.empirical_distribution import ECDF
def ecdf(y):
	ecdf0 = ECDF(y)
	x = np.linspace(np.min(y), np.max(y), len(y)+1)
	f = ecdf0(x)
	return f, x

logncdf

对数正态累积分布函数

def logncdf(x, mu: float = 0, sigma: float = 1):
    z = (np.log(x) - mu) / sigma
    p = 0.5 * erfc(-z / np.sqrt(2))
    return p

lpc

Linear prediction filter coefficients

代码取自:Basic Linear Prediction example

import numpy as np

def lpc(x: np.ndarray, p: int):
    """
    Return `p`th-order linear predictive coefficients for sequence `x` 
    using Levinson-Durbin prediction algorithm
    """
    #step 1: compute autoregression coefficients R_0, ..., R_m
    R = [x.dot(x)] 
    if R[0] == 0:
        return [1] + [0] * (p-2) + [-1]
    else:
        for i in range(1, p + 1):
            r = x[i:].dot(x[:-i])
            R.append(r)
        R = np.array(R)
    #step 2: 
        A = np.array([1, -R[1] / R[0]])
        E = R[0] + R[1] * A[1]
        for k in range(1, p):
            if (E == 0):
                E = 10e-17
            alpha = - A[:k+1].dot(R[k+1:0:-1]) / E
            A = np.hstack([A,0])
            A = A + alpha * A[::-1]
            E *= (1 - alpha**2)
        return A

movmean

import numpy as np

def movmean(x: np.ndarray, k: int):
    """
    Moving mean.
    """
    y = np.zeros_like(x)
    for i in range(x.shape[0]):
        lb = 0 if i-k//2 < 0 else i-k//2
        ub = x.shape[0] if i+k//2 > x.shape[0] else i+k//2
        y[i] = np.mean(x[lb:ub], axis=0)
    return y

pskmod

import numpy as np
def pskmod(x: np.ndarray, M: int, ini_phase: float = 0):
    theta = 2*np.pi*x/M
    y = np.exp(1j*(theta + ini_phase))
    return y
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

falwat

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

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

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

打赏作者

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

抵扣说明:

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

余额充值