1 // Log plot demo.
2 //
3
4 #include "plcdemos.h"
5
6 void plot1( int type );
7
8 //--------------------------------------------------------------------------
9 // main
10 //
11 // Illustration of logarithmic axes, and redefinition of window.
12 //--------------------------------------------------------------------------
13
14 int
main(int argc,char * argv[])15 main( int argc, char *argv[] )
16 {
17 // Parse and process command line arguments
18
19 (void) plparseopts( &argc, argv, PL_PARSE_FULL );
20
21 // Initialize plplot
22
23 plinit();
24 plfont( 2 );
25
26 // Make log plots using two different styles.
27
28 plot1( 0 );
29 plot1( 1 );
30
31 plend();
32 exit( 0 );
33 }
34
35 //--------------------------------------------------------------------------
36 // plot1
37 //
38 // Log-linear plot.
39 //--------------------------------------------------------------------------
40
41 void
plot1(int type)42 plot1( int type )
43 {
44 int i;
45 static PLFLT freql[101], ampl[101], phase[101];
46 PLFLT f0, freq;
47 PLINT nlegend;
48 PLCHAR_VECTOR text[2], symbols[2];
49 PLINT opt_array[2];
50 PLINT text_colors[2];
51 PLINT line_colors[2];
52 PLINT line_styles[2];
53 PLFLT line_widths[2];
54 PLINT symbol_numbers[2], symbol_colors[2];
55 PLFLT symbol_scales[2];
56 PLFLT legend_width, legend_height;
57
58 pladv( 0 );
59
60 // Set up data for log plot
61
62 f0 = 1.0;
63 for ( i = 0; i <= 100; i++ )
64 {
65 freql[i] = -2.0 + i / 20.0;
66 freq = pow( 10.0, freql[i] );
67 ampl[i] = 20.0 * log10( 1.0 / sqrt( 1.0 + pow( ( freq / f0 ), 2. ) ) );
68 phase[i] = -( 180.0 / M_PI ) * atan( freq / f0 );
69 }
70
71 plvpor( 0.15, 0.85, 0.1, 0.9 );
72 plwind( -2.0, 3.0, -80.0, 0.0 );
73
74 // Try different axis and labelling styles.
75
76 plcol0( 1 );
77 switch ( type )
78 {
79 case 0:
80 plbox( "bclnst", 0.0, 0, "bnstv", 0.0, 0 );
81 break;
82 case 1:
83 plbox( "bcfghlnst", 0.0, 0, "bcghnstv", 0.0, 0 );
84 break;
85 }
86
87 // Plot ampl vs freq
88
89 plcol0( 2 );
90 plline( 101, freql, ampl );
91 plcol0( 2 );
92 plptex( 1.6, -30.0, 1.0, -20.0, 0.5, "-20 dB/decade" );
93
94 // Put labels on
95
96 plcol0( 1 );
97 plmtex( "b", 3.2, 0.5, 0.5, "Frequency" );
98 plmtex( "t", 2.0, 0.5, 0.5, "Single Pole Low-Pass Filter" );
99 plcol0( 2 );
100 plmtex( "l", 5.0, 0.5, 0.5, "Amplitude (dB)" );
101 nlegend = 1;
102
103 // For the gridless case, put phase vs freq on same plot
104
105 if ( type == 0 )
106 {
107 plcol0( 1 );
108 plwind( -2.0, 3.0, -100.0, 0.0 );
109 plbox( "", 0.0, 0, "cmstv", 30.0, 3 );
110 plcol0( 3 );
111 plline( 101, freql, phase );
112 plstring( 101, freql, phase, "#(728)" );
113 plcol0( 3 );
114 plmtex( "r", 5.0, 0.5, 0.5, "Phase shift (degrees)" );
115 nlegend = 2;
116 }
117 // Draw a legend
118 // First legend entry.
119 opt_array[0] = PL_LEGEND_LINE;
120 text_colors[0] = 2;
121 text[0] = "Amplitude";
122 line_colors[0] = 2;
123 line_styles[0] = 1;
124 line_widths[0] = 1.;
125 // note from the above opt_array the first symbol (and box) indices
126 // do not have to be specified, at least in C. For Fortran we need
127 // to set the symbols to be something, since the string is always
128 // copied as part of the bindings.
129 symbols[0] = "";
130
131 // Second legend entry.
132 opt_array[1] = PL_LEGEND_LINE | PL_LEGEND_SYMBOL;
133 text_colors[1] = 3;
134 text[1] = "Phase shift";
135 line_colors[1] = 3;
136 line_styles[1] = 1;
137 line_widths[1] = 1.;
138 symbol_colors[1] = 3;
139 symbol_scales[1] = 1.;
140 symbol_numbers[1] = 4;
141 symbols[1] = "#(728)";
142 // from the above opt_arrays we can completely ignore everything
143 // to do with boxes.
144
145 plscol0a( 15, 32, 32, 32, 0.70 );
146 pllegend( &legend_width, &legend_height,
147 PL_LEGEND_BACKGROUND | PL_LEGEND_BOUNDING_BOX, 0,
148 0.0, 0.0, 0.1, 15,
149 1, 1, 0, 0,
150 nlegend, opt_array,
151 1.0, 1.0, 2.0,
152 1., text_colors, (const char **) text,
153 NULL, NULL, NULL, NULL,
154 line_colors, line_styles, line_widths,
155 symbol_colors, symbol_scales, symbol_numbers, (const char **) symbols );
156 }
157