top of page
Writer's pictureJasmine Ge

Analyze Irregularly Distributed Data Points Using Time-Weighted Functions In Timeplus

Imagine you're tracking the temperature in your house throughout the day. Your smart thermostat could record the temperature every minute, generating 1,440 measurements daily. But does it need to? What if it only recorded when the temperature changed for 0.1 degree? This is the kind of real-world challenge we'll explore, using a battery monitoring system as our example.


In this blog, we'll introduce Timeplus' new time-weighted functions, which help solve a common problem in data analysis: how to accurately analyze measurements with a minimal amount of data points.


For visual learners, please check out the following video:




 

Let's say we're monitoring a battery's health and performance. We want to know:


  • Most of the time, at what voltage this battery is delivering?

  • What’s the average, medium or p90 (over 90% of time)?


To answer these questions, we need to track the battery's voltage over time. In our example, we'll look at how a battery's voltage drops from 5 volts to 3 volts during an hour of use.


Here's a snapshot of our data:

Voltage

Timestamp

Comments

5.0

2024-12-23 12:00:00

Initial voltage

4.95

2024-12-23 12:02:30

Rapid drop phase

4.9

2024-12-23 12:05:00


4.8

2024-12-23 12:07:30


4.7

2024-12-23 12:10:00


4.6

2024-12-23 12:15:00 


4.5

2024-12-23 12:20:00 


4.4

2024-12-23 12:30:00


4.3

2024-12-23 12:40:00


4.2

2024-12-23 12:50:00

Transition to plateau phase

4.1

2024-12-23 13:30:00

End of plateau, rapid drop begins

4.0

2024-12-23 13:32:30


3.9

2024-12-23 13:35:00


3.8

2024-12-23 13:36:30


3.7

2024-12-23 13:37:30


3.6

2024-12-23 13:38:20 


3.5

2024-12-23 13:39:00

Approaching discharge critical point

3.4

2024-12-23 13:39:30


3.3

2024-12-23 13:39:45


3.2

2024-12-23 13:39:55


3.1

2024-12-23 13:40:05


3.0

2024-12-23 13:40:12

Fully discharged


The data points can be visualized as follows. Each delta for of the timestamp of the data are even.



 

The Traditional Approach: Why It Falls Short


The simplest way to collect this data would be to take measurements at fixed intervals. Think of it like checking your watch every minute to write down the battery's voltage. To get the average voltage,  simply sum all data points and divide by their total count. 


But there's a catch. Batteries don't discharge at a constant rate. Sometimes the voltage drops quickly, other times it stays steady. If we want to catch every significant change, we'd need to measure very frequently – in our case, every 7 seconds. This gives us 859 measurements over an hour, with many redundant readings during stable periods.


It's like taking a photo every second of a sunset. During the most dramatic moments, you'll want those frequent shots. But when the sky isn't changing much, you're just filling up your camera's memory with nearly identical pictures.




A Smarter Way: Measuring What Matters


Modern battery systems are smarter. Instead of checking the voltage every few seconds, they only record when they detect a meaningful change. This is much more efficient – in our example, we only need 22 measurements instead of 859.



But this creates a new challenge. If we simply average these 22 measurements, we'll get the wrong answer. Why? Because this method treats each measurement as equally important, regardless of how long that voltage level lasted.


Think of it this way: If you spend 45 minutes at 4.2 volts and 15 minutes at 3.8 volts, a simple average would undervalue the time spent at the higher voltage.





The Solution: Introducing Time-Weighted Functions


This is where Timeplus's time-weighted functions come in. Instead of treating each measurement equally, these functions consider how long each value lasted. It's like weighing each measurement by its duration. In the language of Math, the formula of calculating average value is changed from a simple sum(v) / count(v) to a time-aware sum(v * t) / sum(t)




We offer two functions:

  • avg_time_weighted: Calculates a time-aware average

  • median_time_weighted: Finds the middle value, considering time weights


Using these functions is straightforward. Here's how you'd calculate the time-weighted average voltage:

SELECT avg_time_weighted(voltage, timestamp)
FROM battery

For the median:

SELECT median_time_weighted(voltage, timestamp)
FROM battery

Each function needs at least two pieces of information:

  1. What you're measuring (in our case, voltage)

  2. When each measurement was taken (timestamp)


Optionally, you can add a third parameter to specify an end time for your analysis period. When you omit the third parameter, the calculation excludes the last value. If provided, the end time must match the timestamp column's data type, and the function uses the difference between the last time point and this end time as the weight for the final value.


Here's the outcome for our battery example, using avg_time_weighted:

SELECT avg_time_weighted(voltage, timestamp)
FROM battery


Apparently, using the avg_time_weighted function yields a more reasonable outcome than using the avg function directly. We'll use a diagram to better illustrate this.



Similarly, if we want to calculate the median of these voltage data points, we can use median_time_weighted as well:

SELECT median_time_weighted(voltage, timestamp)
FROM battery

 

The Benefits


This approach offers several advantages:

  • More accurate analysis that reflects how long each value persisted

  • Reduced data storage needs – only store significant changes

  • Less network traffic when sending data

  • Faster processing of your measurements



Real-World Applications


While we've focused on battery monitoring, time-weighted functions are valuable in many scenarios:

  • Temperature monitoring in industrial processes

  • Network traffic analysis

  • Environmental sensor readings

  • Financial market data analysis

  • Energy consumption monitoring


 

Summary


Time-weighted functions are available in Timeplus Proton 1.6.6. Ready to try them out with your own data? Download Proton from our GitHub repo and start exploring how these functions can improve your data analysis.


Whether you're working with IoT devices, monitoring systems, or any time-series data that changes irregularly, time-weighted functions can help you get more accurate insights while using fewer resources.


 

Ready to try Timeplus Enterprise? Try for free for 30-days


Join our Timeplus Community! Connect with other users or get support in our Slack community.




bottom of page