Image obtained by using color PostScript driver (psc) and then converted to animated gif using ImageMagick convert tool.
1 2 #pragma package "../" 3 #include <chplplot.h> 4 #include <math.h> 5 #define PI M_PI 6 7 void plot1(int type); 8 9 10 int 11 main(int argc, char *argv[]) 12 { 13 14 (void) plParseOpts(&argc, argv, PL_PARSE_FULL); 15 16 17 plinit(); 18 plfont(2); 19 20 21 plot1(0); 22 plot1(1); 23 24 plend(); 25 exit(0); 26 } 27 28 29 void 30 plot1(int type) 31 { 32 int i; 33 static PLFLT freql[101], ampl[101], phase[101]; 34 PLFLT f0, freq; 35 36 pladv(0); 37 38 39 f0 = 1.0; 40 for (i = 0; i <= 100; i++) { 41 freql[i] = -2.0 + i / 20.0; 42 freq = pow(10.0, freql[i]); 43 ampl[i] = 20.0 * log10(1.0 / sqrt(1.0 + pow((freq / f0), 2.))); 44 phase[i] = -(180.0 / PI) * atan(freq / f0); 45 } 46 47 plvpor(0.15, 0.85, 0.1, 0.9); 48 plwind(-2.0, 3.0, -80.0, 0.0); 49 50 51 plcol0(1); 52 switch (type) { 53 case 0: 54 plbox("bclnst", 0.0, 0, "bnstv", 0.0, 0); 55 break; 56 case 1: 57 plbox("bcfghlnst", 0.0, 0, "bcghnstv", 0.0, 0); 58 break; 59 } 60 61 62 plcol0(2); 63 plline(101, freql, ampl); 64 plcol0(1); 65 plptex(1.6, -30.0, 1.0, -20.0, 0.5, "-20 dB/decade"); 66 67 68 plcol0(1); 69 plmtex("b", 3.2, 0.5, 0.5, "Frequency"); 70 plmtex("t", 2.0, 0.5, 0.5, "Single Pole Low-Pass Filter"); 71 plcol0(2); 72 plmtex("l", 5.0, 0.5, 0.5, "Amplitude (dB)"); 73 74 75 if (type == 0) { 76 plcol0(1); 77 plwind(-2.0, 3.0, -100.0, 0.0); 78 plbox("", 0.0, 0, "cmstv", 30.0, 3); 79 plcol0(3); 80 plline(101, freql, phase); 81 plcol0(3); 82 plmtex("r", 5.0, 0.5, 0.5, "Phase shift (degrees)"); 83 } 84 }
Code description
- 1
- $Id: x04c.c,v 1.17 2002/12/03 08:39:24 airwin Exp $ Log plot demo.
- 9
- --------------------------------------------------------------------------*\ * main * * Illustration of logarithmic axes, and redefinition of window. \*--------------------------------------------------------------------------
- 13
- Parse and process command line arguments
- 16
- Initialize plplot
- 20
- Make log plots using two different styles.
- 28
- --------------------------------------------------------------------------*\ * plot1 * * Log-linear plot. \*--------------------------------------------------------------------------
- 38
- Set up data for log plot
- 50
- Try different axis and labelling styles.
- 61
- Plot ampl vs freq
- 67
- Put labels on
- 74
- For the gridless case, put phase vs freq on same plot