Lines Matching +full:up +full:- +full:samples
2 * refclock_irig - audio IRIG-B/E demodulator/decoder
26 * Audio IRIG-B/E demodulator/decoder
29 * IRIG-B/E signals commonly produced by GPS receivers and other timing
30 * devices. The IRIG signal is an amplitude-modulated carrier with
31 * pulse-width modulated data bits. For IRIG-B, the carrier frequency is
32 * 1000 Hz and bit rate 100 b/s; for IRIG-E, the carrier frequenchy is
37 * kHz and mu-law companding. This is the same standard as used by the
43 * The program processes 8000-Hz mu-law companded samples using separate
44 * signal filters for IRIG-B and IRIG-E, a comb filter, envelope
47 * its value - zero, one or position identifier.
52 * samples of successive baud intervals in order to reliably identify
53 * the reference carrier cycle. A type-II phase-lock loop (PLL) performs
56 * timestamp. A pulse-width discriminator demodulates the data pulses,
60 * with IRIG-B (ten seconds with IRIG-E) and local clock offset samples
62 * samples are processed by a trimmed-mean filter and used to update the
90 * these fields are the carrier amplitude (0-3000), codec gain (0-255),
91 * modulation index (0-1), time constant (4-10), carrier phase error
92 * +-.5) and carrier frequency error (PPM). The last field is the on-
116 * x40 Codec error (overrun). The machine is not fast enough to keep up
122 * within a few tens of microseconds relative to the IRIG-B signal.
123 * Accuracy with IRIG-E was about ten times worse. Unfortunately, Sun
124 * broke the 2.7 audio driver in 2.8, which has a 10-ms sawtooth
130 * probably chew up too much CPU time anyway.
136 * port, where 0 is the mike port (default) and 1 is the line-in port.
151 #define PRECISION (-17) /* precision assumed (about 10 us) */
156 #define BAUD 80 /* samples per baud interval */
159 #define CYCLE 8 /* samples per bit */
168 #define MAXFREQ (250e-6 * SECOND) /* freq tolerance (.025%) */
171 * The on-time synchronization point is the positive-going zero crossing
173 * is 1.03 ms for IRIG-B and 3.47 ms for IRIG-E. The fudge value 2.68 ms
177 * The results with a 2.4-GHz P4 running FreeBSD 6.1 are generally
178 * within .02 ms short-term with .02 ms jitter. The processor load due
181 #define IRIG_B ((1.03 + 2.68) / 1000) /* IRIG-B system delay (s) */
182 #define IRIG_E ((3.47 + 2.68) / 1000) /* IRIG-E system delay (s) */
221 double irig_b; /* IRIG-B signal amplitude */
222 double irig_e; /* IRIG-E signal amplitude */
237 double bpf[9]; /* IRIG-B filter shift register */
238 double lpf[5]; /* IRIG-E filter shift register */
292 irig_start, /* start up driver */
303 * irig_start - open the devices and initialize data for processing
312 struct irigunit *up;
335 up = emalloc_zero(sizeof(*up));
336 pp = peer->procptr;
337 pp->io.clock_recv = irig_receive;
338 pp->io.srcclock = peer;
339 pp->io.datalen = 0;
340 pp->io.fd = fd;
341 if (!io_addclock(&pp->io)) {
343 pp->io.fd = -1;
344 free(up);
347 pp->unitptr = up;
352 peer->precision = PRECISION;
353 pp->clockdesc = DESCRIPTION;
354 memcpy((char *)&pp->refid, REFID, 4);
355 up->tc = MINTC;
356 up->decim = 1;
357 up->gain = 127;
360 * The companded samples are encoded sign-magnitude. The table
363 up->comp[0] = up->comp[OFFSET] = 0.;
364 up->comp[1] = 1; up->comp[OFFSET + 1] = -1.;
365 up->comp[2] = 3; up->comp[OFFSET + 2] = -3.;
368 up->comp[i] = up->comp[i - 1] + step;
369 up->comp[OFFSET + i] = -up->comp[i];
373 DTOLFP(1. / SECOND, &up->tick);
379 * irig_shutdown - shut down the clock
388 struct irigunit *up;
390 pp = peer->procptr;
391 up = pp->unitptr;
392 if (-1 != pp->io.fd)
393 io_closeclock(&pp->io);
394 if (NULL != up)
395 free(up);
400 * irig_receive - receive data from the audio device
402 * This routine reads input samples and adjusts the logical clock to
403 * track the irig clock by dropping or duplicating codec samples.
412 struct irigunit *up;
422 peer = rbufp->recv_peer;
423 pp = peer->procptr;
424 up = pp->unitptr;
427 * Main loop - read until there ain't no more. Note codec
428 * samples are bit-inverted.
430 DTOLFP((double)rbufp->recv_length / SECOND, <emp);
431 L_SUB(&rbufp->recv_time, <emp);
432 up->timestamp = rbufp->recv_time;
433 dpt = rbufp->recv_buffer;
434 for (bufcnt = 0; bufcnt < rbufp->recv_length; bufcnt++) {
435 sample = up->comp[~*dpt++ & 0xff];
439 * runs at the nominal rate of 8000 samples per second,
445 up->phase += (up->freq + clock_codec) / SECOND;
446 up->phase += pp->fudgetime2 / 1e6;
447 if (up->phase >= .5) {
448 up->phase -= 1.;
449 } else if (up->phase < -.5) {
450 up->phase += 1.;
456 L_ADD(&up->timestamp, &up->tick);
458 if (sample > up->signal)
459 up->signal = sample;
460 up->signal += (sample - up->signal) /
466 up->seccnt = (up->seccnt + 1) % SECOND;
467 if (up->seccnt == 0) {
468 if (up->irig_b > up->irig_e) {
469 up->decim = 1;
470 up->fdelay = IRIG_B;
472 up->decim = 10;
473 up->fdelay = IRIG_E;
475 up->irig_b = up->irig_e = 0;
484 if (pp->sloppyclockflag & CLK_FLAG2)
485 up->port = 2;
487 up->port = 1;
488 if (pp->sloppyclockflag & CLK_FLAG3)
489 up->mongain = MONGAIN;
491 up->mongain = 0;
496 * irig_rf - RF processing
498 * This routine filters the RF signal using a bandass filter for IRIG-B
499 * and a lowpass filter for IRIG-E. In case of IRIG-E, the samples are
512 struct irigunit *up;
519 pp = peer->procptr;
520 up = pp->unitptr;
523 * IRIG-B filter. Matlab 4th-order IIR elliptic, 800-1200 Hz
524 * bandpass, 0.3 dB passband ripple, -50 dB stopband ripple,
527 irig_b = (up->bpf[8] = up->bpf[7]) * 6.505491e-001;
528 irig_b += (up->bpf[7] = up->bpf[6]) * -3.875180e+000;
529 irig_b += (up->bpf[6] = up->bpf[5]) * 1.151180e+001;
530 irig_b += (up->bpf[5] = up->bpf[4]) * -2.141264e+001;
531 irig_b += (up->bpf[4] = up->bpf[3]) * 2.712837e+001;
532 irig_b += (up->bpf[3] = up->bpf[2]) * -2.384486e+001;
533 irig_b += (up->bpf[2] = up->bpf[1]) * 1.427663e+001;
534 irig_b += (up->bpf[1] = up->bpf[0]) * -5.352734e+000;
535 up->bpf[0] = sample - irig_b;
536 irig_b = up->bpf[0] * 4.952157e-003
537 + up->bpf[1] * -2.055878e-002
538 + up->bpf[2] * 4.401413e-002
539 + up->bpf[3] * -6.558851e-002
540 + up->bpf[4] * 7.462108e-002
541 + up->bpf[5] * -6.558851e-002
542 + up->bpf[6] * 4.401413e-002
543 + up->bpf[7] * -2.055878e-002
544 + up->bpf[8] * 4.952157e-003;
545 up->irig_b += irig_b * irig_b;
548 * IRIG-E filter. Matlab 4th-order IIR elliptic, 130-Hz lowpass,
549 * 0.3 dB passband ripple, -50 dB stopband ripple, phase delay
552 irig_e = (up->lpf[4] = up->lpf[3]) * 8.694604e-001;
553 irig_e += (up->lpf[3] = up->lpf[2]) * -3.589893e+000;
554 irig_e += (up->lpf[2] = up->lpf[1]) * 5.570154e+000;
555 irig_e += (up->lpf[1] = up->lpf[0]) * -3.849667e+000;
556 up->lpf[0] = sample - irig_e;
557 irig_e = up->lpf[0] * 3.215696e-003
558 + up->lpf[1] * -1.174951e-002
559 + up->lpf[2] * 1.712074e-002
560 + up->lpf[3] * -1.174951e-002
561 + up->lpf[4] * 3.215696e-003;
562 up->irig_e += irig_e * irig_e;
565 * Decimate by a factor of either 1 (IRIG-B) or 10 (IRIG-E).
567 up->badcnt = (up->badcnt + 1) % up->decim;
568 if (up->badcnt == 0) {
569 if (up->decim == 1)
577 * irig_base - baseband processing
581 * data frame at the baud rate and decodes the width-modulated data
591 struct irigunit *up;
601 pp = peer->procptr;
602 up = pp->unitptr;
605 * Synchronous baud integrator. Corresponding samples of current
610 up->envphase = (up->envphase + 1) % BAUD;
611 up->integ[up->envphase] += (sample - up->integ[up->envphase]) /
612 (5 * up->tc);
613 lope = up->integ[up->envphase];
614 carphase = up->envphase % CYCLE;
615 up->lastenv[carphase] = sample;
616 up->lastint[carphase] = lope;
619 * Phase detector. Find the negative-going zero crossing
620 * relative to sample 4 in the 8-sample sycle. A phase change of
623 if (up->lastsig > 0 && lope <= 0)
624 up->zxing += (double)(carphase - 4) / CYCLE;
625 up->lastsig = lope;
631 if (up->envphase == 0) {
632 up->maxsignal = up->intmax; up->noise = up->intmin;
633 up->intmin = 1e6; up->intmax = -1e6;
634 if (up->maxsignal < DRPOUT)
635 up->errflg |= IRIG_ERR_AMP;
636 if (up->maxsignal > 0)
637 up->modndx = (up->maxsignal - up->noise) /
638 up->maxsignal;
640 up->modndx = 0;
641 if (up->modndx < MODMIN)
642 up->errflg |= IRIG_ERR_MOD;
643 if (up->errflg & (IRIG_ERR_AMP | IRIG_ERR_FREQ |
645 up->tc = MINTC;
646 up->tcount = 0;
656 dtemp = up->zxing * up->decim / BAUD;
657 up->yxing = dtemp;
658 up->zxing = 0.;
659 up->phase += dtemp / up->tc;
660 up->freq += dtemp / (4. * up->tc * up->tc);
661 if (up->freq > MAXFREQ) {
662 up->freq = MAXFREQ;
663 up->errflg |= IRIG_ERR_FREQ;
664 } else if (up->freq < -MAXFREQ) {
665 up->freq = -MAXFREQ;
666 up->errflg |= IRIG_ERR_FREQ;
671 * Synchronous demodulator. There are eight samples in the cycle
673 * negative-going zero crossing at sample 4, the maximum
676 * samples, while the end of the pulse is determined from the
677 * raw samples. The raw data bits are demodulated relative to
678 * the slice level and left-shifted in the decoding register.
683 lope = (up->lastint[2] - up->lastint[6]) / 2.;
684 if (lope > up->intmax)
685 up->intmax = lope;
686 if (lope < up->intmin)
687 up->intmin = lope;
695 up->pulse = (up->pulse + 1) % 10;
696 up->cycles <<= 1;
697 if (lope >= (up->maxsignal + up->noise) / 2.)
698 up->cycles |= 1;
699 if ((up->cycles & 0x303c0f03) == 0x300c0300) {
700 if (up->pulse != 0)
701 up->errflg |= IRIG_ERR_SYNCH;
702 up->pulse = 0;
711 env = (up->lastenv[2] - up->lastenv[6]) / 2.;
712 up->dcycles <<= 1;
713 if (env >= up->slice)
714 up->dcycles |= 1;
715 switch(up->pulse) {
718 irig_baud(peer, up->dcycles);
719 if (env < up->envmin)
720 up->envmin = env;
721 up->slice = (up->envmax + up->envmin) / 2;
722 up->envmin = 1e6; up->envmax = -1e6;
726 up->envmax = env;
730 if (env > up->envmax)
731 up->envmax = env;
735 up->envmin = env;
741 * irig_baud - update the PLL and decode the pulse-width signal
750 struct irigunit *up;
754 pp = peer->procptr;
755 up = pp->unitptr;
762 * persist for lots of samples.
764 up->exing = -up->yxing;
765 if (abs(up->envxing - up->envphase) <= 1) {
766 up->tcount++;
767 if (up->tcount > 20 * up->tc) {
768 up->tc++;
769 if (up->tc > MAXTC)
770 up->tc = MAXTC;
771 up->tcount = 0;
772 up->envxing = up->envphase;
774 up->exing -= up->envxing - up->envphase;
777 up->tcount = 0;
778 up->envxing = up->envphase;
786 up->prvstamp = up->chrstamp;
787 dtemp = up->decim * (up->exing / SECOND) + up->fdelay;
789 up->chrstamp = up->timestamp;
790 L_SUB(&up->chrstamp, <emp);
793 * The data bits are collected in ten-bit bauds. The first two
795 * 0-1 bits (0), 2-4 bits (1) and 5-7 bits (PI). The remaining
796 * 8-bit run represents a soft error and is treated as 0.
798 switch (up->dcycles & 0xff) {
800 case 0x00: /* 0-1 bits (0) */
805 case 0xc0: /* 2-4 bits (1) */
811 case 0xf8: /* (5-7 bits (PI) */
819 up->errflg |= IRIG_ERR_DECODE;
825 * irig_decode - decode the data
839 struct irigunit *up;
850 pp = peer->procptr;
851 up = pp->unitptr;
856 up->bits >>= 1;
858 up->bits |= 0x200;
859 } else if (bit == BITP && up->lastbit == BITP) {
862 * Frame sync - two adjacent position identifiers, which
868 if (up->frmcnt != 1)
869 up->errflg |= IRIG_ERR_SYNCH;
870 up->frmcnt = 1;
871 up->refstamp = up->prvstamp;
873 up->lastbit = bit;
874 if (up->frmcnt % SUBFLD == 0) {
878 * little-endian timecode field. Note frame 1 is shifted
881 temp = up->bits;
882 if (up->frmcnt == 10)
884 if (up->xptr >= 2) {
885 up->timecode[--up->xptr] = hexchar[temp & 0xf];
886 up->timecode[--up->xptr] = hexchar[(temp >> 5) &
889 if (up->frmcnt == 0) {
904 up->xptr = 2 * SUBFLD;
905 if (sscanf((char *)up->timecode,
906 "%6s%2d%1d%2s%3d%2d%2d%2d", sbs, &pp->year,
907 &syncdig, spare, &pp->day, &pp->hour,
908 &pp->minute, &pp->second) != 8)
909 pp->leap = LEAP_NOTINSYNC;
911 pp->leap = LEAP_NOWARNING;
912 up->second = (up->second + up->decim) % 60;
926 if (pp->day == 0 || (pp->year != 0 && syncdig ==
928 up->errflg |= IRIG_ERR_SIGERR;
929 if (pp->second != up->second)
930 up->errflg |= IRIG_ERR_CHECK;
931 up->second = pp->second;
938 if (up->errflg == 0 && up->tc == MAXTC) {
939 pp->lastref = pp->lastrec;
940 pp->lastrec = up->refstamp;
945 snprintf(pp->a_lastcode, sizeof(pp->a_lastcode),
947 up->errflg, pp->year, pp->day,
948 pp->hour, pp->minute, pp->second,
949 up->maxsignal, up->gain, up->modndx,
950 up->tc, up->exing * 1e6 / SECOND, up->freq *
951 1e6 / SECOND, ulfptoa(&pp->lastrec, 6));
952 pp->lencode = strlen(pp->a_lastcode);
953 up->errflg = 0;
954 if (pp->sloppyclockflag & CLK_FLAG4) {
955 record_clock_stats(&peer->srcadr,
956 pp->a_lastcode);
960 pp->a_lastcode);
965 up->frmcnt = (up->frmcnt + 1) % FIELD;
970 * irig_poll - called by the transmit procedure
972 * This routine sweeps up the timecode updates since the last poll. For
973 * IRIG-B there should be at least 60 updates; for IRIG-E there should
984 pp = peer->procptr;
986 if (pp->coderecv == pp->codeproc) {
992 if (!(pp->sloppyclockflag & CLK_FLAG4)) {
993 record_clock_stats(&peer->srcadr, pp->a_lastcode);
996 printf("irig %s\n", pp->a_lastcode);
999 pp->polls++;
1005 * irig_gain - adjust codec gain
1018 struct irigunit *up;
1020 pp = peer->procptr;
1021 up = pp->unitptr;
1028 if (up->maxsignal < MINAMP) {
1029 up->gain += 4;
1030 if (up->gain > MAXGAIN)
1031 up->gain = MAXGAIN;
1032 } else if (up->maxsignal > MAXAMP) {
1033 up->gain -= 4;
1034 if (up->gain < 0)
1035 up->gain = 0;
1037 audio_gain(up->gain, up->mongain, up->port);