Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count the battery cycles using rain flow counting using python

I have SOC (state of charge of a battery) when SOC 1st half cycle (0 to 100%) and 2nd half cycle (100% to 0) considering as full cycle see this figure

this is my battery SOC continuous curve

in my data there is no full cycles present there is half cycles and quarter cycles are there how to count the battery full cycles with data here is my SOC data attached google spread sheet https://docs.google.com/spreadsheets/d/126sV3E5EuqCEdrtWIWEPhUJ8-2zPHPTyJhY0WWIIoNQ/edit?usp=sharing

My script:

import rainflow
list_x=list(df["SOC"])
len(list_x)
from scipy.interpolate import splrep, splev
fig= plt.figure(figsize=(19,6))
bspl = splrep(t,list_x,s=130000)
bspl_y = splev(t,bspl)
plt.plot(t,list_x)
plt.plot(t,bspl_y)
rainflow.count_cycles(bspl_y)

is not able to count the cycles from above algorithm. Is there any to solve the problem like this? Thanks in regards

like image 549
aparna podili Avatar asked Oct 27 '25 03:10

aparna podili


1 Answers

To find the peaks, you can use the find_peaks function from SciPy insted of rainflow. For further details, take a look at the docs.

Here is the code:

import numpy as np
from matplotlib import pyplot as plt
from scipy.signal import find_peaks

data = np.loadtxt("SOC data - Sheet1.csv")

upper_peaks, _ = find_peaks(data, distance=500)
lower_peaks, _ = find_peaks(-data, distance=500)

plt.plot(data)
plt.plot(upper_peaks, data[upper_peaks], "x", c="red")
plt.plot(lower_peaks, data[lower_peaks], "x", c="red")
plt.show()

The upper_peaks and lower_peaks arrays consist of the peak indices.

enter image description here

EDIT

Regarding the rainflow library, all the details are provided in the documentation:

Full information about each cycle, including mean value, can be obtained using the extract_cycles function

So, for instance:

data = np.loadtxt("SOC data - Sheet1.csv")
t = np.arange(len(data))
spline = splrep(t, data, s=1.3e5)
data_approx = splev(t, spline)

for rng, mean, count, i_start, i_end in rainflow.extract_cycles(data_approx):
    print(rng, mean, count, i_start, i_end)

The output is the following:

17.65289793762279 54.10071414477933 0.5 0 443
25.807399155539528 58.177964753737704 0.5 443 836
27.120471015563403 57.52142882372577 0.5 836 1224
26.171554867156964 75.25703620717142 1.0 1763 2020
0.15602752160933164 61.70248949691576 1.0 2308 2346
53.98760289880129 70.95499476534471 0.5 1224 1388
26.087835226468783 59.921344374733856 1.0 3545 3860
39.56063119775213 65.70767718686292 1.0 2650 3166
54.215085367144695 70.84125353117301 0.5 1388 2516
45.21289541377841 66.34015855448988 0.5 2516 4002
30.237861475461948 73.8276755236481 0.5 4002 4477
2.421086187342951 59.9192878795886 0.5 4477 4551
like image 196
blunova Avatar answered Oct 29 '25 19:10

blunova



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!