Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct determination of adc_gain and baseline in to_wfdb #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

bemoody
Copy link

@bemoody bemoody commented Sep 1, 2022

hen exporting waveform data to WFDB format, we want to preserve the resolution of the original source tracks as best we can.

This is important for analysis of the data (avoiding artificial noise created by re-scaling the sample values), converting the results into other formats without degradation, and also has benefits for data compression.

(wfdb-python doesn't currently support saving compressed signals, but I'm working on it. You can convert to compressed WFDB format by using wfdb.wrsamp and then using snip to compress the result.)

If the original source tracks in the .vital file are stored in integer format, it's easy - we can just write out those integer values and set the gain and baseline accordingly.

For tracks stored in floating-point formats, I don't know whether the original measurement resolution is known. So far, I've only looked at the file 0001.vital, which contains two floating-point tracks (BIS/EEG1_WAV and BIS/EEG2_WAV). Both of those have trk.gain = 1.0 although the actual resolution appears to be 0.05 µV. The code given here works for these two examples, but the algorithm is not great.

All other issues aside, wfdb-python currently has a bug (MIT-LCP/wfdb-python#418) that is screwing up conversion using wfdb.wrsamp with p_signal. So here I've changed the code to use d_signal instead.

This is just a first attempt; comments welcome!

When exporting signals to WFDB format, we want to preserve the
resolution of the original source values.

If the original sample values are stored as integers (trk.fmt > 2),
then we want to preserve the original samples, and just set the WFDB
gain and baseline accordingly.

If the original sample values are stored as floating-point numbers
(trk.fmt <= 2), then try to guess what the original measurement gain
and baseline might have been.  This uses a simple brute-force search.

Currently this assumes a maximum resolution of 16 bits, as to_wfdb
currently uses 16-bit output format.  It might be desirable to support
higher resolutions in the future, although for higher resolutions the
brute-force method becomes impractical.
@bemoody
Copy link
Author

bemoody commented Sep 2, 2022

The code given here works for these two examples, but the algorithm is not great.

Actually, sorry, I made a mistake and the code doesn't work! In line 779:

    values = np.isfinite(values)

what I meant was values = values[np.isfinite(values)].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant