Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

how to plot binary files?

Status
Not open for further replies.

buenos

Advanced Member level 3
Joined
Oct 24, 2005
Messages
956
Helped
40
Reputation
82
Reaction score
21
Trophy points
1,298
Location
Sunnyvale, California, USA
Activity points
9,073
Hi

I have designed an analog-to-digital converter (data acquisition) card. It samples data and I can save it into a binary file (32kS) where every byte (8-bit) represents a sample. There is nothing else in the file but samples, no header, no coordinates, no sample numbering, no x-axis markings. I need to be able to make a 2D plot where the x axis is the sample number (or byte position) and the y axis is the sample value. Not much to ask, I think.

How do i plot this using scilab (matlab) or gnuplot or any other free program? Or maybe with excel?

I spent hours in trying to make these programs to display my data without much success. Scilab doesnt like by binary files, gnuplot wants to think that the coordinate information is also embedded into the file.

what exact commands do i need to type in?
 

andre_teprom

Super Moderator
Staff member
Joined
Nov 7, 2006
Messages
9,221
Helped
1,147
Reputation
2,313
Reaction score
1,123
Trophy points
1,403
Location
Brazil
Activity points
53,681
Check WAV format.
I may be wrong but think that format only records ackquired analog value.
Sample rate is implicit and constant.
There are a lot tools ( JAVA / C++ ) that show audio files.

Another option is append a coordinate to each line ( incremental ascending numeric value ) in order to use at GNUPLOT or other kind of program.

+++
 

buenos

Advanced Member level 3
Joined
Oct 24, 2005
Messages
956
Helped
40
Reputation
82
Reaction score
21
Trophy points
1,298
Location
Sunnyvale, California, USA
Activity points
9,073
so, you suggest to manually create file format specific headers and write programs to display data?
i dont want to do a development project just for this.
Appending coordinates and converting the binary to hex or decimal would also be difficoult on a 32768-entry data file.
with existing programs like gnuplot or scilab it should be possible in a few seconds if someone knows the right commands to use.
 

andre_teprom

Super Moderator
Staff member
Joined
Nov 7, 2006
Messages
9,221
Helped
1,147
Reputation
2,313
Reaction score
1,123
Trophy points
1,403
Location
Brazil
Activity points
53,681
Sorry

I already used an proprietary WAV format; not really the Windows WAV format.

What is separator for each sample ?
Could you attach some example of your file in order to try simulate here ?

+++
 

albbg

Advanced Member level 4
Joined
Nov 7, 2009
Messages
1,248
Helped
432
Reputation
864
Reaction score
385
Trophy points
1,363
Location
Italy
Activity points
9,456
In scilab you can open a binary file using the function "mopen" and specifying the mode as 'rb' that means read binary.

f.i.: [fd,error] = mopen(filename,'rb')

then you can access the data using the function "mfscanf"
 

buenos

Advanced Member level 3
Joined
Oct 24, 2005
Messages
956
Helped
40
Reputation
82
Reaction score
21
Trophy points
1,298
Location
Sunnyvale, California, USA
Activity points
9,073
the file open seems to work:
-->file1=mopen('H:\temp\adc_input-pga_20dB.bin','rb')
file1 =
1.

but how do i get a vector of 8-BIT values out of it?
mfscanf doesnt return anthing, just an empty matrix, so i have tried mgeti but it complains about the format string. how do i specify 8-bit data in the format string? the help doesnt mention 8-bit.
-->a=mgeti(32768,'%uc',file1)
!--error 9991
Incorrect integer type: %uc

it should display my a-vector, then i would be able to plot(a).
 

albbg

Advanced Member level 4
Joined
Nov 7, 2009
Messages
1,248
Helped
432
Reputation
864
Reaction score
385
Trophy points
1,363
Location
Italy
Activity points
9,456
Could you, please, post your file or part of it ?
 

buenos

