简体   繁体   English

如何使用 Octave 将波形的 16 位 I/Q 交错样本保存到二进制文件?

[英]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.我需要使用 GNU Octave 或 Matlab 生成一个二进制文件,该文件中的数据是波形的 16 位交错 I/Q 数据(16 位实部,16 位虚部)。

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;假设生成的文件名为“output.bin”,如果我们将 bit [0:15] 读取为 little endian,我们将得到第一个样本的“I”; bit[16:31] is the "Q" of the first sample; bit[16:31] 是第一个样本的“Q”;

bit[32:47] -> I of the second sample bit[32:47] -> 第二个样本的 I

bit[48:63] -> Q of the second sample bit[48:63] -> 第二个样本的 Q

. . . . . . . . . . . .

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.但显然我从中得到的不是 16 位交错 I/Q 数据。

I'm new to Octave/Matlab, really couldn't figure it out.我是 Octave/Matlab 的新手,真的想不通。

Thanks a lot!非常感谢!

In MATLAB, see fopen( ) and fwrite( ) for writing out binary files.在 MATLAB 中,请参阅 fopen( ) 和 fwrite( ) 来写出二进制文件。 For R2018a and later, your IQData will already be in interleaved format so you can write directly, but you may need this helper routine:对于 R2018a 及更高版本,您的 IQData 已经采用交错格式,因此您可以直接编写,但您可能需要此帮助程序:

https://www.mathworks.com/matlabcentral/fileexchange/77530-freadcomplex-and-fwritecomplex 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.对于 R2017b 及更早版本,您需要在写入之前复制数据以获取交错格式。 Eg, this would get the real & imag data into interleaved format (as a real variable):例如,这会将 real & imag 数据转换为交错格式(作为实变量):

Data = [Iwave;Qwave];

Then write Data to the output file.然后将Data写入output文件。 In fact, if you were not worried about that extra data copy, this interleaved Data method will work in all versions of MATLAB.事实上,如果您不担心额外的数据副本,这种交错数据方法将适用于 MATLAB 的所有版本。

For conversion to 16-bit floating point, some options are:对于转换为 16 位浮点,一些选项是:

Half Precision: Use the MATLAB function half( ) to convert from double to half precision (R2018b or later).半精度:使用 MATLAB function half( ) 从双精度转换为半精度(R2018b 或更高版本)。 https://www.mathworks.com/help/fixedpoint/ref/half.html https://www.mathworks.com/help/fixedpoint/ref/half.html

If you have R2018a or earlier you will have to convert to half precision manually.如果您有 R2018a 或更早版本,则必须手动转换为半精度。 C Code to do this is given here: https://www.mathworks.com/matlabcentral/fileexchange/23173-ieee-754r-half-precision-floating-point-converter C 代码在这里给出: https://www.mathworks.com/matlabcentral/fileexchange/23173-ieee-754r-half-precision-floating-point-converter

bfloat16: There is no official MATLAB support for this yet. bfloat16:目前还没有官方的 MATLAB 支持。 A simple truncated method to get you close (ie, 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.一个简单的截断方法让你接近(即,没有四舍五入和没有 NaN 检查)将转换为单精度,然后选择最重要的 16 位进行写入。 Eg, something like例如,像

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

Then write b16 to your binary file.然后将 b16 写入二进制文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM