2
$\begingroup$

I have a dataset that contains occurrences of the Kettle in a single-occupancy house for the duration of a month. In this dataset, ‘ts’ column indicates the unix-timestamp (this can be converted to date and time) that the kettle was turned ON; and the ‘second’ column indicates the duration in seconds that the kettle was ON.

My requirement - Find routines in the data, evaluate their periodicities and variabilities. For instance, a routine could be “breakfast”, its periodicity might be every day at a certain time and variability might be a time window within the routine occurred.

What I've tried and I'm a bit unsure of - I plotted the autocorrelation of the virgin signal with different amounts of lag and found there was no significant correlation with any of its lagged versions. But on looking at the plot of this signal, I see there are peaks every day- morning time. Also, I've tried autocorrelation on the denoised(denoised using KNN) version and the under-sampled versions(retaining just first and last samples for each day of the month) and found that the denoised signal also isn't correlated with its lagged versions, but the under-sampled signal is.

I'm unsure first of all about how to find periodicity of a signal like this, and if the autocorrelation plot lets us make any conclusions about periodicity.

from statsmodels.tsa.stattools import acf
from statsmodels.graphics.tsaplots import plot_acf

# Compute the acf array 
acf_array = acf(q4['seconds'])
print(acf_array)

# Plot the acf function
plot_acf(q4['seconds'],alpha=0.05)
plt.show()

More code showing what I've read can be found at - this link. enter image description here

$\endgroup$

2 Answers 2

2
$\begingroup$

One option could be applying the Fourier transform which transforms time to the frequency domain. It is often easy to find recurring patterns when signals are represented in the frequency domain.

$\endgroup$
1
$\begingroup$

How about this answer here? In the accepted answer the procedure is described in detail along with an explanation on how to interpret the final result.

Edit:

The main idea is to try and catch the period of the signal by performing a convolution of the function with itself, as the convolution features peaks at each multiple of the period (see also this page).

The accepted answer is taking the data, rounding them (though it is not necessary), subtracting the mean value in order to avoid a peak of the Fourier transform and then apply the self convolution. Then one needs to adjust the plot in order to clearly see periodicity.

import numpy as np
import scipy.signal

from matplotlib import pyplot as plt

L = np.array([2.762, 2.762, 1.508, 2.758, 2.765, 2.765, 2.761, 1.507, 2.757, 2.757, 2.764, 2.764, 1.512, 2.76, 2.766, 2.766, 2.763, 1.51, 2.759, 2.759, 2.765, 2.765, 1.514, 2.761, 2.758, 2.758, 2.764, 1.513, 2.76, 2.76, 2.757, 2.757, 1.508, 2.763, 2.759, 2.759, 2.766, 1.517, 4.012])
L = np.round(L, 1)
# Remove DC component
L -= np.mean(L)
# Window signal
#L *= scipy.signal.windows.hann(len(L))

fft = np.fft.rfft(L, norm="ortho")

def abs2(x):
    return x.real**2 + x.imag**2

selfconvol=np.fft.irfft(abs2(fft), norm="ortho")
selfconvol=selfconvol/selfconvol[0]

# This figure does not look right as its size is not a multiple of the period
plt.figure()
plt.plot(selfconvol) 
plt.savefig('first.jpg')
plt.show()


# let's get a max, assuming a least 4 periods...
multipleofperiod=np.argmax(selfconvol[1:len(L)//4])
Ltrunk=L[0:(len(L)//multipleofperiod)*multipleofperiod]

fft = np.fft.rfft(Ltrunk, norm="ortho")
selfconvol=np.fft.irfft(abs2(fft), norm="ortho")
selfconvol=selfconvol/selfconvol[0]

plt.figure()
plt.plot(selfconvol)
plt.savefig('second.jpg')
plt.show()

(Code copied and pasted from the answer linked -- I have tried and look after all the issues with my version of Python, 3.10.8)

$\endgroup$
1
  • 1
    $\begingroup$ @Ethan Thank you, just edited my answer. Hope that helps! $\endgroup$
    – Enrico
    Commented Nov 4, 2022 at 0:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.