1 /*
2 * SpanDSP - a series of DSP components for telephony
3 *
4 * modem_monitor.cpp - Display QAM constellations, using the FLTK toolkit.
5 *
6 * Written by Steve Underwood <steveu@coppice.org>
7 *
8 * Copyright (C) 2004 Steve Underwood
9 *
10 * All rights reserved.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2, as
14 * published by the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26 #if defined(HAVE_CONFIG_H)
27 #include "config.h"
28 #endif
29
30 #if defined(HAVE_FL_FL_H) && defined(HAVE_FL_FL_CARTESIAN_H)
31
32 #define __STDC_LIMIT_MACROS
33
34 #include <inttypes.h>
35 #include <stdio.h>
36 #include <math.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <sys/select.h>
41
42 #include <FL/Fl.H>
43 #include <FL/Fl_Overlay_Window.H>
44 #include <FL/Fl_Light_Button.H>
45 #include <FL/Fl_Cartesian.H>
46 #include <FL/Fl_Audio_Meter.H>
47 #include <FL/Fl_Output.H>
48 #include <FL/fl_draw.H>
49
50 #include "spandsp.h"
51
52 #define SYMBOL_TRACKER_POINTS 12000
53 #define CARRIER_TRACKER_POINTS 12000
54
55 struct qam_monitor_s
56 {
57 float constel_scaling;
58
59 Fl_Double_Window *w;
60 Fl_Group *c_const;
61 Fl_Group *c_right;
62 Fl_Group *c_eq;
63 Fl_Group *c_symbol_track;
64
65 /* Constellation stuff */
66 Ca_Canvas *canvas_const;
67 Ca_X_Axis *sig_i;
68 Ca_Y_Axis *sig_q;
69
70 Ca_Point *constel_point[100000];
71 int constel_window;
72 int next_constel_point;
73 int skip;
74
75 /* Equalizer stuff */
76 Ca_Canvas *canvas_eq;
77
78 Ca_X_Axis *eq_x;
79 Ca_Y_Axis *eq_y;
80
81 Ca_Line *eq_re;
82 Ca_Line *eq_im;
83
84 double eq_re_plot[200];
85 double eq_im_plot[200];
86
87 /* Carrier and symbol tracking stuff */
88 Ca_Canvas *canvas_track;
89
90 Ca_X_Axis *track_x;
91 Ca_Y_Axis *symbol_track_y;
92 Ca_Y_Axis *carrier_y;
93
94 Ca_Line *symbol_track;
95 Ca_Line *carrier;
96
97 float symbol_tracker[SYMBOL_TRACKER_POINTS];
98 double symbol_track_plot[SYMBOL_TRACKER_POINTS*2];
99 int symbol_track_points;
100 int symbol_track_ptr;
101 int symbol_track_window;
102
103 float carrier_tracker[CARRIER_TRACKER_POINTS];
104 double carrier_plot[CARRIER_TRACKER_POINTS*2];
105 int carrier_points;
106 int carrier_ptr;
107 int carrier_window;
108
109 /* Audio meter stuff */
110 Fl_Audio_Meter *audio_meter;
111 Fl_Output *audio_level;
112 int32_t power_reading;
113 };
114
115 #include "modem_monitor.h"
116
qam_monitor_clear_constel(qam_monitor_t * s)117 int qam_monitor_clear_constel(qam_monitor_t *s)
118 {
119 int i;
120
121 s->canvas_const->current(s->canvas_const);
122 for (i = 0; i < s->constel_window; i++)
123 {
124 if (s->constel_point[i])
125 {
126 delete s->constel_point[i];
127 s->constel_point[i] = NULL;
128 }
129 }
130 s->next_constel_point = 0;
131 Fl::check();
132 return 0;
133 }
134 /*- End of function --------------------------------------------------------*/
135
qam_monitor_update_constel(qam_monitor_t * s,const complexf_t * pt)136 int qam_monitor_update_constel(qam_monitor_t *s, const complexf_t *pt)
137 {
138 int i;
139
140 s->canvas_const->current(s->canvas_const);
141 if (s->constel_point[s->next_constel_point])
142 delete s->constel_point[s->next_constel_point];
143 s->constel_point[s->next_constel_point++] = new Ca_Point(pt->re, pt->im, FL_BLACK);
144 if (s->next_constel_point >= s->constel_window)
145 s->next_constel_point = 0;
146 if (++s->skip >= 100)
147 {
148 s->skip = 0;
149 Fl::check();
150 }
151 return 0;
152 }
153 /*- End of function --------------------------------------------------------*/
154
qam_monitor_update_equalizer(qam_monitor_t * s,const complexf_t * coeffs,int len)155 int qam_monitor_update_equalizer(qam_monitor_t *s, const complexf_t *coeffs, int len)
156 {
157 int i;
158 float min;
159 float max;
160
161 /* Protect against screwy values */
162 for (i = 0; i < len; i++)
163 {
164 if (isnan(coeffs[i].re) || isinf(coeffs[i].re))
165 break;
166 if (isnan(coeffs[i].im) || isinf(coeffs[i].im))
167 break;
168 if (coeffs[i].re < -20.0f || coeffs[i].re > 20.0f)
169 break;
170 if (coeffs[i].im < -20.0f || coeffs[i].im > 20.0f)
171 break;
172 }
173 if (i != len)
174 return -1;
175
176 if (s->eq_re)
177 delete s->eq_re;
178 if (s->eq_im)
179 delete s->eq_im;
180
181 s->canvas_eq->current(s->canvas_eq);
182 i = 0;
183 min = coeffs[i].re;
184 if (min > coeffs[i].im)
185 min = coeffs[i].im;
186 max = coeffs[i].re;
187 if (max < coeffs[i].im)
188 max = coeffs[i].im;
189 for (i = 0; i < len; i++)
190 {
191 s->eq_re_plot[2*i] = (i - len/2)/2.0;
192 s->eq_re_plot[2*i + 1] = coeffs[i].re*s->constel_scaling;
193 if (min > coeffs[i].re)
194 min = coeffs[i].re;
195 if (max < coeffs[i].re)
196 max = coeffs[i].re;
197
198 s->eq_im_plot[2*i] = (i - len/2)/2.0;
199 s->eq_im_plot[2*i + 1] = coeffs[i].im*s->constel_scaling;
200 if (min > coeffs[i].im)
201 min = coeffs[i].im;
202 if (max < coeffs[i].im)
203 max = coeffs[i].im;
204 }
205
206 s->eq_x->minimum(-len/4.0);
207 s->eq_x->maximum(len/4.0);
208 s->eq_y->maximum((max == min) ? max + 0.2 : max);
209 s->eq_y->minimum(min);
210 s->eq_re = new Ca_Line(len, s->eq_re_plot, 0, 0, FL_BLUE, CA_NO_POINT);
211 s->eq_im = new Ca_Line(len, s->eq_im_plot, 0, 0, FL_RED, CA_NO_POINT);
212 Fl::check();
213 return 0;
214 }
215 /*- End of function --------------------------------------------------------*/
216
qam_monitor_update_int_equalizer(qam_monitor_t * s,const complexi16_t * coeffs,int len)217 int qam_monitor_update_int_equalizer(qam_monitor_t *s, const complexi16_t *coeffs, int len)
218 {
219 int i;
220 float min;
221 float max;
222
223 if (s->eq_re)
224 delete s->eq_re;
225 if (s->eq_im)
226 delete s->eq_im;
227
228 s->canvas_eq->current(s->canvas_eq);
229 i = 0;
230 min = coeffs[i].re;
231 if (min > coeffs[i].im)
232 min = coeffs[i].im;
233 max = coeffs[i].re;
234 if (max < coeffs[i].im)
235 max = coeffs[i].im;
236 for (i = 0; i < len; i++)
237 {
238 if (min > coeffs[i].re)
239 min = coeffs[i].re;
240 if (max < coeffs[i].re)
241 max = coeffs[i].re;
242 s->eq_re_plot[2*i] = (i - len/2)/2.0f;
243 s->eq_re_plot[2*i + 1] = coeffs[i].re*s->constel_scaling;
244
245 if (min > coeffs[i].im)
246 min = coeffs[i].im;
247 if (max < coeffs[i].im)
248 max = coeffs[i].im;
249 s->eq_im_plot[2*i] = (i - len/2)/2.0f;
250 s->eq_im_plot[2*i + 1] = coeffs[i].im*s->constel_scaling;
251 }
252 min *= s->constel_scaling;
253 max *= s->constel_scaling;
254
255 s->eq_x->minimum(-len/4.0);
256 s->eq_x->maximum(len/4.0);
257 s->eq_y->maximum((max == min) ? max + 0.2 : max);
258 s->eq_y->minimum(min);
259 s->eq_re = new Ca_Line(len, s->eq_re_plot, 0, 0, FL_BLUE, CA_NO_POINT);
260 s->eq_im = new Ca_Line(len, s->eq_im_plot, 0, 0, FL_RED, CA_NO_POINT);
261 Fl::check();
262 return 0;
263 }
264 /*- End of function --------------------------------------------------------*/
265
qam_monitor_update_symbol_tracking(qam_monitor_t * s,float total_correction)266 int qam_monitor_update_symbol_tracking(qam_monitor_t *s, float total_correction)
267 {
268 int i;
269 int j;
270 float min;
271 float max;
272
273 s->symbol_tracker[s->symbol_track_ptr++] = total_correction;
274 if (s->symbol_track_points < SYMBOL_TRACKER_POINTS)
275 s->symbol_track_points++;
276 if (s->symbol_track_ptr >= SYMBOL_TRACKER_POINTS)
277 s->symbol_track_ptr = 0;
278
279 s->canvas_track->current(s->canvas_track);
280 if (s->symbol_track)
281 delete s->symbol_track;
282 s->track_x->current();
283 s->symbol_track_y->current();
284
285 min =
286 max = s->symbol_tracker[0];
287 for (i = s->symbol_track_ptr, j = 0; i < s->symbol_track_points; i++, j++)
288 {
289 if (min > s->symbol_tracker[i])
290 min = s->symbol_tracker[i];
291 if (max < s->symbol_tracker[i])
292 max = s->symbol_tracker[i];
293 s->symbol_track_plot[2*j] = j;
294 s->symbol_track_plot[2*j + 1] = s->symbol_tracker[i];
295 }
296 for (i = 0; i < s->symbol_track_ptr; i++, j++)
297 {
298 if (min > s->symbol_tracker[i])
299 min = s->symbol_tracker[i];
300 if (max < s->symbol_tracker[i])
301 max = s->symbol_tracker[i];
302 s->symbol_track_plot[2*j] = j;
303 s->symbol_track_plot[2*j + 1] = s->symbol_tracker[i];
304 }
305 s->symbol_track_y->maximum((fabs(max - min) < 0.05) ? max + 0.05 : max);
306 s->symbol_track_y->minimum(min);
307
308 s->symbol_track = new Ca_Line(s->symbol_track_points, s->symbol_track_plot, 0, 0, FL_RED, CA_NO_POINT);
309 //Fl::check();
310 return 0;
311 }
312 /*- End of function --------------------------------------------------------*/
313
qam_monitor_update_audio_level(qam_monitor_t * s,const int16_t amp[],int len)314 int qam_monitor_update_audio_level(qam_monitor_t *s, const int16_t amp[], int len)
315 {
316 int i;
317 char buf[11];
318 double val;
319
320 for (i = 0; i < len; i++)
321 {
322 s->audio_meter->sample(amp[i]/32768.0);
323 s->power_reading += ((amp[i]*amp[i] - s->power_reading) >> 10);
324 }
325 val = 10.0*log10((double) s->power_reading/(32767.0*32767.0) + 1.0e-10) + 3.14 + 3.02;
326
327 snprintf(buf, sizeof(buf), "%5.1fdBm0", val);
328 s->audio_level->value(buf);
329 return 0;
330 }
331 /*- End of function --------------------------------------------------------*/
332
qam_monitor_update_carrier_tracking(qam_monitor_t * s,float carrier_freq)333 int qam_monitor_update_carrier_tracking(qam_monitor_t *s, float carrier_freq)
334 {
335 int i;
336 int j;
337 float min;
338 float max;
339
340 s->carrier_tracker[s->carrier_ptr++] = carrier_freq;
341 if (s->carrier_points < CARRIER_TRACKER_POINTS)
342 s->carrier_points++;
343 if (s->carrier_ptr >= CARRIER_TRACKER_POINTS)
344 s->carrier_ptr = 0;
345
346 s->canvas_track->current(s->canvas_track);
347 if (s->carrier)
348 delete s->carrier;
349 s->track_x->current();
350 s->carrier_y->current();
351
352 min =
353 max = s->carrier_tracker[0];
354 for (i = s->carrier_ptr, j = 0; i < s->carrier_points; i++, j++)
355 {
356 s->carrier_plot[2*j] = j;
357 s->carrier_plot[2*j + 1] = s->carrier_tracker[i];
358 if (min > s->carrier_tracker[i])
359 min = s->carrier_tracker[i];
360 if (max < s->carrier_tracker[i])
361 max = s->carrier_tracker[i];
362 }
363 for (i = 0; i < s->carrier_ptr; i++, j++)
364 {
365 s->carrier_plot[2*j] = j;
366 s->carrier_plot[2*j + 1] = s->carrier_tracker[i];
367 if (min > s->carrier_tracker[i])
368 min = s->carrier_tracker[i];
369 if (max < s->carrier_tracker[i])
370 max = s->carrier_tracker[i];
371 }
372
373 s->carrier_y->maximum((fabs(max - min) < 0.05) ? max + 0.05 : max);
374 s->carrier_y->minimum(min);
375
376 s->carrier = new Ca_Line(s->carrier_points, s->carrier_plot, 0, 0, FL_BLUE, CA_NO_POINT);
377 //Fl::check();
378 return 0;
379 }
380 /*- End of function --------------------------------------------------------*/
381
qam_monitor_init(float constel_width,float constel_scaling,const char * tag)382 qam_monitor_t *qam_monitor_init(float constel_width, float constel_scaling, const char *tag)
383 {
384 char buf[132 + 1];
385 float x;
386 float y;
387 qam_monitor_t *s;
388
389 if ((s = (qam_monitor_t *) malloc(sizeof(*s))) == NULL)
390 return NULL;
391
392 s->w = new Fl_Double_Window(905, 400, (tag) ? tag : "QAM monitor");
393
394 s->constel_scaling = 1.0/constel_scaling;
395
396 s->c_const = new Fl_Group(0, 0, 380, 400);
397 s->c_const->box(FL_DOWN_BOX);
398 s->c_const->align(FL_ALIGN_TOP | FL_ALIGN_INSIDE);
399
400 s->canvas_const = new Ca_Canvas(60, 30, 300, 300, "Constellation");
401 s->canvas_const->box(FL_PLASTIC_DOWN_BOX);
402 s->canvas_const->color(7);
403 s->canvas_const->align(FL_ALIGN_TOP);
404 s->canvas_const->border(15);
405
406 s->sig_i = new Ca_X_Axis(65, 330, 290, 30, "I");
407 s->sig_i->align(FL_ALIGN_BOTTOM);
408 s->sig_i->minimum(-constel_width);
409 s->sig_i->maximum(constel_width);
410 s->sig_i->label_format("%g");
411 s->sig_i->minor_grid_color(fl_gray_ramp(20));
412 s->sig_i->major_grid_color(fl_gray_ramp(15));
413 s->sig_i->label_grid_color(fl_gray_ramp(10));
414 s->sig_i->grid_visible(CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
415 s->sig_i->minor_grid_style(FL_DOT);
416 s->sig_i->major_step(5);
417 s->sig_i->label_step(1);
418 s->sig_i->axis_color(FL_BLACK);
419 s->sig_i->axis_align(CA_BOTTOM | CA_LINE);
420
421 s->sig_q = new Ca_Y_Axis(20, 35, 40, 290, "Q");
422 s->sig_q->align(FL_ALIGN_LEFT);
423 s->sig_q->minimum(-constel_width);
424 s->sig_q->maximum(constel_width);
425 s->sig_q->minor_grid_color(fl_gray_ramp(20));
426 s->sig_q->major_grid_color(fl_gray_ramp(15));
427 s->sig_q->label_grid_color(fl_gray_ramp(10));
428 //s->sig_q->grid_visible(CA_MINOR_TICK | CA_MAJOR_TICK | CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
429 s->sig_q->grid_visible(CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
430 s->sig_q->minor_grid_style(FL_DOT);
431 s->sig_q->major_step(5);
432 s->sig_q->label_step(1);
433 s->sig_q->axis_color(FL_BLACK);
434
435 s->sig_q->current();
436
437 s->c_const->end();
438
439 s->c_right = new Fl_Group(440, 0, 465, 405);
440
441 s->c_eq = new Fl_Group(380, 0, 265, 200);
442 s->c_eq->box(FL_DOWN_BOX);
443 s->c_eq->align(FL_ALIGN_TOP | FL_ALIGN_INSIDE);
444 s->c_eq->current();
445 s->canvas_eq = new Ca_Canvas(460, 35, 150, 100, "Equalizer");
446 s->canvas_eq->box(FL_PLASTIC_DOWN_BOX);
447 s->canvas_eq->color(7);
448 s->canvas_eq->align(FL_ALIGN_TOP);
449 Fl_Group::current()->resizable(s->canvas_eq);
450 s->canvas_eq->border(15);
451
452 s->eq_x = new Ca_X_Axis(465, 135, 140, 30, "Symbol");
453 s->eq_x->align(FL_ALIGN_BOTTOM);
454 s->eq_x->minimum(-8.0);
455 s->eq_x->maximum(8.0);
456 s->eq_x->label_format("%g");
457 s->eq_x->minor_grid_color(fl_gray_ramp(20));
458 s->eq_x->major_grid_color(fl_gray_ramp(15));
459 s->eq_x->label_grid_color(fl_gray_ramp(10));
460 s->eq_x->grid_visible(CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
461 s->eq_x->minor_grid_style(FL_DOT);
462 s->eq_x->major_step(5);
463 s->eq_x->label_step(1);
464 s->eq_x->axis_align(CA_BOTTOM | CA_LINE);
465 s->eq_x->axis_color(FL_BLACK);
466 s->eq_x->current();
467
468 s->eq_y = new Ca_Y_Axis(420, 40, 40, 90, "Amp");
469 s->eq_y->align(FL_ALIGN_LEFT);
470 s->eq_y->minimum(-0.1);
471 s->eq_y->maximum(0.1);
472 s->eq_y->minor_grid_color(fl_gray_ramp(20));
473 s->eq_y->major_grid_color(fl_gray_ramp(15));
474 s->eq_y->label_grid_color(fl_gray_ramp(10));
475 s->eq_y->grid_visible(CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
476 s->eq_y->minor_grid_style(FL_DOT);
477 s->eq_y->major_step(5);
478 s->eq_y->label_step(1);
479 s->eq_y->axis_color(FL_BLACK);
480 s->eq_y->current();
481
482 s->c_eq->end();
483
484 s->c_symbol_track = new Fl_Group(380, 200, 525, 200);
485 s->c_symbol_track->box(FL_DOWN_BOX);
486 s->c_symbol_track->align(FL_ALIGN_TOP | FL_ALIGN_INSIDE);
487 s->c_symbol_track->current();
488
489 s->canvas_track = new Ca_Canvas(490, 235, 300, 100, "Symbol and carrier tracking");
490 s->canvas_track->box(FL_PLASTIC_DOWN_BOX);
491 s->canvas_track->color(7);
492 s->canvas_track->align(FL_ALIGN_TOP);
493 Fl_Group::current()->resizable(s->canvas_track);
494 s->canvas_track->border(15);
495
496 s->track_x = new Ca_X_Axis(495, 335, 290, 30, "Time (symbols)");
497 s->track_x->align(FL_ALIGN_BOTTOM);
498 s->track_x->minimum(0.0);
499 s->track_x->maximum(2400.0*5.0);
500 s->track_x->label_format("%g");
501 s->track_x->minor_grid_color(fl_gray_ramp(20));
502 s->track_x->major_grid_color(fl_gray_ramp(15));
503 s->track_x->label_grid_color(fl_gray_ramp(10));
504 s->track_x->grid_visible(CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
505 s->track_x->minor_grid_style(FL_DOT);
506 s->track_x->major_step(5);
507 s->track_x->label_step(1);
508 s->track_x->axis_align(CA_BOTTOM | CA_LINE);
509 s->track_x->axis_color(FL_BLACK);
510 s->track_x->current();
511
512 s->symbol_track_y = new Ca_Y_Axis(420, 240, 70, 90, "Cor");
513 s->symbol_track_y->align(FL_ALIGN_LEFT);
514 s->symbol_track_y->minimum(-0.1);
515 s->symbol_track_y->maximum(0.1);
516 s->symbol_track_y->minor_grid_color(fl_gray_ramp(20));
517 s->symbol_track_y->major_grid_color(fl_gray_ramp(15));
518 s->symbol_track_y->label_grid_color(fl_gray_ramp(10));
519 s->symbol_track_y->grid_visible(CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
520 s->symbol_track_y->minor_grid_style(FL_DOT);
521 s->symbol_track_y->major_step(5);
522 s->symbol_track_y->label_step(1);
523 s->symbol_track_y->axis_color(FL_RED);
524 s->symbol_track_y->current();
525
526 s->carrier_y = new Ca_Y_Axis(790, 240, 70, 90, "Freq");
527 s->carrier_y->align(FL_ALIGN_RIGHT);
528 s->carrier_y->minimum(-0.1);
529 s->carrier_y->maximum(0.1);
530 s->carrier_y->minor_grid_color(fl_gray_ramp(20));
531 s->carrier_y->major_grid_color(fl_gray_ramp(15));
532 s->carrier_y->label_grid_color(fl_gray_ramp(10));
533 s->carrier_y->grid_visible(CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
534 s->carrier_y->minor_grid_style(FL_DOT);
535 s->carrier_y->major_step(5);
536 s->carrier_y->label_step(1);
537 s->carrier_y->axis_align(CA_RIGHT);
538 s->carrier_y->axis_color(FL_BLUE);
539 s->carrier_y->current();
540
541 s->c_symbol_track->end();
542
543 s->audio_meter = new Fl_Audio_Meter(672, 10, 16, 150, "");
544 s->audio_meter->box(FL_PLASTIC_UP_BOX);
545 s->audio_meter->type(FL_VERT_AUDIO_METER);
546
547 s->audio_level = new Fl_Output(650, 170, 60, 20, "");
548 s->audio_level->textsize(10);
549
550 s->c_right->end();
551
552 Fl_Group::current()->resizable(s->c_right);
553 s->w->end();
554 s->w->show();
555
556 s->next_constel_point = 0;
557 s->constel_window = 10000;
558
559 s->carrier_points = 0;
560 s->carrier_ptr = 0;
561 s->carrier_window = 100;
562
563 s->symbol_track_points = 0;
564 s->symbol_track_window = 10000;
565 Fl::check();
566 return s;
567 }
568 /*- End of function --------------------------------------------------------*/
569
qam_wait_to_end(qam_monitor_t * s)570 void qam_wait_to_end(qam_monitor_t *s)
571 {
572 fd_set rfds;
573 int res;
574 struct timeval tv;
575
576 fprintf(stderr, "Processing complete. Press the <enter> key to end\n");
577 do
578 {
579 usleep(100000);
580 Fl::check();
581 FD_ZERO(&rfds);
582 FD_SET(0, &rfds);
583 tv.tv_usec = 100000;
584 tv.tv_sec = 0;
585 res = select(1, &rfds, NULL, NULL, &tv);
586 }
587 while (res <= 0);
588 }
589 /*- End of function --------------------------------------------------------*/
590 #endif
591 /*- End of file ------------------------------------------------------------*/
592