1 /*---------------------------------------------------------------------------*\
2
3 FILE........: modem_stats.c
4 AUTHOR......: David Rowe
5 DATE CREATED: June 2015
6
7 Common functions for returning demod stats from fdmdv and cohpsk modems.
8
9 \*---------------------------------------------------------------------------*/
10
11 /*
12 Copyright (C) 2015 David Rowe
13
14 All rights reserved.
15
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU Lesser General Public License version 2.1, as
18 published by the Free Software Foundation. This program is
19 distributed in the hope that it will be useful, but WITHOUT ANY
20 WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
22 License for more details.
23
24 You should have received a copy of the GNU Lesser General Public License
25 along with this program; if not, see <http://www.gnu.org/licenses/>.
26 */
27
28 #include <assert.h>
29 #include <math.h>
30 #include "modem_stats.h"
31 #include "codec2_fdmdv.h"
32 #include "kiss_fft.h"
33
modem_stats_open(struct MODEM_STATS * f)34 void modem_stats_open(struct MODEM_STATS *f)
35 {
36 int i;
37
38 /* zero out all the stats */
39
40 memset(f, 0, sizeof(struct MODEM_STATS));
41
42 /* init the FFT */
43
44 #ifndef __EMBEDDED__
45 for(i=0; i<2*MODEM_STATS_NSPEC; i++)
46 f->fft_buf[i] = 0.0;
47 f->fft_cfg = (void*)kiss_fft_alloc (2*MODEM_STATS_NSPEC, 0, NULL, NULL);
48 assert(f->fft_cfg != NULL);
49 #endif
50 }
51
modem_stats_close(struct MODEM_STATS * f)52 void modem_stats_close(struct MODEM_STATS *f)
53 {
54 #ifndef __EMBEDDED__
55 KISS_FFT_FREE(f->fft_cfg);
56 #endif
57 }
58
59 /*---------------------------------------------------------------------------*\
60
61 FUNCTION....: modem_stats_get_rx_spectrum()
62 AUTHOR......: David Rowe
63 DATE CREATED: 9 June 2012
64
65 Returns the MODEM_STATS_NSPEC point magnitude spectrum of the rx signal in
66 dB. The spectral samples are scaled so that 0dB is the peak, a good
67 range for plotting is 0 to -40dB.
68
69 Note only the real part of the complex input signal is used at
70 present. A complex variable is used for input for compatability
71 with the other rx signal procesing.
72
73 Successive calls can be used to build up a waterfall or spectrogram
74 plot, by mapping the received levels to colours.
75
76 The time-frequency resolution of the spectrum can be adjusted by varying
77 MODEM_STATS_NSPEC. Note that a 2* MODEM_STATS_NSPEC size FFT is reqd to get
78 MODEM_STATS_NSPEC output points. MODEM_STATS_NSPEC must be a power of 2.
79
80 See octave/tget_spec.m for a demo real time spectral display using
81 Octave. This demo averages the output over time to get a smoother
82 display:
83
84 av = 0.9*av + 0.1*mag_dB
85
86 \*---------------------------------------------------------------------------*/
87
88 #ifndef __EMBEDDED__
modem_stats_get_rx_spectrum(struct MODEM_STATS * f,float mag_spec_dB[],COMP rx_fdm[],int nin)89 void modem_stats_get_rx_spectrum(struct MODEM_STATS *f, float mag_spec_dB[], COMP rx_fdm[], int nin)
90 {
91 int i,j;
92 COMP fft_in[2*MODEM_STATS_NSPEC];
93 COMP fft_out[2*MODEM_STATS_NSPEC];
94 float full_scale_dB;
95
96 /* update buffer of input samples */
97
98 for(i=0; i<2*MODEM_STATS_NSPEC-nin; i++)
99 f->fft_buf[i] = f->fft_buf[i+nin];
100 for(j=0; j<nin; j++,i++)
101 f->fft_buf[i] = rx_fdm[j].real;
102 assert(i == 2*MODEM_STATS_NSPEC);
103
104 /* window and FFT */
105
106 for(i=0; i<2*MODEM_STATS_NSPEC; i++) {
107 fft_in[i].real = f->fft_buf[i] * (0.5 - 0.5*cosf((float)i*2.0*M_PI/(2*MODEM_STATS_NSPEC)));
108 fft_in[i].imag = 0.0;
109 }
110
111 kiss_fft((kiss_fft_cfg)f->fft_cfg, (kiss_fft_cpx *)fft_in, (kiss_fft_cpx *)fft_out);
112
113 /* FFT scales up a signal of level 1 FDMDV_NSPEC */
114
115 full_scale_dB = 20*log10(MODEM_STATS_NSPEC*FDMDV_SCALE);
116
117 /* scale and convert to dB */
118
119 for(i=0; i<MODEM_STATS_NSPEC; i++) {
120 mag_spec_dB[i] = 10.0*log10f(fft_out[i].real*fft_out[i].real + fft_out[i].imag*fft_out[i].imag + 1E-12);
121 mag_spec_dB[i] -= full_scale_dB;
122 }
123 }
124 #endif
125