Calculating CMMotionActivity interval - ios

I'm trying to calculate the time interval for a given CMMotionActivity.
A CMMotionActivity is a CMLogItem subclass, and as such - has a the property: #property(readonly, nonatomic) NSTimeInterval timestamp.
This time stamp is defined as:
The time stamp is the amount of time in seconds since the phone
booted.
CMMotionActivity also has the property #property(readonly, nonatomic) NSDate *startDate.
I'm trying to figure out what is the interval for a given activity, aiming to come up with how much time the user was running or walking.
I'm using historical data (not reading events in real time).
I tried calculating the difference between motion events, but that seems wrong as I'm getting events in 5 second intervals but sometimes in much larger intervals.
Is there a way to calculate the "endDate" for a given CMMotionActivity?
I can't seem to find a way to do that in a reliable method either by observing a single event or a chain of events.

I believe a CMMotionActivity is generated every time the state of motion changes. Assuming this, given two CMMMotionActivity objects you can calculate the duration between the two events thereby determining how long the activity of stationary/walking/running/driving/uknowning was.

At least as of iOS 14, each CMMotionActivity appears to cover a period that is ended by the next CMMotionActivity record's .startDate.
Sometimes that next record will be another record with an indicated activity classification. At other times, especially if the classifier can't decide what the next activity is but knows the preceding activity has ended it will be a record with none of the activity bits set that only serves to end the preceding record.
For instance, record #1's stationary period below is ended by the unclassified record #2, whereas record #3's walking period is ended by record #4's stationary period starting:
CMMotionActivity <startDate,2021-02-22 12:01:22 +0000,confidence,2,unknown,0,stationary,1,walking,0,running,0,automotive,0,cycling,0>
CMMotionActivity <startDate,2021-02-22 12:51:17 +0000,confidence,2,unknown,0,stationary,0,walking,0,running,0,automotive,0,cycling,0>
CMMotionActivity <startDate,2021-02-22 12:51:24 +0000,confidence,2,unknown,0,stationary,0,walking,1,running,0,automotive,0,cycling,0>
CMMotionActivity <startDate,2021-02-22 12:52:08 +0000,confidence,2,unknown,0,stationary,1,walking,0,running,0,automotive,0,cycling,0>

Related

A Time Series with a strange behaviour

I hope everyone is doing well.
I am working on a time series project to predict hourly the waiting time (idle time) of a zone.
The idle time of a zone at a given hour is the average idle time of vehicles that start to wait at the given hour in that zone, and the idle time of a vehicle is the amount of time a vehicle should wait in that zone to be booked. For example, if we predict at 16h00 for zone A, a value of 90 minutes, it means a vehicle that starts to wait in this zone between 16h00 and 17h00 will wait 90 minutes to be booked.
For our idle time (our ground truth), at a given hour B, we have to wait 2 days (48 hours) to establish the complete ground truth value for hour B since we have to wait a maximum of two days for vehicles that start to wait at B and are not booked yet. So each time we want to make a prediction, the last 48 points are unstable. For example, if we want to make a prediction at time n, the ground truth of n-1 is partial and incomplete, and we have to wait 48–1 = 47 hours to establish the final value of the waiting time at n-1.
We can resume that problem as the recent past data at prediction time is changing and not fixed.
The following image illustrates what I explained above.enter image description here
My questions are :
Is this kind of behaviour known in the time series field? If that's the case, does it have a specific name?
2-How to mix stable and unstable points in order to make accurate predictions?
Any suggestions? and thank you ahead of time:)

Measure (frequency-weighted) sound levels with AudioKit

I am trying to implement an SLM app for iOS using AudioKit. Therefore I need to determine different loudness values to a) display the current loudness (averaged over a second) and b) do further calculations (e.g. to calculate the "Equivalent Continuous Sound Level" over a longer time span). The app should be able to track frequency-weighted decibel values like dB(A) and dB(C).
I do understand that some of the issues im facing are related to my general lack of understanding in the field of signal and audio processing. My question is how one would approach this task with AudioKit. I will describe my current process and would like to get some input:
Create an instance of AKMicrophone and a AKFrequencyTracker on this microphone
Create a Timer instance with some interval (currently 1/48_000.0)
Inside the timer: retrieve the amplitude and frequency. Calculate a decibel value from the amplitude with 20 * log10(amplitude) + calibrationOffset (calibration offset will be determined per device model with the help of a professional SLM). Calculate offsets for the retrieved frequency according to frequency-weighting (A and C) and apply these to the initial dB value. Store dB, dB(A) and dB(C) values in an array.
Calculate the average for arrays over the give timeframe (1 second).
I read somewhere else that using a Timer this is not the best approach. What else is there that I could use for the "sampling"? What exactly is the frequency of AKFrequencyTracker? Will this frequency be sufficient to determine dB(A) and dB(C) values or will I need an AKFFTTap for this? How are values retrieved from the AKFrequencyTracker averaged, i.e. what time frame is used for the RMS?
Possibly related questions: Get dB(a) level from AudioKit in swift, AudioKit FFT conversion to dB?

