gpt4 book ai didi

python - 如何使用 python 对正弦波的 1 个周期进行切片?

转载 作者:行者123 更新时间:2023-12-01 02:35:24 25 4
gpt4 key购买 nike

我需要通过从正弦波图中提取一个完整的周期来进行一些数据分析。

我有一些 CSV 文件,其中包含 100K 电流和电压值。通常我会从这个 CSV 文件中绘制它并手动提取一个完整的周期。现在我想使用 python 来做到这一点

import pandas as pd

file_path = "/Users/Fang/workspace_wind/test_cycle/the_data/"

## read csv file, selecting the first column only that is I(current)
df = pd.read_csv(file_path+"current1.csv", usecols=[0])

## find the maximum value, for the index use idxmax()
max_val = df['I'].idxmax()
## find the minimum value, for the index use idxmin()
min_val = df['I'].min()


print max_val

我从这段代码开始。到目前为止,我设法了解如何获得半个周期的最高值和最低值。首先,我想将其从第一个最高值切片到第二个最高值(峰到峰)一个完整的周期,但由于幅度并不总是相同,所以我的这种方法不起作用。

这是 CSV 文件的示例 --> sample

到目前为止我发现的最接近的是这个问题here但我并没有真正理解它。

感谢您的帮助和建议。

最佳答案

我会在 NumPy/SciPy 中通过获取两个信号之一的最大值来完成此操作,例如I 或 V,因为(周期性)函数的周期可以定义为两个连续最大值之间的间隔。

下面是一些计算 I (ii_arr) 周期的示例代码:

import numpy as np
import scipy as sp
import scipy.signal

# load the data and define the working arrays
# note the `.transpose()` at the end
ii_arr, vv_arr = np.loadtxt(
'./Downloads/current1.csv', delimiter=',', skiprows=1).transpose()

# since a period is, for example, defined from maximum to maximum
# get maxima indexes of `ii_arr`, the same will work for `vv_arr`.
# (you may want to tweak with the second arguments, see the docs for that)
ii_max_val = scipy.signal.find_peaks_cwt(
ii_arr, np.arange(10000, 20000, 2000))

# just use normal slicing for the first two peaks
ii_period_arr = ii_arr[ii_max_val[0]:ii_max_val[1]]

# ... or for more averaged result
index_diff = int(np.mean(np.diff(ii_max_val)))
# `index_start` can be just about any other valid value
index_start = ii_max_val[0]
ii_period_arr = ii_arr[index_start:index_start + index_diff]

# optionally plot the results
import matplotlib.pyplot as plt
plt.plot(ii_period_arr)
plt.show()

物理学家注:如果 I(t)V(t) 是来自同一设备的信号,这意味着您可以假设 t 两者是相同的,所以我会使用噪声较小的信号来检测周期,并且它们的索引差必须相同。就您而言,我将使用 vv_arr 而不是 ii_arr。我刚刚测试了 ii_arr 以确保代码在最坏的情况下也能正常工作。

关于python - 如何使用 python 对正弦波的 1 个周期进行切片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46297102/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com