简体   繁体   中英

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. From this CSV file, normally I would plot it and manually extract for one complete cycle. Now I would like to do it using 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

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.

Below there is some example code for calculating the period on 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. In your case, I would work with vv_arr instead of ii_arr . I just tested ii_arr to make sure the code was working in the worst case scenario.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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