how to convert into C++ matlab

Nov 2, 2016 at 7:24pm
I have this binary data (data.dat) with 320MB, in this data contains 32e7 line of hex number, such as:
1312cf60 d9 ff e0 ff 05 00 f0 ff 22 00 2f 00 fe ff 33 00 |........"./...3.|
1312cf70 00 00 00 00 f4 ff 1d 00 3d 00 6d 00 53 00 db ff |........=.m.S...|
1312cf80 b7 ff b0 ff 1e 00 0c 00 67 00 d1 ff be ff f8 ff |........g.......|
1312cf90 0b 00 6b 00 38 00 f3 ff cf ff cb ff e4 ff 4b 00 |..k.8.........K.|
....

original like this:
(16,-144)
(-80,-64)
(-80,16)
(16,48)
(96,95)
(111,-32)
(64,-96)
(64,-16)
(31,-48)
(-96,-48)
(-32,79)
(16,48)
(-80,80)
(-48,128)
...

I have a matlab code which can be read it as real value number, and can be easily make them as a complex number:

nsamps = (256*1024);
for i = 1:305
nstart = 1 + (i - 1) * nsamps ;
fid = fopen('data.dat');
fseek(fid,4 * nstart ,'bof');
y = fread(fid,[2,nsamps],'short');
fclose(fid);
x = complex(y(1,:),y(2,:));

I am using C++ and trying to get data as a vector <complex<float> >:

std::ifstream in('data.dat', std::ios_base::in | std::ios_base::binary);
fseek(infile1, 4*nstart, SEEK_SET);
vector<complex<float> > sx;
in.read(reinterpret_cast<char*>(&sx), sizeof(int));


and very confuse to get complex data using C++. Can anyone give me a help?

Thank you very much,

Nate Duong.






Nov 3, 2016 at 7:24am
You cannot read a vector this way. In mathlab it looks like it reads an array (or better vector) of numbers as short and then stores it in the complex container. Do the same c++.

1
2
std::vector<short> buff{nsamps};
fread(buff.data(),sizeof(short),buff.size(),infile1);
Topic archived. No new replies allowed.