2 * rtl-sdr, turns your Realtek RTL2832 based DVB dongle into a SDR receiver
3 * Copyright (C) 2012 by Steve Markgraf <steve@steve-m.de>
4 * Copyright (C) 2012 by Hoernchen <la@tfc-server.de>
5 * Copyright (C) 2012 by Kyle Keen <keenerd@gmail.com>
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, see <http://www.gnu.org/licenses/>.
23 * written because people could not do real time
24 * FM demod on Atom hardware with GNU radio
25 * based on rtl_sdr.c and rtl_tcp.c
26 * todo: realtime ARMv5
27 * remove float math (disqualifies complex.h)
28 * in-place array operations
30 * nicer FIR than square
31 * scale squelch to other input parameters
32 * test all the demodulations
35 * frequency ranges could be stored better
51 #include "getopt/getopt.h"
52 #define usleep(x) Sleep(x/1000)
53 #define round(x) (x > 0.0 ? floor(x + 0.5): ceil(x - 0.5))
56 #include <semaphore.h>
62 #define DEFAULT_SAMPLE_RATE 24000
63 #define DEFAULT_ASYNC_BUF_NUMBER 32
64 #define DEFAULT_BUF_LENGTH (1 * 16384)
65 #define MAXIMUM_OVERSAMPLE 16
66 #define MAXIMUM_BUF_LENGTH (MAXIMUM_OVERSAMPLE * DEFAULT_BUF_LENGTH)
67 #define AUTO_GAIN -100
69 static pthread_t demod_thread;
70 static sem_t data_ready;
71 static int do_exit = 0;
72 static rtlsdr_dev_t *dev = NULL;
73 static int lcm_post[17] = {1,1,1,3,1,5,3,7,1,9,5,11,3,13,7,15,1};
82 int downsample; /* min 1, max 256 */
88 int terminate_on_squelch;
90 uint8_t buf[MAXIMUM_BUF_LENGTH];
92 int signal[MAXIMUM_BUF_LENGTH]; /* 16 bit signed i/q pairs */
93 int16_t signal2[MAXIMUM_BUF_LENGTH]; /* signal has lowpass, signal2 has demod */
101 uint32_t sample_rate;
104 int fir[256]; /* fir_len == downsample */
111 void (*mode_demod)(struct fm_state*);
117 "rtl_fm, a simple narrow band FM demodulator for RTL2832 based DVB-T receivers\n\n"
118 "Use:\trtl_fm -f freq [-options] [filename]\n"
119 "\t-f frequency_to_tune_to [Hz]\n"
120 "\t (use multiple -f for scanning, requires squelch)\n"
121 "\t (ranges supported, -f 118M:137M:25k)\n"
122 "\t[-s sample_rate (default: 24k)]\n"
123 "\t[-d device_index (default: 0)]\n"
124 "\t[-g tuner_gain (default: automatic)]\n"
125 "\t[-l squelch_level (default: 0/off)]\n"
126 "\t[-o oversampling (default: 1, 4 recommended)]\n"
127 "\t[-p ppm_error (default: 0)]\n"
128 "\t[-E sets lower edge tuning (default: center)]\n"
129 "\t[-N enables NBFM mode (default: on)]\n"
130 "\t[-W enables WBFM mode (default: off)]\n"
131 "\t (-N -s 170k -o 4 -A -r 32k -l 0 -D)\n"
132 "\tfilename (a '-' dumps samples to stdout)\n"
133 "\t (omitting the filename also uses stdout)\n\n"
134 "Experimental options:\n"
135 "\t[-r output_rate (default: same as -s)]\n"
136 "\t[-t squelch_delay (default: 20)]\n"
137 "\t (+values will mute/scan, -values will exit)\n"
138 "\t[-M enables AM mode (default: off)]\n"
139 "\t[-L enables LSB mode (default: off)]\n"
140 "\t[-U enables USB mode (default: off)]\n"
141 //"\t[-D enables DSB mode (default: off)]\n"
142 "\t[-R enables raw mode (default: off, 2x16 bit output)]\n"
143 "\t[-F enables high quality FIR (default: off/square)]\n"
144 "\t[-D enables de-emphasis (default: off)]\n"
145 "\t[-A enables high speed arctan (default: off)]\n\n"
146 "Produces signed 16 bit ints, use Sox or aplay to hear them.\n"
147 "\trtl_fm ... - | play -t raw -r 24k -e signed-integer -b 16 -c 1 -V1 -\n"
148 "\t | aplay -r 24k -f S16_LE -t raw -c 1\n"
149 "\t -s 22.5k - | multimon -t raw /dev/stdin\n\n");
155 sighandler(int signum)
157 if (CTRL_C_EVENT == signum) {
158 fprintf(stderr, "Signal caught, exiting!\n");
160 rtlsdr_cancel_async(dev);
166 static void sighandler(int signum)
168 fprintf(stderr, "Signal caught, exiting!\n");
170 rtlsdr_cancel_async(dev);
174 void rotate_90(unsigned char *buf, uint32_t len)
175 /* 90 rotation is 1+0j, 0+1j, -1+0j, 0-1j
176 or [0, 1, -3, 2, -4, -5, 7, -6] */
180 for (i=0; i<len; i+=8) {
181 /* uint8_t negation = 255 - x */
182 tmp = 255 - buf[i+3];
186 buf[i+4] = 255 - buf[i+4];
187 buf[i+5] = 255 - buf[i+5];
189 tmp = 255 - buf[i+6];
195 void low_pass(struct fm_state *fm, unsigned char *buf, uint32_t len)
196 /* simple square window FIR */
199 while (i < (int)len) {
200 fm->now_r += ((int)buf[i] - 128);
201 fm->now_j += ((int)buf[i+1] - 128);
204 if (fm->prev_index < fm->downsample) {
207 fm->signal[i2] = fm->now_r * fm->output_scale;
208 fm->signal[i2+1] = fm->now_j * fm->output_scale;
217 void build_fir(struct fm_state *fm)
218 /* for now, a simple triangle
219 * fancy FIRs are equally expensive, so use one */
220 /* point = sum(sample[i] * fir[i] * fir_len / fir_sum) */
223 len = fm->downsample;
224 for(i = 0; i < (len/2); i++) {
227 for(i = len-1; i >= (len/2); i--) {
228 fm->fir[i] = len - i;
231 for(i = 0; i < len; i++) {
232 fm->fir_sum += fm->fir[i];
236 void low_pass_fir(struct fm_state *fm, unsigned char *buf, uint32_t len)
237 /* perform an arbitrary FIR, doubles CPU use */
238 // possibly bugged, or overflowing
241 while (i < (int)len) {
243 fm->now_r += ((int)buf[i] - 128) * fm->fir[i3] * fm->downsample / fm->fir_sum;
244 fm->now_j += ((int)buf[i+1] - 128) * fm->fir[i3] * fm->downsample / fm->fir_sum;
247 if (fm->prev_index < fm->downsample) {
250 fm->signal[i2] = fm->now_r * fm->output_scale;
251 fm->signal[i2+1] = fm->now_j * fm->output_scale;
260 int low_pass_simple(int16_t *signal2, int len, int step)
261 // no wrap around, length must be multiple of step
264 for(i=0; i < len; i+=step) {
266 for(i2=0; i2<step; i2++) {
267 sum += (int)signal2[i + i2];
269 //signal2[i/step] = (int16_t)(sum / step);
270 signal2[i/step] = (int16_t)(sum);
272 signal2[i/step + 1] = signal2[i/step];
276 void low_pass_real(struct fm_state *fm)
277 /* simple square window FIR */
278 // add support for upsampling?
281 int fast = (int)fm->sample_rate / fm->post_downsample;
282 int slow = fm->output_rate;
283 while (i < fm->signal2_len) {
284 fm->now_lpr += fm->signal2[i];
286 fm->prev_lpr_index += slow;
287 if (fm->prev_lpr_index < fast) {
290 fm->signal2[i2] = (int16_t)(fm->now_lpr / (fast/slow));
291 fm->prev_lpr_index -= fast;
295 fm->signal2_len = i2;
298 /* define our own complex math ops
299 because ARMv5 has no hardware float */
301 void multiply(int ar, int aj, int br, int bj, int *cr, int *cj)
307 int polar_discriminant(int ar, int aj, int br, int bj)
311 multiply(ar, aj, br, -bj, &cr, &cj);
312 angle = atan2((double)cj, (double)cr);
313 return (int)(angle / 3.14159 * (1<<14));
316 int fast_atan2(int y, int x)
317 /* pre scaled for int16 */
320 int pi4=(1<<12), pi34=3*(1<<12); // note pi = 1<<14
329 angle = pi4 - pi4 * (x-yabs) / (x+yabs);
331 angle = pi34 - pi4 * (x+yabs) / (yabs-x);
339 int polar_disc_fast(int ar, int aj, int br, int bj)
342 multiply(ar, aj, br, -bj, &cr, &cj);
343 return fast_atan2(cj, cr);
346 void fm_demod(struct fm_state *fm)
349 pcm = polar_discriminant(fm->signal[0], fm->signal[1],
350 fm->pre_r, fm->pre_j);
351 fm->signal2[0] = (int16_t)pcm;
352 for (i = 2; i < (fm->signal_len); i += 2) {
353 if (fm->custom_atan) {
354 pcm = polar_disc_fast(fm->signal[i], fm->signal[i+1],
355 fm->signal[i-2], fm->signal[i-1]);
357 pcm = polar_discriminant(fm->signal[i], fm->signal[i+1],
358 fm->signal[i-2], fm->signal[i-1]);
360 fm->signal2[i/2] = (int16_t)pcm;
362 fm->pre_r = fm->signal[fm->signal_len - 2];
363 fm->pre_j = fm->signal[fm->signal_len - 1];
364 fm->signal2_len = fm->signal_len/2;
367 void am_demod(struct fm_state *fm)
368 // todo, fix this extreme laziness
371 for (i = 0; i < (fm->signal_len); i += 2) {
372 // hypot uses floats but won't overflow
373 //fm->signal2[i/2] = (int16_t)hypot(fm->signal[i], fm->signal[i+1]);
374 pcm = fm->signal[i] * fm->signal[i];
375 pcm += fm->signal[i+1] * fm->signal[i+1];
376 fm->signal2[i/2] = (int16_t)sqrt(pcm); // * fm->output_scale;
378 fm->signal2_len = fm->signal_len/2;
379 // lowpass? (3khz) highpass? (dc)
382 void usb_demod(struct fm_state *fm)
385 for (i = 0; i < (fm->signal_len); i += 2) {
386 pcm = fm->signal[i] + fm->signal[i+1];
387 fm->signal2[i/2] = (int16_t)pcm; // * fm->output_scale;
389 fm->signal2_len = fm->signal_len/2;
392 void lsb_demod(struct fm_state *fm)
395 for (i = 0; i < (fm->signal_len); i += 2) {
396 pcm = fm->signal[i] - fm->signal[i+1];
397 fm->signal2[i/2] = (int16_t)pcm; // * fm->output_scale;
399 fm->signal2_len = fm->signal_len/2;
402 void raw_demod(struct fm_state *fm)
404 /* hacky and pointless code */
406 for (i = 0; i < (fm->signal_len); i++) {
407 fm->signal2[i] = (int16_t)fm->signal[i];
409 fm->signal2_len = fm->signal_len;
412 void deemph_filter(struct fm_state *fm)
414 static int avg; // cheating...
417 // avg = avg * (1 - alpha) + sample * alpha;
418 for (i = 0; i < fm->signal2_len; i++) {
419 d = fm->signal2[i] - avg;
421 avg += (d + fm->deemph_a/2) / fm->deemph_a;
423 avg += (d - fm->deemph_a/2) / fm->deemph_a;
425 fm->signal2[i] = (int16_t)avg;
429 int mad(int *samples, int len, int step)
430 /* mean average deviation */
432 int i=0, sum=0, ave=0;
435 for (i=0; i<len; i+=step) {
438 ave = sum / (len * step);
440 for (i=0; i<len; i+=step) {
441 sum += abs(samples[i] - ave);
443 return sum / (len / step);
446 int post_squelch(struct fm_state *fm)
447 /* returns 1 for active signal, 0 for no signal */
449 int dev_r, dev_j, len, sq_l;
450 /* only for small samples, big samples need chunk processing */
451 len = fm->signal_len;
452 sq_l = fm->squelch_level;
453 dev_r = mad(&(fm->signal[0]), len, 2);
454 dev_j = mad(&(fm->signal[1]), len, 2);
455 if ((dev_r > sq_l) || (dev_j > sq_l)) {
456 fm->squelch_hits = 0;
463 static void optimal_settings(struct fm_state *fm, int freq, int hopping)
465 int r, capture_freq, capture_rate;
466 fm->downsample = (1000000 / fm->sample_rate) + 1;
468 capture_rate = fm->downsample * fm->sample_rate;
469 capture_freq = fm->freqs[freq] + capture_rate/4;
470 capture_freq += fm->edge * fm->sample_rate / 2;
471 fm->output_scale = (1<<15) / (128 * fm->downsample);
472 if (fm->output_scale < 1) {
473 fm->output_scale = 1;}
474 fm->output_scale = 1;
475 /* Set the frequency */
476 r = rtlsdr_set_center_freq(dev, (uint32_t)capture_freq);
479 fprintf(stderr, "Oversampling input by: %ix.\n", fm->downsample);
480 fprintf(stderr, "Oversampling output by: %ix.\n", fm->post_downsample);
481 fprintf(stderr, "Buffer size: %0.2fms\n",
482 1000 * 0.5 * lcm_post[fm->post_downsample] * (float)DEFAULT_BUF_LENGTH / (float)capture_rate);
484 fprintf(stderr, "WARNING: Failed to set center freq.\n");}
486 fprintf(stderr, "Tuned to %u Hz.\n", capture_freq);}
488 /* Set the sample rate */
489 fprintf(stderr, "Sampling at %u Hz.\n", capture_rate);
490 if (fm->output_rate > 0) {
491 fprintf(stderr, "Output at %u Hz.\n", fm->output_rate);
493 fprintf(stderr, "Output at %u Hz.\n", fm->sample_rate/fm->post_downsample);}
494 r = rtlsdr_set_sample_rate(dev, (uint32_t)capture_rate);
496 fprintf(stderr, "WARNING: Failed to set sample rate.\n");}
500 void full_demod(struct fm_state *fm)
502 int i, sr, freq_next, hop = 0;
503 rotate_90(fm->buf, fm->buf_len);
504 if (fm->fir_enable) {
505 low_pass_fir(fm, fm->buf, fm->buf_len);
507 low_pass(fm, fm->buf, fm->buf_len);
510 if (fm->mode_demod == &raw_demod) {
511 fwrite(fm->signal2, 2, fm->signal2_len, fm->file);
514 sr = post_squelch(fm);
515 if (!sr && fm->squelch_hits > fm->conseq_squelch) {
516 if (fm->terminate_on_squelch) {
518 if (fm->freq_len == 1) { /* mute */
519 for (i=0; i<fm->signal_len; i++) {
525 if (fm->post_downsample > 1) {
526 fm->signal2_len = low_pass_simple(fm->signal2, fm->signal2_len, fm->post_downsample);}
527 if (fm->output_rate > 0) {
532 /* ignore under runs for now */
533 fwrite(fm->signal2, 2, fm->signal2_len, fm->file);
535 freq_next = (fm->freq_now + 1) % fm->freq_len;
536 optimal_settings(fm, freq_next, 1);
537 fm->squelch_hits = fm->conseq_squelch + 1; /* hair trigger */
538 /* wait for settling and flush buffer */
540 rtlsdr_read_sync(dev, NULL, 4096, NULL);
544 static void rtlsdr_callback(unsigned char *buf, uint32_t len, void *ctx)
546 struct fm_state *fm2 = ctx;
552 memcpy(fm2->buf, buf, len);
554 /* single threaded uses 25% less CPU? */
555 /* full_demod(fm2); */
556 sem_getvalue(&data_ready, &dr_val);
558 sem_post(&data_ready);}
561 static void *demod_thread_fn(void *arg)
563 struct fm_state *fm2 = arg;
565 sem_wait(&data_ready);
567 if (fm2->exit_flag) {
569 rtlsdr_cancel_async(dev);}
574 double atofs(char* f)
575 /* standard suffixes */
579 chop = malloc((strlen(f)+1)*sizeof(char));
580 strncpy(chop, f, strlen(f)-1);
581 switch (f[strlen(f)-1]) {
595 void frequency_range(struct fm_state *fm, char *arg)
597 char *start, *stop, *step;
600 stop = strchr(start, ':') + 1;
602 step = strchr(stop, ':') + 1;
604 for(i=(int)atofs(start); i<=(int)atofs(stop); i+=(int)atofs(step))
606 fm->freqs[fm->freq_len] = (uint32_t)i;
613 int main(int argc, char **argv)
616 struct sigaction sigact;
619 char *filename = NULL;
620 int n_read, r, opt, wb_mode = 0;
621 int i, gain = AUTO_GAIN; // tenths of a dB
623 uint32_t dev_index = 0;
626 char vendor[256], product[256], serial[256];
627 fm.freqs[0] = 100000000;
628 fm.sample_rate = DEFAULT_SAMPLE_RATE;
629 fm.squelch_level = 0;
630 fm.conseq_squelch = 20;
631 fm.terminate_on_squelch = 0;
636 fm.post_downsample = 1; // once this works, default = 4
639 fm.output_rate = -1; // flag for disabled
640 fm.mode_demod = &fm_demod;
641 sem_init(&data_ready, 0, 0);
643 while ((opt = getopt(argc, argv, "d:f:g:s:b:l:o:t:r:p:EFANWMULRD")) != -1) {
646 dev_index = atoi(optarg);
649 if (strchr(optarg, ':'))
650 {frequency_range(&fm, optarg);}
653 fm.freqs[fm.freq_len] = (uint32_t)atofs(optarg);
658 gain = (int)(atof(optarg) * 10);
661 fm.squelch_level = (int)atof(optarg);
664 fm.sample_rate = (uint32_t)atofs(optarg);
667 fm.output_rate = (int)atofs(optarg);
670 fm.post_downsample = (int)atof(optarg);
671 if (fm.post_downsample < 1 || fm.post_downsample > MAXIMUM_OVERSAMPLE) {
672 fprintf(stderr, "Oversample must be between 1 and %i\n", MAXIMUM_OVERSAMPLE);}
675 fm.conseq_squelch = (int)atof(optarg);
676 if (fm.conseq_squelch < 0) {
677 fm.conseq_squelch = -fm.conseq_squelch;
678 fm.terminate_on_squelch = 1;
682 ppm_error = atoi(optarg);
697 fm.mode_demod = &fm_demod;
701 fm.mode_demod = &fm_demod;
702 fm.sample_rate = 170000;
703 fm.output_rate = 32000;
705 fm.post_downsample = 4;
707 fm.squelch_level = 0;
710 fm.mode_demod = &am_demod;
713 fm.mode_demod = &usb_demod;
716 fm.mode_demod = &lsb_demod;
719 fm.mode_demod = &raw_demod;
726 /* quadruple sample_rate to limit to Δθ to ±π/2 */
727 fm.sample_rate *= fm.post_downsample;
729 if (fm.freq_len > 1) {
730 fm.terminate_on_squelch = 0;
733 if (argc <= optind) {
737 filename = argv[optind];
740 buffer = malloc(lcm_post[fm.post_downsample] * DEFAULT_BUF_LENGTH * sizeof(uint8_t));
742 device_count = rtlsdr_get_device_count();
744 fprintf(stderr, "No supported devices found.\n");
748 fprintf(stderr, "Found %d device(s):\n", device_count);
749 for (i = 0; i < device_count; i++) {
750 rtlsdr_get_device_usb_strings(i, vendor, product, serial);
751 fprintf(stderr, " %d: %s, %s, SN: %s\n", i, vendor, product, serial);
753 fprintf(stderr, "\n");
755 fprintf(stderr, "Using device %d: %s\n",
756 dev_index, rtlsdr_get_device_name(dev_index));
758 r = rtlsdr_open(&dev, dev_index);
760 fprintf(stderr, "Failed to open rtlsdr device #%d.\n", dev_index);
764 sigact.sa_handler = sighandler;
765 sigemptyset(&sigact.sa_mask);
767 sigaction(SIGINT, &sigact, NULL);
768 sigaction(SIGTERM, &sigact, NULL);
769 sigaction(SIGQUIT, &sigact, NULL);
770 sigaction(SIGPIPE, &sigact, NULL);
772 SetConsoleCtrlHandler( (PHANDLER_ROUTINE) sighandler, TRUE );
775 /* WBFM is special */
777 fm.freqs[0] += 16000;
781 fm.deemph_a = (int)round(1.0/((1.0-exp(-1.0/(fm.output_rate * 75e-6)))));
784 optimal_settings(&fm, 0, 0);
787 /* Set the tuner gain */
788 if (gain == AUTO_GAIN) {
789 r = rtlsdr_set_tuner_gain_mode(dev, 0);
791 r = rtlsdr_set_tuner_gain_mode(dev, 1);
792 r = rtlsdr_set_tuner_gain(dev, gain);
795 fprintf(stderr, "WARNING: Failed to set tuner gain.\n");
796 } else if (gain == AUTO_GAIN) {
797 fprintf(stderr, "Tuner gain set to automatic.\n");
799 fprintf(stderr, "Tuner gain set to %0.2f dB.\n", gain/10.0);
801 r = rtlsdr_set_freq_correction(dev, ppm_error);
803 if (strcmp(filename, "-") == 0) { /* Write samples to stdout */
806 _setmode(_fileno(fm.file), _O_BINARY);
809 fm.file = fopen(filename, "wb");
811 fprintf(stderr, "Failed to open %s\n", filename);
816 /* Reset endpoint before we start reading from it (mandatory) */
817 r = rtlsdr_reset_buffer(dev);
819 fprintf(stderr, "WARNING: Failed to reset buffers.\n");}
821 pthread_create(&demod_thread, NULL, demod_thread_fn, (void *)(&fm));
822 rtlsdr_read_async(dev, rtlsdr_callback, (void *)(&fm),
823 DEFAULT_ASYNC_BUF_NUMBER,
824 lcm_post[fm.post_downsample] * DEFAULT_BUF_LENGTH);
827 fprintf(stderr, "\nUser cancel, exiting...\n");}
829 fprintf(stderr, "\nLibrary error %d, exiting...\n", r);}
830 rtlsdr_cancel_async(dev);
832 if (fm.file != stdout) {
837 return r >= 0 ? r : -r;