Debian Codegraph @

examples/filterdemo/filterdemo_cli

raw symbols SCIP
1 #!/usr/bin/env python
2 import getopt, sys
3
4 from cvxopt import matrix
5 from cvxopt.solvers import options
6 from cvxopt.modeling import op, variable, max
7
8 from math import cos, log10, pi
9
10 import pygtk
11 pygtk.require('2.0')
12 import gtk
13
14 import matplotlib
15 matplotlib.use('GTKAgg') # or 'GTK'
16 from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as FigureCanvas
17 import pylab
18
19 def frange(a,b,N):
20 return [ a+k*float((b-a))/N for k in range(N) ]
21
22 def design_lowpass(N, d1, wc, ws, solver=None, Q=50):
23
24 h = variable(N+1)
25 d1 = 10**(d1/20.0) # convert from dB
26
27 n1 = int(round(N*Q*wc/pi));
28 w1 = matrix(frange(0,wc,n1))
29 G1 = matrix([cos(wi*j) for j in range(N+1) for wi in w1], (n1,N+1))
30
31 n2 = int(round(N*Q*(pi-ws)/pi));
32 w2 = matrix(frange(ws,pi,n2))
33 G2 = matrix([cos(wi*j) for j in range(N+1) for wi in w2], (n2,N+1))
34
35 options['show_progress'] = 0
36 options['LPX_K_MSGLEV'] = 0
37 options['MSK_IPAR_LOG']= 0
38 op(max(abs(G2*h)), [G1*h <= d1, G1*h >= 1.0/d1]).solve(solver=solver)
39
40 return (h.value, max(abs(G2*h.value)))
41
42 def make_plot(h, d2, co, sb, pr, N, output=None):
43 w = matrix(frange(0,pi,N*50));
44 C = w*matrix(range(N+1), (1,N+1), 'd');
45 for i in range(len(C)): C[i] = cos(C[i])
46
47 fig = pylab.figure()
48 ax = fig.add_subplot(111)
49 ylim = [round((20*log10(d2)-40)/10)*10,10]
50 ax.plot(list(w/pi),[20*log10(abs(x)) for x in C*h])
51 ax.plot(2*[co],[-pr,ylim[0]],'g--')
52 ax.plot(2*[co],[10,pr],'g--')
53 ax.plot(2*[sb],[10,20*log10(d2)],'g--')
54 ax.plot([sb, 1],2*[20*log10(d2)],'g--')
55 ax.plot([0, co],2*[-pr],'g--')
56 ax.plot([0, co],2*[pr],'g--')
57
58 pylab.setp(ax,ylim=ylim)
59 pylab.setp(ax,xlabel="Normalized frequency")
60 pylab.setp(ax,ylabel="Attenuation [dB]")
61 ax.grid()
62
63 def usage():
64 print("""
65 Usage:
66 filterdemo_cli --cutoff=CO --stopband=SB --ripple=RP --order=N [options]
67
68 Arguments:
69 CO: normalized cutoff frequency
70 SB: normalized stopband frequency, 0.1 <= co < sb-0.1 <= 0.5,
71 RP: maximum passband ripple in dB, 0.01 <= rp <= 3,
72 N : filterorder, 5 <= or <= 50.
73
74 Options:
75 --solver = SOLVER One of default, mosek, glpk
76 --output = FILENAME Output filename.
77 """)
78 sys.exit(2)
79
80 def main():
81 try:
82 opts, args = getopt.getopt(sys.argv[1:], "",
83 ['cutoff=', 'stopband=', 'ripple=', 'order=',
84 'solver=', 'output='])
85 except getopt.GetoptError:
86 usage()
87
88 if opts==[]: usage()
89
90 co, sb, pr, N, output = [None]*5
91 solver = "default"
92
93 try:
94 for o, a in opts:
95 if o == "--cutoff": co = float(a);
96 if o == "--stopband": sb = float(a);
97 if o == "--ripple": pr = float(a);
98 if o == "--order": N = int(a);
99 if o == "--solver": solver = a;
100 if o == "--output": output = a;
101 except: usage()
102
103 if None in [co, sb, pr, N]: usage()
104
105 if not (0.1 <= co < sb-0.01+1E-8 <= 0.5):
106 print("invalid cutoff and stopband frequencies")
107 usage()
108
109 if not (0.01 <= pr <= 3):
110 print("invalid value of passband ripple")
111 usage()
112
113 if not (5 <= N <= 50):
114 print("invalid filterorder")
115 usage()
116
117 if not solver in ['default','mosek','glpk']:
118 print("invalid solver")
119 usage()
120
121
122 try:
123 [h, d2] = design_lowpass(N, pr, co*pi, sb*pi, solver)
124 except:
125 print("Please tighten filter specifications.")
126 sys.exit(2)
127
128
129 make_plot(h, d2, co, sb, pr, N, output);
130
131 if (output != None): savefig(output)
132
133 pylab.show()
134
135 if __name__ == "__main__":
136 main()