Host Engineering Forum

General Category => Do-more CPUs and Do-more Designer Software => Topic started by: Bolt on January 15, 2021, 07:25:39 PM

Title: Averaging TimeStamps
Post by: Bolt on January 15, 2021, 07:25:39 PM
Say I have a MemoryBlock[V0].StopTime, an unix timestamp.  I would like to calculate the average .StopTime.  I can create a Task, Index Loop through the MemoryBlock, do an EPOCH2DT, calculate the (UDT0.Hours x 3600 + UDT0.Minute x 60 + UDT0.Seconds), and create an average "seconds into day" of that number.  It's kind of a kludge, especially when you want to do something with that number, need to do a bunch of modulus math to put it back into UDT1.Hours, Minutes, Seconds.  Also becomes even more tricky when sometimes are before midnight, and others after midnight.

Is there an easier way to do this?  Anybody have any tricks up their sleeves?
Title: Re: Averaging TimeStamps
Post by: franji1 on January 16, 2021, 09:20:04 AM
To be accurate, this must be done with integer MATH.

Take the first one and save it off as a base value.  Create a block of signed DWORDS the same size as your UDT.  Iterate through your UDT block, subtracting off the base value and storing each adjusted value in the corresponding index of the new block.

SUM up the new block, divide by the count to get THAT average as a signed integer.

Add that integer average back to your base value.  That is now the average unix date/time value.

Convert that EPOCH 2DT to get an actual time stamp
Title: Re: Averaging TimeStamps
Post by: franji1 on January 16, 2021, 09:25:41 AM
This only works because your unix values should all be close together, hence the offset values will all be relatively small, able to sum them up as a signed DWORD with 32 bits of precision. If you  tried to sump up the original values, you would have to use floating pont, which only has 24 bits of precision.
Title: Re: Averaging TimeStamps
Post by: Bolt on January 16, 2021, 11:44:32 PM
I guess I should clarify I'm looking for the average time of day, not average Unix time. I see where I mislead you, sorry. Each MemoryBlock.StopTime is daily, if you will. Say all around 23:00. Trying to guesstimate the next StopTime