如果你看看上面@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