From 9ff41f2ed99f73538c6342cc5998b86397feb03d Mon Sep 17 00:00:00 2001 From: Hugot FX Date: Wed, 21 Feb 2018 09:08:43 +0100 Subject: [PATCH] going to python3 + c converter for binary formats --- README.txt | 7 +++++-- demodam.py | 10 ++++++---- demodfm.py | 9 +++++---- fcrec.c | 53 +++++++++++++++++++++++++++++++++++++++-------------- makefile | 9 ++++++--- try-pyam | 7 +++---- try-pyfm | 7 +++---- 7 files changed, 67 insertions(+), 35 deletions(-) diff --git a/README.txt b/README.txt index 52af2c5..bd6af85 100644 --- a/README.txt +++ b/README.txt @@ -2,7 +2,7 @@ Light Funcube Reader For educational purpose -Beware, wont compile, needs HIDAPI to work +Beware, wont compile, needs HIDAPI and fcdctl to work see //github.com/csete/fcdctl.git example: @@ -15,6 +15,9 @@ example: or process AM demodulation with ./try-pyam fcrec.dat +dependencies + Install sox + TODO - correct build against HIDAPI + correct build against HIDAPI and fcdctl diff --git a/demodam.py b/demodam.py index ac4f752..3e29c14 100644 --- a/demodam.py +++ b/demodam.py @@ -12,8 +12,8 @@ c=0 liq = [] llen = len(ll) -llen2 = llen/2 -for c in xrange(llen2): +llen2 = int(llen/2) +for c in range(llen2): i = ll[2*c] q = ll[2*c+1] liq.append((i, q)) @@ -27,7 +27,7 @@ coeff = 10. mx=0 mn=0 -for c in xrange(llen2): +for c in range(llen2): i = float(ll[2*c]) q = float(ll[2*c+1]) v = coeff * math.sqrt(i*i + q*q) @@ -37,14 +37,16 @@ print("mx "+str(mx)) print("mn "+str(mn)) +mid=0.5*(mx+mn) # ascii dump fd = open("demodam.dat", 'w') -for v in lv: fd.write(str(v)+"\n") +for v in lv: fd.write(str(int(v-mid))+"\n") fd.close() # scipy wav dump #import scipy.io.wavfile #import numpy +#lv = map(lambda x: x/(mx-mid), lv) #scipy.io.wavfile.write("sample.wav", freq, numpy.array(lv)) diff --git a/demodfm.py b/demodfm.py index 4435765..01f53e6 100644 --- a/demodfm.py +++ b/demodfm.py @@ -12,8 +12,8 @@ c=0 liq = [] llen = len(ll) -llen2 = llen/2 -for c in xrange(llen2): +llen2 = int(llen/2) +for c in range(llen2): i = ll[2*c] q = ll[2*c+1] liq.append((i, q)) @@ -37,7 +37,7 @@ def regul(dtheta): theta0=0. mx=0 mn=0 -for c in xrange(llen2): +for c in range(llen2): i = ll[2*c] q = ll[2*c+1] theta1 = math.atan2(float(q), float(i)) @@ -54,11 +54,12 @@ def regul(dtheta): # ascii dump fd = open("demodfm.dat", 'w') -for v in lv: fd.write(str(v)+"\n") +for v in lv: fd.write(str(int(v))+"\n") fd.close() # scipy wav dump #import scipy.io.wavfile #import numpy +#lv = map(lambda x: 0.5*x/mx, lv) #scipy.io.wavfile.write("sample.wav", freq, numpy.array(lv)) diff --git a/fcrec.c b/fcrec.c index ade87e4..eac3c4f 100644 --- a/fcrec.c +++ b/fcrec.c @@ -9,8 +9,6 @@ #include -// based on the #defines for TLGE_P10_0DB etc. from fcdhidcmd.h : -double lnagainvalues[]={-5.0,-2.5,-999,-999,0,2.5,5,7.5,10,12.5,15,17.5,20,25,30}; inline double sqr(double x) { return x*x; } @@ -21,7 +19,7 @@ void writeAscii(FILE *fdo, short int *buf, int n) } /********************************************************************/ -/** statistics **/ +/** statistics && power **/ struct statT { int nb; @@ -29,8 +27,15 @@ struct statT { double sx2; }; +void stats_reset(struct statT *st) +{ + st->nb = 0; + st->sx = st->sx2 = 0.; +} + void stats_sum(struct statT *st, short int *buf, int n) { + /// add content of buffer to stats in st int c=0; for (c=0; csx += buf[c]; @@ -59,14 +64,26 @@ void stats_edit(FILE *fdo, struct statT *st) fprintf(fdo, "sdev %g\n", sqrt(var)); } +void stats_add(struct statT *st1, struct statT *st2) +{ + /// add stat of st2 to st1 + st1->nb += st2->nb; + st1->sx += st2->sx; + st1->sx2 += st2->sx2; +} + int stats_high(struct statT *st, double sql) { + /// tells if var (or power) is above level return stats_var(st, stats_mean(st)) > sql; } /****************************************************************/ /** SDR stuff **/ +// based on the #defines for TLGE_P10_0DB etc. from fcdhidcmd.h : +double lnagainvalues[]={-5.0,-2.5,-999,-999,0,2.5,5,7.5,10,12.5,15,17.5,20,25,30}; + snd_pcm_t *handle; snd_pcm_uframes_t frames; snd_pcm_hw_params_t *params; @@ -78,10 +95,10 @@ void dumpSdr() fcdGetFwVerStr(version); printf("# funcube version %s\n", version); - uint8_t f[8]; - fcdAppGetParam(FCD_CMD_APP_GET_FREQ_HZ, f, 8); - printf("# frequency %g MHz\n", (*(int*)f)/1e6); - + // uint8_t f[8]; + // fcdAppGetParam(FCD_CMD_APP_GET_FREQ_HZ, f, 8); + // printf("# frequency %g MHz\n", (*(int*)f)/1e6); + uint8_t lnagain; fcdAppGetParam(FCD_CMD_APP_GET_LNA_GAIN, &lnagain, 1); printf("# lna gain %g\n", lnagainvalues[lnagain]); @@ -195,7 +212,7 @@ void readFuncube(char *buffer, double time, int sz, double sql) sz is size of frames */ int dir, rc; - long loops; + long loops, rloops=0; unsigned int val; printf("dumping file fcrec.raw\n"); @@ -208,7 +225,8 @@ void readFuncube(char *buffer, double time, int sz, double sql) /* time seconds in microseconds divided by period time */ loops = time * 1000000 / val; printf("loops %ld\n", loops); - struct statT st = { 0, 0., 0. }; + struct statT sta = { 0, 0., 0. }, // stats for all loops + st; // stat for just one loop while (loops > 0) { rc = snd_pcm_readi(handle, buffer, frames); @@ -222,15 +240,21 @@ void readFuncube(char *buffer, double time, int sz, double sql) else if (rc != (int)frames) fprintf(stderr, "short read, read %d frames\n", rc); // read has been fully satistory - else if (stats_high(&st, sql)) { - if (write(fdob, buffer, sz*rc) != sz*rc) abort(); - writeAscii(fdoa, (short int *)buffer, sz*rc/2); + else { + stats_reset(&st); + stats_sum(&st, (short int *)buffer, sz*rc/2); + if (stats_high(&st, sql)) { + if (write(fdob, buffer, sz*rc) != sz*rc) abort(); + writeAscii(fdoa, (short int *)buffer, sz*rc/2); + rloops++; + } + stats_add(&sta, &st); } - stats_sum(&st, (short int *)buffer, sz*rc/2); loops--; } - stats_edit(fdoa, &st); + printf("loops really done (squelch) %ld\n", rloops); + stats_edit(stdout, &sta); close(fdob); fclose(fdoa); } @@ -249,6 +273,7 @@ int main(int argc, char **argv) { double time = atof(argv[3]); double sql=0; if (argc>4) sql = atof(argv[4]); + printf("squelch %g\n", sql); openFuncubeSdr(freq, gain); //dumpSdr(); char *buffer =openFuncubePcm(freq); diff --git a/makefile b/makefile index 5007c95..b2998e7 100644 --- a/makefile +++ b/makefile @@ -1,10 +1,13 @@ -all : fcrec +all : fcrec convertasc convertbin : convertbin.c gcc -O3 -Wall -Wextra convertbin.c -lm -o convertbin -fcrec : fcrec.o fcd.o hid-libusb.o - gcc -O3 -Wall -Wextra fcrec.o fcd.o hid-libusb.o `pkg-config --libs libusb-1.0` -lm -lpthread -lasound -o fcrec +convertasc : convertasc.c + gcc -O3 -Wall -Wextra convertasc.c -lm -o convertasc + +fcrec : fcrec.o fcd.o + gcc -O3 -Wall -Wextra fcrec.o fcd.o `pkg-config --libs hidapi-hidraw` `pkg-config --libs libusb-1.0` -lm -lpthread -lasound -o fcrec %.o : %.c gcc -c -O3 -Wall -Wextra $< `pkg-config --cflags libusb-1.0` -o $@ diff --git a/try-pyam b/try-pyam index de905a6..ec8ba70 100755 --- a/try-pyam +++ b/try-pyam @@ -1,11 +1,10 @@ #!/bin/sh -rm -f sample.wav -python demodam.py $1 +rm -f sample.{raw,wav} +python3 demodam.py $1 # convert txt to wav -convertasc.opt demodam.dat S16LE -cp -v sample.bin sample.raw +./convertasc demodam.dat S16LE sox -r 96000 -L -t s16 -c 1 sample.raw sample.wav gain 0 # play wav file diff --git a/try-pyfm b/try-pyfm index 64758f0..62f4d81 100755 --- a/try-pyfm +++ b/try-pyfm @@ -1,11 +1,10 @@ #!/bin/sh -rm -f sample.wav -python demodfm.py $1 +rm -f sample.{raw,wav} +python3 demodfm.py $1 # convert txt to wav -convertasc.opt demodfm.dat S16LE -cp -v sample.bin sample.raw +make convertasc && ./convertasc demodfm.dat S16LE sox -r 96000 -L -t s16 -c 1 sample.raw sample.wav gain 0 # play wav file