简体   繁体   English

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

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

I need to do some data analysis by extracting one complete cycle from a sine wave graph. 我需要通过从正弦波图中提取一个完整的周期来进行一些数据分析。

I have some CSV file consists of like 100K Current and Voltage value. 我有一些CSV文件,例如100K的电流和电压值。 From this CSV file, normally I would plot it and manually extract for one complete cycle. 通常,我将从这个CSV文件中对其进行绘制并手动提取一个完整的周期。 Now I would like to do it using python 现在我想用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

I started with this code. 我从这段代码开始。 So far, I managed to understand how to get the highest value and the lowest value for half the cycle. 到目前为止,我设法了解了如何在半个周期内获得最高值和最低值。 At first, I want to slice it from the first highest value to the second highest value (peak to peak) for a one complete cycle but since the amplitude is not always the same, this method of mine is not going to work. 首先,我想在一个完整的周期内将其从第一个最大值切为第二个最大值(从峰到峰),但是由于振幅并不总是相同,因此这种方法无法奏效。

This is the example of the CSV file --> sample 这是CSV文件的示例 -> 示例

The closest that I found so far is this question here but I did not really understand it. 到目前为止,我发现的最接近的问题是这里的问题但我并没有真正理解它。

Thank you for your help and suggestion. 感谢您的帮助和建议。

I would do it in NumPy / SciPy by obtaining the maxima of one of the two signals, eg I or V, since the period of a (periodic) function can be defined as the interval between two consecutive maxima. 我可以通过获取两个信号之一(例如I或V)的最大值来在NumPy / SciPy中执行此操作,因为(周期性)函数的周期可以定义为两个连续最大值之间的间隔。

Below there is some example code for calculating the period on I ( ii_arr ): 下面是一些示例代码,用于计算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()

Physicist's note: if they are I(t) and V(t) are signals from the same device, this means you could assume that the t is the same in both, so I would use the signal with less noise for the detection of the period and their index difference must be the same. 物理学家的注释:如果I(t)V(t)是来自同一设备的信号,则意味着您可以假设两者中的t相同,因此我将使用噪声较小的信号来检测周期和它们的索引差必须相同。 In your case, I would work with vv_arr instead of ii_arr . 在您的情况下,我将使用vv_arr而不是ii_arr I just tested ii_arr to make sure the code was working in the worst case scenario. 我只是测试了ii_arr以确保代码在最坏的情况下都能正常工作。

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

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