Parsing Raw Files
PPG2 and Accelerometer data are too voluminous to store as individual database rows, so the SDK writes them to binary .wiff files instead. Raw File Transfer & Export covers how those files get onto the phone and how to export them. This page covers how to decode them into samples inside your app.
The binary layout itself is documented in Raw files.
You do not need these parsers to export files, and the SDK does not parse .wiff files into the database on your behalf. Use them when your app needs the individual samples, for example to draw a waveform or run your own analysis.
The parsers
| Parser | Produces | Data | Sample rate |
|---|---|---|---|
Ppg2WiffParser | Ppg2WiffSlotModel | PPG2 (green, red, infra-red) | 32 to 512 Hz, read from the file |
AccWiffParser | AccWiffSlotModel | Accelerometer | 32 Hz |
Both are constructed with an optional logger (Ppg2WiffParser(), AccWiffParser()). The manager also has one instance of each, with the SDK's logger: manager.ppg2WiffFileParser and manager.accWiffFileParser. Both expose a single method:
fun run(files: List<File>, listener: ParserListener<T>?)
files are .wiff files of one type: the files in the app's internal folder (see Location of the files), or files you exported. The files are parsed in the order of the list.
A single run() call accepts at most 100 files. Above that the parser does no work and reports the problem through onError.
The two parsers run on different threads:
| Parser | Where run() works | Where the listener is called |
|---|---|---|
Ppg2WiffParser | On the thread that calls run(). run() returns when all files are parsed | On that same thread, before run() returns |
AccWiffParser | run() returns at once. The files are parsed on the SDK's callback scheduler (the main thread by default) | On the callback scheduler |
So call Ppg2WiffParser.run() from a background thread, never from the main thread. AccWiffParser needs the SDK to be initialized, because it reads a setting from the SDK's database before it starts.
Use one run() at a time per parser instance: start the next one after onFinish.
ParserListener
Slots are delivered as they are decoded, and once more all together at the end.
interface ParserListener<T> {
fun onStart(file: File)
fun onNewSlotsAvailable(slots: List<T>)
fun onFinish(slots: List<T>)
fun onFileFinished(file: File)
fun onError(t: Throwable)
}
| Callback | When |
|---|---|
onStart | A file is about to be parsed |
onNewSlotsAvailable | The next slots are decoded: usually one second of samples (of one PPG2 channel, or of the accelerometer) |
onFileFinished | One file has been fully parsed. Only AccWiffParser calls it; Ppg2WiffParser does not |
onFinish | Every file in the call has been handled; receives the complete list of slots of all files |
onError | More than 100 files were passed. Nothing was parsed |
A slot is one sample.
What the parsers do with a problem in a file:
- A file that does not exist, or that cannot be parsed, is skipped. This is written to the SDK logs and not reported through
onError; the parser goes on with the next file. For such a fileonStartmay be called withoutonFileFinished. - Since 3.0.0 a sample whose time is not plausible (before 2021-01-01, or more than 10 minutes after the phone's clock) is not delivered. A time packet in the file that is outside that window is ignored and the parser's clock goes on, so one bad time packet no longer misdates every sample after it.
Because onFinish receives all slots, the parser keeps every slot in memory until the next run(). Parse a few files at a time.
Parsing PPG2 files
import com.corsano.sdk.ble.data.Ppg2WiffSlotModel
import com.corsano.sdk.data.download.parsing.ParserListener
import com.corsano.sdk.data.download.parsing.Ppg2WiffParser
// The PPG2 files in the app's internal folder, oldest first.
val ppg2Files = File(context.filesDir, "ppg2")
.listFiles { _, name -> name.startsWith("PPG2_") && name.endsWith(".wiff") }
?.sortedBy { it.name }
.orEmpty()
// In a coroutine: run() blocks until the files are parsed.
withContext(Dispatchers.IO) {
Ppg2WiffParser().run(
files = ppg2Files,
listener = object : ParserListener<Ppg2WiffSlotModel> {
override fun onStart(file: File) {
Log.d("Corsano-SDK", "Parsing ${file.name}")
}
override fun onNewSlotsAvailable(slots: List<Ppg2WiffSlotModel>) {
// one second of one channel; give it to your chart or analysis
}
override fun onFileFinished(file: File) {
// not called by Ppg2WiffParser
}
override fun onFinish(slots: List<Ppg2WiffSlotModel>) {
Log.d("Corsano-SDK", "Parsed ${slots.size} PPG2 slots")
}
override fun onError(t: Throwable) {
Log.e("Corsano-SDK", "PPG2 parsing failed: $t")
}
}
)
}
Ppg2WiffSlotModel
| Field | Type | Description |
|---|---|---|
rawIndex | Int | Index of the sample within its one-second packet, starting at 1 |
timestamp | Double | Time of the sample, UTC milliseconds |
chunkIndex | Int | Index of the chunk the sample belongs to |
bodyPose | Int? | Body position reported by the bracelet |
ledPdPos | Int? | LED / photodiode position the sample was measured on |
offset | Int? | Sensor offset |
exp | Int? | Exposure |
led | Int? | LED drive current. A packet holds one value per quarter of the second; this is the one of the sample's quarter |
gain | Int? | Always 2. The gain the sensor used is already applied to value |
value | Int? | The PPG sample value (19 bits) |
metricType | Ppg2Type | GREEN, RED or INFRA_RED |
quality | Int? | Deprecated, do not use |
A channel is identified by the pair (metricType, ledPdPos), not by metricType alone: the same PPG type is reported on several photodiode positions within the same second (for example GREEN on ledPdPos 6 and 12). If you group, order or timestamp samples, key on both.
Two consequences follow, and both changed how parsed data comes out:
- Timestamps. Time is tracked per PPG type and photodiode, so each channel keeps its own clock and all channels of a chunk start on the same timestamp. Previously the clock was shared per type, so every extra channel pushed it forward by a full second and timestamps drifted by one second per second.
- Values. The rollover correction is also tracked per (type,
ledPdPos). Previously it was tracked perledPdPosalone, so types sharing a photodiode position overwrote each other's last value.
Changed in 3.0.0. Ppg2WiffParser now gives the same values and timestamps as Corsano's reference Python parser:
- Values. The gain of each quarter of a second is used to build the value again. Before, every sample measured at gain 4 was 65 536 too low.
- Timestamps. Each clock is set again from the file's time packets and file headers once it is more than 6 seconds off, and all timing is reset at the start of each file.
- A sample counted before the file's first time is no longer delivered (it was dated 1970).
If you have PPG2 data parsed by an earlier SDK version, its timestamps and some of its values differ from what the same files produce now. Re-parse the original .wiff files rather than reconciling the two.
Parsing Accelerometer files
AccWiffParser has the same run method and listener. run() returns at once and the listener is called on the callback scheduler (see The parsers):
import com.corsano.sdk.ble.data.AccWiffSlotModel
import com.corsano.sdk.data.download.parsing.AccWiffParser
AccWiffParser().run(
files = accFiles, // File(context.filesDir, "acc"): the files named ACC_….wiff
listener = object : ParserListener<AccWiffSlotModel> {
override fun onStart(file: File) {}
override fun onNewSlotsAvailable(slots: List<AccWiffSlotModel>) {}
override fun onFileFinished(file: File) {}
override fun onFinish(slots: List<AccWiffSlotModel>) {
Log.d("Corsano-SDK", "Parsed ${slots.size} accelerometer slots")
}
override fun onError(t: Throwable) {
Log.e("Corsano-SDK", "ACC parsing failed: $t")
}
}
)
AccWiffSlotModel
| Field | Type | Description |
|---|---|---|
rawIndex | Int | Index of the sample within its one-second packet, 1 to 32 |
timestamp | Double | Time of the sample, UTC milliseconds |
chunkIndex | Int | Index of the chunk the sample belongs to |
bodyPose | Int? | Body position reported by the bracelet |
valueX | Int? | X axis |
valueY | Int? | Y axis |
valueZ | Int? | Z axis |
quality | Int? | Deprecated, do not use |
Samples at the start of a file that come before the file's first time packet get their time counted back from that packet. Since 3.0.0, if a file holds no plausible time at all, its samples are not delivered.
See Accelerometer data for the units and for how to derive the movement norm.
Parsing ECG files
ECG has its own parser with a dedicated listener, and it is documented alongside the rest of the ECG flow: see ECG measurement.