2017-09-19 39 views
0

我需要通过从正弦波图中提取一个完整周期来进行一些数据分析。如何使用python切片1个正弦波的周期?

我有一些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但我并没有真正了解它最接近的一次。

谢谢你的帮助和建议。

+0

生成最小样本数据? – Divakar

+0

hi @Divakar如果你想尝试,我添加了一个CSV示例文件。 – Fang

+0

三个连续零(或中点)交叉点之间的切片。 – SiHa

回答

1

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

下面有计算上我的时间(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以确保代码在最坏的情况下工作。

+0

当然,I/O部分也可以在Pandas中完成。 – norok2

+0

谢谢你的回答。我试图在这里理解你的建议。你写'np.arrange(10000,20000,2000)'。你从索引10000开始直到19999与步骤2000对吗?你能解释你为什么使用它吗?特别是那个'2000步骤'。 – Fang

+0

我从一个比I阵列噪声波动大得多的数字开始,然后我想要有几个连续的小波步骤来确保结果不是偶然的。该算法通过建议对于不同宽度的小波变换保持一致的峰值。你可以阅读它的文档以获取更多细节。 – norok2