Python:使用单列文件跨时间打印



我必须编写一个函数,使我能够从一系列值中找到局部的最大值和最小值。

函数的数据是每个"x"的x、y;峰值";。输出是包含x、y max和min"的4个矢量;峰值";。

为了找到最大峰值,我必须";支架";在每个数据点上,并检查它是否大于或小于两侧的邻居,以便确定它是否是峰值(保存为最大/最小峰值(。两端的点只有一个邻居,不考虑用于此分析的点。

然后编写一个程序来读取数据文件,并调用该函数来计算峰值。程序必须生成一个图形,显示输入的数据和计算出的峰值。

第一个文件是(2001,(大小的float64数组。所有数据都在列0中。该文件表示信号在时间上的幅度,采样频率为200Hz。Asume初始时间为0。

图形应该像这个

程序还必须生成一个显示2个表的.xls文件;1个具有最小峰值,另一个具有最大峰值。每个表格必须有标题,由2列组成,一列显示峰值出现的时间,另一列显示每个峰值的振幅。

不允许熊猫进入。

first file is a .txt file, and is single column, 2001 rows total
0
0.0188425
0.0376428
0.0563589
0.0749497
0.0933749
0.111596
0.129575
0.147277
0.164669
0.18172
...

当前尝试:

import numpy as np
import matplotlib.pyplot as plt
filename = 'location/file_name.txt'
T = np.loadtxt(filename,comments='#',delimiter='n')
x = T[::1]  # all the files of column 0 are x vales
a = np.empty(x, dtype=array)
y = np.linspace[::1/200]
X, Y = np.meshgrid(x,y)

这符合您的要求。我不得不生成随机数据,因为你没有分享你的数据。你当然可以从最小值和最大值构建你的电子表格。

import numpy as np
import matplotlib.pyplot as plt
#filename = 'location/file_name.txt'
#T = np.loadtxt(filename,comments='#',delimiter='n')
#
#y = T[::1]  # all the files of column 0 are x vales
y = np.random.random(200) * 2.0
minima = []
maxima = []
for i in range(0,y.shape[0]-1):
if y[i-1] < y[i] and y[i+1] < y[i]:
maxima.append( (i/200, y[i]) )
if y[i-1] > y[i] and y[i+1] > y[i]:
minima.append( (i/200, y[i]) )
minima = np.array(minima)
maxima = np.array(maxima)
print(minima)
print(maxima)
x = np.linspace(0, 1, 200 )
plt.plot( x, y )
plt.scatter( maxima[:,0], maxima[:,1] )
plt.show()

最新更新