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.flag = uni.mix.tx.flag = FALSE;
80 uni.mix.rx.gain = uni.mix.tx.gain = 1.0;
91 rx[k].iqfix = newCorrectIQ(0.0, 1.0);
92 rx[k].filt.coef = newFIR_Bandpass_COMPLEX(-4800.0,
96 rx[k].filt.ovsv = newFiltOvSv(FIRcoef(rx[k].filt.coef),
97 FIRsize(rx[k].filt.coef),
99 normalize_vec_COMPLEX(rx[k].filt.ovsv->zfvec,
100 rx[k].filt.ovsv->fftlen);
103 rx[k].filt.save = newvec_COMPLEX(rx[k].filt.ovsv->fftlen, "RX filter cache");
104 memcpy((char *) rx[k].filt.save,
105 (char *) rx[k].filt.ovsv->zfvec,
106 rx[k].filt.ovsv->fftlen * sizeof(COMPLEX));
109 /* note we overload the internal filter buffers
111 rx[k].buf.i = newCXB(FiltOvSv_fetchsize(rx[k].filt.ovsv),
112 FiltOvSv_fetchpoint(rx[k].filt.ovsv),
114 rx[k].buf.o = newCXB(FiltOvSv_storesize(rx[k].filt.ovsv),
115 FiltOvSv_storepoint(rx[k].filt.ovsv),
119 rx[k].osc.freq = -11025.0;
120 rx[k].osc.phase = 0.0;
121 rx[k].osc.gen = newOSC(uni.buflen,
126 "SDR RX Oscillator");
128 rx[k].agc.gen = newDigitalAgc(agcMED, // Mode
134 CXBsize(rx[k].buf.o), // BufSize
138 CXBbase(rx[k].buf.o));
139 rx[k].agc.flag = TRUE;
142 rx[k].am.gen = newAMD(48000.0, // REAL samprate
143 0.0, // REAL f_initial
144 -500.0, // REAL f_lobound,
145 500.0, // REAL f_hibound,
146 400.0, // REAL f_bandwid,
147 CXBsize(rx[k].buf.o), // int size,
148 CXBbase(rx[k].buf.o), // COMPLEX *ivec,
149 CXBbase(rx[k].buf.o), // COMPLEX *ovec,
150 AMdet, // AM Mode AMdet == rectifier,
151 // SAMdet == synchronous detector
152 "AM detector blew"); // char *tag
153 rx[k].fm.gen = newFMD(48000, // REAL samprate
154 0.0, // REAL f_initial
155 -6000.0, // REAL f_lobound
156 6000.0, // REAL f_hibound
157 10000.0, // REAL f_bandwid
158 CXBsize(rx[k].buf.o), // int size
159 CXBbase(rx[k].buf.o), // COMPLEX *ivec
160 CXBbase(rx[k].buf.o), // COMPLEX *ovec
161 "New FM Demod structure"); // char *error message;
163 /* noise reduction */
164 rx[k].anf.gen = new_lmsr(rx[k].buf.o, // CXB signal,
166 0.01, // REAL adaptation_rate,
167 0.00001, // REAL leakage,
168 45, // int adaptive_filter_size,
170 rx[k].anf.flag = FALSE;
171 rx[k].anr.gen = new_lmsr(rx[k].buf.o, // CXB signal,
173 0.01, // REAL adaptation_rate,
174 0.00001, // REAL leakage,
175 45, // int adaptive_filter_size,
177 rx[k].anr.flag = FALSE;
179 rx[k].nb.thresh = 3.3;
180 rx[k].nb.gen = new_noiseblanker(rx[k].buf.i, rx[k].nb.thresh);
181 rx[k].nb.flag = FALSE;
183 rx[k].nb_sdrom.thresh = 2.5;
184 rx[k].nb_sdrom.gen = new_noiseblanker(rx[k].buf.i, rx[k].nb_sdrom.thresh);
185 rx[k].nb_sdrom.flag = FALSE;
187 rx[k].spot.gen = newSpotToneGen(-12.0, // gain
194 rx[k].scl.pre.val = 1.0;
195 rx[k].scl.pre.flag = FALSE;
196 rx[k].scl.post.val = 1.0;
197 rx[k].scl.post.flag = FALSE;
199 memset((char *) &rx[k].squelch, 0, sizeof(rx[k].squelch));
200 rx[k].squelch.thresh = -30.0;
201 rx[k].squelch.power = 0.0;
202 rx[k].squelch.flag = rx[k].squelch.running = rx[k].squelch.set = FALSE;
203 rx[k].squelch.num = (int) (0.0395 * uni.samplerate + 0.5);
205 rx[k].mode = uni.mode.sdr;
206 rx[k].bin.flag = FALSE;
209 REAL pos = 0.5, // 0 <= pos <= 1, left->right
210 theta = (1.0 - pos) * M_PI / 2.0;
211 rx[k].azim = Cmplx(cos(theta), sin(theta));
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));
515 if (rx[k].scl.post.flag)
516 for (i = 0; i < n; i++)
517 CXBdata(rx[k].buf.o, i) = Cscl(CXBdata(rx[k].buf.o, i),
521 // position in stereo field
524 for (i = 0; i < n; i++)
525 CXBdata(rx[k].buf.o, i) = Cscl(rx[k].azim, CXBreal(rx[k].buf.o, i));
528 /* demod processing */
532 if (rx[k].anr.flag) lmsr_adapt(rx[k].anr.gen);
533 if (rx[k].anf.flag) lmsr_adapt(rx[k].anf.gen);
537 do_rx_AM(int k) { AMDemod(rx[k].am.gen); }
540 do_rx_FM(int k) { FMDemod(rx[k].fm.gen); }
547 memcpy(CXBbase(rx[k].buf.o),
548 CXBbase(rx[k].buf.i),
549 sizeof(COMPLEX) * CXBhave(rx[k].buf.i));
550 if (rx[k].agc.flag) DigitalAgc(rx[k].agc.gen, rx[k].tick);
555 int i, n = min(CXBhave(rx[k].buf.i), uni.buflen);
556 for (i = 0; i < n; i++) CXBdata(rx[k].buf.o, i) = cxzero;
559 /* overall dispatch for RX processing */
564 switch (rx[k].mode) {
569 case DSB: do_rx_SBCW(k); break;
571 case SAM: do_rx_AM(k); break;
572 case FMN: do_rx_FM(k); break;
573 case DRM: do_rx_DRM(k); break;
575 default: do_rx_SPEC(k); break;
580 //==============================================================
583 /* pre-condition for (nearly) all TX modes */
588 if (tx.scl.pre.flag) {
589 int i, n = CXBhave(tx.buf.i);
590 for (i = 0; i < n; i++)
591 CXBdata(tx.buf.i, i) = Cmplx(CXBreal(tx.buf.i, i) * tx.scl.pre.val, 0.0);
594 correctIQ(tx.buf.i, tx.iqfix);
596 if (tx.spr.flag) SpeechProcessor(tx.spr.gen);
598 if (tx.tick == 0) reset_OvSv(tx.filt.ovsv);
599 filter_OvSv(tx.filt.ovsv);
604 CXBhave(tx.buf.o) = CXBhave(tx.buf.i);
606 if (tx.agc.flag) DigitalAgc(tx.agc.gen, tx.tick);
607 if (tx.scl.post.flag) {
608 int i, n = CXBhave(tx.buf.o);
609 for (i = 0; i < n; i++)
610 CXBdata(tx.buf.o, i) = Cscl(CXBdata(tx.buf.o, i), tx.scl.post.val);
612 if (uni.meter.flag) do_meter(CXBbase(tx.buf.o), uni.buflen);
613 if (tx.osc.gen->Frequency != 0.0) {
615 ComplexOSC(tx.osc.gen);
616 for (i = 0; i < CXBhave(tx.buf.o); i++)
617 CXBdata(tx.buf.o, i) = Cmul(CXBdata(tx.buf.o, i), OSCCdata(tx.osc.gen, i));
621 /* modulator processing */
625 int i, n = min(CXBhave(tx.buf.o), uni.buflen);
627 if ((tx.norm = CXBnorm(tx.buf.o)) > 0.0)
628 for (i = 0; i < n; i++) {
629 tx.scl.dc = Cadd(Cscl(tx.scl.dc, 0.99),
630 Cscl(CXBdata(tx.buf.o, i), -0.01));
631 CXBdata(tx.buf.o, i) = Cadd(CXBdata(tx.buf.o, i), tx.scl.dc);
637 int i, n = min(CXBhave(tx.buf.o), uni.buflen);
639 if ((tx.norm = CXBnorm(tx.buf.o)) > 0.0)
640 for (i = 0; i < n; i++) {
641 tx.scl.dc = Cadd(Cscl(tx.scl.dc, 0.999),
642 Cscl(CXBdata(tx.buf.o, i), -0.001));
643 CXBreal(tx.buf.o, i) =
644 0.49995 + 0.49995 * (CXBreal(tx.buf.o, i) - tx.scl.dc.re);
645 CXBimag(tx.buf.o, i) = 0.0;
651 int i, n = min(CXBhave(tx.buf.o), uni.buflen);
652 if ((tx.norm = CXBnorm(tx.buf.o)) > 0.0)
653 for (i = 0; i < n; i++) {
654 tx.scl.dc = Cadd(Cscl(tx.scl.dc, 0.999),
655 Cscl(CXBdata(tx.buf.o, i), 0.001));
656 tx.osc.phase += (CXBreal(tx.buf.o, i) - tx.scl.dc.re) * CvtMod2Freq;
657 if (tx.osc.phase >= TWOPI) tx.osc.phase -= TWOPI;
658 if (tx.osc.phase < 0.0) tx.osc.phase += TWOPI;
659 CXBdata(tx.buf.o, i) =
660 Cscl(Cmplx(cos(tx.osc.phase), sin(tx.osc.phase)), 0.99999);
666 int i, n = min(CXBhave(tx.buf.i), uni.buflen);
667 for (i = 0; i < n; i++) CXBdata(tx.buf.o, i) = cxzero;
670 /* general TX processing dispatch */
680 case DSB: do_tx_SBCW(); break;
682 case SAM: do_tx_AM(); break;
683 case FMN: do_tx_FM(); break;
686 default: do_tx_NIL(); break;
691 //========================================================================
692 /* overall buffer processing;
693 come here when there are buffers to work on */
696 process_samples(float *bufl, float *bufr,
697 float *auxl, float *auxr,
701 switch (uni.mode.trx) {
705 // make copies of the input for all receivers
706 for (k = 0; k < uni.multirx.nrx; k++)
707 if (uni.multirx.act[k]) {
708 for (i = 0; i < n; i++)
709 CXBimag(rx[k].buf.i, i) = bufl[i], CXBreal(rx[k].buf.i, i) = bufr[i];
710 CXBhave(rx[k].buf.i) = n;
713 // prepare buffers for mixing
714 memset((char *) bufl, 0, n * sizeof(float));
715 memset((char *) bufr, 0, n * sizeof(float));
718 for (k = 0; k < uni.multirx.nrx; k++)
719 if (uni.multirx.act[k]) {
720 do_rx(k), rx[k].tick++;
722 for (i = 0; i < n; i++)
723 bufl[i] += CXBimag(rx[k].buf.o, i),
724 bufr[i] += CXBreal(rx[k].buf.o, i);
725 CXBhave(rx[k].buf.o) = n;
729 for (i = 0; i < n; i++)
730 bufl[i] += auxl[i] * uni.mix.rx.gain,
731 bufr[i] += auxr[i] * uni.mix.rx.gain;
738 for (i = 0; i < n; i++)
739 bufl[i] += auxl[i] * uni.mix.tx.gain,
740 bufr[i] += auxr[i] * uni.mix.tx.gain;
742 for (i = 0; i < n; i++)
743 CXBimag(tx.buf.i, i) = bufl[i], CXBreal(tx.buf.i, i) = bufr[i];
744 CXBhave(tx.buf.i) = n;
748 for (i = 0; i < n; i++)
749 bufl[i] = (float) CXBimag(tx.buf.o, i), bufr[i] = (float) CXBreal(tx.buf.o, i);
750 CXBhave(tx.buf.o) = n;