Skip to content

Commit

Permalink
going to python3 + c converter for binary formats
Browse files Browse the repository at this point in the history
  • Loading branch information
ulysse71 committed Feb 21, 2018
1 parent 4133e4f commit 9ff41f2
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 35 deletions.
7 changes: 5 additions & 2 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

10 changes: 6 additions & 4 deletions demodam.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)
Expand All @@ -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))

9 changes: 5 additions & 4 deletions demodfm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand All @@ -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))

53 changes: 39 additions & 14 deletions fcrec.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

#include <math.h>

// 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; }

Expand All @@ -21,16 +19,23 @@ void writeAscii(FILE *fdo, short int *buf, int n)
}

/********************************************************************/
/** statistics **/
/** statistics && power **/

struct statT {
int nb;
double sx;
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; c<n; c++) {
st->sx += buf[c];
Expand Down Expand Up @@ -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;
Expand All @@ -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]);
Expand Down Expand Up @@ -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");
Expand All @@ -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);
Expand All @@ -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);
}
Expand All @@ -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);
Expand Down
9 changes: 6 additions & 3 deletions makefile
Original file line number Diff line number Diff line change
@@ -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 $@
Expand Down
7 changes: 3 additions & 4 deletions try-pyam
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 3 additions & 4 deletions try-pyfm
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 9ff41f2

Please sign in to comment.