-
Notifications
You must be signed in to change notification settings - Fork 1
/
rbfilter.cpp
1309 lines (1047 loc) · 41 KB
/
rbfilter.cpp
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
/*! \mainpage FilterCalcs
Copyright (c) 1971 .. 2016 Roger Burghall
The original of this code was written in Fortran for the Digital
Equipment Corporation PDP-12, beginning in the early 1970s, and then
ported to the BBC Model B, and extended to provide more of the
functions described below. This port took place in the late 1980s. A second
port to Windows in the 1990's was abandoned in favour of a third port,
to a Linux PC, only begun in 2013.
No responsibility is accepted for the consequences of using this software,
and no guarantee is given for its correctness. It is necessary for the user
to understand what they are doing!
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
This program is intended to enable the user to design active or digital
filters, first in terms of T and q values and zero or one simple lag,
before carrying out circuit design to realise these stages as
either Sallen and Key or Rauch circuits, or as digital filters.
The intention is to permit the frequency and step responses to be
computed, the response to an arbitrary waveform (eventually; not implemented
in version 1.0), and to design Sallen and Key or Rauch circuits, or transform
the filter to the z-domain (i.e. to adapt the design for discrete implementation).
These functions were already implemented in the BBC Model B version of the suite.
Thanks are due to Graham Watts for help with the mathematics of impulse
and step response functions.
Versions 0.1 to 0.51 RB
Developing code to design filters, compute frequency and time responses etc.
Takes specification via control window; returns information on graphics window and terminal window.
Version 0.6 June 2016 RB
Added circuit diagram window and code to show and hide diagrams.
Version 0.7 August 2016 RB
Normalise the cut-off frequency of the prototype filter (0.71: actually works!)
Version 0.8 August 2016 RB
Added a slider at the top of the graphics window.
Version 1.0 September 2016 RB
First usable version of this port.
Version 1.01 August 2018 RB
Altered logic to hide unwanted combo-boxes when filter class, circuit type or response
is changed.
Version 2.01 March 2022 RB
Conversion to GTK3 and Cairo graphics.
Version 2.1 March 31st 2022 RB
Candidate for actual use (Beta?).
Version 2.11 April 27th RB
Corrected Chebyshev calculation
Removed "Atten" input.
*/
/*! \file rbfilter.cpp
rbfilter.cpp contains code to display windows to provide
a GUI for controlling the calculation of active filters
and in order to display results.
If porting to another OS this file is expected to need considerable modification.
It is intended that operating system dependant code should be kept in this file only.
*/
#include <gtk/gtk.h>
#include <math.h>
#include <cairo.h>
#include <complex.h>
#include <assert.h>
#include <iostream>
#include <X11/Xlib.h>
#include <string.h>
#include "Drawing.h"
#include "Calcs.h"
#define VERSION "2.2"
#if SHOW_FILE_COMPILING
#pragma message "Compiling Drawing.cpp"
// or #warning "Compiling Drawing.cpp"?
#endif
#define XSIZE 640
#define YSIZE 480
#define ZOOM_X 75.0
#define ZOOM_Y 75.0
#define HEIGHT 400
#define WIDTH 300
#pragma GCC diagnostic ignored "-Wwrite-strings"
#pragma GCC diagnostic ignored "-Wformat-security"
GtkWidget *window1; /// Window for controls.
GtkWidget *window2; /// Window for graphs.
static gboolean on_draw(GtkWidget *widget, cairo_t *cr, gpointer user_data);
cairo_t *cr;
static gdouble slider_position;
extern TFilter filter;
GtkWidget *app, /* *window, */ *cct_diagram;
GtkWidget *image;
double xyscale;
gchar str[256];
GtkWidget *Window1, *Area1;
GtkWidget *scale1;
GtkAdjustment *scale_adj1;
static GdkPixbuf *pixmap = NULL;
bool square = FALSE;
GdkColor TriColour;
// Combi boxes
GtkWidget *Ripple = NULL, *Bandwidth, *Type, *Type2, *Circuit, *Freq, *Atten, *SamplingFreq, *Order;
GtkWidget *LabelR, *LabelB, *LabelS;
// Buttons.
static GtkWidget *vbox1, *vbox2, *sp_button, *t_button, *f_button, *s_button /*, *z_button*/ ;
DrawMode Draw_mode;
/* Surface to store current scribbles */
static cairo_surface_t *surface = NULL;
class callback {
public:
static void clicked(GtkWidget *button, gpointer data);
static gint quit(GtkWidget *Widget, GdkEvent *event, gpointer data);
};
char *Dstring = "";
/**
Define a method to be called if a button is clicked.
Read all the combo-boxes and correct the filter object in
case calculations have already been done.
Currently we do not check for valid entries.
*/
void callback::clicked(GtkWidget *button, gpointer data)
{
void DrawMap(cairo_t *cr, gdouble, gdouble);
void Draw_poles(cairo_t *cr, gdouble x, gdouble y);
void DrawStep(cairo_t *cr, gdouble x, gdouble y);
void DrawBode(GtkWidget *area, cairo_t *cr, gdouble x, gdouble y);
// void on_draw(void);
// static gboolean on_draw(GtkWidget *widget, cairo_t *cr, gpointer user_data);
double scale(TFilter);
const gchar *text;
int i, w, h;
int n;
// cairo_t cr1;
Dstring = (char *)data;
// OK button clicked.
#if VERBOSE
g_print("<callback::clicked>\n");
g_print(Dstring);
#endif
/// Read 'Type'.
text = (gchar *)gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(Type));
if(strncmp(text, "L", 1) == 0) filter.fclass = Lowpass;
else if(strncmp(text, "H", 1) == 0) filter.fclass = Highpass;
else if(strncmp(text, "B", 1) == 0) filter.fclass = Bandpass;
/// Read 'Type2': shape of response.
text = (gchar *)gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(Type2));
if(strncmp(text, "Be", 2) == 0) filter.sclass = Bessel;
else if(strncmp(text, "Bu", 2) == 0) filter.sclass = Butterworth;
else if(strncmp(text, "C", 1) == 0) filter.sclass = Chebyshev;
/// Read realisation.
text = (gchar *)gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(Circuit));
if(strncmp(text, "S", 1) == 0) {
filter.cclass = SallKey;
gtk_widget_hide(LabelS);
}
else if(strncmp(text, "R", 1) == 0) {
filter.cclass = Rauch;
gtk_widget_hide(LabelS);
}
else if(strncmp(text, "D", 1) == 0) {
filter.cclass = ZDomain;
gtk_widget_show(LabelS);
}
/// Read frequency.
text = gtk_entry_get_text(GTK_ENTRY(Freq));
if(strlen(text)) n = sscanf(text, "%lf", &filter.frequency);
if((n == 0) || (filter.frequency <= 0.0)) {
filter.frequency = 1.0;
}
/// read (prototype) filter order.
text = gtk_entry_get_text(GTK_ENTRY(Order));
if(strlen(text)) sscanf(text, "%d", &filter.poles);
if((n == 0) || (filter.poles < 2) || ((filter.poles < 3) && (filter.fclass != Bandpass))) {
filter.poles = 3;
}
/// Read Ripple in dB.
text = gtk_entry_get_text(GTK_ENTRY(Ripple));
if(strlen(text)) sscanf(text, "%lf", &filter.ripple);
if((n == 0) || (filter.ripple < 0.0)) {
filter.ripple = 1.0;
}
/// Read Bandwidth.
text = gtk_entry_get_text(GTK_ENTRY(Bandwidth));
if(strlen(text)) sscanf(text, "%lf", &filter.bandwidth);
if((n == 0) || (filter.bandwidth <= 2)) {
filter.bandwidth = filter.frequency/2.0;
}
/// Read Sampling frequency; must be more than 2*frequency above.
text = gtk_entry_get_text(GTK_ENTRY(SamplingFreq));
if(strlen(text)) sscanf(text, "%lf", &filter.samplingfreq);
filter.Calculate( );
bode_calc(filter);
filter.step_calc( );
xyscale = scale(filter);
gtk_widget_show(Area1);
gtk_window_get_size((GtkWindow *)window2, &w, &h);
if(strncmp(Dstring, "Syn", 3) == 0) {
gtk_widget_hide(window2);
switch(filter.cclass) {
case Rauch: filter.gain = -1.0; filter.Synth_Rauch( );
switch(filter.fclass) {
case Lowpass: gtk_image_set_from_file ((GtkImage *)image, "./Circuit_diagrams/low_pass_MF.png"); break;
case Bandpass: gtk_image_set_from_file ((GtkImage *)image, "./Circuit_diagrams/band_pass_MF.png"); break;
case Highpass: gtk_image_set_from_file ((GtkImage *)image, "./Circuit_diagrams/high_pass_MF.png"); break;
}
gtk_widget_show(image);
gtk_widget_show(cct_diagram);
break;
case SallKey: filter.gain = 1.0; filter.Synth_SallKey( );
switch(filter.fclass) {
case Lowpass: gtk_image_set_from_file ((GtkImage *)image, "./Circuit_diagrams/low_pass_SK.png"); break;
case Bandpass: gtk_image_set_from_file ((GtkImage *)image, "./Circuit_diagrams/band_pass_SK.png"); break;
case Highpass: gtk_image_set_from_file ((GtkImage *)image, "./Circuit_diagrams/high_pass_SK.png"); break;
}
gtk_widget_show(image);
gtk_widget_show(cct_diagram);
break;
case ZDomain: filter.gain = 1.0; filter.bilinear( );
gtk_image_set_from_file ((GtkImage *)image, "./Circuit_diagrams/DirectForm1.png");
gtk_widget_show(image);
gtk_widget_show(cct_diagram);
break;
default: cout << "Unknown cclass!\n"; exit(-1);
}
cout << "\n";
for(i=0; i<filter.poles/2; i++) {
cout << "Stage " << i << ": T = " << filter.st[i].T << "; q = " << filter.st[i].q << "\n";
}
if(filter.poles % 2) cout << "Simple lag: " << "Tau = " << filter.tau << "\n";
} else gtk_widget_show(window2);
gtk_adjustment_set_value (GTK_ADJUSTMENT(scale_adj1), 0.0);
gtk_widget_queue_draw(window2);
}
/// Callback function warns if certain comboboxes are changed.
static gboolean combo_changed(GtkWidget *area, cairo_t *cr, gpointer user_data)
{
gchar *text;
#if VERBOSE
cout << "combo_changed\n";
#endif
/// Circuit type - is it Digital?
text = (gchar *)gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(Circuit));
if(strncmp(text, "D", 1) == 0) {
gtk_widget_show(LabelS);
gtk_widget_show(SamplingFreq);
} else {
gtk_widget_hide(LabelS);
gtk_widget_hide(SamplingFreq);
}
/// Filter type - is it Bandpass?
text = (gchar *)gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(Type));
if(strncmp(text, "B", 1) == 0) {
gtk_widget_show(LabelB);
gtk_widget_show(Bandwidth);
} else {
gtk_widget_hide(LabelB);
gtk_widget_hide(Bandwidth);
}
/// Filter response - is it Chebyshev?
text = (gchar *)gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(Type2));
if(strncmp(text, "C", 1) == 0) {
gtk_widget_show(LabelR);
gtk_widget_show(Ripple);
} else {
gtk_widget_hide(LabelR);
gtk_widget_hide(Ripple);
}
gtk_widget_hide(Area1);
gtk_widget_hide(cct_diagram);
gtk_widget_queue_draw(window1);
return(true);
}
//gboolean slider_changed(GtkWidget *adj, cairo_t *cr, gpointer user_data)
gdouble slider_changed(GtkAdjustment *adj)
{
slider_position = gtk_adjustment_get_value((GtkAdjustment *)scale_adj1);
gtk_widget_queue_draw(window2);
return slider_position;
}
static gboolean on_draw(GtkWidget *area, cairo_t *cr, gpointer user_data)
//void on_draw(void)
{
void Draw_poles(cairo_t *, gdouble, gdouble);
void DrawStep(cairo_t *, gdouble, gdouble);
void DrawBode(GtkWidget *area, cairo_t *, gdouble, gdouble);
#if VERBOSE
cout << "cr = " << (long)cr << "\n";
g_print("<On_draw>\n");
#endif
gtk_widget_show_all(window2);
if(strncmp(Dstring, "S-p", 2) == 0) {
gtk_widget_hide(scale1);
Draw_mode = Splane;
Draw_poles(cr, 250.0, 250.0);
}
if(strncmp(Dstring, "T-d", 2) == 0) {
// gtk_adjustment_set_value (GTK_ADJUSTMENT(scale_adj1), 0.0);
gtk_widget_show(scale1);
Draw_mode = Step;
DrawStep(cr, 250, 250);
}
if(strncmp(Dstring, "F-d", 2) == 0) {
// gtk_adjustment_set_value (GTK_ADJUSTMENT(scale_adj1), (double)FSTEPS/2.0);
gtk_widget_show(scale1);
Draw_mode = Bode;
DrawBode(area, cr, 250, 250);
}
#if VERBOSE
g_print("</On_draw>\n");
#endif
return(TRUE);
}
/// Define a method to be called if the close icon is clicked.
gint callback::quit(GtkWidget *Widget, GdkEvent *event, gpointer data)
{
// g_print("'Quit' callback\n");
gtk_main_quit( );
return FALSE;
}
/// I would like to make the application ignore the second window 'quit' event, but since I can't I'll quit the application.
gint pseudo_quit(GtkWidget *Widget, GdkEvent *event, gpointer data)
{
g_print("Second window 'Quit' callback\n");
gtk_main_quit( );
return FALSE;
}
/// Draw the Bode diagram.
void DrawBode(GtkWidget *area, cairo_t *cr, gdouble x, gdouble y)
{
extern double n_to_freq(int n, double f1, int nmax=FSTEPS, int decades=2);
char str[80], string1[80];
gdouble ftest, gain;
gint i;
gdouble xx1, yy1, xx2, yy2, xx, yy;
gdouble width, height;
double a, f;
GdkRectangle da; /* GtkDrawingArea size */
gdouble dx = 5.0, dy = 5.0; /* Pixels between each point */
gdouble clip_x1 = 0.0, clip_y1 = 0.0, clip_x2 = 0.0, clip_y2 = 0.0;
gdouble left, right, top, bottom;
GdkWindow *window = gtk_widget_get_window(Area1);
assert(window != NULL);
gtk_window_set_title (GTK_WINDOW(window2), (gchar *)"Bode diagram");
#if VERBOSE
cout << "DrawBode: cr = " << (long)cr << "\n";
cout << "Size request\n";
#endif
gtk_widget_set_size_request(window2, 750, 750);
/* Determine GtkDrawingArea dimensions. x, y is the position relative to the parent window. Width and height are obvious. */
gdk_window_get_geometry((GdkWindow*)window, &da.x, &da.y, &da.width, &da.height);
#if VERBOSE
cout << "Set line width\n";
#endif
cairo_set_line_width(cr, 0.02);
#if VERBOSE
cout << "set source rgb\n";
#endif
/* Draw on a black background */
cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
cairo_paint(cr);
#if VERBOSE
cout << "stroke\n";
#endif
cairo_stroke(cr);
#if VERBOSE
cout << "translate\n";
#endif
/* Change the transformation matrix */
/* translate points in user space to the device space. */
// cairo_translate(cr, 7*da.width / 8, da.height / 2);
cairo_translate(cr, 1, 1);
/* Scales the user space into the device space. */
#if VERBOSE
cout << "scale\n";
#endif
cairo_scale(cr, ZOOM_X, ZOOM_Y);
/* Determine the data points to calculate (ie. those in the clipping zone) */
// cairo_device_to_user_distance(cr, &dx, &dy);
#if VERBOSE
cout << "extents\n";
#endif
cairo_clip_extents(cr, &clip_x1, &clip_y1, &clip_x2, &clip_y2);
#if VERBOSE
g_print("<DrawBode>\n");
#endif
// Let's pick colour blue for the foreground.
cairo_set_source_rgb(cr, 0.5, 0.5, 1.0);
#if VERBOSE
cout << "select font\n";
#endif
cairo_select_font_face(cr, "NimbusSans-Regular", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
#if VERBOSE
cout << "set font size\n";
#endif
cairo_set_font_size(cr, 0.075);
width = clip_x2 - clip_x1;
left = clip_x1 + width / 100.0;
width = clip_x2 - left;
height = clip_y1 - clip_y2;
yy = (2.0 * clip_y1 + clip_y2) / 3.0;
#if VERBOSE
cout << "draw lines\n";
#endif
#if 0
// Diagonal line test.
cairo_move_to(cr, left, clip_y1);
cairo_line_to(cr, clip_x2, clip_y2);
#endif
cairo_move_to(cr, left, clip_y1);
cairo_line_to(cr, left, clip_y2);
cairo_move_to(cr, left, yy);
cairo_line_to(cr, clip_x2, yy);
cairo_stroke(cr);
/// Draw the frequency response curve.
for(i=1; i<FSTEPS; i++) {
xx1 = left + (i-1) * width / FSTEPS;
yy1 = yy + 0.5*log10(filter.freq_resp[i-1]) * 0.6*height;
xx2 = left + i * width / FSTEPS;
yy2 = yy + 0.5*log10(filter.freq_resp[i]) * 0.6*height;
if(i == 1) cairo_move_to(cr, xx1, yy1);
cairo_line_to(cr, xx2, yy2);
}
cairo_stroke(cr);
cairo_select_font_face(cr, "NimbusSans-Regular", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
cairo_set_font_size(cr, 0.2);
cairo_move_to(cr, left, 0.9*clip_y1+0.1*clip_y2);
ftest = filter.fmax * slider_position / 1000.0;
gain = filter.freq_resp[(int)slider_position];
strcpy(string1, "No data");
sprintf(string1, "%0.3f Hz: gain = %0.3f", ftest, gain);
// sprintf(string1, "%0.2f: ", slider_position);
cairo_set_source_rgb(cr, 0.9, 0.9, 1.0);
cairo_show_text(cr, string1);
cairo_stroke(cr);
/// Draw frequency marker against bode curve.
xx1 = left + width * slider_position / 1000.0;
yy1 = yy + 0.5*log10(filter.freq_resp[(int)slider_position]) * 0.6*height;
yy2 = yy1 - 0.05 * height;
cairo_move_to(cr, xx1-0.04*width, yy1);
cairo_line_to(cr, xx1+0.04*width, yy1);
yy1 += 0.05 * height;
cairo_move_to(cr, xx1, yy1);
cairo_line_to(cr, xx1, yy2);
cairo_stroke(cr);
#if VERBOSE
g_print("</DrawBode>\n");
#endif
}
/// Draw the s-plane diagram.
void Draw_poles(cairo_t *cr, gdouble x, gdouble y)
{
GdkRectangle da; /* GtkDrawingArea size */
gdouble dx = 5.0, dy = 5.0; /* Pixels between each point */
gdouble clip_x1 = 0.0, clip_y1 = 0.0, clip_x2 = 0.0, clip_y2 = 0.0;
gdouble left, right, top, bottom;
gint i;
gchar string1[64];
gdouble xx1, yy1, xx2, yy2, xx, yy;
gdouble width, height, xyscale;
GdkWindow *window = gtk_widget_get_window(Area1);
gdouble scale(TFilter);
assert(window != NULL);
gtk_window_set_title (GTK_WINDOW(window2), (gchar *)"S-plane");
#if VERBOSE
cout << "DrawBode: cr = " << (long)cr << "\n";
cout << "Size request\n";
#endif
gtk_widget_set_size_request(window2, 400, 750);
/* Determine GtkDrawingArea dimensions. x, y is the position relative to the parent window. Width and height are obvious. */
gdk_window_get_geometry((GdkWindow*)window, &da.x, &da.y, &da.width, &da.height);
#if VERBOSE
cout << "Set line width\n";
#endif
cairo_set_line_width(cr, 0.02);
#if VERBOSE
cout << "set source rgb\n";
#endif
/* Draw on a blue background */
cairo_set_source_rgb(cr, 0.5, 0.5, 0.8);
cairo_paint(cr);
#if VERBOSE
cout << "stroke\n";
#endif
cairo_stroke(cr);
#if VERBOSE
cout << "translate\n";
#endif
/* translate points in user space to the device space. */
cairo_translate(cr, 1, 1);
/* Scales the user space into the device space. */
#if VERBOSE
cout << "scale\n";
#endif
cairo_scale(cr, ZOOM_X, ZOOM_Y);
#if VERBOSE
cout << "extents\n";
#endif
cairo_clip_extents(cr, &clip_x1, &clip_y1, &clip_x2, &clip_y2);
#if VERBOSE
g_print("<Draw_poles>\n");
#endif
// Let's pick colour blue for the foreground.
cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
#if VERBOSE
cout << "select font\n";
#endif
cairo_select_font_face(cr, "NimbusSans-Regular", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
#if VERBOSE
cout << "set font size\n";
#endif
cairo_set_font_size(cr, 0.25);
width = clip_x2 - clip_x1;
left = clip_x1 + width / 100.0;
width = clip_x2 - left;
height = clip_y2 - clip_y1;
yy = (2.0 * clip_y1 + clip_y2) / 3.0;
cairo_move_to(cr, left+0.9*width, clip_y1);
cairo_line_to(cr, left+0.9*width, clip_y2);
cairo_move_to(cr, left, clip_y1+0.5*height);
cairo_line_to(cr, left+width, clip_y1+0.5*height);
cairo_stroke(cr);
xyscale = scale(filter);
for(i=0; i<filter.poles; i++) {
xx = filter.st[i].pole.real() * 0.5 * width / xyscale + left+0.9*width;
yy = filter.st[i].pole.imag() * 0.5*height / xyscale + 0.5*height;
cairo_move_to(cr, xx-0.025*width, yy);
cairo_line_to(cr, xx+0.025*width, yy);
cairo_move_to(cr, xx, yy-0.025*height);
cairo_line_to(cr, xx, yy+0.025*height);
cairo_move_to(cr, xx-0.25*width, yy);
sprintf(string1, "%0.2f + i*%0.2f ", filter.st[i].pole.real(), filter.st[i].pole.imag());
cairo_show_text(cr, string1);
cairo_move_to(cr, left+0.025*width, (i+1) * height / 20.0);
if(i < filter.poles/2) {
if(filter.st[i].T < 0.001) sprintf(string1, "T = %0.6f q = %0.2f ", filter.st[i].T, filter.st[i].q);
else if(filter.st[i].T < 0.01) sprintf(string1, "T = %0.5f q = %0.2f ", filter.st[i].T, filter.st[i].q);
else sprintf(string1, "T = %0.4f q = %0.2f ", filter.st[i].T, filter.st[i].q);
cairo_show_text(cr, string1);
}
}
if(filter.poles % 2) {
cairo_move_to(cr, left+0.025*width, (filter.poles / 2 + 1) * height / 20.0);
if(filter.tau < 0.001) sprintf(string1, "Tau = %0.6f ", -filter.tau);
else if(filter.tau < 0.01) sprintf(string1, "Tau = %0.5f ", -filter.tau);
else sprintf(string1, "Tau = %0.4f ", -filter.tau);
cairo_show_text(cr, string1);
}
cairo_stroke(cr);
if(filter.zeroes) {
// Show the zeroes.
cairo_move_to(cr, left+0.8*width, clip_y1+0.5*height-0.025*width);
sprintf(string1, "%d zeroes", filter.zeroes);
cairo_show_text(cr, string1);
// Draw the zero as a circle.
cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
cairo_move_to(cr, left+0.9*width+0.025*width, clip_y1+0.5*height);
cairo_arc(cr, left+0.9*width, clip_y1+0.5*height, 0.025*width, 0.0, 2.0*M_PI);
}
cairo_stroke(cr);
}
/// Draw the filter's step response.
void DrawStep(cairo_t *cr, gdouble x, gdouble y)
{
char str[80], string1[80];
gint i, w, h;
gdouble yy, xx1, yy1, xx2, yy2;
gdouble width, height;
gdouble t, v;
GdkRectangle da; /* GtkDrawingArea size */
gdouble dx = 5.0, dy = 5.0; /* Pixels between each point */
gdouble clip_x1 = 0.0, clip_y1 = 0.0, clip_x2 = 0.0, clip_y2 = 0.0;
gdouble left, right, top, bottom;
GdkWindow *window = gtk_widget_get_window(Area1);
assert(window != NULL);
gtk_window_set_title (GTK_WINDOW(window2), (gchar *)"Step response");
#if VERBOSE
cout << "DrawStep: cr = " << (long)cr << "\n";
cout << "Size request\n";
#endif
gtk_widget_set_size_request(window2, 750, 750);
/* Determine GtkDrawingArea dimensions. x, y is the position relative to the parent window. Width and height are obvious. */
gdk_window_get_geometry((GdkWindow*)window, &da.x, &da.y, &da.width, &da.height);
#if VERBOSE
cout << "Set line width\n";
#endif
cairo_set_line_width(cr, 0.02);
#if VERBOSE
cout << "set source rgb\n";
#endif
/* Draw on a black background */
cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
cairo_paint(cr);
#if VERBOSE
cout << "stroke\n";
#endif
cairo_stroke(cr);
#if VERBOSE
cout << "translate\n";
#endif
/* Change the transformation matrix */
/* translate points in user space to the device space. */
cairo_translate(cr, 1, 1);
/* Scales the user space into the device space. */
#if VERBOSE
cout << "scale\n";
#endif
cairo_scale(cr, ZOOM_X, ZOOM_Y);
/* Determine the data points to calculate (ie. those in the clipping zone) */
// cairo_device_to_user_distance(cr, &dx, &dy);
#if VERBOSE
cout << "extents\n";
#endif
cairo_clip_extents(cr, &clip_x1, &clip_y1, &clip_x2, &clip_y2);
#if VERBOSE
g_print("<DrawBode>\n");
#endif
// Let's pick colour blue for the foreground.
cairo_set_source_rgb(cr, 0.5, 0.5, 1.0);
#if VERBOSE
cout << "select font\n";
#endif
cairo_select_font_face(cr, "NimbusSans-Regular", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
#if VERBOSE
cout << "set font size\n";
#endif
cairo_set_font_size(cr, 0.075);
width = clip_x2 - clip_x1;
height = clip_y2 - clip_y1;
left = clip_x1 + width / 100.0;
yy = (2.0 * clip_y1 + clip_y2) / 3.0;
#if VERBOSE
cout << "draw axes\n";
#endif
#if 0
// Diagonal line test.
cairo_move_to(cr, left, clip_y1);
cairo_line_to(cr, clip_x2, clip_y2);
#endif
/// Draw suitable axes.
cairo_move_to(cr, left, clip_y1);
cairo_line_to(cr, left, clip_y2); // Vertical axis.
switch(filter.fclass) {
case Lowpass:
yy = (0.2 * clip_y1 + 0.8 * clip_y2);
break;
case Bandpass:
yy = (0.5 * clip_y1 + 0.5 * clip_y2);
break;
case Highpass:
yy = (0.4 * clip_y1 + 0.6 * clip_y2);
break;
}
cairo_move_to(cr, left, yy);
cairo_line_to(cr, clip_x2, yy); // Horizontal axis.
cairo_stroke(cr);
filter.step_calc( );
/// Draw the step response curve.
for(i=1; i<TSTEPS; i++) {
xx1 = left + (i-1) * width / TSTEPS;
xx2 = left + i * width / TSTEPS;
switch(filter.fclass) {
case Lowpass:
yy1 = (0.2 * clip_y1 + 0.8 * clip_y2) - filter.step_resp[i-1] * 0.6*height;
yy2 = (0.2 * clip_y1 + 0.8 * clip_y2) - filter.step_resp[i] * 0.6*height;
break;
case Bandpass:
yy1 = (0.5 * clip_y1 + 0.5 * clip_y2) - filter.step_resp[i-1] * 0.4*height;
yy2 = (0.5 * clip_y1 + 0.5 * clip_y2) - filter.step_resp[i] * 0.4*height;
break;
case Highpass:
yy1 = (0.4 * clip_y1 + 0.6 * clip_y2) - filter.step_resp[i-1] * 0.5*height;
yy2 = (0.4 * clip_y1 + 0.6 * clip_y2) - filter.step_resp[i] * 0.5*height;
default: break;
}
if(i == 1) cairo_move_to(cr, xx1, yy1);
cairo_line_to(cr, xx2, yy2);
}
cairo_stroke(cr);
/// Draw frequency marker against bode curve.
t = filter.tmax * slider_position / TSTEPS;
switch(filter.fclass) {
case Lowpass:
yy1 = (0.2 * clip_y1 + 0.8 * clip_y2) - filter.step_resp[(int)slider_position] * 0.6*height;
break;
case Bandpass:
yy1 = (0.5 * clip_y1 + 0.5 * clip_y2) - filter.step_resp[(int)slider_position] * 0.4*height;
break;
case Highpass:
yy1 = (0.4 * clip_y1 + 0.6 * clip_y2) - filter.step_resp[(int)slider_position] * 0.5*height;
default: break;
}
xx1 = left + width * slider_position / 1000.0;
cairo_move_to(cr, xx1-0.04*width, yy1);
cairo_line_to(cr, xx1+0.04*width, yy1);
cairo_move_to(cr, xx1, yy1-0.05*height);
cairo_line_to(cr, xx1, yy1+0.05*height);
cairo_stroke(cr);
cairo_select_font_face(cr, "NimbusSans-Regular", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
cairo_set_font_size(cr, 0.2);
cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
cairo_move_to(cr, left, 0.9*clip_y1+0.1*clip_y2);
strcpy(string1, "No data");
sprintf(string1, "t=%0.3f: f(t)=%0.3f", slider_position * filter.tmax / 1000.0, filter.step_resp[(int)slider_position]);
cairo_show_text(cr, string1);
cairo_stroke(cr);
#if 0
/// List the stage Ts and qs etc.
if(filter.type != '0') for(i=0; i<filter.poles; i+=2) {
if(i == filter.poles - 1) sprintf(str, "Tau = %1.3lf ", filter.st[i/2].T);
else sprintf(str, "T = %1.3lf, q = %1.3lf", filter.st[i/2].T, filter.st[i/2].q);
gdk_draw_string(widget->window, Font1, widget->style->black_gc, w / 10, 20 + i * 15, str);
}
#if VERBOSE
g_print("</DrawStep>\n");
#endif
#endif
}
/** Draw a rectangle on the screen, and add the axes, poles and
the Ts and qs. */
void
DrawMap(cairo_t *cr, gdouble x, gdouble y)
{
#if 0
#if VERBOSE
g_print("<DrawMap>\n");
#endif
switch(Draw_mode) {
case Splane: Draw_poles(widget, 250, 250);
break;
case Step: DrawStep(widget, 250, 250);
break;
case Bode: DrawBode(widget, 250, 250);
break;
default: cout << "\n\n***UNKNOWN DRAW MODE!***\n\n";
break;
}
#if VERBOSE
g_print("</DrawMap>\n");
#endif
#endif
}
/** Define a function to be called if the window is re-exposed. */
static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event)
{
return(TRUE);
}
/** If a button is pressed, do what? */
static gboolean
button_press_event( GtkWidget *widget, GdkEventButton *event )
{
return TRUE;
}
/** Redraw the diagram if pointer moves. */
static gboolean
motion_notify_event( GtkWidget *widget, GdkEventMotion *event )
{
return TRUE;
}
/*!
'configure_event( )' redraws the window during resizing.
*/
static gboolean
configure_event( GtkWidget *widget, GdkEventConfigure *event )
{
return TRUE;
}
bool Check_band(void)
{
return(TRUE);
}
/*!
'Type_event' call-back function to process changed data in filter class combo-box.
*/
void Type_event(GtkComboBox *widget, gpointer data)
{
gtk_widget_hide(cct_diagram);
}
/*!
'Type2_event' call-back function to process changed data in filter class combo-box.
*/
void Type2_event(GtkComboBox *widget, gpointer data)
{
}
/*!
'Circuit_event' call-back function to process changed data in filter class combo-box.
*/
void Circuit_event(GtkComboBox *widget, gpointer data)
{
}
/*!
'Freq_callback' call-back function to process changed data in frequency combo-box.
*/
void Freq_callback(GtkWidget *widget, GtkWidget *textbox)
{
#if VERBOSE
puts("Freq_callback\n");
#endif
}
/*!
'Atten_callback' call-back function to process changed data in attenuation combo-box.
*/
void Atten_callback(GtkWidget *widget, GtkWidget *textbox)
{
}
/*!
'SFreq_callback' call-back function to process changed data in frequency combo-box.
*/
void SFreq_callback(GtkWidget *widget, GtkWidget *textbox)
{
}
/*!
'q_callback' call-back function to process changed data in filter 'q' combo-box.
*/
void q_callback(GtkWidget *widget, GtkWidget *textbox)
{
}
/*!