python – Matlab的缓冲区是否等同于numpy?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – Matlab的缓冲区是否等同于numpy?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1601字,纯文字阅读大概需要3分钟。
内容图文
我看到有一个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 – Matlab的缓冲区是否等同于numpy?全部内容,希望文章能够帮你解决python – Matlab的缓冲区是否等同于numpy?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。