iOS prevent date/time update?

a relatively simple question that I've not been able to find a clear answer to. My app is more complex, but answering this question will suffice.
Suppose you're writing a stopwatch app. When the user taps "start", the app stores the current date and time in startTime:
startTime = [NSDate date];
When the user tapes "stop", the app stores the current date and time in stopTime:
stopTime = [NSDate date];
The duration is calculated by:
duration = [stopTime timeIntervalSinceDate:startTime];
and is displayed with something like:
[durationLabel setText:[NSString stringWithFormat:#"%1.2f", duration]];
The typical durations that my app is timing range from 2 to 50 seconds. I need accuracy to 1/100th of a second (e.g. 2.86 seconds).
I'm assuming that there is some protocol that iOS devices use to keep their clocks accurate (cellular or NTP sources?). My concern is that between starting and stopping the stopwatch, the clock on the iOS device is updated which can result in a shift of the current date/time either ahead or back. If this were to happen, the duration calculated would be inaccurate.
I've seen a few posts relating to timing methods for purposes of improving code efficiency. Some suggest using mach_time.h functions, which I'm not familiar with. It's not obvious to me which is the best approach to use.
Is it possible to disable iOS from updating the date & time? Is mach_absolute_time() unaffected by iOS clock updates?
Many thanks!
Tim
You are correct in thinking that CFAbsoluteTime and its derivatives (NSDate dateand so on) are potentially skewed by network updates on 'real' time. Add that to the fact that NSTimer has an accuracy of 50-100ms and you have a timer that is not suited to the most critical of time-sensitive operations.
The answer to this problem seems to be CACurrentMediaTime.
It is a member of the Core Animation group, but there shouldn't be any problem integrating it into non-animation based applications.
CACurrentMediaTime is a wrapper of mach_absolute_time() and makes sense of the "mach absolute time unit," which from my understanding is no fun to tinker with. mach_absolute_time() is calculated by running a non-network synced timer since the device was last booted.
There is relatively little information on CACurrentMediaTime but here are some sources and further reading:
Apple's sparse documentation of CACurrentMediaTime
Stack Overflow - NSTimer vs CACurrentMediaTime()
http://bendodsonapps.com/weblog/2013/01/29/ca-current-media-time/
http://blog.spacemanlabs.com/2011/09/all-in-the-timing-keeping-track-of-time-passed-on-ios/
http://forum.sparrow-framework.org/topic/accurate-timer
Note: If you do use CACurrentMediaTime, make sure you include and link the QuartzCore.framework
Check out this here. I would say forget about the current time check and use a precision timer since it won't rely on the current time but instead uses an interval.

How to giva a time accurate timer in Fire Monkey?

I have to display a timer in 10th second for a sport competition. I have do this using the OnTimer event of a TTimer. the interval is set to 100. My routine display the current min:sec.10th (ex.: 02:45.7 ) correctly but it seem that my timer loose about 4 second at each minutes if I comp. to normal clock.
There is a better way to get a time accuracy timer in Delphi XE2 (or XE3) ?
You can use a timer to display the current value of the clock, but use a different approach to calculate the elapsed time.
You have to know that Windows timers are not time accurate, and even if you set it to elapse every 100 milliseconds, it can take more to fire the OnTimer event and even it can miss some intervals if for some reason elapses two or more times before your application process it.
You can, for example, use the system high-resolution performance counter to track times with nano-second accuracy.
You can also use the Delphi TStopwatch class, which encapsulates the system calls and falls back to other method (GetTickCount) if the high resolution performance counter is not available in your machine.
Take also a look at the How to Accurately Measure Elapsed Time Using High-Resolution Performance Counter delphi.about.com article.

Esper (C.E.P.) query to calculate candlesticks every full minute

I am using Complex Event Processing (Esper) technology to provide a real-time candlestick calculations in my system. I am doing fine with calculating values, however I find it difficult to ensure that candle window starts at full minutes (for one minute candle) and ends before the next minute starts (i.e. candle 1[06:00.000 - 06:00.999], candle 2[06:01.000 - 06:01.999], etc... ).
Is there a pattern or command in Esper's query language that is able to provide such functionality?
I'd appreciate constructive comments and directions.
In Esper you can use a pattern to fire every minute at the zero second, i.e.
insert into TriggerEvent select * from pattern[pattern[every timer:interval(1 min).]
// named window to hold candle data, compute next candle
on TriggerEvent select * from NamedWindowCandle ....
// delete old data
on TriggerEvent delete from NamedWindowCandle
-rg
Local time is often different from exchange time, also there is latency in delivering tick data. Minute bars are often computed using exchange timestamp. The exchange timestamp value must be extracted from tick events. New minute bar event is sent when the tick timestamps enter new minute.

Resources