c++ - 在 C++ 中将 uint8_t 缓冲区转换为复杂的浮点缓冲区

标签 c++ signals signal-processing gnuradio

我正在从要解调的软件 radio 中读取 IQ 数据缓冲区。我收到的数据是一个 8 位无符号整数的缓冲区。我需要将其转换为缓冲区以键入复数 float 以解调信号(我计划使用 Liquid DSP 库)。我在转换缓冲区时遇到困难。

在 GNURadio 中,我制定了我的逻辑并将我的代码输出写入一个二进制文件,然后我可以将其用作测试的输入源。到目前为止,唯一可行的是将 uint8_t 缓冲区写入文件,对数据的其他操作会破坏逻辑。

GNURadio Block Diagram

这是我尝试过的 C++ 代码片段:

uint8_t buffer[buffersize];

uint8_t I;
uint8_t Q;

float Ifloat;
float Qfloat;

complex<float> complexsample;

ofstream myfile;
myfile.open("example.bin", ios::out | ios::app | ios::binary);

for (int x = 0; x < (buffersize/2); x++) {

    memcpy(&I, buffer + (2 * x), 1);
    memcpy(&Q, buffer + (1 + (2 * x)), 1);
    //writing I and Q above to a binary file works
        //myfile.write((char*) &I, sizeof(I));
        //myfile.write((char*) &Q, sizeof(Q));

    Ifloat = (float) I;
    Qfloat = (float) Q;
    //when I write Ifloat and Qfloat to a binary file then pass the 
    //file as an input source into the Add Const block things stop working
        //myfile.write((char*) &IIfloat, sizeof(Ifloat));
        //myfile.write((char*) &Qfloat, sizeof(Qfloat));


    Ifloat -= 127.0;
    Qfloat -= 127.0;
    //what I would do to turn the turn the unsigned value into a signed value
        //myfile.write((char*) &IIfloat, sizeof(Ifloat));
        //myfile.write((char*) &Qfloat, sizeof(Qfloat));

    complexsample.real(Ifloat);
    complexsample.imag(Qfloat);
    //what I would do to turn the I and Q floats into a single complex sample
        //myfile.write((char*) &complexsample, sizeof(complexsample));
}

最佳答案

DrPaulBrewer 编写了一个简单的程序来进行我正在寻找的精确转换。他的 GitHub 链接是 https://gist.github.com/DrPaulBrewer/917f990cc0a51f7febb5

他的源码是:

void main(int argc, char *argv[])
{
int byte1, byte2; // int -- not unsigned char -- see fgetc man page
float _Complex fc;
const size_t fc_size = sizeof(fc);
FILE *infile,*outfile;
const float scale = 1.0/128.0;
const char *infilename = argv[1];
const char *outfilename = argv[2];
if (argc<3){
printf("usage: rtlsdr-to-gqrx infile outfile\n");
exit(1);
}
// printf("in= %s out= %s \n", infilename, outfilename);
infile=fopen(infilename,"rb");
outfile=fopen(outfilename,"wb");
if ((infile==NULL) || (outfile==NULL)){
printf("Error opening files\n");
exit(1);
}
while ((byte1=fgetc(infile)) != EOF){
if ((byte2=fgetc(infile)) == EOF){
exit(0);
}
fc = scale*(byte1-127) + I*scale*(byte2-127);
fwrite(&fc,fc_size,1,outfile);
}
}

关于c++ - 在 C++ 中将 uint8_t 缓冲区转换为复杂的浮点缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26683867/

相关文章:

c++ - 无需为每个操作系统重建的插件系统?

c++ - 基于编译时元编程的定点算法。乘法溢出?

c++ - CAD程序的数据结构。一次数据鼠标拾取渲染

linux - 信号处理的安全全局状态

audio - 如何将包含纯音的音频文件转换回序列化数据?

c# - 实时获取吉他输入的频率

c++ - 使用 C/C++ 将后缀/前缀表达式显示为解析树

c - 如何为具有安全区域的功能设置时间限制

c - 发出信号并杀死 C

用神经网络分离音频信号源