Debian Codegraph @

texmf-dist/doc/latex/circuit-macros/examples/dpv/randomDPV.m4

raw
1 .PS
2 # randomDPV.m4
3 gen_init(svg_font(sans-serif,9bp__))
4 NeedDpicTools
5
6 ################################################################
7 # hscale(right length,nlabels,
8 # minlabel,maxlabel,[T][N])
9 # arg5=T: top axis, N: nolabels
10 define(`hscale',`[ define(`hs_nl',`ifelse(`$2',,5,`$2')')
11 A: line `$1' ; Start: A.start; End: A.end
12 td = 1/(hs_nl-1)*A.len/5
13 define(`hs_ud',`ifinstr(`$5',T,down,up)')
14 # Major tics
15 for i=1 to hs_nl-2 do { line hs_ud graphsize/20 thick 0.4 \
16 from i/(hs_nl-1) between A.start and A.end }
17 # Minor tics
18 for i=0 to hs_nl-2 do { Ts: i/(hs_nl-1) between A.start and A.end
19 for j=1 to 4 do { line thick 0.4 hs_ud graphsize/20/2 from Ts+(j*td,0) } }
20 # Labels
21 ifinstr(`$5',N,,`for_(0,eval(hs_nl-1),1,
22 `Ts: m4x/(hs_nl-1) between A.start and A.end
23 s_box("%g",`$3'+m4x/(hs_nl-1)*(`$4'-(`$3'))) \
24 at Ts ifelse(`$5',T,above,below)') ')
25 ]')
26 # vscale(up length,nlabels,
27 # minlabel,maxlabel,[R][N] )
28 # arg5=R: right axis, N: nolabels
29 define(`vscale',`[ define(`vs_nl',`ifelse(`$2',,5,`$2')')
30 A: line `$1' ; Start: A.start; End: A.end
31 td = 1/(vs_nl-1)*A.len/5
32 define(`vs_lr',`ifinstr(`$5',R,left,right)')
33 # Major tics
34 for i=1 to vs_nl-2 do { line vs_lr graphsize/20 thick 0.4 \
35 from i/(vs_nl-1) between A.start and A.end }
36 # Minor tics
37 for i=0 to vs_nl-2 do { Ts: i/(vs_nl-1) between A.start and A.end
38 for j=1 to 4 do { line thick 0.4 vs_lr graphsize/20/2 from Ts+(0,j*td) } }
39 # Labels
40 ifinstr(`$5',N,,`for_(0,eval(vs_nl-1),1,
41 `Ts: m4x/(vs_nl-1) between A.start and A.end
42 s_box("%g",`$3'+m4x/(vs_nl-1)*(`$4'-(`$3'))) \
43 at Ts ifelse(`$5',R,ljust,rjust)')')
44 ]')
45 # f_gauss(x,mean,stddev)
46 define f_gauss`{ std_gauss(($1-($2))/($3))/($3) }'
47 define std_gauss`{ expe(-($1)^2/2)/sqrt(twopi_) }'
48 ################################################################
49
50 [
51 n = 2000 # number of samples
52 stddev = 0.25
53 graphsize = 2
54
55 B: box wid graphsize ht graphsize ; { move left 0.4 from B.w }
56 # Gemerate n Gaussian random numbers
57 randn(z,n,0,stddev)
58 m = 0
59 for i=1 to n-1 by 2 do { m +=1; x[m] = z[i]; y[m] = z[i+1]
60 dot(at B+(x[m],y[m]))
61 }
62
63 hscale(right B.wid,5,-1,1) with .Start at B.sw
64 hscale(right B.wid,5,-1,1,TN) with .Start at B.nw
65 vscale(up B.ht,5,-1,1) with .Start at B.sw
66 vscale(up B.ht,5,-1,1,RN) with .Start at B.se
67
68 N: box invis wid B.wid ht B.ht with .sw at B.nw+(0,graphsize/10)
69 nb = 20
70 binmax = n/5*0.5
71 dx = N.wid/nb
72 histbins(x,m,-1,1,nb,hbin)
73 for i=0 to nb-1 do { line up hbin[i]/binmax * N.ht \
74 from N.sw+((0.5+i)*dx,0) thick dx/(1bp__) outlined "gray" }
75 box wid N.wid ht N.ht at N
76 # vscale(up N.ht,5,0,1) with .Start at N.sw
77
78 E: box invis wid B.wid ht B.ht with .sw at B.se+(graphsize/10,0)
79 nb = 20
80 binmax = n/5*0.5
81 dy = E.ht/nb
82 histbins(y,m,-1,1,nb,vbin)
83 for i=0 to nb-1 do { line right vbin[i]/binmax * E.wid \
84 from E.sw+(0,(0.5+i)*dy) thick dy/(1bp__) outlined "gray" }
85 box wid E.wid ht E.ht at E
86 # hscale(right E.wid,5,0,1) with .Start at E.sw
87
88 ng = 16
89 for i=0 to ng do { x = i/ng*N.wid
90 U: x,(f_gauss(x,N.wid/2,stddev)*m*dx)/binmax*N.ht
91 X[i]: N.sw+U
92 Y[i]: E.nw+(U.y,-U.x) }
93 fitcurve(X,ng)
94 fitcurve(Y,ng)
95
96 ]
97 #showbox_
98
99 command "</g>"
100 .PE