1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
|
/*****************************************************************************/
/*
* modempsp.c -- Linux Userland Soundmodem FSK PSP VE enhanced demodulator.
*
* Copyright (C) 1999-2000, 2003
* Thomas Sailer (t.sailer@alumni.ethz.ch)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*
*/
/*****************************************************************************/
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "modem.h"
#include <stdio.h>
#include "raisedcosine.h"
/* --------------------------------------------------------------------- */
extern double df9ic_rxfilter(double t);
extern double df9ic_txfilter(double t);
/* --------------------------------------------------------------------- */
#define RCOSALPHA (3.0/8)
#define FILTERRELAX 1.4
#define DESCRAM17_TAPSH1 0
#define DESCRAM17_TAPSH2 5
#define DESCRAM17_TAPSH3 17
/* --------------------------------------------------------------------- */
#include "psp.h"
/* --------------------------------------------------------------------- */
static inline int isqr(int x)
{
return x * x;
}
static inline u_int8_t rev_nibble(u_int8_t in)
{
u_int8_t nibbletab[16] = {0x0,0x8,0x4,0xc,0x2,0xa,0x6,0xe,0x1,0x9,0x5,0xd,0x3,0xb,0x7,0xf};
return nibbletab[in & 0xf];
}
static inline u_int8_t rev_byte(u_int8_t in) {
return (rev_nibble(in) << 4) | rev_nibble(in >> 4);
}
#ifdef __i386__
static inline int16_t fir(const int32_t *p1, const int16_t *p2, int len)
{
int32_t sum, temp;
__asm__("\n0:\n\t"
"movswl (%4),%1\n\t"
"imull (%3),%1\n\t"
"addl $-2,%4\n\t"
"addl $4,%3\n\t"
"addl %1,%0\n\t"
"decl %2\n\t"
"jnz 0b\n\t"
: "=r" (sum), "=r" (temp), "=r" (len), "=S" (p1), "=D" (p2)
: "0" (0), "2" (len), "3" (p1), "4" (p2));
return sum >> 16;
}
#else
static inline int16_t fir(const int32_t *p1, const int16_t *p2, int len)
{
int32_t sum = 0;
for(; len > 0; len--, p1++, p2--)
sum += ((int32_t)*p1) * ((int32_t)*p2);
return sum >> 16;
}
#endif
/* --------------------------------------------------------------------- */
#define max(a, b) (((a) > (b)) ? (a) : (b))
#define MAXFIRLEN 64U
#define FILTEROVER 16U
#define FILTERSPANBITS 8U
#define WHICHFILTER(x) (((x)>>12)&0xFU) /* must correspond to FILTEROVER */
#define WHICHSAMPLE(x) ((x)>>16)
#define SNRESTSHIFT 7
#define PARAMSMOOTH 0.95
#define PARSMMUL1 ((int)(PARAMSMOOTH*(1<<16)))
#define PARSMMUL2 ((1<<16)-PARSMMUL1)
struct venode {
unsigned int metric;
unsigned int data;
};
struct demodstate {
struct modemchannel *chan;
unsigned int filtermode;
unsigned int bps, firlen;
unsigned int pllinc, pllcorr;
int pll;
u_int16_t stime;
unsigned int descram;
unsigned int shistcnt;
unsigned int snracc, snrcnt;
int16_t shist[16];
/*
* 0: DC level
* 1: postcursor of previous bit
* 2: bit amplitude
* 3: precursor of following bit
*/
int16_t params[4];
struct venode venodes[8];
int32_t filter[FILTEROVER][MAXFIRLEN];
};
#define DCD_TIME_SHIFT 7
#define DCD_TIME (1<<DCD_TIME_SHIFT)
static int16_t filter(struct demodstate *s, int16_t *samples, int ph)
{
return fir(s->filter[WHICHFILTER(ph)], samples + WHICHSAMPLE(ph), s->firlen);
}
static const struct modemparams demodparams[] = {
{ "bps", "Bits/s", "Bits per second", "9600", MODEMPAR_NUMERIC, { n: { 4800, 38400, 100, 1200 } } },
{ "filter", "Filter Curve", "Filter Curve", "df9ic/g3ruh", MODEMPAR_COMBO,
{ c: { { "df9ic/g3ruh", "rootraisedcosine", "raisedcosine", "hamming" } } } },
{ NULL }
};
static void *demodconfig(struct modemchannel *chan, unsigned int *samplerate, const char *params[])
{
struct demodstate *s;
unsigned int i;
if (!(s = calloc(1, sizeof(struct demodstate))))
logprintf(MLOG_FATAL, "out of memory\n");
s->chan = chan;
if (params[0]) {
s->bps = strtoul(params[0], NULL, 0);
if (s->bps < 4800)
s->bps = 4800;
if (s->bps > 38400)
s->bps= 38400;
} else
s->bps = 9600;
s->filtermode = 0;
if (params[1]) {
for (i = 1; i < 4; i++)
if (!strcmp(params[1], demodparams[1].u.c.combostr[i])) {
s->filtermode = i;
break;
}
}
*samplerate = s->bps + s->bps / 2;
return s;
}
static inline int estparams(struct demodstate *s, unsigned int deschist)
{
int16_t params[ESTPARAMS];
const int16_t *shist, *sptr, *cptr;
int sum;
unsigned int i, j;
deschist &= ESTDATAMASK;
if (estsingular[deschist >> 5] & (1 << (deschist & 31))) {
logprintf(250, "singular pattern\n");
return 0;
}
shist = s->shist + s->shistcnt + ESTSYMBOLS - 1;
cptr = estmat[deschist];
for (i = 0; i < ESTPARAMS; i++) {
sptr = shist;
sum = 0;
for (j = 0; j < ESTSYMBOLS; j++, sptr--, cptr++)
sum += (int)(*sptr) * (int)(*cptr);
params[i] = sum >> ESTSHIFT;
sum = PARSMMUL1 * s->params[i] + PARSMMUL2 * params[i];
s->params[i] = sum >> 16;
}
/* clamp params to sane values */
if (s->params[2] < 250)
s->params[2] = 250;
sum = s->params[2] >> 1;
if (abs(s->params[1]) > sum)
s->params[1] = (s->params[1] > 0) ? sum : -sum;
if (abs(s->params[3]) > sum)
s->params[3] = (s->params[3] > 0) ? sum : -sum;
logprintf(250, "Par: est: %5d %5d %5d %5d sm: %5d %5d %5d %5d\n",
params[0], params[1], params[2], params[3],
s->params[0], s->params[1], s->params[2], s->params[3]);
return 1;
}
static inline void viterbieq(struct demodstate *s, int16_t curs)
{
int blevel = s->params[0] - s->params[1] - s->params[2] - s->params[3] - curs;
int ampl0 = 2 * s->params[1];
int ampl1 = 2 * s->params[2];
int ampl2 = 2 * s->params[3];
unsigned int metric0, metric1;
struct venode *node1, *node2;
unsigned char ch;
unsigned int i;
float snr;
if (s->shistcnt & 1) {
node1 = s->venodes+4;
node2 = s->venodes;
} else {
node1 = s->venodes;
node2 = s->venodes+4;
}
s->shist[s->shistcnt++] = curs;
s->shistcnt &= 15;
/* unrolled viterbi equalizer loop */
metric0 = node1[0].metric + isqr(blevel);
metric1 = node1[2].metric + isqr(blevel + ampl2);
if (metric0 < metric1) {
node2[0].metric = metric0;
node2[0].data = node1[0].data << 1;
} else {
node2[0].metric = metric1;
node2[0].data = node1[2].data << 1;
}
metric0 = node1[0].metric + isqr(blevel + ampl0);
metric1 = node1[2].metric + isqr(blevel + ampl2 + ampl0);
if (metric0 < metric1) {
node2[1].metric = metric0;
node2[1].data = (node1[0].data << 1) | 1;
} else {
node2[1].metric = metric1;
node2[1].data = (node1[2].data << 1) | 1;
}
metric0 = node1[1].metric + isqr(blevel + ampl1);
metric1 = node1[3].metric + isqr(blevel + ampl2 + ampl1);
if (metric0 < metric1) {
node2[2].metric = metric0;
node2[2].data = node1[1].data << 1;
} else {
node2[2].metric = metric1;
node2[2].data = node1[3].data << 1;
}
metric0 = node1[1].metric + isqr(blevel + ampl1 + ampl0);
metric1 = node1[3].metric + isqr(blevel + ampl2 + ampl1 + ampl0);
if (metric0 < metric1) {
node2[3].metric = metric0;
node2[3].data = (node1[1].data << 1) | 1;
} else {
node2[3].metric = metric1;
node2[3].data = (node1[3].data << 1) | 1;
}
/* end of viterbi "loop"; special actions periodically */
if (s->shistcnt & 7)
return;
/* find node with best metric */
metric0 = ~0;
metric1 = 0;
for (i = 0; i < 4; i++)
if (node2[i].metric < metric0) {
metric0 = node2[i].metric;
metric1 = node2[i].data;
}
/* decide data, send to decoder */
s->descram <<= 8;
s->descram |= (metric1 >> 16) & 0xff; /* 16 is about 5*constraint length */
i = ~(s->descram ^ (s->descram >> 1));
i ^= (i >> DESCRAM17_TAPSH3) ^ (i >> (DESCRAM17_TAPSH3-DESCRAM17_TAPSH2));
ch = rev_byte(i);
pktput(s->chan, &ch, 1);
if (logcheck(257)) {
char buf2[9];
for (i = 0; i < 8; i++)
buf2[i] = '0' + ((ch >> i) & 1);
buf2[8] = 0;
logprintf(257, "fskrx: %s\n", buf2);
}
/* subtract metric */
s->snracc += metric0 >> SNRESTSHIFT;
for (i = 0; i < 4; i++) {
node2[i].metric -= metric0;
if (node2[i].metric > 0x7fffffff)
node2[i].metric = 0x7fffffff; /* prevent overflow */
}
/* new parameter estimate */
estparams(s, metric1 >> (16-ESTSYMBOLS));
/* SNR estimate */
s->snrcnt++;
if (s->snrcnt < (1<<(SNRESTSHIFT-3)))
return;
s->snrcnt = 0;
metric0 = isqr(s->params[1]) + isqr(s->params[2]) + isqr(s->params[3]);
metric1 = metric0;
if (!metric1)
metric1 = 1;
snr = -10*M_LOG10E*log(s->snracc / (float)metric1);
logprintf(128, "SNR: signal power %u noise+interference pwr %u S/(N+I) %6.2fdB\n", metric0, s->snracc, snr);
pktsetdcd(s->chan, s->snracc < (metric0 >> 2));
s->snracc = 0;
}
static void demodrx(struct demodstate *s, unsigned nsamples)
{
int16_t *samples;
int16_t curs, nexts, mids;
#if 0
int32_t gardner;
int corr;
#endif
samples = alloca((nsamples + s->firlen) * sizeof(samples[0]));
audioread(s->chan, samples, nsamples + s->firlen, s->stime);
s->stime += nsamples;
samples += s->firlen;
while (WHICHSAMPLE(s->pll + s->pllinc) < nsamples) {
#if 0
for (corr = 0; corr < 16; corr++)
printf("%d\n", filter(s, samples, s->pll+corr*s->pllinc/16));
#endif
curs = filter(s, samples, s->pll);
mids = filter(s, samples, s->pll+s->pllinc/2);
nexts = filter(s, samples, s->pll+s->pllinc);
#if 0
gardner = ((nexts > 0 ? 1 : -1) - (curs > 0 ? 1 : -1)) * mids;
#endif
s->pll += s->pllinc;
#if 0
corr = (gardner * s->pllinc) >> 20;
s->pll -= corr;
#elif 0
if (gardner < 0)
s->pll += s->pllinc >> 4;
else
s->pll -= s->pllinc >> 4;
#else
if ((curs > 0) ^ (nexts > 0)) {
if ((curs > 0) ^ (mids > 0))
s->pll -= s->pllinc >> 5;
if ((nexts > 0) ^ (mids > 0))
s->pll += s->pllinc >> 5;
}
#endif
viterbieq(s, curs);
}
s->pll -= (nsamples << 16);
}
static void demoddemodulate(void *state)
{
struct demodstate *s = (struct demodstate *)state;
s->stime = audiocurtime(s->chan);
for (;;)
demodrx(s, 256);
}
static void demodinit(void *state, unsigned int samplerate, unsigned int *bitrate)
{
struct demodstate *s = (struct demodstate *)state;
float coeff[FILTEROVER][MAXFIRLEN];
double tmul;
float max1, max2, t;
int i, j;
s->firlen = (samplerate * FILTERSPANBITS + s->bps - 1) / s->bps;
if (s->firlen > MAXFIRLEN) {
logprintf(MLOG_WARNING, "demodfsk: input filter length too long\n");
s->firlen = MAXFIRLEN;
}
tmul = ((double)s->bps) / FILTEROVER / ((double)samplerate);
switch (s->filtermode) {
case 1: /* root raised cosine */
for (i = 0; i < FILTEROVER*s->firlen; i++) {
t = (signed)(i - s->firlen*FILTEROVER/2) * tmul;
coeff[((unsigned)i) % FILTEROVER][((unsigned)i) / FILTEROVER] = root_raised_cosine_time(t, RCOSALPHA);
}
break;
case 2: /* raised cosine */
for (i = 0; i < FILTEROVER*s->firlen; i++) {
t = (signed)(i - s->firlen*FILTEROVER/2) * tmul;
coeff[((unsigned)i) % FILTEROVER][((unsigned)i) / FILTEROVER] = raised_cosine_time(t, RCOSALPHA);
}
break;
case 3: /* hamming */
tmul *= FILTERRELAX;
for (i = 0; i < FILTEROVER*s->firlen; i++)
coeff[((unsigned)i) % FILTEROVER][((unsigned)i) / FILTEROVER] =
sinc((i - (signed)s->firlen*FILTEROVER/2)*tmul)
* hamming((double)i / (double)(FILTEROVER*s->firlen-1));
break;
default: /* DF9IC */
for (i = 0; i < FILTEROVER*s->firlen; i++) {
t = (signed)(i - s->firlen*FILTEROVER/2) * tmul;
coeff[((unsigned)i) % FILTEROVER][((unsigned)i) / FILTEROVER] = df9ic_rxfilter(t);
}
break;
}
max1 = 0;
for (i = 0; i < FILTEROVER; i++) {
max2 = 0;
for (j = 0; j < s->firlen; j++)
max2 += fabs(coeff[i][j]);
if (max2 > max1)
max1 = max2;
}
max2 = ((float)0x7fffffff / (float)0x7fff) / max1;
for (i = 0; i < FILTEROVER; i++)
for (j = 0; j < s->firlen; j++)
s->filter[i][j] = max2 * coeff[i][j];
s->pllinc = (0x10000 * samplerate + s->bps/2) / s->bps;
s->pll = 0;
s->pllcorr = s->pllinc / 8;
s->shistcnt = s->snracc = s->snrcnt = 0;
s->params[0] = 0;
s->params[1] = 300;
s->params[2] = 11000;
s->params[3] = 300;
for (i = 0; i < 8; i++)
s->venodes[i].metric = 0x7fffffff;
*bitrate = s->bps;
}
/* --------------------------------------------------------------------- */
struct demodulator fskpspdemodulator = {
NULL,
"fskpsp",
demodparams,
demodconfig,
demodinit,
demoddemodulate,
free
};
/* --------------------------------------------------------------------- */
|