Skip to content

Apply exponential moving average to remaining time when unplugged

Fushan Wen requested to merge work/fuf/sosfilt into master

This applies exponential moving average to the remaining time history records to calculate the remaining time, to mitigate the impact of a short-time heavy load on the estimation of remaining time.

CCBUG: 434432

You can use

dbus-monitor --system "type='signal',sender='org.freedesktop.UPower',path='/org/freedesktop/UPower/devices/battery_BAT0',interface='org.freedesktop.DBus.Properties',member='PropertiesChanged'" 2>&1 | sed -un '/TimeToEmpty/{n;s/.*variant.*int64//;p}' | tee rec.log

to collect the battery remaining time history records.

Real-life test data

import scipy
import matplotlib.pyplot as plt

x = (12439,7128,7802,9457,2820,6794,1435,1316,9240,9218,6661,7875,3197,7815,10920,4177,4937,5410,9131,10796,2790,8913,9024,12291,10625,4628,8936,4488,8744,10466,7358,8699,10418,8658,7397,6236,3375,8527,7196,8508,8484,8462,9328,3534,10083,10063,8360,8338,8443,9575,8274,9903,6960,8228,8208,2279,1860,2176,2153,2131,2015,1992,2159,2492,6453,6431,7477,6305,7349,8889,6328,6221,1771,6142,7262,2115,6067,3319,6537,6983,7033,8417,6431,2436,6947,6921,5910,5812,6871,5867,5789,9385,5785,6726,5687,2154,6653,5680,5583,6602,6579,6555,6533,5988,1195,2957,5423,5330,6301,6280,2181,4500,1096,6018,6061,5119,1940,2508,5095,6014,7110,1720,4936,6711,2201,6900,4846,6586,6823,4801,956,877,4722,4700,5459,4598,5436,1090,5002,1383,5241,4467,5191,4370,5167,1271,4285,5066,5045,1010,3968,4211,3844,1639,4042,4778,5711,1521,3964,733,3937,3387,3801,789,5049,1513,3641,4943,1617,5066,3039,1770,3785,1433,4078,4055,4032,2904,3935,3387,3928,2828,3343,3879,999,3783,2949,1117,3173,904,3602,4298,3047,2558,1486,3458,2174,2571,943,2831,3347,584,539,3221,2737,2714,2659,3143,3120,919,2203,2605,3017,2343,888,940,2473,2072,642,2333,441,404,912,2179,3517,1050,3153,1041,2416,1732,2048,465,885,2223,1861,2201,1863,2150,2128,1780,2104,2081,528,2387,477,1616,1592,1570,1308,1546,524,1477,1438,1656,568,1160,1820,1160,1101,1302,1279,1469,1236,1201,499,970,1518,1104,1266,898,1403,1192,1169,969,1146,1004,384,373,1546,1214,844,961,775,793,939,915,761,769,777,294,617,259,266,243,719,564,570,216,646,623,600,555,561)
plt.plot(x, label="Origin", linestyle='dashed')

sos_1 = scipy.signal.butter(4, 0.3, "lowpass", output="sos")
y_sos_1 = scipy.signal.sosfilt(sos_1, x)
plt.plot(y_sos_1, label="N=4, Wn = 0.3")

sos_3 = scipy.signal.butter(4, 0.1, "lowpass", output="sos")
y_sos_3 = scipy.signal.sosfilt(sos_3, x)
plt.plot(y_sos_3, label="N=4, Wn = 0.1")

weight = 0.05
current = x[0]
moving_average = []
for n in x:
    current = current * (1 - weight) + n * weight
    moving_average.append(current)

plt.plot(moving_average, label="moving average")


plt.legend(loc='best')
plt.ylabel("Remaining Time")
plt.show()
Edited by Fushan Wen

Merge request reports