66 # Used to guarantee to use at least Wx2.8
77 import wxversion
88 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')
1111 #wxversion.select('3.0-classic', True)
1212 #wxversion.select('3.0.2-phoenix', optionsRequired=True)
1313
2424print (wx .PlatformInfo )
2525print (matplotlib .__version__ )
2626
27+
2728class Knob :
29+
2830 """
2931 Knob - simple class with a "setKnob" method.
3032 A Knob instance is attached to a Param instance, e.g., param.attach(knob)
3133 Base class is for documentation purposes.
3234 """
35+
3336 def setKnob (self , value ):
3437 pass
3538
3639
3740class Param :
41+
3842 """
3943 The idea of the "Param" class is that some parameter in the GUI may have
4044 several knobs that both control it and reflect the parameter's state, e.g.
@@ -47,6 +51,7 @@ class Param:
4751 - the other knobs in the knob list have a "set" method which gets
4852 called for the others.
4953 """
54+
5055 def __init__ (self , initialValue = None , minimum = 0. , maximum = 1. ):
5156 self .minimum = minimum
5257 self .maximum = maximum
@@ -75,17 +80,26 @@ def constrain(self, value):
7580
7681
7782class SliderGroup (Knob ):
83+
7884 def __init__ (self , parent , label , param ):
7985 self .sliderLabel = wx .StaticText (parent , label = label )
8086 self .sliderText = wx .TextCtrl (parent , - 1 , style = wx .TE_PROCESS_ENTER )
8187 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 )
8490 self .setKnob (param .value )
8591
8692 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 )
89103 sizer .Add (self .slider , 1 , wx .EXPAND )
90104 self .sizer = sizer
91105
@@ -104,30 +118,34 @@ def sliderTextHandler(self, evt):
104118 self .param .set (value )
105119
106120 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 )
109123
110124
111125class FourierDemoFrame (wx .Frame ):
126+
112127 def __init__ (self , * args , ** kwargs ):
113128 wx .Frame .__init__ (self , * args , ** kwargs )
114129
115130 self .fourierDemoWindow = FourierDemoWindow (self )
116- self .frequencySliderGroup = SliderGroup (self , label = 'Frequency f0:' , \
131+ self .frequencySliderGroup = SliderGroup (
132+ self ,
133+ label = 'Frequency f0:' ,
117134 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 )
120137
121138 sizer = wx .BoxSizer (wx .VERTICAL )
122139 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 )
127144 self .SetSizer (sizer )
128145
129146
130147class FourierDemoWindow (wx .Window , Knob ):
148+
131149 def __init__ (self , * args , ** kwargs ):
132150 wx .Window .__init__ (self , * args , ** kwargs )
133151 self .lines = []
@@ -166,7 +184,9 @@ def mouseDown(self, evt):
166184 self .state = 'time'
167185 else :
168186 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 )
170190
171191 def mouseMotion (self , evt ):
172192 if self .state == '' :
@@ -175,12 +195,12 @@ def mouseMotion(self, evt):
175195 if x is None : # outside the axes
176196 return
177197 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 )
179199 if self .state == 'frequency' :
180- self .f0 .set (f0Init + (f0Init * ( x - x0 )/ x0 ))
200+ self .f0 .set (f0Init + (f0Init * ( x - x0 ) / x0 ))
181201 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 )))
184204
185205 def mouseUp (self , evt ):
186206 self .state = ''
@@ -193,26 +213,37 @@ def draw(self):
193213 color = (1. , 0. , 0. )
194214 self .lines += self .subplot1 .plot (x1 , y1 , color = color , linewidth = 2 )
195215 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 )
202224 self .subplot1 .set_xlim ([- 6 , 6 ])
203225 self .subplot1 .set_ylim ([0 , 1 ])
204226 self .subplot2 .set_xlim ([- 2 , 2 ])
205227 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 )
210240
211241 def compute (self , f0 , A ):
212242 f = np .arange (- 6. , 6. , 0.02 )
213243 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 ))
216247 return f , X , t , x
217248
218249 def repaint (self ):
@@ -227,8 +258,14 @@ def setKnob(self, value):
227258
228259
229260class App (wx .App ):
261+
230262 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 ))
232269 self .frame1 .Show ()
233270 return True
234271
0 commit comments