2017-09-19 42 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