繁体   English   中英

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

[英]How do I slice 1 cycle of sine wave using python?

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

我有一些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文件的示例 -> 示例

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

感谢您的帮助和建议。

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

下面是一些示例代码,用于计算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以确保代码在最坏的情况下都能正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM