簡體   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