6
6
# Used to guarantee to use at least Wx2.8
7
7
import wxversion
8
8
wxversion .ensureMinimal ('2.8' )
9
- #wxversion.select('2.8')
10
- #wxversion.select('2.9.5')
9
+ # wxversion.select('2.8')
10
+ # wxversion.select('2.9.5')
11
11
#wxversion.select('3.0-classic', True)
12
12
#wxversion.select('3.0.2-phoenix', optionsRequired=True)
13
13
24
24
print (wx .PlatformInfo )
25
25
print (matplotlib .__version__ )
26
26
27
+
27
28
class Knob :
29
+
28
30
"""
29
31
Knob - simple class with a "setKnob" method.
30
32
A Knob instance is attached to a Param instance, e.g., param.attach(knob)
31
33
Base class is for documentation purposes.
32
34
"""
35
+
33
36
def setKnob (self , value ):
34
37
pass
35
38
36
39
37
40
class Param :
41
+
38
42
"""
39
43
The idea of the "Param" class is that some parameter in the GUI may have
40
44
several knobs that both control it and reflect the parameter's state, e.g.
@@ -47,6 +51,7 @@ class Param:
47
51
- the other knobs in the knob list have a "set" method which gets
48
52
called for the others.
49
53
"""
54
+
50
55
def __init__ (self , initialValue = None , minimum = 0. , maximum = 1. ):
51
56
self .minimum = minimum
52
57
self .maximum = maximum
@@ -75,17 +80,26 @@ def constrain(self, value):
75
80
76
81
77
82
class SliderGroup (Knob ):
83
+
78
84
def __init__ (self , parent , label , param ):
79
85
self .sliderLabel = wx .StaticText (parent , label = label )
80
86
self .sliderText = wx .TextCtrl (parent , - 1 , style = wx .TE_PROCESS_ENTER )
81
87
self .slider = wx .Slider (parent , - 1 )
82
- #self.slider.SetMax(param.maximum*1000)
83
- self .slider .SetRange (0 , param .maximum * 1000 )
88
+ # self.slider.SetMax(param.maximum*1000)
89
+ self .slider .SetRange (0 , param .maximum * 1000 )
84
90
self .setKnob (param .value )
85
91
86
92
sizer = wx .BoxSizer (wx .HORIZONTAL )
87
- sizer .Add (self .sliderLabel , 0 , wx .EXPAND | wx .ALIGN_CENTER | wx .ALL , border = 2 )
88
- sizer .Add (self .sliderText , 0 , wx .EXPAND | wx .ALIGN_CENTER | wx .ALL , border = 2 )
93
+ sizer .Add (
94
+ self .sliderLabel ,
95
+ 0 ,
96
+ wx .EXPAND | wx .ALIGN_CENTER | wx .ALL ,
97
+ border = 2 )
98
+ sizer .Add (
99
+ self .sliderText ,
100
+ 0 ,
101
+ wx .EXPAND | wx .ALIGN_CENTER | wx .ALL ,
102
+ border = 2 )
89
103
sizer .Add (self .slider , 1 , wx .EXPAND )
90
104
self .sizer = sizer
91
105
@@ -104,30 +118,34 @@ def sliderTextHandler(self, evt):
104
118
self .param .set (value )
105
119
106
120
def setKnob (self , value ):
107
- self .sliderText .SetValue ('%g' % value )
108
- self .slider .SetValue (value * 1000 )
121
+ self .sliderText .SetValue ('%g' % value )
122
+ self .slider .SetValue (value * 1000 )
109
123
110
124
111
125
class FourierDemoFrame (wx .Frame ):
126
+
112
127
def __init__ (self , * args , ** kwargs ):
113
128
wx .Frame .__init__ (self , * args , ** kwargs )
114
129
115
130
self .fourierDemoWindow = FourierDemoWindow (self )
116
- self .frequencySliderGroup = SliderGroup (self , label = 'Frequency f0:' , \
131
+ self .frequencySliderGroup = SliderGroup (
132
+ self ,
133
+ label = 'Frequency f0:' ,
117
134
param = self .fourierDemoWindow .f0 )
118
- self .amplitudeSliderGroup = SliderGroup (self , label = ' Amplitude a:' , \
119
- param = self .fourierDemoWindow .A )
135
+ self .amplitudeSliderGroup = SliderGroup (self , label = ' Amplitude a:' ,
136
+ param = self .fourierDemoWindow .A )
120
137
121
138
sizer = wx .BoxSizer (wx .VERTICAL )
122
139
sizer .Add (self .fourierDemoWindow , 1 , wx .EXPAND )
123
- sizer .Add (self .frequencySliderGroup .sizer , 0 , \
124
- wx .EXPAND | wx .ALIGN_CENTER | wx .ALL , border = 5 )
125
- sizer .Add (self .amplitudeSliderGroup .sizer , 0 , \
126
- wx .EXPAND | wx .ALIGN_CENTER | wx .ALL , border = 5 )
140
+ sizer .Add (self .frequencySliderGroup .sizer , 0 ,
141
+ wx .EXPAND | wx .ALIGN_CENTER | wx .ALL , border = 5 )
142
+ sizer .Add (self .amplitudeSliderGroup .sizer , 0 ,
143
+ wx .EXPAND | wx .ALIGN_CENTER | wx .ALL , border = 5 )
127
144
self .SetSizer (sizer )
128
145
129
146
130
147
class FourierDemoWindow (wx .Window , Knob ):
148
+
131
149
def __init__ (self , * args , ** kwargs ):
132
150
wx .Window .__init__ (self , * args , ** kwargs )
133
151
self .lines = []
@@ -166,7 +184,9 @@ def mouseDown(self, evt):
166
184
self .state = 'time'
167
185
else :
168
186
self .state = ''
169
- self .mouseInfo = (evt .xdata , evt .ydata , max (self .f0 .value , .1 ), self .A .value )
187
+ self .mouseInfo = (
188
+ evt .xdata , evt .ydata , max (
189
+ self .f0 .value , .1 ), self .A .value )
170
190
171
191
def mouseMotion (self , evt ):
172
192
if self .state == '' :
@@ -175,12 +195,12 @@ def mouseMotion(self, evt):
175
195
if x is None : # outside the axes
176
196
return
177
197
x0 , y0 , f0Init , AInit = self .mouseInfo
178
- self .A .set (AInit + (AInit * ( y - y0 )/ y0 ), self )
198
+ self .A .set (AInit + (AInit * ( y - y0 ) / y0 ), self )
179
199
if self .state == 'frequency' :
180
- self .f0 .set (f0Init + (f0Init * ( x - x0 )/ x0 ))
200
+ self .f0 .set (f0Init + (f0Init * ( x - x0 ) / x0 ))
181
201
elif self .state == 'time' :
182
- if (x - x0 )/ x0 != - 1. :
183
- self .f0 .set (1. / (1. / f0Init + (1. / f0Init * ( x - x0 )/ x0 )))
202
+ if (x - x0 ) / x0 != - 1. :
203
+ self .f0 .set (1. / (1. / f0Init + (1. / f0Init * ( x - x0 ) / x0 )))
184
204
185
205
def mouseUp (self , evt ):
186
206
self .state = ''
@@ -193,26 +213,37 @@ def draw(self):
193
213
color = (1. , 0. , 0. )
194
214
self .lines += self .subplot1 .plot (x1 , y1 , color = color , linewidth = 2 )
195
215
self .lines += self .subplot2 .plot (x2 , y2 , color = color , linewidth = 2 )
196
- #Set some plot attributes
197
- self .subplot1 .set_title ("Click and drag waveforms to change frequency and amplitude" , fontsize = 12 )
198
- self .subplot1 .set_ylabel ("Frequency Domain Waveform X(f)" , fontsize = 8 )
199
- self .subplot1 .set_xlabel ("frequency f" , fontsize = 8 )
200
- self .subplot2 .set_ylabel ("Time Domain Waveform x(t)" , fontsize = 8 )
201
- self .subplot2 .set_xlabel ("time t" , fontsize = 8 )
216
+ # Set some plot attributes
217
+ self .subplot1 .set_title (
218
+ "Click and drag waveforms to change frequency and amplitude" ,
219
+ fontsize = 12 )
220
+ self .subplot1 .set_ylabel ("Frequency Domain Waveform X(f)" , fontsize = 8 )
221
+ self .subplot1 .set_xlabel ("frequency f" , fontsize = 8 )
222
+ self .subplot2 .set_ylabel ("Time Domain Waveform x(t)" , fontsize = 8 )
223
+ self .subplot2 .set_xlabel ("time t" , fontsize = 8 )
202
224
self .subplot1 .set_xlim ([- 6 , 6 ])
203
225
self .subplot1 .set_ylim ([0 , 1 ])
204
226
self .subplot2 .set_xlim ([- 2 , 2 ])
205
227
self .subplot2 .set_ylim ([- 2 , 2 ])
206
- self .subplot1 .text (0.05 , .95 , r'$X(f) = \mathcal{F}\{x(t)\}$' , \
207
- verticalalignment = 'top' , transform = self .subplot1 .transAxes )
208
- self .subplot2 .text (0.05 , .95 , r'$x(t) = a \cdot \cos(2\pi f_0 t) e^{-\pi t^2}$' , \
209
- verticalalignment = 'top' , transform = self .subplot2 .transAxes )
228
+ self .subplot1 .text (
229
+ 0.05 ,
230
+ .95 ,
231
+ r'$X(f) = \mathcal{F}\{x(t)\}$' ,
232
+ verticalalignment = 'top' ,
233
+ transform = self .subplot1 .transAxes )
234
+ self .subplot2 .text (
235
+ 0.05 ,
236
+ .95 ,
237
+ r'$x(t) = a \cdot \cos(2\pi f_0 t) e^{-\pi t^2}$' ,
238
+ verticalalignment = 'top' ,
239
+ transform = self .subplot2 .transAxes )
210
240
211
241
def compute (self , f0 , A ):
212
242
f = np .arange (- 6. , 6. , 0.02 )
213
243
t = np .arange (- 2. , 2. , 0.01 )
214
- x = A * np .cos (2 * np .pi * f0 * t )* np .exp (- np .pi * t ** 2 )
215
- X = A / 2 * (np .exp (- np .pi * (f - f0 )** 2 ) + np .exp (- np .pi * (f + f0 )** 2 ))
244
+ x = A * np .cos (2 * np .pi * f0 * t ) * np .exp (- np .pi * t ** 2 )
245
+ X = A / 2 * \
246
+ (np .exp (- np .pi * (f - f0 ) ** 2 ) + np .exp (- np .pi * (f + f0 ) ** 2 ))
216
247
return f , X , t , x
217
248
218
249
def repaint (self ):
@@ -227,8 +258,14 @@ def setKnob(self, value):
227
258
228
259
229
260
class App (wx .App ):
261
+
230
262
def OnInit (self ):
231
- self .frame1 = FourierDemoFrame (parent = None , title = "Fourier Demo" , size = (640 , 480 ))
263
+ self .frame1 = FourierDemoFrame (
264
+ parent = None ,
265
+ title = "Fourier Demo" ,
266
+ size = (
267
+ 640 ,
268
+ 480 ))
232
269
self .frame1 .Show ()
233
270
return True
234
271
0 commit comments