Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
392 views
in Technique[技术] by (71.8m points)

matlab - How to save 16-Bit I/Q interleaved samples of a waveform to binary file using Octave?

I need to generate a binary file using GNU Octave or Matlab, the data in that file is 16 bit interleaved I/Q data (16 bit real component, 16 bit imaginary component) of a waveform.

Say the generated file is called "output.bin", and if we read bit [0:15] as little endian, we'll get the "I" of the first sample; bit[16:31] is the "Q" of the first sample;

bit[32:47] -> I of the second sample

bit[48:63] -> Q of the second sample

. . . . . .

All I could found is something like this:

points = 1000;

% Determines the frequency offset from the carrier
cycles = 101;
phaseInc = 2*pi*cycles/points;
phase = phaseInc * (0:points-1);

% Create an IQ waveform
Iwave = cos(phase);
Qwave = sin(phase);
IQData = Iwave+1i*Qwave;
IQData = IQData(:)';

save -binary output.bin IQData

. . . . . .

But apparently what I'll get from this is not 16-bit interleaved I/Q data.

I'm new to Octave/Matlab, really couldn't figure it out.

Thanks a lot!

question from:https://stackoverflow.com/questions/65946989/how-to-save-16-bit-i-q-interleaved-samples-of-a-waveform-to-binary-file-using-oc

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In MATLAB, see fopen( ) and fwrite( ) for writing out binary files. For R2018a and later, your IQData will already be in interleaved format so you can write directly, but you may need this helper routine:

https://www.mathworks.com/matlabcentral/fileexchange/77530-freadcomplex-and-fwritecomplex

For R2017b and earlier you would need to copy the data to get it in interleaved format before you did the writing. E.g., this would get the real & imag data into interleaved format (as a real variable):

Data = [Iwave;Qwave];

Then write Data to the output file. In fact, if you were not worried about that extra data copy, this interleaved Data method will work in all versions of MATLAB.

For conversion to 16-bit floating point, some options are:

Half Precision: Use the MATLAB function half( ) to convert from double to half precision (R2018b or later). https://www.mathworks.com/help/fixedpoint/ref/half.html

If you have R2018a or earlier you will have to convert to half precision manually. C Code to do this is given here: https://www.mathworks.com/matlabcentral/fileexchange/23173-ieee-754r-half-precision-floating-point-converter

bfloat16: There is no official MATLAB support for this yet. A simple truncated method to get you close (i.e., without the round-to-even and without NaN checking) would be to convert to single precision and then pick off the most significant 16-bits for writing. E.g., something like

u16 = typecast(single(Data),'uint16');
b16 = u16(1:2:end);

Then write b16 to your binary file.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...