/Users/lyon/j4p/src/sound/filterDesign/TimeDomainBean.java

1    package sound.filterDesign; 
2     
3    // Decompiled by Jad v1.5.8c. Copyright 2001 Pavel Kouznetsov. 
4    // Jad home page: http://www.geocities.com/kpdus/jad.html 
5    // Decompiler options: packimports(3)  
6    // Source File Name:   TimeDomainBean.java 
7     
8    import java.awt.*; 
9    import java.awt.event.*; 
10   import java.beans.PropertyChangeSupport; 
11   import java.io.Serializable; 
12   import java.util.Vector; 
13    
14   public class TimeDomainBean extends Canvas 
15           implements ItemListener, 
16                      BodeListener, 
17                      MouseListener, 
18                      MouseMotionListener, 
19                      ActionListener, 
20                      Serializable { 
21    
22       public TimeDomainBean() { 
23           lineColor = Color.red; 
24           impulseColor = Color.blue; 
25           titleColor = Color.black; 
26           title = ""; 
27           myMouseListeners = new Vector(); 
28           pcs = new PropertyChangeSupport(this); 
29           timeFrame = new TimeFrame(this); 
30           autoscale = true; 
31           showdelta = true; 
32           zoom = false; 
33           right = false; 
34           result = new Vector(); 
35           racine = new Vector(); 
36           lastParam = new Vector(); 
37           residue = new Residue(); 
38           pop = new PopupMenu(); 
39           m1 = new CheckboxMenuItem("Autoscale", true); 
40           pop.add(m1); 
41           pop.add("-"); 
42           m4 = new CheckboxMenuItem("Show impluse", true); 
43           pop.add(m4); 
44           pop.add("-"); 
45           m2 = new MenuItem("Display equation"); 
46           pop.add(m2); 
47           m3 = new MenuItem("Set parameters"); 
48           pop.add(m3); 
49           add(pop); 
50           m1.addItemListener(this); 
51           m2.addActionListener(this); 
52           m3.addActionListener(this); 
53           m4.addItemListener(this); 
54           setBackground(Color.white); 
55           setForeground(Color.black); 
56           setFont(new Font("Dialog", 0, 12)); 
57           setCursor(new Cursor(1)); 
58           addMouseListener(this); 
59           addMouseMotionListener(this); 
60           newMouseX = newMouseY = oldMouseX = oldMouseY = 0; 
61           precision = 5; 
62           xMin = 0.0D; 
63           xMax = 20D; 
64           yMin = -1D; 
65           yMax = 1.0D; 
66           dW = dH = 40; 
67           gain = 1.0D; 
68           delta = 0.0D; 
69           thick = 1; 
70           lastParam.addElement(new Parameter(xMin, xMax, yMin, yMax)); 
71       } 
72    
73       public synchronized void paint(Graphics g) { 
74           update(g); 
75       } 
76    
77       public synchronized void update(Graphics g) { 
78           Dimension dimension = getSize(); 
79           if (offGraphics == null || 
80                   dimension.width != offDimension.width || 
81                   dimension.height != offDimension.height) { 
82               offDimension = dimension; 
83               offImage = createImage(dimension.width, dimension.height); 
84               offGraphics = offImage.getGraphics(); 
85           } 
86           width = getSize().width; 
87           height = getSize().height; 
88           offGraphics.setColor(getBackground()); 
89           offGraphics.fillRect(0, 
90                   0, 
91                   offDimension.width, 
92                   offDimension.height); 
93           if (!zoom) 
94               calculate(); 
95           offGraphics.setColor(getForeground()); 
96           offGraphics.drawRect(0, 0, width - 1, height - 1); 
97           paintMe(offGraphics); 
98           g.drawImage(offImage, 0, 0, this); 
99       } 
100   
101      public void calculate() { 
102          if (residue.getReadiness()) { 
103              result.removeAllElements(); 
104              double d = ((xMax - xMin) / (double) (width - dW)) * 
105                      (double) precision; 
106              Complex complex = new Complex(); 
107              Complex complex4 = new Complex(xMin, 0.0D); 
108              for (int i = 0; i < Residue.roots.length; i++) { 
109                  for (int j = 0; j < residue.coeffs[i].length; j++) { 
110                      Complex complex2 = exp(Residue.roots[i].mul(complex4)); 
111                      complex2 = 
112                              complex2.mul( 
113                                      complex4.pow(j).div(factorial(j))); 
114                      complex2 = complex2.mul(residue.coeffs[i][j]); 
115                      complex = complex.add(complex2); 
116                  } 
117   
118              } 
119   
120              result.addElement(new Double(complex.getReal() + delta)); 
121              if (autoscale) 
122                  yMin = yMax = complex.getReal() + delta; 
123              for (double d1 = xMin + d; d1 <= xMax; d1 += d) { 
124                  Complex complex1 = new Complex(); 
125                  Complex complex5 = new Complex(d1, 0.0D); 
126                  for (int k = 0; k < Residue.roots.length; k++) { 
127                      for (int l = 0; l < residue.coeffs[k].length; l++) { 
128                          Complex complex3 = exp( 
129                                  Residue.roots[k].mul(complex5)); 
130                          complex3 = 
131                                  complex3.mul( 
132                                          complex5.pow(l).div(factorial(l))); 
133                          complex3 = complex3.mul(residue.coeffs[k][l]); 
134                          complex1 = complex1.add(complex3); 
135                      } 
136   
137                  } 
138   
139                  result.addElement(new Double(complex1.getReal() + delta)); 
140                  if (autoscale) { 
141                      if (yMin > complex1.getReal() + delta) 
142                          yMin = complex1.getReal() + delta; 
143                      if (yMax < complex1.getReal() + delta) 
144                          yMax = complex1.getReal() + delta; 
145                  } 
146              } 
147   
148          } 
149      } 
150   
151      public void paintMe(Graphics g) { 
152          drawAxes(g); 
153          if (result.size() != 0) { 
154              g.setColor(lineColor); 
155              int i = dW; 
156              int k = height - 
157                      (int) Math.rint( 
158                              ((((Double) result.elementAt(0)).doubleValue() - 
159                      yMin) * 
160                      (double) (height - dH - 1)) / 
161                      (yMax - yMin)) - 
162                      dH; 
163              for (int i2 = 1; i2 < result.size(); i2++) { 
164                  int i1 = i + precision; 
165                  int k1 = height - 
166                          (int) Math.rint( 
167                                  ((((Double) result.elementAt(i2)).doubleValue() - 
168                          yMin) * 
169                          (double) (height - dH - 1)) / 
170                          (yMax - yMin)) - 
171                          dH; 
172                  if (k1 > (height - dH) + 1) 
173                      k1 = height - dH; 
174                  g.drawLine(i, k, i1, k1); 
175                  if (thick == 2) 
176                      g.drawLine(i, k - 1, i1, k1 - 1); 
177                  i = i1; 
178                  k = k1; 
179              } 
180   
181          } 
182          if (zoom) { 
183              int j; 
184              int l; 
185              if (oldMouseX < newMouseX) { 
186                  j = oldMouseX; 
187                  l = newMouseX; 
188              } else { 
189                  j = newMouseX; 
190                  l = oldMouseX; 
191              } 
192              int j1; 
193              int l1; 
194              if (oldMouseY > newMouseY) { 
195                  j1 = oldMouseY; 
196                  l1 = newMouseY; 
197              } else { 
198                  j1 = newMouseY; 
199                  l1 = oldMouseY; 
200              } 
201              g.setColor(getForeground()); 
202              g.drawRect(j, l1, l - j, j1 - l1); 
203          } 
204          g.setColor(titleColor); 
205          FontMetrics fontmetrics = g.getFontMetrics(); 
206          g.drawString(title, 
207                  ((dW + width) - fontmetrics.stringWidth(title)) / 2, 
208                  (fontmetrics.getMaxAscent() - fontmetrics.getMaxDescent()) + 
209                  2); 
210          if (showdelta) 
211              drawDelta(g); 
212      } 
213   
214      public double factorial(int i) { 
215          double d = 1.0D; 
216          if (i < 1) 
217              return d; 
218          for (int j = 2; j <= i; j++) 
219              d *= j; 
220   
221          return d; 
222      } 
223   
224      public void actionPerformed(ActionEvent actionevent) { 
225          Object obj = actionevent.getSource(); 
226          if (obj == m3) 
227              timeFrame.wake(); 
228      } 
229   
230      public void itemStateChanged(ItemEvent itemevent) { 
231          Object obj = itemevent.getSource(); 
232          if (obj == m1) { 
233              setAutoscale(m1.getState()); 
234              return; 
235          } 
236          if (obj == m4) 
237              showImpulse(m4.getState()); 
238      } 
239   
240      public void changeOccured(BodeEvent bodeevent) { 
241          Vector vector1 = new Vector(); 
242          if (bodeevent.getType() == 9 || bodeevent.getType() == 16) { 
243              gain = bodeevent.getGain(); 
244              Vector vector = bodeevent.getPoles(); 
245              synchronized (this) { 
246                  if (vector != null && vector.size() > 0) { 
247                      racine.removeAllElements(); 
248                      for (int i = 0; i < vector.size(); i++) { 
249                          Pole pole1 = (Pole) vector.elementAt(i); 
250                          racine.addElement(new Pole(pole1)); 
251                          vector1.addElement( 
252                                  new Complex(pole1.getReal(), 
253                                          pole1.getImag())); 
254                      } 
255   
256                      rootsToPoly(racine, false); 
257                  } else { 
258                      pole = new double[1]; 
259                      pole[0] = 1.0D; 
260                  } 
261              } 
262              vector = bodeevent.getZeros(); 
263              if (vector != null && vector.size() > 0) { 
264                  rootsToPoly(vector, true); 
265              } else { 
266                  zero = new double[1]; 
267                  zero[0] = 1.0D; 
268              } 
269              residue.partial(zero, pole, vector1); 
270              delta = residue.getDelta(); 
271          } 
272          repaint(); 
273      } 
274   
275      public void mouseEntered(MouseEvent mouseevent) { 
276      } 
277   
278      public void mouseClicked(MouseEvent mouseevent) { 
279      } 
280   
281      public void mouseExited(MouseEvent mouseevent) { 
282          fireMyMouseEvent( 
283                  new MyMouseEvent(this, "Impulse reponse", 0.0D, 0.0D)); 
284      } 
285   
286      public void mouseMoved(MouseEvent mouseevent) { 
287          if (mouseevent.getX() >= dW && mouseevent.getY() <= height - dH) { 
288              double d = ((double) (mouseevent.getX() - dW) * (xMax - xMin)) / 
289                      (double) (width - dW) + 
290                      xMin; 
291              double d1 = ((double) (height - dH - mouseevent.getY()) * 
292                      (yMax - yMin)) / 
293                      (double) (height - dH) + 
294                      yMin; 
295              fireMyMouseEvent( 
296                      new MyMouseEvent(this, "Impulse reponse", d, d1)); 
297              return; 
298          } else { 
299              fireMyMouseEvent( 
300                      new MyMouseEvent(this, "Impulse reponse", 0.0D, 0.0D)); 
301              return; 
302          } 
303      } 
304   
305      public void mouseReleased(MouseEvent mouseevent) { 
306          if ((mouseevent.getModifiers() & 0x10) == 16 && zoom) { 
307              zoom = false; 
308              right = false; 
309              if (oldMouseX != newMouseX && oldMouseY != newMouseY) { 
310                  if (oldMouseX > newMouseX) { 
311                      int i = oldMouseX; 
312                      oldMouseX = newMouseX; 
313                      newMouseX = i; 
314                  } 
315                  if (oldMouseY < newMouseY) { 
316                      int j = oldMouseY; 
317                      oldMouseY = newMouseY; 
318                      newMouseY = j; 
319                  } 
320                  if (oldMouseX < dW) 
321                      oldMouseX = dW; 
322                  lastParam.addElement( 
323                          new Parameter(xMin, xMax, yMin, yMax)); 
324                  if (lastParam.size() > 40) 
325                      lastParam.removeElementAt(0); 
326                  double d = ((double) (oldMouseX - dW) * (xMax - xMin)) / 
327                          (double) (width - dW) + 
328                          xMin; 
329                  xMax = ((double) (newMouseX - dW) * (xMax - xMin)) / 
330                          (double) (width - dW) + 
331                          xMin; 
332                  xMin = d; 
333                  d = ((double) (height - dH - oldMouseY) * (yMax - yMin)) / 
334                          (double) (height - dH) + 
335                          yMin; 
336                  yMax = ((double) (height - dH - newMouseY) * (yMax - yMin)) / 
337                          (double) (height - dH) + 
338                          yMin; 
339                  yMin = d; 
340                  repaint(); 
341              } 
342          } 
343          mouseevent.consume(); 
344      } 
345   
346      public void mousePressed(MouseEvent mouseevent) { 
347          if (mouseevent.isShiftDown()) { 
348              zoom = false; 
349              popup(mouseevent.getX(), mouseevent.getY()); 
350          } else if ((mouseevent.getModifiers() & 0x10) == 16) { 
351              right = true; 
352              oldMouseX = newMouseX = mouseevent.getX(); 
353              oldMouseY = newMouseY = mouseevent.getY(); 
354          } else { 
355              right = false; 
356              int i; 
357              if ((i = lastParam.size()) > 1) { 
358                  Parameter parameter = (Parameter) lastParam.elementAt( 
359                          i - 1); 
360                  lastParam.removeElementAt(i - 1); 
361                  xMin = parameter.getMinX(); 
362                  xMax = parameter.getMaxX(); 
363                  yMin = parameter.getMinY(); 
364                  yMax = parameter.getMaxY(); 
365                  repaint(); 
366              } 
367          } 
368          mouseevent.consume(); 
369      } 
370   
371      public void mouseDragged(MouseEvent mouseevent) { 
372          if (right) { 
373              zoom = true; 
374              newMouseX = mouseevent.getX(); 
375              newMouseY = mouseevent.getY(); 
376              repaint(); 
377          } 
378          double d = ((double) (mouseevent.getX() - dW) * (xMax - xMin)) / 
379                  (double) (width - dW) + 
380                  xMin; 
381          double d1 = ((double) (height - dH - mouseevent.getY()) * 
382                  (yMax - yMin)) / 
383                  (double) (height - dH) + 
384                  yMin; 
385          fireMyMouseEvent(new MyMouseEvent(this, "Impulse reponse", d, d1)); 
386          mouseevent.consume(); 
387      } 
388   
389      public void setAutoscale(boolean flag) { 
390          autoscale = flag; 
391          repaint(); 
392      } 
393   
394      public void showImpulse(boolean flag) { 
395          showdelta = flag; 
396          repaint(); 
397      } 
398   
399      public void popup(int i, int j) { 
400          pop.show(this, i, j); 
401      } 
402   
403      public void drawAxes(Graphics g) { 
404          int i = dW; 
405          int j = width - 2; 
406          int k = 1; 
407          int l = height - dH; 
408          g.drawLine(i, k, i, l); 
409          g.drawLine(i, k, i - 3, k + 5); 
410          g.drawLine(i, k, i + 3, k + 5); 
411          g.drawString(d2str(yMax), 1, 10); 
412          g.drawString(d2str(yMin), 1, l + 5); 
413          g.drawLine(i, l, j, l); 
414          g.drawLine(j, l, j - 5, l + 3); 
415          g.drawLine(j, l, j - 5, l - 3); 
416          g.drawString(d2str(xMin), i - 5, l + 15); 
417          g.drawString(d2str(xMax), j - 35, l + 15); 
418      } 
419   
420      public void drawDelta(Graphics g) { 
421          if (xMin == 0.0D && delta != 0.0D) { 
422              int i = height - 
423                      (int) Math.rint( 
424                              ((delta - yMin) * (double) (height - dH - 1)) / 
425                      (yMax - yMin)) - 
426                      dH; 
427              int j = height - 
428                      (int) Math.rint( 
429                              (-yMin * (double) (height - dH - 1)) / 
430                      (yMax - yMin)) - 
431                      dH; 
432              g.setColor(impulseColor); 
433              g.drawString("0.0", 2, j + 3); 
434              g.drawString(d2str(delta), 1, i + 3); 
435              g.drawLine(dW, j, dW, i); 
436              g.drawLine(dW + 1, j, dW + 1, i); 
437              if (delta < 0.0D) { 
438                  g.drawLine(dW, i, dW - 3, i - 3); 
439                  g.drawLine(dW, i, dW + 3, i - 3); 
440                  return; 
441              } 
442              g.drawLine(dW, i, dW - 3, i + 3); 
443              g.drawLine(dW, i, dW + 3, i + 3); 
444          } 
445      } 
446   
447      public String d2str(double d) { 
448          int i = 0; 
449          int j = 0; 
450          int k = 0; 
451          if ((new Double(d)).isInfinite()) 
452              return "Inf"; 
453          if (d != 0.0D) { 
454              byte byte0 = 1; 
455              if (d < 0.0D) { 
456                  byte0 = -1; 
457                  d = Math.abs(d); 
458              } 
459              while (d >= 10D) { 
460                  d /= 10D; 
461                  i++; 
462              } 
463              while (d < 1.0D) { 
464                  d *= 10D; 
465                  i--; 
466              } 
467              j = (int) d; 
468              if (j == 10) { 
469                  j = 1; 
470                  i++; 
471              } 
472              d *= 10D; 
473              k = (int) Math.rint(d - (double) (j * 10)); 
474              if (k == 10) { 
475                  j++; 
476                  k = 0; 
477              } 
478              j *= byte0; 
479          } 
480          String s = j + "." + k + "E" + i; 
481          return s; 
482      } 
483   
484      public Complex exp(Complex complex) { 
485          double d = Math.exp(complex.getReal()); 
486          return new Complex(d * Math.cos(complex.getImag()), 
487                  d * Math.sin(complex.getImag())); 
488      } 
489   
490      public void rootsToPoly(Vector vector, boolean flag) { 
491          Vector vector1 = new Vector(); 
492          if (flag) { 
493              for (int i = 0; i < vector.size(); i++) { 
494                  Zero zero1 = new Zero((Zero) vector.elementAt(i)); 
495                  vector1.addElement(new Complex(zero1)); 
496                  if (zero1.getImag() != 0.0D) 
497                      vector1.addElement(new Complex(zero1.conj())); 
498              } 
499   
500              Object obj = null; 
501          } else { 
502              for (int j = 0; j < vector.size(); j++) { 
503                  Pole pole1 = new Pole((Pole) vector.elementAt(j)); 
504                  vector1.addElement(new Complex(pole1)); 
505                  if (pole1.getImag() != 0.0D) 
506                      vector1.addElement(new Complex(pole1.conj())); 
507              } 
508   
509              Object obj1 = null; 
510          } 
511          Complex acomplex[] = new Complex[vector1.size() + 1]; 
512          for (int k = 0; k < acomplex.length; k++) 
513              acomplex[k] = new Complex(); 
514   
515          acomplex[0] = new Complex(1.0D); 
516          acomplex[1] = new Complex((Complex) vector1.elementAt(0)); 
517          for (int l = 1; l < vector1.size(); l++) { 
518              Complex complex = new Complex((Complex) vector1.elementAt(l)); 
519              for (int i1 = l + 1; i1 > 0; i1--) 
520                  acomplex[i1] = 
521                          acomplex[i1 - 1].mul(complex).add(acomplex[i1]); 
522   
523          } 
524   
525          if (flag) { 
526              zero = new double[acomplex.length]; 
527              for (int j1 = 0; j1 < acomplex.length; j1++) 
528                  zero[j1] = acomplex[j1].getReal(); 
529   
530              return; 
531          } 
532          pole = new double[acomplex.length]; 
533          for (int k1 = 0; k1 < acomplex.length; k1++) 
534              pole[k1] = acomplex[k1].getReal(); 
535   
536      } 
537   
538      public void oldParams(Vector vector) { 
539          params = vector; 
540          xMin = ((Double) params.elementAt(0)).doubleValue(); 
541          xMax = ((Double) params.elementAt(1)).doubleValue(); 
542          yMin = ((Double) params.elementAt(2)).doubleValue(); 
543          yMax = ((Double) params.elementAt(3)).doubleValue(); 
544          precision = ((Integer) params.elementAt(4)).intValue(); 
545          gain = ((Double) params.elementAt(5)).doubleValue(); 
546          thick = ((Integer) params.elementAt(6)).intValue(); 
547          lastParam.removeAllElements(); 
548          lastParam.addElement(new Parameter(xMin, xMax, yMin, yMax)); 
549          repaint(); 
550      } 
551   
552      public Vector newParams() { 
553          params = new Vector(); 
554          params.addElement(new Double(xMin)); 
555          params.addElement(new Double(xMax)); 
556          params.addElement(new Double(yMin)); 
557          params.addElement(new Double(yMax)); 
558          params.addElement(new Integer(precision)); 
559          params.addElement(new Double(gain)); 
560          return params; 
561      } 
562   
563      public void setBackground(Color color) { 
564          Color color1 = getBackground(); 
565          super.setBackground(color); 
566          pcs.firePropertyChange("background", color1, color); 
567      } 
568   
569      public void setForeground(Color color) { 
570          Color color1 = getForeground(); 
571          super.setForeground(color); 
572          pcs.firePropertyChange("foreground", color1, color); 
573      } 
574   
575      public void setLineColor(Color color) { 
576          lineColor = color; 
577          repaint(); 
578      } 
579   
580      public Color getLineColor() { 
581          return lineColor; 
582      } 
583   
584      public void setImpulseColor(Color color) { 
585          impulseColor = color; 
586          repaint(); 
587      } 
588   
589      public Color getImpulseColor() { 
590          return impulseColor; 
591      } 
592   
593      public void setTitleColor(Color color) { 
594          titleColor = color; 
595          repaint(); 
596      } 
597   
598      public Color getTitleColor() { 
599          return titleColor; 
600      } 
601   
602      public String getTitle() { 
603          return title; 
604      } 
605   
606      public void setTitle(String s) { 
607          title = s; 
608      } 
609   
610      public Dimension getMinimumSize() { 
611          return new Dimension(20, 20); 
612      } 
613   
614      public synchronized void addMyMouseListener( 
615              MyMouseListener mymouselistener) { 
616          myMouseListeners.addElement(mymouselistener); 
617      } 
618   
619      public synchronized void removeMyMouseListener( 
620              MyMouseListener mymouselistener) { 
621          myMouseListeners.removeElement(mymouselistener); 
622      } 
623   
624      public void fireMyMouseEvent(MyMouseEvent mymouseevent) { 
625          Vector vector = null; 
626          synchronized (this) { 
627              vector = (Vector) myMouseListeners.clone(); 
628          } 
629          for (int i = 0; i < vector.size(); i++) { 
630              MyMouseListener mymouselistener = (MyMouseListener) vector.elementAt( 
631                      i); 
632              mymouselistener.mouseOccured(mymouseevent); 
633          } 
634   
635      } 
636   
637      boolean autoscale; 
638      boolean showdelta; 
639      boolean zoom; 
640      boolean right; 
641      private Color lineColor; 
642      private Color impulseColor; 
643      private Color titleColor; 
644      private String title; 
645      double zero[]; 
646      double pole[]; 
647      Vector racine; 
648      Vector result; 
649      Vector lastParam; 
650      double xMin; 
651      double xMax; 
652      double yMin; 
653      double yMax; 
654      double gain; 
655      double delta; 
656      int width; 
657      int height; 
658      int precision; 
659      int thick; 
660      int dW; 
661      int dH; 
662      int newMouseX; 
663      int newMouseY; 
664      int oldMouseX; 
665      int oldMouseY; 
666      Vector params; 
667      TimeFrame timeFrame; 
668      Residue residue; 
669      PopupMenu pop; 
670      MenuItem m2; 
671      MenuItem m3; 
672      CheckboxMenuItem m1; 
673      CheckboxMenuItem m4; 
674      Dimension offDimension; 
675      transient Image offImage; 
676      transient Graphics offGraphics; 
677      private Vector myMouseListeners; 
678      private PropertyChangeSupport pcs; 
679  } 
680