From 6a3cfd608e7a3c5d7f0087be43c5197762bae298 Mon Sep 17 00:00:00 2001 From: Andreas Eversberg Date: Sun, 5 Feb 2017 08:54:56 +0100 Subject: Add ascii-art spectrum analyzer for SDR support --- src/common/Makefile.am | 6 +- src/common/debug.c | 6 + src/common/display.h | 18 ++- src/common/display_iq.c | 9 +- src/common/display_spectrum.c | 247 ++++++++++++++++++++++++++++++++++++++++++ src/common/fft.c | 96 ++++++++++++++++ src/common/fft.h | 3 + src/common/main_common.c | 12 ++ src/common/sdr.c | 2 + 9 files changed, 391 insertions(+), 8 deletions(-) create mode 100644 src/common/display_spectrum.c create mode 100644 src/common/fft.c create mode 100644 src/common/fft.h (limited to 'src/common') diff --git a/src/common/Makefile.am b/src/common/Makefile.am index ce8bba1..c19c71b 100644 --- a/src/common/Makefile.am +++ b/src/common/Makefile.am @@ -23,16 +23,18 @@ libcommon_a_SOURCES = \ ../common/cause.c \ ../common/emphasis.c \ ../common/compandor.c \ + ../common/fft.c \ ../common/sender.c \ ../common/display_wave.c \ - ../common/display_iq.c \ ../common/main_common.c if HAVE_SDR AM_CPPFLAGS += -DHAVE_SDR libcommon_a_SOURCES += \ - ../common/sdr.c + ../common/sdr.c \ + ../common/display_iq.c \ + ../common/display_spectrum.c endif if HAVE_UHD diff --git a/src/common/debug.c b/src/common/debug.c index ab79b0f..af056d2 100644 --- a/src/common/debug.c +++ b/src/common/debug.c @@ -97,10 +97,16 @@ void _printdebug(const char *file, const char __attribute__((unused)) *function, clear_console_text(); // printf("%s%s:%d %s() %s: %s\033[0;39m", debug_cat[cat].color, file, line, function, debug_level[level], buffer); display_wave_limit_scroll(1); +#ifdef HAVE_SDR display_iq_limit_scroll(1); + display_spectrum_limit_scroll(1); +#endif printf("%s%s:%d %s: %s\033[0;39m", debug_cat[cat].color, file, line, debug_level[level], buffer); display_wave_limit_scroll(0); +#ifdef HAVE_SDR display_iq_limit_scroll(0); + display_spectrum_limit_scroll(0); +#endif print_console_text(); fflush(stdout); } diff --git a/src/common/display.h b/src/common/display.h index 8590298..59b1b4c 100644 --- a/src/common/display.h +++ b/src/common/display.h @@ -1,3 +1,5 @@ +#define DISPLAY_INTERVAL 0.04 + #define MAX_DISPLAY_WIDTH 1024 typedef struct sender sender_t; @@ -14,10 +16,18 @@ typedef struct display_wave { typedef struct display_iq { int interval_pos; int interval_max; - int offset; - float buffer[MAX_DISPLAY_IQ * 2]; + float buffer[MAX_DISPLAY_IQ * 2]; } dispiq_t; +#define MAX_DISPLAY_SPECTRUM 256 + +typedef struct display_spectrum { + int interval_pos; + int interval_max; + double buffer_I[MAX_DISPLAY_SPECTRUM]; + double buffer_Q[MAX_DISPLAY_SPECTRUM]; +} dispspectrum_t; + void get_win_size(int *w, int *h); void display_wave_init(sender_t *sender, int samplerate); @@ -30,3 +40,7 @@ void display_iq_on(int on); void display_iq_limit_scroll(int on); void display_iq(float *samples, int length); +void display_spectrum_init(int samplerate); +void display_spectrum_on(int on); +void display_spectrum_limit_scroll(int on); +void display_spectrum(float *samples, int length); diff --git a/src/common/display_iq.c b/src/common/display_iq.c index 1d77c56..d9f6648 100644 --- a/src/common/display_iq.c +++ b/src/common/display_iq.c @@ -41,8 +41,8 @@ void display_iq_init(int samplerate) memset(&disp, 0, sizeof(disp)); disp.interval_max = (double)samplerate * DISPLAY_INTERVAL + 0.5; /* should not happen due to low interval */ - if (disp.interval_max < MAX_DISPLAY_IQ + 1) - disp.interval_max = MAX_DISPLAY_IQ + 1; + if (disp.interval_max < MAX_DISPLAY_IQ - 1) + disp.interval_max = MAX_DISPLAY_IQ - 1; } void display_iq_on(int on) @@ -132,8 +132,9 @@ void display_iq(float *samples, int length) pos = 0; continue; } - buffer[pos++] = *samples++; - buffer[pos++] = *samples++; + buffer[pos * 2] = samples[i * 2]; + buffer[pos * 2 + 1] = samples[i * 2 + 1]; + pos++; if (pos == MAX_DISPLAY_IQ) { memset(&screen, ' ', sizeof(screen)); memset(&overdrive, 0, sizeof(overdrive)); diff --git a/src/common/display_spectrum.c b/src/common/display_spectrum.c new file mode 100644 index 0000000..fcdd706 --- /dev/null +++ b/src/common/display_spectrum.c @@ -0,0 +1,247 @@ +/* display spectrum of IQ data + * + * (C) 2016 by Andreas Eversberg + * All Rights Reserved + * + * 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 3 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, see . + */ + +#include +#include +#include +#include +#include +#include "sample.h" +#include "sender.h" +#include "fft.h" + +#define HEIGHT 20 + +static double buffer_max[MAX_DISPLAY_SPECTRUM]; +static char screen[HEIGHT][MAX_DISPLAY_WIDTH]; +static uint8_t screen_color[HEIGHT][MAX_DISPLAY_WIDTH]; +static int spectrum_on = 0; +static double db = 100; + +static dispspectrum_t disp; + +void display_spectrum_init(int samplerate) +{ + memset(&disp, 0, sizeof(disp)); + disp.interval_max = (double)samplerate * DISPLAY_INTERVAL + 0.5; + /* should not happen due to low interval */ + if (disp.interval_max < MAX_DISPLAY_SPECTRUM - 1) + disp.interval_max = MAX_DISPLAY_SPECTRUM - 1; + memset(buffer_max, 0, sizeof(buffer_max)); +} + +void display_spectrum_on(int on) +{ + int j; + int w, h; + + get_win_size(&w, &h); + + if (spectrum_on) { + memset(&screen, ' ', sizeof(screen)); + printf("\0337\033[H"); + for (j = 0; j < HEIGHT; j++) { + screen[j][w] = '\0'; + puts(screen[j]); + } + printf("\0338"); fflush(stdout); + } + + if (on < 0) { + if (++spectrum_on == 2) + spectrum_on = 0; + } else + spectrum_on = on; +} + +void display_spectrum_limit_scroll(int on) +{ + int w, h; + + if (!spectrum_on) + return; + + get_win_size(&w, &h); + + printf("\0337"); + printf("\033[%d;%dr", (on) ? HEIGHT + 1 : 1, h); + printf("\0338"); +} + +/* + * plot spectrum data: + * + */ +void display_spectrum(float *samples, int length) +{ + int width, h; + int pos, max; + double *buffer_I, *buffer_Q; + int color = 9; /* default color */ + int i, j, k, count; + int m; + double I, Q, v, c; + int s, e; + + if (!spectrum_on) + return; + + get_win_size(&width, &h); + if (width > MAX_DISPLAY_SPECTRUM) + width = MAX_DISPLAY_SPECTRUM; + + int heigh[width], low[width]; + + pos = disp.interval_pos; + max = disp.interval_max; + buffer_I = disp.buffer_I; + buffer_Q = disp.buffer_Q; + + for (i = 0; i < length; i++) { + if (pos >= MAX_DISPLAY_SPECTRUM) { + if (++pos == max) + pos = 0; + continue; + } + buffer_I[pos] = samples[i * 2]; + buffer_Q[pos] = samples[i * 2 + 1]; + pos++; + if (pos == MAX_DISPLAY_SPECTRUM) { + /* calculate number of taps */ + for (m = 0; m < 16; m++) { + if ((1 << m) == MAX_DISPLAY_SPECTRUM) + break; + } + if (m == 16) { + fprintf(stderr, "Size of spectrum is not a power of 2, please fix!\n"); + abort(); + } + fft_process(1, m, buffer_I, buffer_Q); + count = 0; + c = 0.0; + k = 0; + for (j = 0; j < MAX_DISPLAY_SPECTRUM; j++) { + /* scale result vertically */ + I = buffer_I[(j + MAX_DISPLAY_SPECTRUM / 2) % MAX_DISPLAY_SPECTRUM]; + Q = buffer_Q[(j + MAX_DISPLAY_SPECTRUM / 2) % MAX_DISPLAY_SPECTRUM]; + v = sqrt(I*I + Q*Q); + v = log10(v) * 20 + db; + if (v < 0) + v = 0; + v /= db; + /* scale down result horizontally by combining values */ + if (v > c) + c = v; + buffer_max[j] -= DISPLAY_INTERVAL / 10.0; + if (c > buffer_max[j]) + buffer_max[j] = c; + count += width; + while (count >= MAX_DISPLAY_SPECTRUM) { + if (k >= width) { + fprintf(stderr, "Too many output values, please fix!\n"); + abort(); + } + /* heigh is the maximum value */ + heigh[k] = (double)(HEIGHT * 2 - 1) * (1.0 - buffer_max[j]); + if (heigh[k] < 0) + heigh[k] = 0; + if (heigh[k] >= (HEIGHT * 2)) + heigh[k] = (HEIGHT * 2) - 1; + /* low is the current value */ + low[k] = (double)(HEIGHT * 2 - 1) * (1.0 - c); + if (low[k] < 0) + low[k] = 0; + if (low[k] >= (HEIGHT * 2)) + low[k] = (HEIGHT * 2) - 1; + k++; + c = 0.0; + count -= MAX_DISPLAY_SPECTRUM; + } + } + if (k != width) { + fprintf(stderr, "k must be %d but is %d, please fix!\n", width, k); + abort(); + } + /* plot scaled buffer */ + memset(&screen, ' ', sizeof(screen)); + memset(&screen_color, 7, sizeof(screen_color)); /* all white */ + sprintf(screen[0], "(spectrum log %.0f dB", db); + *strchr(screen[0], '\0') = ')'; + for (j = 0; j < width; j++) { + s = e = low[j]; + if (j > 0 && low[j - 1] > e) + e = low[j - 1] - 1; + if (j < width - 1 && low[j + 1] > e) + e = low[j + 1] - 1; + if (s == e) { + if ((s & 1) == 0) + screen[s >> 1][j] = '\''; + else + screen[s >> 1][j] = '.'; + screen_color[s >> 1][j] = 3; + } else { + if ((s & 1) == 0) + screen[s >> 1][j] = ':'; + else + screen[s >> 1][j] = '.'; + screen_color[s >> 1][j] = 3; + if ((e & 1) == 0) + screen[e >> 1][j] = '\''; + else + screen[e >> 1][j] = ':'; + screen_color[e >> 1][j] = 3; + for (k = (s >> 1) + 1; k < (e >> 1); k++) { + screen[k][j] = ':'; + screen_color[k][j] = 3; + } + } + s = heigh[j]; + e = low[j]; + if ((s >> 1) < (e >> 1)) { + if ((s & 1) == 0) + screen[s >> 1][j] = ':'; + else + screen[s >> 1][j] = '.'; + screen_color[s >> 1][j] = 4; + for (k = (s >> 1) + 1; k < (e >> 1); k++) { + screen[k][j] = ':'; + screen_color[k][j] = 4; + } + } + } + printf("\0337\033[H"); + for (j = 0; j < HEIGHT; j++) { + for (k = 0; k < width; k++) { + if (screen_color[j][k] != color) { + color = screen_color[j][k]; + printf("\033[1;3%dm", color); + } + putchar(screen[j][k]); + } + printf("\n"); + } + /* reset color and position */ + printf("\033[0;39m\0338"); fflush(stdout); + } + } + + disp.interval_pos = pos; +} + + diff --git a/src/common/fft.c b/src/common/fft.c new file mode 100644 index 0000000..fb819e1 --- /dev/null +++ b/src/common/fft.c @@ -0,0 +1,96 @@ +/* Fast Fourier Transformation (FFT) + * + * (C) 2016 by Andreas Eversberg + * All Rights Reserved + * + * 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 3 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, see . + */ + +#include +#include +#include "fft.h" + +/* + * Code based closely to work by Paul Bourke + * + * This computes an in-place complex-to-complex FFT + * x and y are the real and imaginary arrays of 2^m points. + * dir = 1 gives forward transform + * dir = -1 gives reverse transform + */ +void fft_process(int dir, int m, double *x, double *y) +{ + int n, i, i1, j, k, i2, l, l1, l2; + double c1, c2, tx, ty, t1, t2, u1, u2, z; + + /* Calculate the number of points */ + n = 1 << m; + + /* Do the bit reversal */ + i2 = n >> 1; + j = 0; + for (i = 0; i < n - 1; i++) { + if (i < j) { + tx = x[i]; + ty = y[i]; + x[i] = x[j]; + y[i] = y[j]; + x[j] = tx; + y[j] = ty; + } + k = i2; + while (k <= j) { + j -= k; + k >>= 1; + } + j += k; + } + + /* Compute the FFT */ + c1 = -1.0; + c2 = 0.0; + l2 = 1; + for (l = 0; l < m; l++) { + l1 = l2; + l2 <<= 1; + u1 = 1.0; + u2 = 0.0; + for (j = 0; j < l1; j++) { + for (i = j; i < n; i += l2) { + i1 = i + l1; + t1 = u1 * x[i1] - u2 * y[i1]; + t2 = u1 * y[i1] + u2 * x[i1]; + x[i1] = x[i] - t1; + y[i1] = y[i] - t2; + x[i] += t1; + y[i] += t2; + } + z = u1 * c1 - u2 * c2; + u2 = u1 * c2 + u2 * c1; + u1 = z; + } + c2 = sqrt((1.0 - c1) / 2.0); + if (dir == 1) + c2 = -c2; + c1 = sqrt((1.0 + c1) / 2.0); + } + + /* Scaling for forward transform */ + if (dir == 1) { + for (i = 0; i < n; i++) { + x[i] /= n; + y[i] /= n; + } + } +} diff --git a/src/common/fft.h b/src/common/fft.h new file mode 100644 index 0000000..8387a8e --- /dev/null +++ b/src/common/fft.h @@ -0,0 +1,3 @@ + +void fft_process(int dir, int m, double *x, double *y); + diff --git a/src/common/main_common.c b/src/common/main_common.c index bf6c46d..13fb5d6 100644 --- a/src/common/main_common.c +++ b/src/common/main_common.c @@ -501,14 +501,26 @@ next_char: goto next_char; case 'w': /* toggle display */ +#ifdef HAVE_SDR display_iq_on(0); + display_spectrum_on(0); +#endif display_wave_on(-1); goto next_char; +#ifdef HAVE_SDR case 'q': /* toggle display */ display_wave_on(0); + display_spectrum_on(0); display_iq_on(-1); goto next_char; + case 's': + /* toggle spectrum */ + display_wave_on(0); + display_iq_on(0); + display_spectrum_on(-1); + goto next_char; +#endif case 'i': /* dump info */ dump_info(); diff --git a/src/common/sdr.c b/src/common/sdr.c index dd956b0..a0c0880 100644 --- a/src/common/sdr.c +++ b/src/common/sdr.c @@ -92,6 +92,7 @@ void *sdr_open(const char __attribute__((__unused__)) *audiodev, double *tx_freq int c; display_iq_init(samplerate); + display_spectrum_init(samplerate); bandwidth = 2.0 * (max_deviation + max_modulation); PDEBUG(DSDR, DEBUG_INFO, "Using Bandwidth of 2 * (%.1f + %.1f) = %.1f\n", max_deviation / 1000, max_modulation / 1000, bandwidth / 1000); @@ -345,6 +346,7 @@ int sdr_read(void *inst, sample_t **samples, int num, int channels) } } display_iq(buff, count); + display_spectrum(buff, count); for (c = 0; c < channels; c++) { rot = sdr->chan[c].rx_rot; -- cgit v1.2.3