[英]Is there a Python equivalent to MATLAB 'time' function?
I'm porting a MATLAB R2011b code to Python 3.5.1. 我正在将MATLAB R2011b代码移植到Python 3.5.1。 The original MATLAB code, which was written around 10 years ago, contains a 'time' function as bellow:
大约10年前编写的原始MATLAB代码包含以下“时间”函数:
t_x=time(x,fsample);
The output is: 输出为:
debug> x
x =
-0.067000 -0.067000 -0.068000 -0.069000 -0.069000 -0.070000 -0.070000 -0.071000 -0.071000 -0.072000
debug> fsample
fsample = 10000
debug> t_x
t_x =
0.00000 0.10000 0.20000 0.30000 0.40000 0.50000 0.60000 0.70000 0.80000 0.90000
I'd like to do the same thing in Python, but I cannot find any equivalent function in Python. 我想在Python中做同样的事情,但在Python中找不到任何等效函数。 (The function name 'time' is too general that it's hard to search on Google.) It seems this 'time' function returns 1000/fsample (eg, if fsample=10000, then 0.1) multiplied by the index of 'x'.
(函数名称“时间”太笼统,很难在Google上搜索。)看来,此“时间”函数返回1000 / fsample(例如,如果fsample = 10000,则为0.1)乘以“ x”的索引。 Does anyone know a similar function in Python?
有人知道Python中有类似的功能吗?
... Please note that this 'time' function is different from the 'time' function introduced in MATLAB R2014b: [ http://www.mathworks.com/help/matlab/ref/time.html?searchHighlight=time&s_tid=gn_loc_drop][1] ...请注意,此“时间”功能与MATLAB R2014b中引入的“时间”功能不同:[ http://www.mathworks.com/help/matlab/ref/time.html?searchHighlight=time&s_tid=gn_loc_drop ] [1]
It should be simple enough to implement a similar function. 实现类似功能应该足够简单。
For numpy arrays 对于numpy数组
import numpy as np
def time(x, fsample):
return np.linspace(0, (1000/fsample)*(len(x) - 1), num=len(x))
For simple python lists 对于简单的python列表
def time(x, fsample):
return [i*(1000/fsample) for i in range(len(x))]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.