Shifting data when reading from binary file using fread

I am trying to read experimental data from a binary file using fread. While the resulting data is read almost correct, the data is slightly shifting in a seemingly unpredictable manner. An example of this is given below (note that the direction of shifting is different for all signals):
fig1.png
While the result should look more like:
fig2.png
I am using the following code:
fid = fopen('sound_s3');
data = fread(fid,[1024, inf],'int32'); % reading in the 1024 signals of ~50k samples each
fclose(fid);
data = data/2^31; % converting bit to pressure
% plotting the examples
figure
plot(data(1,1:20000)');
xlabel('sample');ylabel('pressure')
figure
data = data - smoothdata(data,2,'movmean',1000);
plot(data(1,1:20000)');
xlabel('sample');ylabel('pressure')
Does anyone know what could cause this issue? I have tried basically all 'fread' options, and I am sure that the data is stored in 32 bit precision.
Note that I am sure that the signal is measured and should look like the second image.
Thanks in advance!

11 Comments

Did you try using big endian?
Walter's probably got it but just in case there's something really funky going on; give us the vendor datasheet that describes the data format used by the data acq card/system.
It isn't big endian, I had tried that already.
I am not sure what excactly the vendor datasheet should contain but I found the following about the data acq system:
Decimator Normalization Factor: 8.003663365181634
Fixed Point Fraction Length: 31
Sampling Frequency: 46875
I am not exactly sure what a Dec. Norm. Factor is and will look into it now. Might it be the cause of my issues?
Comparing that information to the plot, what I suspect is that you need to read the values with 'int32=>double', then divide by 2^31, and multiply by 8.003663365181634 . However it would not surprise me if instead what you need to do is read as '*uint32', and then
neg = data >= 2^31;
data(neg) = data(neg) - 2^31;
scaled = double(data) ./ 2^31 * 8.003663365181634;
scaled(neg) = -scaled(neg);
Me, neither, by the exact name w/o any more context.
What I see is Fixed Point Fraction that makes me think isn't just an integer but a float in an integer represenation.
Who's the vendor/what's the device? Somewhere there's got be sample code to read the data; oftentimes there will be a Matlab version, too.
@Walter Roberson, in my case I care mostly about relative values. So a scaling factor does not solve my problem. Both reading as 'int32=>double' and '*uint32' did not change the values.
@dpb, the device I am using is:
Specs:
However, they have a built in analysis tool. For my specific goal I can not use this tool, but I assume this is why it is not easy to find any sample code.
They also want you to buy a Credit package as a requirement for analyzing measurements using their tool. Which might be another reason why this information is not easily accesible.
The architecture is a bit messy; and Yes, they don't want people doing their own analysis.
Didn't see a thing that described the format...that's simply rude.
At this point unless you can find some other Rosetta stone, would seem next step would be to take a file and decode it with their tool and reverse engineer the file structure with the few hints that are given....or, find a non-proprietary solution.
I got lost in the question of bit depth. One part says that the daq are single bit and then a second later it was talking about 32 bits per sample.
Thanks for the help anyways! I will try to look if I can further reverse engineer the files. If I find anything I will let you know.

Sign in to comment.

Answers (0)

Products

Release

R2019a

Asked:

on 29 May 2019

Commented:

on 30 May 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!