Advanced Member level 3
Joined
Oct 24, 2005
Messages
956
Helped
40
Reputation
82
Reaction score
21
Trophy points
1,298
Location
Sunnyvale, California, USA
Activity points
9,073
here is a file.
it has a rubbish waveform, i have to fix something in my circuit, but the file format will remain the same.
 

Attachments

  • adc_input-pga_20dB.zip
    5.2 KB · Views: 4

albbg

Advanced Member level 4
Joined
Nov 7, 2009
Messages
1,248
Helped
432
Reputation
864
Reaction score
385
Trophy points
1,363
Location
Italy
Activity points
9,456
I think you can try this code:

fd=mopen('adc_input-pga_20dB.bin','rb');
x=mfscanf(-1,fd,'%c');
y=ascii(x);
mclose('all');
plot(y)
 
  • Like
Reactions: buenos

    buenos

    Points: 2
    Helpful Answer Positive Rating

buenos

Advanced Member level 3
Joined
Oct 24, 2005
Messages
956
Helped
40
Reputation
82
Reaction score
21
Trophy points
1,298
Location
Sunnyvale, California, USA
Activity points
9,073
hi
in case of the new attached file, it doesnt display what is in the file, it displays something else.
i have edited it with XVI32 hex editor. basically there has to be 56 samples then zeroes until the end of file then the last 3 bytes are 55-aa-ff. Instead of this, it displays 72 samples only, and the samples from 56-72 should be zero they dont seem to be.
 

Attachments

  • swappedsamples2.zip
    275 bytes · Views: 4

    V

    Points: 2
    Helpful Answer Positive Rating

buenos

Advanced Member level 3
Joined
Oct 24, 2005
Messages
956
Helped
40
Reputation
82
Reaction score
21
Trophy points
1,298
Location
Sunnyvale, California, USA
Activity points
9,073
you mean that they are not zero. OK, but thats not the problem.
1. at the beginning of the file it shows 76 samples instead of 56. 20extra, which 20 is the extra?
2. it should display 32768 samples, regardless of the values of the samples.

---------- Post added at 21:16 ---------- Previous post was at 21:02 ----------

ok, if i zero those left over bytes at address 0x2FC8, then it displays 49 samples.
it looks like it has one big problem:
it has skipped all the zero samples.
how to prevent it from doing that?
 

albbg

Advanced Member level 4
Joined
Nov 7, 2009
Messages
1,248
Helped
432
Reputation
864
Reaction score
385
Trophy points
1,363
Location
Italy
Activity points
9,456
You are right, the problem is the ascii function. I think the procedure I suggested you is not correct. Please try the following:

fd=mopen('swappedsamples2.bin','rb');
x=0;
while(meof(fd) == 0)
x = [x mget(1,'uc',fd)];
end
x = x(2:length(x));
plot(x)
mclose('all');

In this case we are considering unsigned numbers. If you have to recover signed numbers use x = [x mget(1,'c',fd)];
 

    V

    Points: 2
    Helpful Answer Positive Rating

_Eduardo_

Full Member level 5
Joined
Aug 31, 2009
Messages
295
Helped
118
Reputation
238
Reaction score
103
Trophy points
1,323
Location
Argentina
Activity points
2,842
There's no need to read byte to byte, mget stop when reach the eof().
Just put as argument the filesize or an arbitrary value many times greater.

fd=mopen('d:\swappedsamples2.bin','rb');
x = mget(100000,'c',fd) ;
mclose('all');
plot(x)​
 
  • Like
Reactions: buenos

    buenos

    Points: 2
    Helpful Answer Positive Rating

buenos

Advanced Member level 3
Joined
Oct 24, 2005
Messages
956
Helped
40
Reputation
82
Reaction score
21
Trophy points
1,298
Location
Sunnyvale, California, USA
Activity points
9,073
thanks for you too.
what i do, is to modify the suggested text (with file path/name) in notepad then copy/paste it into scilab, then it generates the plot. If it is 2 more lines, it doesnt matter for the copy/paste. maybe it takes 3 seconds instead of 1 to generate it.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top