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