Skip to main content

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.

note

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​

ParserProducesDataSample rate
Ppg2WiffParserPpg2WiffSlotModelPPG2 (green, red, infra-red)32 to 512 Hz, read from the file
AccWiffParserAccWiffSlotModelAccelerometer32 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.

warning

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:

ParserWhere run() worksWhere the listener is called
Ppg2WiffParserOn the thread that calls run(). run() returns when all files are parsedOn that same thread, before run() returns
AccWiffParserrun() 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)
}
CallbackWhen
onStartA file is about to be parsed
onNewSlotsAvailableThe next slots are decoded: usually one second of samples (of one PPG2 channel, or of the accelerometer)
onFileFinishedOne file has been fully parsed. Only AccWiffParser calls it; Ppg2WiffParser does not
onFinishEvery file in the call has been handled; receives the complete list of slots of all files
onErrorMore 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 file onStart may be called without onFileFinished.
  • 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​

FieldTypeDescription
rawIndexIntIndex of the sample within its one-second packet, starting at 1
timestampDoubleTime of the sample, UTC milliseconds
chunkIndexIntIndex of the chunk the sample belongs to
bodyPoseInt?Body position reported by the bracelet
ledPdPosInt?LED / photodiode position the sample was measured on
offsetInt?Sensor offset
expInt?Exposure
ledInt?LED drive current. A packet holds one value per quarter of the second; this is the one of the sample's quarter
gainInt?Always 2. The gain the sensor used is already applied to value
valueInt?The PPG sample value (19 bits)
metricTypePpg2TypeGREEN, RED or INFRA_RED
qualityInt?Deprecated, do not use
warning

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 per ledPdPos alone, so types sharing a photodiode position overwrote each other's last value.
note

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​

FieldTypeDescription
rawIndexIntIndex of the sample within its one-second packet, 1 to 32
timestampDoubleTime of the sample, UTC milliseconds
chunkIndexIntIndex of the chunk the sample belongs to
bodyPoseInt?Body position reported by the bracelet
valueXInt?X axis
valueYInt?Y axis
valueZInt?Z axis
qualityInt?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.