如果你看看上面@Kevin Reid发布的流程图,你可以看到它将输入数据减去127,乘以0.008,并将对转换为复数。
缺少的是确切的类型。它在the GNU Radio FAQ。从那里我们了解到uchar是一个无符号字符(8位),复杂的数据类型是python中的'complex64'。
如果numpy的进行,如在内存中的操作,它看起来像这样:
import numpy as np
import sys
(scriptName, inFileName, outFileName) = sys.argv;
ubytes = np.fromfile(inFileName, dtype='uint8', count=-1)
# we need an even number of bytes
# discard last byte if the count is odd
if len(ubytes)%2==1:
ubytes = ubytes[0:-1]
print "read "+str(len(ubytes))+" bytes from "+inFileName
# scale the unsigned byte data to become a float in the interval 0.0 to 1.0
ufloats = 0.008*(ubytes.astype(float)-127.0)
ufloats.shape = (len(ubytes)/2, 2)
# turn the pairs of floats into complex numbers, needed by gqrx and other gnuradio software
IQ_data = (ufloats[:,0]+1j*ufloats[:,1]).astype('complex64')
IQ_data.tofile(outFileName)
我已经测试从rtl_sdr文件格式,这个翻译的gqrx IQ样本输入文件格式,它似乎在适合记忆的内容中正常工作。
但请注意,此脚本仅适用于数据,其中均为输入和输出文件可放入内存。对于大于系统内存大约1/5的输入文件,sdr记录容易超过,最好一次读取一个字节。
我们可以通过使用循环时间读取数据1个字节避免内存高耗能,与GNU C.以下程序这不是最干净的代码,我也许应该补充fclose
和检查ferror
,但它适用于爱好目的。
#include <complex.h>
#include <stdio.h>
#include <stdlib.h>
// rtlsdr-to-gqrx Copyright 2014 Paul Brewer KI6CQ
// License: CC BY-SA 3.0 or GNU GPL 3.0
// IQ file converter
// from rtl_sdr recording format -- interleaved unsigned char
// to gqrx/gnuradio .cfile playback format -- complex64
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);
}
}
顺便提一句,流程图看起来像是可以用python和numpy写得很平常的东西,但是我们不会去玩gnuradio伴侣GUI。 – Paul 2014-10-14 03:38:08
我有一个gqrx问题,使用流图的capture.cfile输出和rtl_sdr记录的capture.raw。来自gqrx的解调音频非常慢。其他人也报道了这一点。解决方案是在gqrx“sample_rate”框中以及file =,rate =字符串中设置采样率。显然,file =,rate = gqrx字符串中的采样率被忽略,采样率框为空的默认值可能为96k。 – Paul 2014-10-14 04:49:21