-
Notifications
You must be signed in to change notification settings - Fork 145
/
oscplot.c
7395 lines (6310 loc) · 226 KB
/
oscplot.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (C) 2012-2013 Analog Devices, Inc.
*
* Licensed under the GPL-2.
*
**/
#include <stdio.h>
#include <gtk/gtk.h>
#include <gtkdatabox.h>
#include <gtkdatabox_grid.h>
#include <gtkdatabox_points.h>
#include <gtkdatabox_lines.h>
#include <gtkdatabox_markers.h>
#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>
#include <math.h>
#include <matio.h>
#include <sys/time.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <complex.h>
#include <fftw3.h>
#include <iio.h>
#include "osc.h"
#include "oscplot.h"
#include "config.h"
#include "iio_widget.h"
#include "datatypes.h"
#include "osc_plugin.h"
#include "math_expression_generator.h"
#include "iio_utils.h"
/* add backwards compat for <matio-1.5.0 */
#if MATIO_MAJOR_VERSION == 1 && MATIO_MINOR_VERSION < 5
typedef int mat_dim;
#else
typedef size_t mat_dim;
#endif
/* timersub, macros are _BSD_SOURCE, and aren't included in windows */
#ifndef timersub
#define timersub(a, b, result) \
do { \
(result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
(result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
if ((result)->tv_usec < 0) { \
--(result)->tv_sec; \
(result)->tv_usec += 1000000; \
} \
} while (0)
#endif /* timersub */
extern void *find_setup_check_fct_by_devname(const char *dev_name);
static int (*plugin_setup_validation_fct)(struct iio_device *, const char **) = NULL;
static unsigned object_count = 0;
static void create_plot (OscPlot *plot);
static void plot_setup(OscPlot *plot);
static void capture_button_clicked_cb (GtkToggleToolButton *btn, gpointer data);
static void single_shot_clicked_cb (GtkToggleToolButton *btn, gpointer data);
static void update_grid(OscPlot *plot, gfloat min, gfloat max);
static void add_grid(OscPlot *plot);
static void rescale_databox(OscPlotPrivate *priv, GtkDatabox *box, gfloat border);
static bool call_all_transform_functions(OscPlotPrivate *priv);
static void capture_start(OscPlotPrivate *priv);
static void plot_profile_save(OscPlot *plot, char *filename);
static void transform_add_plot_markers(OscPlot *plot, Transform *transform);
static void osc_plot_finalize(GObject *object);
static void osc_plot_dispose(GObject *object);
static void save_as(OscPlot *plot, const char *filename, int type);
static void treeview_expand_update(OscPlot *plot);
static void treeview_icon_color_update(OscPlot *plot);
static int enabled_channels_of_device(GtkTreeView *treeview, const char *name, unsigned *enabled_mask);
static int enabled_channels_count(OscPlot *plot);
static int num_of_channels_of_device(GtkTreeView *treeview, const char *name);
static gboolean get_iter_by_name(GtkTreeView *tree, GtkTreeIter *iter, const char *dev_name, const char *ch_name);
static void set_marker_labels (OscPlot *plot, gchar *buf, enum marker_types type);
static void channel_color_icon_set_color(GdkPixbuf *pb, GdkRGBA *color);
static int comboboxtext_set_active_by_string(GtkComboBox *combo_box, const char *name);
static int comboboxtext_get_active_text_as_int(GtkComboBoxText* combobox);
static gboolean check_valid_setup(OscPlot *plot);
static int device_find_by_name(struct iio_context *ctx, const char *name);
static int channel_find_by_name(struct iio_context *ctx, int device_index, const char *name);
static void device_rx_info_update(OscPlotPrivate *priv);
static gdouble prefix2scale (char adc_scale);
static struct iio_device * transform_get_device_parent(Transform *transform);
static gboolean tree_get_selected_row_iter(GtkTreeView *treeview, GtkTreeIter *iter);
static void set_channel_shadow_of_enabled(gpointer data, gpointer user_data);
static gfloat * plot_channels_get_nth_data_ref(GSList *list, guint n);
static void transform_add_own_markers(OscPlot *plot, Transform *transform);
static void transform_remove_own_markers(Transform *transform);
static bool set_channel_state_in_tree_model(GtkTreeModel *model, GtkTreeIter* chn_iter, gboolean state);
/* IDs of signals */
enum {
CAPTURE_EVENT_SIGNAL,
DESTROY_EVENT_SIGNAL,
NEWPLOT_EVENT_SIGNAL,
LAST_SIGNAL
};
/* signals will be configured during class init */
static guint oscplot_signals[LAST_SIGNAL] = { 0 };
/* Columns of the device treestore */
enum {
ELEMENT_NAME,
IS_DEVICE,
IS_CHANNEL,
CHANNEL_TYPE,
DEVICE_SELECTABLE,
DEVICE_ACTIVE,
CHANNEL_ACTIVE,
ELEMENT_REFERENCE,
EXPANDED,
CHANNEL_SETTINGS,
CHANNEL_COLOR_ICON,
SENSITIVE,
PLOT_TYPE,
NUM_COL
};
/* Horizontal Scale Types */
enum {
HOR_SCALE_SAMPLES,
HOR_SCALE_TIME,
HOR_SCALE_NUM_OPTIONS
};
/* Types of channels that can be displayed on a plot */
enum {
PLOT_IIO_CHANNEL = 0,
PLOT_MATH_CHANNEL,
NUM_PLOT_CHANNELS_TYPES
};
#define MATH_CHANNELS_DEVICE "Math"
#define OSC_COLOR(r, g, b, a) { \
.red = (double) ((r) << 8) / 65535.0, \
.green = (double) ((g) << 8) / 65535.0, \
.blue = (double) ((b) << 8) / 65535.0, \
.alpha = (a) \
}
static GdkRGBA color_graph[] = {
OSC_COLOR(138, 226, 52, 1.0),
OSC_COLOR(239, 41, 41, 1.0),
OSC_COLOR(114, 159, 207, 1.0),
OSC_COLOR(252, 175, 62, 1.0),
OSC_COLOR(211, 215, 208, 1.0),
OSC_COLOR(252, 233, 79, 1.0),
OSC_COLOR(173, 127, 168, 1.0),
OSC_COLOR(233, 185, 110, 1.0),
OSC_COLOR(115, 210, 22, 1.0),
OSC_COLOR(204, 0, 0, 1.0),
OSC_COLOR(52, 101, 164, 1.0),
OSC_COLOR(245, 121, 0, 1.0),
OSC_COLOR(186, 189, 182, 1.0),
OSC_COLOR(237, 212, 0, 1.0),
OSC_COLOR(117, 80, 123, 1.0),
OSC_COLOR(193, 125, 17, 1.0),
};
#define NUM_GRAPH_COLORS (sizeof(color_graph) / sizeof(color_graph[0]))
static GdkRGBA color_grid = {
.red = 0.778210117, /* = 51000 / 65535 */
.green = 0.778210117, /* = 51000 / 65535 */
.blue = 0,
.alpha = 1.0
};
static GdkRGBA color_marker = {
.red = 1.0,
.green = 0,
.blue = 1.0,
.alpha = 1.0
};
typedef struct channel_settings PlotChn;
typedef struct iio_channel_settings PlotIioChn;
typedef struct math_channel_settings PlotMathChn;
struct channel_settings {
unsigned type;
char *name;
char *parent_name;
struct iio_device *dev;
struct iio_context *ctx;
GdkRGBA graph_color;
gfloat * (*get_data_ref)(PlotChn *);
void (*assert_used_iio_channels)(PlotChn *, bool);
void (*destroy)(PlotChn *);
};
struct iio_channel_settings {
PlotChn base;
struct iio_channel *iio_chn;
bool apply_inverse_funct;
bool apply_multiply_funct;
bool apply_add_funct;
double multiply_value;
double add_value;
};
struct math_channel_settings {
PlotChn base;
GSList *iio_channels;
gfloat ***iio_channels_data;
int num_channels;
char *iio_device_name;
char *txt_math_expression;
void (*math_expression)(float ***channels_data, float *out_data, unsigned long long chn_sample_cnt);
void *math_lib_handler;
float *data_ref;
};
/* Helpers */
#define TIME_SETTINGS(obj) ((struct _time_settings *)obj->settings)
#define FFT_SETTINGS(obj) ((struct _fft_settings *)obj->settings)
#define CONSTELLATION_SETTINGS(obj) ((struct _constellation_settings *)obj->settings)
#define XCORR_SETTINGS(obj) ((struct _cross_correlation_settings *)obj->settings)
#define FREQ_SPECTRUM_SETTINGS(obj) ((struct _freq_spectrum_settings *)obj->settings)
#define MATH_SETTINGS(obj) ((struct _math_settings *)obj->settings)
#define PLOT_CHN(obj) ((PlotChn *)obj)
#define PLOT_IIO_CHN(obj) ((PlotIioChn *)obj)
#define PLOT_MATH_CHN(obj) ((PlotMathChn *)obj)
struct int_and_plot {
int int_obj;
OscPlot *plot;
};
struct string_and_plot {
char *string_obj;
OscPlot *plot;
};
struct plot_geometry {
gint width;
gint height;
};
struct _OscPlotPrivate
{
GtkBuilder *builder;
int object_id;
/* Graphical User Interface */
GtkWidget *window;
GtkWidget *databox;
GtkWidget *capture_graph;
GtkWidget *capture_button;
GtkWidget *ss_button;
GtkWidget *channel_list_view;
GtkWidget *show_grid;
GtkWidget *plot_type;
GtkWidget *plot_domain;
GtkWidget *enable_auto_scale;
GtkWidget *hor_scale;
GtkWidget *hor_units;
GtkWidget *marker_label;
GtkWidget *devices_label;
GtkWidget *phase_label;
GtkWidget *saveas_button;
GtkWidget *saveas_dialog;
GtkWidget *saveas_type_dialog;
GtkWidget *title_edit_dialog;
GtkWidget *fullscreen_button;
GtkWidget *menu_fullscreen;
GtkWidget *menu_show_options;
GtkWidget *y_axis_max;
GtkWidget *y_axis_min;
GtkWidget *viewport_saveas_channels;
GtkWidget *saveas_channels_list;
GtkWidget *saveas_select_channel_message;
GtkWidget *device_combobox;
GtkWidget *sample_count_widget;
unsigned int sample_count;
GtkWidget *fft_size_widget;
GtkWidget *fft_win_widget;
GtkWidget *fft_win_correction;
GtkWidget *fft_avg_widget;
GtkWidget *fft_pwr_offset_widget;
GtkWidget *device_settings_menu;
GtkWidget *math_settings_menu;
GtkWidget *device_trigger_menuitem;
GtkWidget *math_menuitem;
GtkWidget *plot_trigger_menuitem;
GtkWidget *channel_settings_menu;
GtkWidget *math_channel_settings_menu;
GtkWidget *channel_expression_edit_menuitem;
GtkWidget *channel_iio_color_menuitem;
GtkWidget *channel_math_color_menuitem;
GtkWidget *channel_math_menuitem;
GtkWidget *channel_remove_menuitem;
GtkWidget *math_dialog;
GtkWidget *capture_options_box;
GtkWidget *saveas_settings_box;
GtkWidget *save_mat_scale;
GtkWidget *new_plot_button;
GtkWidget *cmb_saveas_type;
GtkWidget *math_expression_dialog;
GtkWidget *math_expression_textview;
GtkWidget *math_device_select;
GtkWidget *math_channel_name_entry;
GtkWidget *math_expr_error;
GtkCssProvider *provider;
GtkTextBuffer* tbuf;
GtkTextBuffer* devices_buf;
GtkTextBuffer* phase_buf;
GtkTextBuffer* math_expression;
OscPlotPreferences *preferences;
struct iio_context *ctx;
unsigned int nb_input_devices;
unsigned int nb_plot_channels;
struct plot_geometry size;
int frame_counter;
double fps;
struct timeval last_update;
int last_hor_unit;
int do_a_rescale_flag;
gulong capture_button_hid;
gint deactivate_capture_btn_flag;
bool single_shot_mode;
/* A reference to the device holding the most recent created transform */
struct iio_device *current_device;
/* List of transforms for this plot */
TrList *transform_list;
/* Active transform type for this window */
int active_transform_type;
/* Transform currently holding the fft marker */
Transform *tr_with_marker;
/* Type of "Save As" currently selected*/
gint active_saveas_type;
/* The set of markers */
struct marker_type markers[MAX_MARKERS + 2];
struct marker_type *markers_copy;
enum marker_types marker_type;
/* Settings list of all channel */
GSList *ch_settings_list;
/* Databox data */
GtkDataboxGraph *grid;
gfloat gridy[25], gridx[25];
/* Spectrum mode - Parameters */
unsigned fft_count;
double start_freq;
double filter_bw;
gint line_thickness;
gint redraw_function;
gboolean stop_redraw;
gboolean redraw;
bool spectrum_data_ready;
gboolean fullscreen_state;
bool profile_loaded_scale;
bool save_as_png;
char *saveas_filename;
struct int_and_plot fix_marker;
struct string_and_plot add_mrk;
struct string_and_plot remove_mrk;
struct string_and_plot peak_mrk;
struct string_and_plot fix_mrk;
struct string_and_plot single_mrk;
struct string_and_plot dual_mrk;
struct string_and_plot image_mrk;
struct string_and_plot off_mrk;
gulong fixed_marker_hid;
gint plot_x_pos;
gint plot_y_pos;
gfloat plot_left;
gfloat plot_right;
gfloat plot_top;
gfloat plot_bottom;
int read_scale_params;
GMutex g_marker_copy_lock;
void (*quit_callback)(void *user_data);
void *qcb_user_data;
};
G_DEFINE_TYPE_WITH_PRIVATE(OscPlot, osc_plot, GTK_TYPE_WIDGET)
static void osc_plot_class_init(OscPlotClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->dispose = osc_plot_dispose;
gobject_class->finalize = osc_plot_finalize;
oscplot_signals[CAPTURE_EVENT_SIGNAL] = g_signal_new("osc-capture-event",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
G_STRUCT_OFFSET (OscPlotClass, capture_event),
NULL,
NULL,
g_cclosure_marshal_VOID__BOOLEAN,
G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
oscplot_signals[DESTROY_EVENT_SIGNAL] = g_signal_new("osc-destroy-event",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
G_STRUCT_OFFSET (OscPlotClass, destroy_event),
NULL,
NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
oscplot_signals[NEWPLOT_EVENT_SIGNAL] = g_signal_new("osc-newplot-event",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
G_STRUCT_OFFSET (OscPlotClass, newplot_event),
NULL,
NULL,
g_cclosure_marshal_VOID__POINTER,
G_TYPE_NONE, 1, G_TYPE_POINTER);
}
static void osc_plot_init(OscPlot *plot)
{
plot->priv = osc_plot_get_instance_private (plot);
}
GtkWidget *osc_plot_new(struct iio_context *ctx)
{
GtkWidget *plot;
plot = GTK_WIDGET(g_object_new(OSC_PLOT_TYPE, NULL));
OSC_PLOT(plot)->priv->ctx = ctx;
create_plot(OSC_PLOT(plot));
return plot;
}
GtkWidget *osc_plot_new_with_pref(struct iio_context *ctx, OscPlotPreferences *pref)
{
GtkWidget *plot;
plot = GTK_WIDGET(g_object_new(OSC_PLOT_TYPE, NULL));
OSC_PLOT(plot)->priv->ctx = ctx;
OSC_PLOT(plot)->priv->preferences = pref;
create_plot(OSC_PLOT(plot));
return plot;
}
void osc_plot_destroy (OscPlot *plot)
{
g_object_unref(plot->priv->provider);
gtk_widget_destroy(plot->priv->window);
gtk_widget_destroy(GTK_WIDGET(plot));
}
void osc_plot_reset_numbering (void)
{
object_count = 0;
}
void osc_plot_set_visible (OscPlot *plot, bool visible)
{
gtk_widget_set_visible(plot->priv->window, visible);
}
struct iio_buffer * osc_plot_get_buffer(OscPlot *plot)
{
struct extra_dev_info *dev_info;
dev_info = iio_device_get_data(plot->priv->current_device);
return dev_info->buffer;
}
void osc_plot_data_update (OscPlot *plot)
{
if (call_all_transform_functions(plot->priv))
plot->priv->redraw = TRUE;
if (plot->priv->single_shot_mode) {
plot->priv->single_shot_mode = false;
gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(plot->priv->capture_button), false);
}
}
static bool is_frequency_transform(OscPlotPrivate *priv)
{
return priv->active_transform_type == FFT_TRANSFORM ||
priv->active_transform_type == COMPLEX_FFT_TRANSFORM ||
priv->active_transform_type == FREQ_SPECTRUM_TRANSFORM;
}
void osc_plot_update_rx_lbl(OscPlot *plot, bool initial_update)
{
OscPlotPrivate *priv = plot->priv;
TrList *tr_list = priv->transform_list;
struct extra_dev_info *dev_info = NULL;
char buf[20];
double corr;
int i;
device_rx_info_update(priv);
/* Skip rescaling graphs, updating labels and others if the redrawing is currently halted. */
if (priv->redraw_function <= 0 && !initial_update)
return;
if (is_frequency_transform(priv)) {
gfloat top, bottom, left, right;
gfloat padding;
/* In FFT mode we need to scale the x-axis according to the selected sampling frequency */
for (i = 0; i < tr_list->size; i++) {
if(!initial_update)
Transform_setup(tr_list->transforms[i]);
gtk_databox_graph_set_hide(tr_list->transforms[i]->graph, TRUE);
}
dev_info = iio_device_get_data(transform_get_device_parent(tr_list->transforms[i - 1]));
sprintf(buf, "%cHz", dev_info->adc_scale);
gtk_label_set_text(GTK_LABEL(priv->hor_scale), buf);
if (priv->active_transform_type == COMPLEX_FFT_TRANSFORM)
corr = dev_info->adc_freq / 2.0;
else
corr = 0;
if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(priv->enable_auto_scale)) && !initial_update)
return;
if (priv->profile_loaded_scale)
return;
update_grid(plot, -corr, dev_info->adc_freq / 2.0);
padding = (dev_info->adc_freq / 2.0 + corr) * 0.05;
gtk_databox_get_total_limits(GTK_DATABOX(priv->databox), &left, &right,
&top, &bottom);
gtk_databox_set_total_limits(GTK_DATABOX(priv->databox),
-corr - padding, dev_info->adc_freq / 2.0 + padding,
top, bottom);
} else {
switch (gtk_combo_box_get_active(GTK_COMBO_BOX(priv->hor_units))) {
case 0:
gtk_label_set_text(GTK_LABEL(priv->hor_scale), "Samples");
break;
case 1:
gtk_label_set_text(GTK_LABEL(priv->hor_scale), "µs");
break;
}
}
}
void osc_plot_restart (OscPlot *plot)
{
OscPlotPrivate *priv = plot->priv;
if (priv->redraw_function > 0)
{
priv->stop_redraw = TRUE;
plot_setup(plot);
add_grid(plot);
gtk_widget_queue_draw(priv->databox);
priv->frame_counter = 0;
priv->fps = 0.0;
gettimeofday(&(priv->last_update), NULL);
capture_start(priv);
}
}
bool osc_plot_running_state (OscPlot *plot)
{
return !!plot->priv->redraw_function;
}
void osc_plot_draw_start (OscPlot *plot)
{
OscPlotPrivate *priv = plot->priv;
gtk_toggle_tool_button_set_active((GtkToggleToolButton *)priv->capture_button, TRUE);
}
void osc_plot_draw_stop (OscPlot *plot)
{
OscPlotPrivate *priv = plot->priv;
gtk_toggle_tool_button_set_active((GtkToggleToolButton *)priv->capture_button, FALSE);
}
void osc_plot_save_to_ini (OscPlot *plot, char *filename)
{
plot_profile_save(plot, filename);
}
void osc_plot_save_as (OscPlot *plot, char *filename, int type)
{
save_as(plot, filename, type);
}
const char * osc_plot_get_active_device (OscPlot *plot)
{
OscPlotPrivate *priv = plot->priv;
GtkTreeModel *model;
GtkTreeIter iter;
gboolean next_iter;
gboolean active;
struct iio_device *dev;
model = gtk_tree_view_get_model(GTK_TREE_VIEW(priv->channel_list_view));
next_iter = gtk_tree_model_get_iter_first(model, &iter);
while (next_iter) {
gtk_tree_model_get(model, &iter, ELEMENT_REFERENCE, &dev, DEVICE_ACTIVE, &active, -1);
if (active)
return get_iio_device_label_or_name(dev);
next_iter = gtk_tree_model_iter_next(model, &iter);
}
return NULL;
}
int osc_plot_get_fft_avg (OscPlot *plot)
{
return gtk_spin_button_get_value(GTK_SPIN_BUTTON(plot->priv->fft_avg_widget));
}
int osc_plot_get_marker_type (OscPlot *plot)
{
return plot->priv->marker_type;
}
void osc_plot_set_marker_type (OscPlot *plot, int mtype)
{
plot->priv->marker_type = mtype;
set_marker_labels(plot, NULL, mtype);
}
void * osc_plot_get_markers_copy(OscPlot *plot)
{
return plot->priv->markers_copy;
}
void osc_plot_set_markers_copy (OscPlot *plot, void *value)
{
plot->priv->markers_copy = value;
}
void osc_plot_set_domain (OscPlot *plot, int domain)
{
OscPlotPrivate *priv = plot->priv;
if (gtk_toggle_tool_button_get_active((GtkToggleToolButton *)priv->capture_button))
return;
gtk_combo_box_set_active(GTK_COMBO_BOX(priv->plot_domain), domain);
}
int osc_plot_get_plot_domain (OscPlot *plot)
{
return gtk_combo_box_get_active(GTK_COMBO_BOX(plot->priv->plot_domain));
}
GMutex * osc_plot_get_marker_lock (OscPlot *plot)
{
return &plot->priv->g_marker_copy_lock;
}
bool osc_plot_set_sample_count (OscPlot *plot, gdouble count)
{
OscPlotPrivate *priv = plot->priv;
int ret;
if (gtk_toggle_tool_button_get_active((GtkToggleToolButton *)priv->capture_button))
return false;
if (gtk_combo_box_get_active(GTK_COMBO_BOX(priv->plot_domain)) == FFT_PLOT ||
gtk_combo_box_get_active(GTK_COMBO_BOX(priv->plot_domain)) == SPECTRUM_PLOT) {
char s_count[32];
snprintf(s_count, sizeof(s_count), "%d", (int)count);
ret = comboboxtext_set_active_by_string(GTK_COMBO_BOX(priv->fft_size_widget), s_count);
priv->sample_count = (int)count;
} else {
switch (gtk_combo_box_get_active(GTK_COMBO_BOX(priv->hor_units))) {
case 0:
gtk_spin_button_set_value(GTK_SPIN_BUTTON(priv->sample_count_widget), count);
priv->sample_count = (int)count;
break;
case 1:
gtk_spin_button_set_value(GTK_SPIN_BUTTON(priv->sample_count_widget), count);
break;
}
ret = 1;
}
return (ret) ? true : false;
}
double osc_plot_get_sample_count (OscPlot *plot) {
OscPlotPrivate *priv = plot->priv;
int count;
if (gtk_combo_box_get_active(GTK_COMBO_BOX(priv->plot_domain)) == FFT_PLOT ||
gtk_combo_box_get_active(GTK_COMBO_BOX(priv->plot_domain)) == SPECTRUM_PLOT)
count = comboboxtext_get_active_text_as_int(GTK_COMBO_BOX_TEXT(priv->fft_size_widget));
else
count = gtk_spin_button_get_value(GTK_SPIN_BUTTON(priv->sample_count_widget));
return count;
}
void osc_plot_set_channel_state(OscPlot *plot, const char *dev, unsigned int channel, bool state)
{
OscPlotPrivate *priv;
struct iio_context *ctx;
struct iio_device *iio_dev;
struct iio_channel *iio_ch;
if (!plot || !dev)
return;
priv = plot->priv;
if (!priv)
return;
ctx = priv->ctx;
if (!ctx)
return;
if (gtk_toggle_tool_button_get_active((GtkToggleToolButton *)priv->capture_button))
return;
iio_dev = iio_context_find_device(ctx, dev);
if (!iio_dev || !is_input_device(iio_dev))
return;
if (channel >= iio_device_get_channels_count(iio_dev))
return;
iio_ch = iio_device_get_channel(iio_dev, channel);
if (!iio_ch)
return;
GtkTreeView *tree = GTK_TREE_VIEW(priv->channel_list_view);
GtkTreeModel *model = gtk_tree_view_get_model(tree);
GtkTreeIter ch_iter;
const char *ch;
ch = iio_channel_get_id(iio_ch);
get_iter_by_name(tree, &ch_iter, dev, ch);
set_channel_state_in_tree_model(model, &ch_iter, state);
check_valid_setup(plot);
}
void osc_plot_xcorr_revert (OscPlot *plot, int revert)
{
TrList *tr_list = plot->priv->transform_list;
Transform *transform;
int i;
for (i = 0; i < tr_list->size; i++) {
transform = tr_list->transforms[i];
XCORR_SETTINGS(transform)->revert_xcorr = revert;
}
}
void osc_plot_set_quit_callback(OscPlot *plot,
void (*qcallback)(void *user_data), void *user_data)
{
g_return_if_fail(plot);
g_return_if_fail(qcallback);
plot->priv->quit_callback = qcallback;
plot->priv->qcb_user_data = user_data;
}
int osc_plot_get_id(OscPlot *plot)
{
return plot->priv->object_id;
}
void osc_plot_set_id(OscPlot *plot, int id)
{
plot->priv->object_id = id;
}
void osc_plot_spect_mode(OscPlot *plot, bool enable)
{
OscPlotPrivate *priv = plot->priv;
GtkComboBox *cbox = GTK_COMBO_BOX(priv->plot_domain);
g_return_if_fail(plot);
if (enable) {
if (!comboboxtext_set_active_by_string(cbox, "Spectrum Mode"))
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(cbox),
"Spectrum Mode");
gtk_widget_hide(priv->capture_button);
gtk_widget_hide(priv->ss_button);
} else {
GtkTreeIter iter;
GtkTreeModel *model;
model = gtk_combo_box_get_model(cbox);
if (gtk_tree_model_get_iter_from_string(model, &iter,
"Spectrum Mode")) {
gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
}
gtk_widget_show(priv->capture_button);
gtk_widget_show(priv->ss_button);
}
}
void osc_plot_spect_set_start_f(OscPlot *plot, double freq_mhz)
{
g_return_if_fail(plot);
plot->priv->start_freq = freq_mhz;
}
void osc_plot_spect_set_len(OscPlot *plot, unsigned fft_count)
{
g_return_if_fail(plot);
plot->priv->fft_count = fft_count;
}
void osc_plot_spect_set_filter_bw(OscPlot *plot, double bw)
{
g_return_if_fail(plot);
plot->priv->filter_bw = bw;
}
static void osc_plot_dispose(GObject *object)
{
G_OBJECT_CLASS(osc_plot_parent_class)->dispose(object);
}
static void osc_plot_finalize(GObject *object)
{
G_OBJECT_CLASS(osc_plot_parent_class)->finalize(object);
}
/* Ref:
* A Family of Cosine-Sum Windows for High-Resolution Measurements
* Hans-Helge Albrecht
* Physikalisch-Technische Bendesanstalt
* Acoustics, Speech, and Signal Processing, 2001. Proceedings. (ICASSP '01).
* 2001 IEEE International Conference on (Volume:5 )
* pgs. 3081-3084
*
* While this doesn't use any of his code - I did find the coeffients that were nicely
* typed in by Joe Henning as part of his MATLAB Window Utilities
* (https://www.mathworks.com/matlabcentral/fileexchange/46092-window-utilities)
*
*/
static double window_function(gchar *win, int j, int n)
{
/* Strings need to match what is in glade */
if (!g_strcmp0(win, "Hanning")) {
double a = 2.0 * M_PI / (n - 1);
return 0.5 * (1.0 - cos(a * j));
} else if (!g_strcmp0(win, "Boxcar")) {
return 1.0;
} else if (!g_strcmp0(win, "Triangular")) {
double a = fabs(j - (n - 1)/ 2.0) / ((n - 1.0) / 2.0);
return 1.0 - a;
} else if (!g_strcmp0(win, "Welch")) {
double a = (j - (n - 1.0) / 2.0) / ((n - 1.0) / 2.0);
return 1.0 - (a * a);
} else if (!g_strcmp0(win, "Cosine")) {
double a = M_PI * j / (n - 1);
return sin(a);
} else if (!g_strcmp0(win, "Hamming")) {
double a0 = 0.5383553946707251, a1 = .4616446053292749;
return a0 - a1 * cos(j * 2.0 * M_PI / (n - 1));
} else if (!g_strcmp0(win, "Exact Blackman")) {
/* https://ieeexplore.ieee.org/document/940309 */
double a0 = 7938.0/18608.0, a1 = 9240.0/18608.0, a2 = 1430.0/18608.0;
double a = j * 2.0 * M_PI / (n - 1);
return a0 - a1 * cos(a) + a2 * cos(2.0 * a);
} else if (!g_strcmp0(win, "3 Term Cosine")) {
double a0 = 4.243800934609435e-1, a1 = 4.973406350967378e-1, a2 = 7.827927144231873e-2;
double a = j * 2.0 * M_PI / (n - 1);
return a0 - a1 * cos(a) + a2 * cos(2.0 * a);
} else if (!g_strcmp0(win, "4 Term Cosine")) {
double a0 = 3.635819267707608e-1, a1 = 4.891774371450171e-1, a2 = 1.365995139786921e-1,
a3 = 1.064112210553003e-2;
double a = j * 2.0 * M_PI / (n - 1);
return a0 - a1 * cos(a) + a2 * cos(2.0 * a) - a3 * cos(3.0 * a);
} else if (!g_strcmp0(win, "5 Term Cosine")) {
double a0 = 3.232153788877343e-1, a1 = 4.714921439576260e-1, a2 = 1.755341299601972e-1,
a3 = 2.849699010614994e-2, a4 = 1.261357088292677e-3;
double a = j * 2.0 * M_PI / (n - 1);
return a0 - a1 * cos(a) + a2 * cos(2.0 * a) - a3 * cos(3.0 * a) + a4 * cos(4.0 * a);
} else if (!g_strcmp0(win, "6 Term Cosine")) {
double a0 = 2.935578950102797e-1, a1 = 4.519357723474506e-1, a2 = 2.014164714263962e-1,
a3 = 4.792610922105837e-2, a4 = 5.026196426859393e-3, a5 = 1.375555679558877e-4;
double a = j * 2.0 * M_PI / (n - 1);
return a0 - a1 * cos(1.0 * a) + a2 * cos(2.0 * a) - a3 * cos(3.0 * a) + a4 * cos(4.0 * a) -
a5 * cos(5.0 * a);
} else if (!g_strcmp0(win, "7 Term Cosine")) {
double a0 = 2.712203605850388e-1, a1 = 4.334446123274422e-1, a2 = 2.180041228929303e-1,
a3 = 6.578534329560609e-2, a4 = 1.076186730534183e-2, a5 = 7.700127105808265e-4,
a6 = 1.368088305992921e-5;
double a = j * 2.0 * M_PI / (n - 1);
return a0 - a1 * cos(1.0 * a) + a2 * cos(2.0 * a) - a3 * cos(3.0 * a) + a4 * cos(4.0 * a) -
a5 * cos(5.0 * a) + a6 * cos(6.0 * a);
} else if (!g_strcmp0(win, "Blackman-Harris")) {
double a0 = 3.58750287312166e-1, a1 = 4.88290107472600e-1, a2 = 1.41279712970519e-1,
a3 = 1.16798922447150e-2;
double a = j * 2.0 * M_PI / (n - 1);
return a0 - a1 * cos(a) + a2 * cos(2.0 * a) - a3 * cos(3.0 * a);
} else if (!g_strcmp0(win, "Flat Top")) {
double a0 = 2.1557895e-1, a1 = 4.1663158e-1, a2 = 2.77263158e-1,
a3 = 8.3578947e-2, a4 = 6.947368e-3;
double a = j * 2.0 * M_PI / (n - 1);
return a0 - a1 * cos(a) + a2 * cos(2.0 * a) - a3 * cos(3.0 * a) + a4 * cos(4.0 * a);
}
printf("unknown window function\n");
return 0;
}
/* This equalized power, so full scale is always 0dBFS */
static double window_function_offset(gchar *win)
{
/* Strings need to match what is in glade */
if (!g_strcmp0(win, "Hanning")) {
return 1.77;
} else if (!g_strcmp0(win, "Boxcar")) {
return -4.25;
} else if (!g_strcmp0(win, "Triangular")) {
return 1.77;
} else if (!g_strcmp0(win, "Welch")) {
return -0.73;
} else if (!g_strcmp0(win, "Cosine")) {
return -0.33;
} else if (!g_strcmp0(win, "Hamming")) {
return 1.13;
} else if (!g_strcmp0(win, "Exact Blackman")) {
return 3.15;
} else if (!g_strcmp0(win, "3 Term Cosine")) {
return 3.19;
} else if (!g_strcmp0(win, "4 Term Cosine")) {
return 4.54;
} else if (!g_strcmp0(win, "5 Term Cosine")) {
return 5.56;
} else if (!g_strcmp0(win, "6 Term Cosine")) {
return 6.39;
} else if (!g_strcmp0(win, "7 Term Cosine")) {
return 7.08;
} else if (!g_strcmp0(win, "Blackman-Harris")) {
return 4.65;
} else if (!g_strcmp0(win, "Flat Top")) {
return 9.08;
}
printf("missed\n");
return 0;
}
static void do_fft(Transform *tr)
{