matlab缓冲区设置,python – Matlab的缓冲区是否等同于numpy?

我看到有一个array_split并拆分methods但是当你必须拆分一个长度不是块大小的整数倍的数组时,这些都不是很方便.而且,这些方法输入是切片的数量而不是切片大小.我需要更像Matlab的buffer方法,它更适合信号处理.

例如,如果我想将信号缓冲到大小为60的块,我需要这样做:np.vstack(np.hsplit(x.iloc [0:((len(x)// 60)* 60)],len (x)// 60))这很麻烦.

解决方法:

我编写了以下例程来处理我需要的用例,但是我还没有实现/测试“underlap”.

请随时提出改进建议.

def buffer(X, n, p=0, opt=None):

'''Mimic MATLAB routine to generate buffer array

MATLAB docs here: https://se.mathworks.com/help/signal/ref/buffer.html

Parameters

----------

x: ndarray

Signal array

n: int

Number of data segments

p: int

Number of values to overlap

opt: str

Initial condition options. default sets the first `p` values to zero,

while 'nodelay' begins filling the buffer immediately.

Returns

-------

result : (n,n) ndarray

Buffer array created from X

'''

import numpy as np

if opt not in [None, 'nodelay']:

raise ValueError('{} not implemented'.format(opt))

i = 0

first_iter = True

while i < len(X):

if first_iter:

if opt == 'nodelay':

# No zeros at array start

result = X[:n]

i = n

else:

# Start with `p` zeros

result = np.hstack([np.zeros(p), X[:n-p]])

i = n-p

# Make 2D array and pivot

result = np.expand_dims(result, axis=0).T

first_iter = False

continue

# Create next column, add `p` results from last col if given

col = X[i:i+(n-p)]

if p != 0:

col = np.hstack([result[:,-1][-p:], col])

i += n-p

# Append zeros if last row and not length `n`

if len(col) < n:

col = np.hstack([col, np.zeros(n-len(col))])

# Combine result with next row

result = np.hstack([result, np.expand_dims(col, axis=0).T])

return result

标签:python,numpy,matlab,buffer

来源: https://codeday.me/bug/20190828/1757124.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值