3 This file is part of a program that implements a Software-Defined Radio.
5 Copyright (C) 2004 by Frank Brickle, AB2KT and Bob McGwier, N4HY
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 The authors can be reached by email at
29 The DTTS Microwave Society
36 //========================================================================
37 /* initialization and termination */
39 /* global and general info,
40 not specifically attached to
41 tx, rx, or scheduling */
46 uni.samplerate = loc.def.rate;
47 uni.buflen = loc.def.size;
48 uni.mode.sdr = loc.def.mode;
52 uni.meter.chan.path = loc.path.meter;
53 uni.meter.chan.size = loc.mult.ring * sizeof(REAL);
54 uni.meter.val = -200.0;
55 uni.meter.chan.c = openChan(uni.meter.chan.path, uni.meter.chan.size);
58 uni.wisdom.path = loc.path.wisdom;
59 uni.wisdom.bits = FFTW_OUT_OF_PLACE | FFTW_ESTIMATE;
61 FILE *f = fopen(uni.wisdom.path, "r");
65 char *line = alloca(WBUFLEN);
66 fgets(line, WBUFLEN, f);
67 if ((strlen(line) > WSTRLEN) &&
68 (fftw_import_wisdom_from_string(line) != FFTW_FAILURE))
69 uni.wisdom.bits = FFTW_OUT_OF_PLACE | FFTW_MEASURE | FFTW_USE_WISDOM;
77 uni.multirx.nrx = loc.def.nrx;
79 uni.mix.rx = uni.mix.tx = 1.0;
90 rx[k].iqfix = newCorrectIQ(0.0, 1.0);
91 rx[k].filt.coef = newFIR_Bandpass_COMPLEX(-4800.0,
95 rx[k].filt.ovsv = newFiltOvSv(FIRcoef(rx[k].filt.coef),
96 FIRsize(rx[k].filt.coef),
98 normalize_vec_COMPLEX(rx[k].filt.ovsv->zfvec,
99 rx[k].filt.ovsv->fftlen);
102 rx[k].filt.save = newvec_COMPLEX(rx[k].filt.ovsv->fftlen, "RX filter cache");
103 memcpy((char *) rx[k].filt.save,
104 (char *) rx[k].filt.ovsv->zfvec,
105 rx[k].filt.ovsv->fftlen * sizeof(COMPLEX));
108 /* note we overload the internal filter buffers
110 rx[k].buf.i = newCXB(FiltOvSv_fetchsize(rx[k].filt.ovsv),
111 FiltOvSv_fetchpoint(rx[k].filt.ovsv),
113 rx[k].buf.o = newCXB(FiltOvSv_storesize(rx[k].filt.ovsv),
114 FiltOvSv_storepoint(rx[k].filt.ovsv),
118 rx[k].osc.freq = -11025.0;
119 rx[k].osc.phase = 0.0;
120 rx[k].osc.gen = newOSC(uni.buflen,
125 "SDR RX Oscillator");
127 rx[k].agc.gen = newDigitalAgc(agcMED, // Mode
133 CXBsize(rx[k].buf.o), // BufSize
137 CXBbase(rx[k].buf.o));
138 rx[k].agc.flag = TRUE;
141 rx[k].am.gen = newAMD(48000.0, // REAL samprate
142 0.0, // REAL f_initial
143 -500.0, // REAL f_lobound,
144 500.0, // REAL f_hibound,
145 400.0, // REAL f_bandwid,
146 CXBsize(rx[k].buf.o), // int size,
147 CXBbase(rx[k].buf.o), // COMPLEX *ivec,
148 CXBbase(rx[k].buf.o), // COMPLEX *ovec,
149 AMdet, // AM Mode AMdet == rectifier,
150 // SAMdet == synchronous detector
151 "AM detector blew"); // char *tag
152 rx[k].fm.gen = newFMD(48000, // REAL samprate
153 0.0, // REAL f_initial
154 -6000.0, // REAL f_lobound
155 6000.0, // REAL f_hibound
156 10000.0, // REAL f_bandwid
157 CXBsize(rx[k].buf.o), // int size
158 CXBbase(rx[k].buf.o), // COMPLEX *ivec
159 CXBbase(rx[k].buf.o), // COMPLEX *ovec
160 "New FM Demod structure"); // char *error message;
162 /* noise reduction */
163 rx[k].anf.gen = new_lmsr(rx[k].buf.o, // CXB signal,
165 0.01, // REAL adaptation_rate,
166 0.00001, // REAL leakage,
167 45, // int adaptive_filter_size,
169 rx[k].anf.flag = FALSE;
170 rx[k].anr.gen = new_lmsr(rx[k].buf.o, // CXB signal,
172 0.01, // REAL adaptation_rate,
173 0.00001, // REAL leakage,
174 45, // int adaptive_filter_size,
176 rx[k].anr.flag = FALSE;
178 rx[k].nb.thresh = 3.3;
179 rx[k].nb.gen = new_noiseblanker(rx[k].buf.i, rx[k].nb.thresh);
180 rx[k].nb.flag = FALSE;
182 rx[k].nb_sdrom.thresh = 2.5;
183 rx[k].nb_sdrom.gen = new_noiseblanker(rx[k].buf.i, rx[k].nb_sdrom.thresh);
184 rx[k].nb_sdrom.flag = FALSE;
186 rx[k].spot.gen = newSpotToneGen(-12.0, // gain
193 rx[k].scl.pre.val = 1.0;
194 rx[k].scl.pre.flag = FALSE;
195 rx[k].scl.post.val = 1.0;
196 rx[k].scl.post.flag = FALSE;
198 memset((char *) &rx[k].squelch, 0, sizeof(rx[k].squelch));
199 rx[k].squelch.thresh = -30.0;
200 rx[k].squelch.power = 0.0;
201 rx[k].squelch.flag = rx[k].squelch.running = rx[k].squelch.set = FALSE;
202 rx[k].squelch.num = (int) (0.0395 * uni.samplerate + 0.5);
204 rx[k].mode = uni.mode.sdr;
205 rx[k].bin.flag = FALSE;
208 REAL pos = 0.5, // 0 <= pos <= 1, left->right
209 theta = (1.0 - pos) * M_PI / 2.0;
210 rx[k].azim = Cmplx(cos(theta), sin(theta));
211 fprintf(stderr, "azim %f %f\n", rx[k].azim.re, rx[k].azim.im);
223 tx.iqfix = newCorrectIQ(0.0, 1.0);
224 tx.filt.coef = newFIR_Bandpass_COMPLEX(300.0,
228 tx.filt.ovsv = newFiltOvSv(FIRcoef(tx.filt.coef),
229 FIRsize(tx.filt.coef),
231 normalize_vec_COMPLEX(tx.filt.ovsv->zfvec,
232 tx.filt.ovsv->fftlen);
235 tx.filt.save = newvec_COMPLEX(tx.filt.ovsv->fftlen, "TX filter cache");
236 memcpy((char *) tx.filt.save,
237 (char *) tx.filt.ovsv->zfvec,
238 tx.filt.ovsv->fftlen * sizeof(COMPLEX));
241 tx.buf.i = newCXB(FiltOvSv_fetchsize(tx.filt.ovsv),
242 FiltOvSv_fetchpoint(tx.filt.ovsv),
244 tx.buf.o = newCXB(FiltOvSv_storesize(tx.filt.ovsv),
245 FiltOvSv_storepoint(tx.filt.ovsv),
251 tx.osc.gen = newOSC(uni.buflen,
256 "SDR TX Oscillator");
258 tx.agc.gen = newDigitalAgc(agcFAST, // Mode
264 CXBsize(tx.buf.o), // BufSize
271 tx.spr.gen = newSpeechProc(0.4, 10.0, CXBbase(tx.buf.i), CXBsize(tx.buf.i));
275 tx.scl.pre.val = 1.0;
276 tx.scl.pre.flag = FALSE;
277 tx.scl.post.val = 1.0;
278 tx.scl.post.flag = FALSE;
280 tx.mode = uni.mode.sdr;
283 /* not much else to do for TX */
286 /* how the outside world sees it */
289 setup_workspace(void) {
294 for (k = 0; k < uni.multirx.nrx; k++) {
296 uni.multirx.act[k] = FALSE;
298 uni.multirx.act[0] = TRUE;
305 destroy_workspace(void) {
309 delSpeechProc(tx.spr.gen);
310 delDigitalAgc(tx.agc.gen);
312 delvec_COMPLEX(tx.filt.save);
313 delFiltOvSv(tx.filt.ovsv);
314 delFIR_Bandpass_COMPLEX(tx.filt.coef);
315 delCorrectIQ(tx.iqfix);
320 for (k = 0; k < uni.multirx.nrx; k++) {
321 delSpotToneGen(rx[k].spot.gen);
322 delDigitalAgc(rx[k].agc.gen);
323 del_nb(rx[k].nb_sdrom.gen);
324 del_nb(rx[k].nb.gen);
325 del_lmsr(rx[k].anf.gen);
326 del_lmsr(rx[k].anr.gen);
327 delAMD(rx[k].am.gen);
328 delFMD(rx[k].fm.gen);
329 delOSC(rx[k].osc.gen);
330 delvec_COMPLEX(rx[k].filt.save);
331 delFiltOvSv(rx[k].filt.ovsv);
332 delFIR_Bandpass_COMPLEX(rx[k].filt.coef);
333 delCorrectIQ(rx[k].iqfix);
340 closeChan(uni.meter.chan.c);
343 //////////////////////////////////////////////////////////////////////////
345 //////////////////////////////////////////////////////////////////////////
347 //========================================================================
354 for (i = 0; i < CXBhave(buff); i++)
355 sum += Csqrmag(CXBdata(buff, i));
359 //========================================================================
362 /* tap off S-meter from some buf */
365 do_meter(COMPLEX *vec, int len) {
370 switch (uni.meter.type) {
371 case AVG_SIGNAL_STRENGTH:
372 for (i = 0; i < len; i++)
373 uni.meter.val += Csqrmag(vec[i]);
375 uni.meter.avgval = 0.9 * uni.meter.avgval + log10(uni.meter.val + 1e-20);
377 case SIGNAL_STRENGTH:
378 for (i = 0; i < len; i++)
379 uni.meter.val += Csqrmag(vec[i]);
380 uni.meter.avgval = uni.meter.val = 10.0 * log10(uni.meter.val + 1e-20);
383 for(i = 0; i < len; i++)
384 uni.meter.val = max(fabs(vec[i].re), uni.meter.val);
385 uni.meter.val = 20.0 * log10(uni.meter.val + 1e-10);
388 for(i = 0; i < len; i++)
389 uni.meter.val = max(fabs(vec[i].im), uni.meter.val);
390 uni.meter.val = 20.0 * log10(uni.meter.val + 1e-10);
396 putChan_nowait(uni.meter.chan.c,
397 (char *) &uni.meter.val,
398 sizeof(uni.meter.val));
401 //========================================================================
405 should_do_rx_squelch(int k) {
406 if (rx[k].squelch.flag) {
407 int i, n = CXBhave(rx[k].buf.o);
408 rx[k].squelch.power = 0.0;
409 for (i = 0; i < n; i++)
410 rx[k].squelch.power += Csqrmag(CXBdata(rx[k].buf.o, i));
411 return rx[k].squelch.thresh > 10.0 * log10(rx[k].squelch.power);
413 return rx[k].squelch.set = FALSE;
417 // slew into silence first time
421 rx[k].squelch.set = TRUE;
422 if (!rx[k].squelch.running) {
423 int i, m = rx[k].squelch.num, n = CXBhave(rx[k].buf.o) - m;
424 for (i = 0; i < m; i++)
425 CXBdata(rx[k].buf.o, i) = Cscl(CXBdata(rx[k].buf.o, i), 1.0 - (REAL) i / m);
426 memset((void *) (CXBbase(rx[k].buf.o) + m), 0, n * sizeof(COMPLEX));
427 rx[k].squelch.running = TRUE;
429 memset((void *) CXBbase(rx[k].buf.o), 0, CXBhave(rx[k].buf.o) * sizeof(COMPLEX));
433 // slew out from silence to full scale
437 if (rx[k].squelch.running) {
438 int i, m = rx[k].squelch.num;
439 for (i = 0; i < m; i++)
440 CXBdata(rx[k].buf.o, i) = Cscl(CXBdata(rx[k].buf.o, i), (REAL) i / m);
441 rx[k].squelch.running = FALSE;
445 /* pre-condition for (nearly) all RX modes */
449 int i, n = min(CXBhave(rx[k].buf.i), uni.buflen);
452 // do shrinkage here?
455 if (rx[k].scl.pre.flag)
456 for (i = 0; i < n; i++)
457 CXBdata(rx[k].buf.i, i) = Cscl(CXBdata(rx[k].buf.i, i),
460 if (rx[k].nb.flag) noiseblanker(rx[k].nb.gen);
461 if (rx[k].nb_sdrom.flag) SDROMnoiseblanker(rx[k].nb_sdrom.gen);
463 correctIQ(rx[k].buf.i, rx[k].iqfix);
465 /* 2nd IF conversion happens here */
467 if (rx[k].osc.gen->Frequency != 0.0) {
468 ComplexOSC(rx[k].osc.gen);
469 for (i = 0; i < n; i++)
470 CXBdata(rx[k].buf.i, i) = Cmul(CXBdata(rx[k].buf.i, i),
471 OSCCdata(rx[k].osc.gen, i));
474 /* filtering, metering, squelch, & AGC */
476 if (rx[k].mode != SPEC) {
479 reset_OvSv(rx[k].filt.ovsv);
481 filter_OvSv(rx[k].filt.ovsv);
482 CXBhave(rx[k].buf.o) = CXBhave(rx[k].buf.i);
485 do_meter(CXBbase(rx[k].buf.o), uni.buflen);
487 if (should_do_rx_squelch(k))
490 else if (rx[k].agc.flag)
491 DigitalAgc(rx[k].agc.gen, rx[k].tick);
493 } else if (uni.meter.flag)
494 do_meter(CXBbase(rx[k].buf.o), uni.buflen);
499 int i, n = CXBhave(rx[k].buf.o);
501 if (!rx[k].squelch.set) {
504 if (rx[k].spot.flag) {
505 // remember whether it's turned itself off during this pass
506 rx[k].spot.flag = SpotTone(rx[k].spot.gen);
507 for (i = 0; i < n; i++)
508 CXBdata(rx[k].buf.o, i) = Cadd(CXBdata(rx[k].buf.o, i),
509 CXBdata(rx[k].spot.gen->buf, i));
514 // mix in sidetone here?
519 if (rx[k].scl.post.flag)
520 for (i = 0; i < n; i++)
521 CXBdata(rx[k].buf.o, i) = Cscl(CXBdata(rx[k].buf.o, i),
525 // position in stereo field
528 for (i = 0; i < n; i++)
529 CXBdata(rx[k].buf.o, i) = Cscl(rx[k].azim, CXBreal(rx[k].buf.o, i));
533 for (i = 0; i < n; i++)
534 CXBimag(rx[k].buf.o, i) = CXBreal(rx[k].buf.o, i);
538 /* demod processing */
542 if (rx[k].anr.flag) lmsr_adapt(rx[k].anr.gen);
543 if (rx[k].anf.flag) lmsr_adapt(rx[k].anf.gen);
547 do_rx_AM(int k) { AMDemod(rx[k].am.gen); }
550 do_rx_FM(int k) { FMDemod(rx[k].fm.gen); }
557 memcpy(CXBbase(rx[k].buf.o),
558 CXBbase(rx[k].buf.i),
559 sizeof(COMPLEX) * CXBhave(rx[k].buf.i));
560 if (rx[k].agc.flag) DigitalAgc(rx[k].agc.gen, rx[k].tick);
565 int i, n = min(CXBhave(rx[k].buf.i), uni.buflen);
566 for (i = 0; i < n; i++) CXBdata(rx[k].buf.o, i) = cxzero;
569 /* overall dispatch for RX processing */
574 switch (rx[k].mode) {
579 case DSB: do_rx_SBCW(k); break;
581 case SAM: do_rx_AM(k); break;
582 case FMN: do_rx_FM(k); break;
583 case DRM: do_rx_DRM(k); break;
585 default: do_rx_SPEC(k); break;
590 //==============================================================
593 /* pre-condition for (nearly) all TX modes */
598 if (tx.scl.pre.flag) {
599 int i, n = CXBhave(tx.buf.i);
600 for (i = 0; i < n; i++)
601 CXBdata(tx.buf.i, i) = Cmplx(CXBreal(tx.buf.i, i) * tx.scl.pre.val, 0.0);
605 // mix in CW tone here?
608 correctIQ(tx.buf.i, tx.iqfix);
610 if (tx.spr.flag) SpeechProcessor(tx.spr.gen);
612 if (tx.tick == 0) reset_OvSv(tx.filt.ovsv);
613 filter_OvSv(tx.filt.ovsv);
618 CXBhave(tx.buf.o) = CXBhave(tx.buf.i);
620 if (tx.agc.flag) DigitalAgc(tx.agc.gen, tx.tick);
621 if (tx.scl.post.flag) {
622 int i, n = CXBhave(tx.buf.o);
623 for (i = 0; i < n; i++)
624 CXBdata(tx.buf.o, i) = Cscl(CXBdata(tx.buf.o, i), tx.scl.post.val);
626 if (uni.meter.flag) do_meter(CXBbase(tx.buf.o), uni.buflen);
627 if (tx.osc.gen->Frequency != 0.0) {
629 ComplexOSC(tx.osc.gen);
630 for (i = 0; i < CXBhave(tx.buf.o); i++)
631 CXBdata(tx.buf.o, i) = Cmul(CXBdata(tx.buf.o, i), OSCCdata(tx.osc.gen, i));
635 /* modulator processing */
639 int i, n = min(CXBhave(tx.buf.o), uni.buflen);
641 if ((tx.norm = CXBnorm(tx.buf.o)) > 0.0)
642 for (i = 0; i < n; i++) {
643 tx.scl.dc = Cadd(Cscl(tx.scl.dc, 0.99),
644 Cscl(CXBdata(tx.buf.o, i), -0.01));
645 CXBdata(tx.buf.o, i) = Cadd(CXBdata(tx.buf.o, i), tx.scl.dc);
651 int i, n = min(CXBhave(tx.buf.o), uni.buflen);
653 if ((tx.norm = CXBnorm(tx.buf.o)) > 0.0)
654 for (i = 0; i < n; i++) {
655 tx.scl.dc = Cadd(Cscl(tx.scl.dc, 0.999),
656 Cscl(CXBdata(tx.buf.o, i), -0.001));
657 CXBreal(tx.buf.o, i) =
658 0.49995 + 0.49995 * (CXBreal(tx.buf.o, i) - tx.scl.dc.re);
659 CXBimag(tx.buf.o, i) = 0.0;
665 int i, n = min(CXBhave(tx.buf.o), uni.buflen);
666 if ((tx.norm = CXBnorm(tx.buf.o)) > 0.0)
667 for (i = 0; i < n; i++) {
668 tx.scl.dc = Cadd(Cscl(tx.scl.dc, 0.999),
669 Cscl(CXBdata(tx.buf.o, i), 0.001));
670 tx.osc.phase += (CXBreal(tx.buf.o, i) - tx.scl.dc.re) * CvtMod2Freq;
671 if (tx.osc.phase >= TWOPI) tx.osc.phase -= TWOPI;
672 if (tx.osc.phase < 0.0) tx.osc.phase += TWOPI;
673 CXBdata(tx.buf.o, i) =
674 Cscl(Cmplx(cos(tx.osc.phase), sin(tx.osc.phase)), 0.99999);
680 int i, n = min(CXBhave(tx.buf.i), uni.buflen);
681 for (i = 0; i < n; i++) CXBdata(tx.buf.o, i) = cxzero;
684 /* general TX processing dispatch */
694 case DSB: do_tx_SBCW(); break;
696 case SAM: do_tx_AM(); break;
697 case FMN: do_tx_FM(); break;
700 default: do_tx_NIL(); break;
705 //========================================================================
706 /* overall buffer processing;
707 come here when there are buffers to work on */
710 process_samples(float *bufl, float *bufr,
711 float *auxl, float *auxr,
715 switch (uni.mode.trx) {
719 // make copies of the input for all receivers
720 for (k = 0; k < uni.multirx.nrx; k++)
721 if (uni.multirx.act[k]) {
722 for (i = 0; i < n; i++)
723 CXBimag(rx[k].buf.i, i) = bufl[i], CXBreal(rx[k].buf.i, i) = bufr[i];
724 CXBhave(rx[k].buf.i) = n;
727 // prepare buffers for mixing
728 memset((char *) bufl, 0, n * sizeof(float));
729 memset((char *) bufr, 0, n * sizeof(float));
732 for (k = 0; k < uni.multirx.nrx; k++)
733 if (uni.multirx.act[k]) {
734 do_rx(k), rx[k].tick++;
736 for (i = 0; i < n; i++)
737 bufl[i] += CXBimag(rx[k].buf.o, i),
738 bufr[i] += CXBreal(rx[k].buf.o, i);
739 CXBhave(rx[k].buf.o) = n;
742 for (i = 0; i < n; i++)
743 bufl[i] += auxl[i] * uni.mix.rx,
744 bufr[i] += auxr[i] * uni.mix.rx;
750 for (i = 0; i < n; i++)
751 bufl[i] += auxl[i] * uni.mix.tx,
752 bufr[i] += auxr[i] * uni.mix.tx;
754 for (i = 0; i < n; i++)
755 CXBimag(tx.buf.i, i) = bufl[i], CXBreal(tx.buf.i, i) = bufr[i];
756 CXBhave(tx.buf.i) = n;
760 for (i = 0; i < n; i++)
761 bufl[i] = (float) CXBimag(tx.buf.o, i), bufr[i] = (float) CXBreal(tx.buf.o, i);
762 CXBhave(tx.buf.o) = n;