import pandas as pd import os from scipy import integrate, signal import numpy as np import matplotlib import matplotlib.pyplot as plt matplotlib.rcParams['font.sans-serif'] = ['SimHei'] # 显示中文 matplotlib.rcParams['axes.unicode_minus'] = False # 用来正常显示正负号 #y = pd.read_excel(r'C:\Users\ppddcsm\Desktop\第一批数据拆分\第一批1号1振\A1-1-600.xlsx', usecols=[1],index_col=False, header=None ,skiprows=[0]) folder_path = r'C:\Users\ppddcsm\Desktop\第二批数据拆分\第二批1号1振' file_names = os.listdir(folder_path) for file_name in file_names: file_path = os.path.join(folder_path, file_name) y = pd.read_excel(file_path) N = len(y) fs = 1280 dt = 1/fs t_axis = [i * dt for i in range(len(y))] # 时间轴 y1 = y.swapaxes(0, 1) # 矩阵转置 data = y1.fillna(-1).values #获取数据,将缺失值标记设置为-1,并转换为NumPy数组对象 t = data.flatten() # 展平数组 a = np.array(t) # 梯形法 cumtrapz累计计算积分,cumtrapz(y, x=None, dx=1.0, axis=-1, initial=None)。y: 需要被积分的数值序列;x: y中元素的间距,积分变量,若为空,则y元素的间距默认为dx; # 续:dx: 如果x为空,y中元素的间距由dx给出;axis: 确定积分轴;initial: 如果提供,则用该值作为返回值的第一个数值。 y_int = integrate.cumtrapz(np.array(a), x=None, dx=0.00078125, initial=0)*1000 # m到mm转换要乘1000 result = signal.detrend(y_int) # 去趋势 plt.figure(figsize=(16, 6)) plt.subplot(121) plt.plot(t_axis, y, label="原始加速度信号") plt.ylabel("m/s^2") plt.legend(loc="upper right") plt.subplot(122) plt.plot(t_axis, y_int, label="积分后的速度信号") plt.ylabel("mm/s") plt.legend(loc="upper right") plt.figure(figsize=(8, 6)) plt.plot(t_axis, result, label="去趋势后的速度信号") plt.ylabel("mm/s") plt.legend(loc="upper right")
时间: 2024-04-19 08:27:35 浏览: 187
这段代码的作用是读取指定文件夹下的 Excel 文件,进行数据处理和绘图。
首先,它导入了所需的库,包括 `pandas`、`os`、`scipy`、`numpy` 和 `matplotlib`。
然后,它定义了文件夹路径 `folder_path` 和获取该路径下所有文件名的列表 `file_names`。
接下来,通过遍历文件名列表,使用 `os.path.join()` 函数将文件名和文件夹路径拼接成完整的文件路径,然后使用 `pd.read_excel()` 函数读取 Excel 文件的数据。
之后,它计算了一些变量,如数据长度 `N`、采样频率 `fs`、时间步长 `dt` 和时间轴 `t_axis`。
然后,它对数据进行一些转换和处理,将数据转置为列向量,将缺失值标记为-1,并转换为 NumPy 数组。
接下来,它使用 `integrate.cumtrapz()` 函数对数组进行积分,并乘以1000将单位从 m 转换为 mm。
然后,它使用 `signal.detrend()` 函数去除积分后的速度信号的趋势。
最后,它使用 `matplotlib.pyplot` 绘制了三个子图,分别是原始加速度信号、积分后的速度信号和去趋势后的速度信号。
如果你有具体的问题或需要进一步的帮助,请告诉我。
阅读全文
相关推荐













