comparison spandsp-0.0.3/spandsp-0.0.3/tests/modem_monitor.cpp @ 5:f762bf195c4b

import spandsp-0.0.3
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Fri, 25 Jun 2010 16:00:21 +0200
parents
children
comparison
equal deleted inserted replaced
4:26cd8f1ef0b1 5:f762bf195c4b
1 /*
2 * SpanDSP - a series of DSP components for telephony
3 *
4 * constel.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 * $Id: modem_monitor.cpp,v 1.11 2006/10/24 13:45:29 steveu Exp $
26 */
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #if defined(HAVE_FL_FL_H) && defined(HAVE_FL_FL_CARTESIAN_H)
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 "../src/spandsp/complex.h"
51
52 #define SYMBOL_TRACKER_POINTS 12000
53 #define CARRIER_TRACKER_POINTS 12000
54
55 struct qam_monitor_s
56 {
57 Fl_Double_Window *w;
58 Fl_Group *c_const;
59 Fl_Group *c_right;
60 Fl_Group *c_eq;
61 Fl_Group *c_symbol_track;
62
63 /* Constellation stuff */
64 Ca_Canvas *canvas_const;
65 Ca_X_Axis *sig_i;
66 Ca_Y_Axis *sig_q;
67
68 Ca_Point *constel_point[100000];
69 int constel_window;
70 int next_constel_point;
71 int skip;
72
73 /* Equalizer stuff */
74 Ca_Canvas *canvas_eq;
75
76 Ca_X_Axis *eq_x;
77 Ca_Y_Axis *eq_y;
78
79 Ca_Line *eq_re;
80 Ca_Line *eq_im;
81
82 double eq_re_plot[200];
83 double eq_im_plot[200];
84
85 /* Carrier and symbol tracking stuff */
86 Ca_Canvas *canvas_track;
87
88 Ca_X_Axis *track_x;
89 Ca_Y_Axis *symbol_track_y;
90 Ca_Y_Axis *carrier_y;
91
92 Ca_Line *symbol_track;
93 Ca_Line *carrier;
94
95 float symbol_tracker[SYMBOL_TRACKER_POINTS];
96 double symbol_track_plot[SYMBOL_TRACKER_POINTS*2];
97 int symbol_track_points;
98 int symbol_track_ptr;
99 int symbol_track_window;
100
101 float carrier_tracker[CARRIER_TRACKER_POINTS];
102 double carrier_plot[CARRIER_TRACKER_POINTS*2];
103 int carrier_points;
104 int carrier_ptr;
105 int carrier_window;
106
107 /* Audio meter stuff */
108 Fl_Audio_Meter *audio_meter;
109 Fl_Output *audio_level;
110 int32_t power_reading;
111 };
112
113 #include "modem_monitor.h"
114
115 int qam_monitor_update_constel(qam_monitor_t *s, const complexf_t *pt)
116 {
117 int i;
118
119 s->canvas_const->current(s->canvas_const);
120 if (s->constel_point[s->next_constel_point])
121 delete s->constel_point[s->next_constel_point];
122 s->constel_point[s->next_constel_point++] = new Ca_Point(pt->re, pt->im, FL_BLACK);
123 if (s->next_constel_point >= s->constel_window)
124 s->next_constel_point = 0;
125 if (++s->skip >= 100)
126 {
127 s->skip = 0;
128 Fl::check();
129 }
130 return 0;
131 }
132 /*- End of function --------------------------------------------------------*/
133
134 int qam_monitor_update_equalizer(qam_monitor_t *s, const complexf_t *coeffs, int len)
135 {
136 int i;
137 float min;
138 float max;
139
140 /* Protect against screwy values */
141 for (i = 0; i < len; i++)
142 {
143 if (isnan(coeffs[i].re) || isinf(coeffs[i].re))
144 break;
145 if (isnan(coeffs[i].im) || isinf(coeffs[i].im))
146 break;
147 if (coeffs[i].re < -20.0 || coeffs[i].re > 20.0)
148 break;
149 if (coeffs[i].im < -20.0 || coeffs[i].im > 20.0)
150 break;
151 }
152 if (i != len)
153 return -1;
154
155 if (s->eq_re)
156 delete s->eq_re;
157 if (s->eq_im)
158 delete s->eq_im;
159
160 s->canvas_eq->current(s->canvas_eq);
161 i = 0;
162 min = coeffs[i].re;
163 if (min > coeffs[i].im)
164 min = coeffs[i].im;
165 max = coeffs[i].re;
166 if (max < coeffs[i].im)
167 max = coeffs[i].im;
168 for (i = 0; i < len; i++)
169 {
170 s->eq_re_plot[2*i] = (i - len/2)/2.0;
171 s->eq_re_plot[2*i + 1] = coeffs[i].re;
172 if (min > coeffs[i].re)
173 min = coeffs[i].re;
174 if (max < coeffs[i].re)
175 max = coeffs[i].re;
176
177 s->eq_im_plot[2*i] = (i - len/2)/2.0;
178 s->eq_im_plot[2*i + 1] = coeffs[i].im;
179 if (min > coeffs[i].im)
180 min = coeffs[i].im;
181 if (max < coeffs[i].im)
182 max = coeffs[i].im;
183 }
184
185 s->eq_x->minimum(-len/4.0);
186 s->eq_x->maximum(len/4.0);
187 s->eq_y->maximum((max == min) ? max + 0.2 : max);
188 s->eq_y->minimum(min);
189 s->eq_re = new Ca_Line(len, s->eq_re_plot, 0, 0, FL_BLUE, CA_NO_POINT);
190 s->eq_im = new Ca_Line(len, s->eq_im_plot, 0, 0, FL_RED, CA_NO_POINT);
191 Fl::check();
192 return 0;
193 }
194 /*- End of function --------------------------------------------------------*/
195
196 int qam_monitor_update_symbol_tracking(qam_monitor_t *s, float total_correction)
197 {
198 int i;
199 int j;
200 float min;
201 float max;
202
203 s->symbol_tracker[s->symbol_track_ptr++] = total_correction;
204 if (s->symbol_track_points < SYMBOL_TRACKER_POINTS)
205 s->symbol_track_points++;
206 if (s->symbol_track_ptr >= SYMBOL_TRACKER_POINTS)
207 s->symbol_track_ptr = 0;
208
209 s->canvas_track->current(s->canvas_track);
210 if (s->symbol_track)
211 delete s->symbol_track;
212 s->track_x->current();
213 s->symbol_track_y->current();
214
215 min =
216 max = s->symbol_tracker[0];
217 for (i = s->symbol_track_ptr, j = 0; i < s->symbol_track_points; i++, j++)
218 {
219 s->symbol_track_plot[2*j] = j;
220 s->symbol_track_plot[2*j + 1] = s->symbol_tracker[i];
221 if (min > s->symbol_tracker[i])
222 min = s->symbol_tracker[i];
223 if (max < s->symbol_tracker[i])
224 max = s->symbol_tracker[i];
225 }
226 for (i = 0; i < s->symbol_track_ptr; i++, j++)
227 {
228 s->symbol_track_plot[2*j] = j;
229 s->symbol_track_plot[2*j + 1] = s->symbol_tracker[i];
230 if (min > s->symbol_tracker[i])
231 min = s->symbol_tracker[i];
232 if (max < s->symbol_tracker[i])
233 max = s->symbol_tracker[i];
234 }
235 s->symbol_track_y->maximum((fabs(max - min) < 0.05) ? max + 0.05 : max);
236 s->symbol_track_y->minimum(min);
237
238 s->symbol_track = new Ca_Line(s->symbol_track_points, s->symbol_track_plot, 0, 0, FL_RED, CA_NO_POINT);
239 //Fl::check();
240 return 0;
241 }
242 /*- End of function --------------------------------------------------------*/
243
244 int qam_monitor_update_audio_level(qam_monitor_t *s, const int16_t amp[], int len)
245 {
246 int i;
247 char buf[11];
248 double val;
249
250 for (i = 0; i < len; i++)
251 {
252 s->audio_meter->sample(amp[i]/32768.0);
253 s->power_reading += ((amp[i]*amp[i] - s->power_reading) >> 10);
254 }
255 if (s->power_reading <= 0)
256 val = -90.0;
257 else
258 val = log10((double) s->power_reading/(32767.0f*32767.0f))*10.0f + 3.14 + 3.02;
259
260 snprintf(buf, sizeof(buf), "%5.1fdBm0", val);
261 s->audio_level->value(buf);
262 return 0;
263 }
264 /*- End of function --------------------------------------------------------*/
265
266 int qam_monitor_update_carrier_tracking(qam_monitor_t *s, float carrier_freq)
267 {
268 int i;
269 int j;
270 float min;
271 float max;
272
273 s->carrier_tracker[s->carrier_ptr++] = carrier_freq;
274 if (s->carrier_points < CARRIER_TRACKER_POINTS)
275 s->carrier_points++;
276 if (s->carrier_ptr >= CARRIER_TRACKER_POINTS)
277 s->carrier_ptr = 0;
278
279 s->canvas_track->current(s->canvas_track);
280 if (s->carrier)
281 delete s->carrier;
282 s->track_x->current();
283 s->carrier_y->current();
284
285 min =
286 max = s->carrier_tracker[0];
287 for (i = s->carrier_ptr, j = 0; i < s->carrier_points; i++, j++)
288 {
289 s->carrier_plot[2*j] = j;
290 s->carrier_plot[2*j + 1] = s->carrier_tracker[i];
291 if (min > s->carrier_tracker[i])
292 min = s->carrier_tracker[i];
293 if (max < s->carrier_tracker[i])
294 max = s->carrier_tracker[i];
295 }
296 for (i = 0; i < s->carrier_ptr; i++, j++)
297 {
298 s->carrier_plot[2*j] = j;
299 s->carrier_plot[2*j + 1] = s->carrier_tracker[i];
300 if (min > s->carrier_tracker[i])
301 min = s->carrier_tracker[i];
302 if (max < s->carrier_tracker[i])
303 max = s->carrier_tracker[i];
304 }
305
306 s->carrier_y->maximum((fabs(max - min) < 0.05) ? max + 0.05 : max);
307 s->carrier_y->minimum(min);
308
309 s->carrier = new Ca_Line(s->carrier_points, s->carrier_plot, 0, 0, FL_BLUE, CA_NO_POINT);
310 //Fl::check();
311 return 0;
312 }
313 /*- End of function --------------------------------------------------------*/
314
315 qam_monitor_t *qam_monitor_init(float constel_width, const char *tag)
316 {
317 char buf[132 + 1];
318 float x;
319 float y;
320 qam_monitor_t *s;
321
322 if ((s = (qam_monitor_t *) malloc(sizeof(*s))) == NULL)
323 return NULL;
324
325 s->w = new Fl_Double_Window(905, 400, (tag) ? tag : "QAM monitor");
326
327 s->c_const = new Fl_Group(0, 0, 380, 400);
328 s->c_const->box(FL_DOWN_BOX);
329 s->c_const->align(FL_ALIGN_TOP | FL_ALIGN_INSIDE);
330
331 s->canvas_const = new Ca_Canvas(60, 30, 300, 300, "Constellation");
332 s->canvas_const->box(FL_PLASTIC_DOWN_BOX);
333 s->canvas_const->color(7);
334 s->canvas_const->align(FL_ALIGN_TOP);
335 s->canvas_const->border(15);
336
337 s->sig_i = new Ca_X_Axis(65, 330, 290, 30, "I");
338 s->sig_i->align(FL_ALIGN_BOTTOM);
339 s->sig_i->minimum(-constel_width);
340 s->sig_i->maximum(constel_width);
341 s->sig_i->label_format("%g");
342 s->sig_i->minor_grid_color(fl_gray_ramp(20));
343 s->sig_i->major_grid_color(fl_gray_ramp(15));
344 s->sig_i->label_grid_color(fl_gray_ramp(10));
345 s->sig_i->grid_visible(CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
346 s->sig_i->minor_grid_style(FL_DOT);
347 s->sig_i->major_step(5);
348 s->sig_i->label_step(1);
349 s->sig_i->axis_color(FL_BLACK);
350 s->sig_i->axis_align(CA_BOTTOM | CA_LINE);
351
352 s->sig_q = new Ca_Y_Axis(20, 35, 40, 290, "Q");
353 s->sig_q->align(FL_ALIGN_LEFT);
354 s->sig_q->minimum(-constel_width);
355 s->sig_q->maximum(constel_width);
356 s->sig_q->minor_grid_color(fl_gray_ramp(20));
357 s->sig_q->major_grid_color(fl_gray_ramp(15));
358 s->sig_q->label_grid_color(fl_gray_ramp(10));
359 //s->sig_q->grid_visible(CA_MINOR_TICK | CA_MAJOR_TICK | CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
360 s->sig_q->grid_visible(CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
361 s->sig_q->minor_grid_style(FL_DOT);
362 s->sig_q->major_step(5);
363 s->sig_q->label_step(1);
364 s->sig_q->axis_color(FL_BLACK);
365
366 s->sig_q->current();
367
368 s->c_const->end();
369
370 s->c_right = new Fl_Group(440, 0, 465, 405);
371
372 s->c_eq = new Fl_Group(380, 0, 265, 200);
373 s->c_eq->box(FL_DOWN_BOX);
374 s->c_eq->align(FL_ALIGN_TOP | FL_ALIGN_INSIDE);
375 s->c_eq->current();
376 s->canvas_eq = new Ca_Canvas(460, 35, 150, 100, "Equalizer");
377 s->canvas_eq->box(FL_PLASTIC_DOWN_BOX);
378 s->canvas_eq->color(7);
379 s->canvas_eq->align(FL_ALIGN_TOP);
380 Fl_Group::current()->resizable(s->canvas_eq);
381 s->canvas_eq->border(15);
382
383 s->eq_x = new Ca_X_Axis(465, 135, 140, 30, "Symbol");
384 s->eq_x->align(FL_ALIGN_BOTTOM);
385 s->eq_x->minimum(-8.0);
386 s->eq_x->maximum(8.0);
387 s->eq_x->label_format("%g");
388 s->eq_x->minor_grid_color(fl_gray_ramp(20));
389 s->eq_x->major_grid_color(fl_gray_ramp(15));
390 s->eq_x->label_grid_color(fl_gray_ramp(10));
391 s->eq_x->grid_visible(CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
392 s->eq_x->minor_grid_style(FL_DOT);
393 s->eq_x->major_step(5);
394 s->eq_x->label_step(1);
395 s->eq_x->axis_align(CA_BOTTOM | CA_LINE);
396 s->eq_x->axis_color(FL_BLACK);
397 s->eq_x->current();
398
399 s->eq_y = new Ca_Y_Axis(420, 40, 40, 90, "Amp");
400 s->eq_y->align(FL_ALIGN_LEFT);
401 s->eq_y->minimum(-0.1);
402 s->eq_y->maximum(0.1);
403 s->eq_y->minor_grid_color(fl_gray_ramp(20));
404 s->eq_y->major_grid_color(fl_gray_ramp(15));
405 s->eq_y->label_grid_color(fl_gray_ramp(10));
406 s->eq_y->grid_visible(CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
407 s->eq_y->minor_grid_style(FL_DOT);
408 s->eq_y->major_step(5);
409 s->eq_y->label_step(1);
410 s->eq_y->axis_color(FL_BLACK);
411 s->eq_y->current();
412
413 s->c_eq->end();
414
415 s->c_symbol_track = new Fl_Group(380, 200, 525, 200);
416 s->c_symbol_track->box(FL_DOWN_BOX);
417 s->c_symbol_track->align(FL_ALIGN_TOP | FL_ALIGN_INSIDE);
418 s->c_symbol_track->current();
419
420 s->canvas_track = new Ca_Canvas(490, 235, 300, 100, "Symbol and carrier tracking");
421 s->canvas_track->box(FL_PLASTIC_DOWN_BOX);
422 s->canvas_track->color(7);
423 s->canvas_track->align(FL_ALIGN_TOP);
424 Fl_Group::current()->resizable(s->canvas_track);
425 s->canvas_track->border(15);
426
427 s->track_x = new Ca_X_Axis(495, 335, 290, 30, "Time (symbols)");
428 s->track_x->align(FL_ALIGN_BOTTOM);
429 s->track_x->minimum(0.0);
430 s->track_x->maximum(2400.0*5.0);
431 s->track_x->label_format("%g");
432 s->track_x->minor_grid_color(fl_gray_ramp(20));
433 s->track_x->major_grid_color(fl_gray_ramp(15));
434 s->track_x->label_grid_color(fl_gray_ramp(10));
435 s->track_x->grid_visible(CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
436 s->track_x->minor_grid_style(FL_DOT);
437 s->track_x->major_step(5);
438 s->track_x->label_step(1);
439 s->track_x->axis_align(CA_BOTTOM | CA_LINE);
440 s->track_x->axis_color(FL_BLACK);
441 s->track_x->current();
442
443 s->symbol_track_y = new Ca_Y_Axis(420, 240, 70, 90, "Cor");
444 s->symbol_track_y->align(FL_ALIGN_LEFT);
445 s->symbol_track_y->minimum(-0.1);
446 s->symbol_track_y->maximum(0.1);
447 s->symbol_track_y->minor_grid_color(fl_gray_ramp(20));
448 s->symbol_track_y->major_grid_color(fl_gray_ramp(15));
449 s->symbol_track_y->label_grid_color(fl_gray_ramp(10));
450 s->symbol_track_y->grid_visible(CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
451 s->symbol_track_y->minor_grid_style(FL_DOT);
452 s->symbol_track_y->major_step(5);
453 s->symbol_track_y->label_step(1);
454 s->symbol_track_y->axis_color(FL_RED);
455 s->symbol_track_y->current();
456
457 s->carrier_y = new Ca_Y_Axis(790, 240, 70, 90, "Freq");
458 s->carrier_y->align(FL_ALIGN_RIGHT);
459 s->carrier_y->minimum(-0.1);
460 s->carrier_y->maximum(0.1);
461 s->carrier_y->minor_grid_color(fl_gray_ramp(20));
462 s->carrier_y->major_grid_color(fl_gray_ramp(15));
463 s->carrier_y->label_grid_color(fl_gray_ramp(10));
464 s->carrier_y->grid_visible(CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
465 s->carrier_y->minor_grid_style(FL_DOT);
466 s->carrier_y->major_step(5);
467 s->carrier_y->label_step(1);
468 s->carrier_y->axis_align(CA_RIGHT);
469 s->carrier_y->axis_color(FL_BLUE);
470 s->carrier_y->current();
471
472 s->c_symbol_track->end();
473
474 s->audio_meter = new Fl_Audio_Meter(672, 10, 16, 150, "");
475 s->audio_meter->box(FL_PLASTIC_UP_BOX);
476 s->audio_meter->type(FL_VERT_AUDIO_METER);
477
478 s->audio_level = new Fl_Output(650, 170, 60, 20, "");
479 s->audio_level->textsize(10);
480
481 s->c_right->end();
482
483 Fl_Group::current()->resizable(s->c_right);
484 s->w->end();
485 s->w->show();
486
487 s->next_constel_point = 0;
488 s->constel_window = 10000;
489
490 s->carrier_points = 0;
491 s->carrier_ptr = 0;
492 s->carrier_window = 100;
493
494 s->symbol_track_points = 0;
495 s->symbol_track_window = 10000;
496 Fl::check();
497 return s;
498 }
499 /*- End of function --------------------------------------------------------*/
500
501 void qam_wait_to_end(qam_monitor_t *s)
502 {
503 fd_set rfds;
504 int res;
505 struct timeval tv;
506
507 fprintf(stderr, "Processing complete. Press the <enter> key to end\n");
508 do
509 {
510 usleep(100000);
511 Fl::check();
512 FD_ZERO(&rfds);
513 FD_SET(0, &rfds);
514 tv.tv_usec = 100000;
515 tv.tv_sec = 0;
516 res = select(1, &rfds, NULL, NULL, &tv);
517 }
518 while (res <= 0);
519 }
520 /*- End of function --------------------------------------------------------*/
521 #endif
522 /*- End of file ------------------------------------------------------------*/

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.