ikarosilva / wfdb-app-toolbox

The WFDB Toolbox for MATLAB/Octave is a collection of functions for reading, writing, and processing physiologic signals in the formats used by PhysioNet (see README for details).
http://physionet.org/physiotools/matlab/wfdb-app-matlab/
GNU General Public License v3.0
93 stars 46 forks source link

mat2wfdb format 32 'sporadically' writing wrong signals #127

Closed cx1111 closed 8 years ago

cx1111 commented 8 years ago

Depending on the input array, mat2wfdb may give a wrong offset. One way to make it surely fail is to input an array of integers from 1 to 130 for example.

I suspect it has to do with the 2 step 'baseline' calculation.

sami10007 commented 8 years ago

Can you please send here an example data and example code which you are doing. Please, include also "expected output" and "your output".

cx1111 commented 8 years ago

I'm working to rewrite a lot of the function right now. But now that I look at it again, it may not be something wrong with the function, but how I read it?

Try this:

clear;clc;
x=ones(10,3);x(:,1)=1:10;x(:,2)=51:60;x(:,3)=-100:-91;
mat2wfdb(x, 'testsignal', 100, 32);

What I did was compare the original x array to what I see when I use:rdsamp -r testsignal -p (from the wfdb software package). But today I noticed that if I call rdsamp -r testsignal and manually subtract the base and divide by the gain specified in the output header file, the values DO match the original x matrix in matlab. So maybe the problem is with the wfdb software package's rdsamp? I'll be investigating this week.

sami10007 commented 8 years ago

Please, see my answer here. You can use the parameter rawUnits. It is about if you should remove base and gain. The parameter takes it into account. Please, let me know what you get with rawUnits.

cx1111 commented 8 years ago

I'm actually talking about the original wfdb software package written in C and called from the terminal, which the matlab toolbox is based on.

sami10007 commented 8 years ago

Yes. Can you use Ikaro's branch? I think some parameters rawUnits>0 use the C code directly. You can access it with Ikaro's branch too.

cx1111 commented 8 years ago

So we found the problem. The output fields and files written are technically valid, except sometimes the baseline calculated by mat2wfdb exceeds the range of an integer which the WFDB library uses to store the baseline field. That's why when I do the conversion manually the values map back to the original but not when I call rdsamp -p. Baseline will be out of the int range if the input signal channel is all positive or all negative.

A possible long term solution would be to change the wfdb library to store the baseline field as a double but then it wouldn't work on 32 bit systems.

The solution for now which I'll be working on right now is to constrain the baseline chosen by mat2wfdb to within -2^31 and 2^31-1.