home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / IDIOMS.ZIP / 5-19.C < prev    next >
C/C++ Source or Header  |  1991-12-04  |  2KB  |  75 lines

  1. /* Copyright (c) 1992 by AT&T Bell Laboratories. */
  2. /* Advanced C++ Programming Styles and Idioms */
  3. /* James O. Coplien */
  4. /* All rights reserved. */
  5.  
  6. #include <complex.h>
  7.  
  8. typedef double time;
  9.  
  10. class SeriesRLCStepResponse {
  11. public:
  12.     virtual complex current(time t) { return object->current(t); };
  13.     double frequency() const { return 1.0 / (L * C); }
  14.     SeriesRLCStepResponse(double r, double l,
  15.         double c, double initialCurrent, short isenvelope=1);
  16. protected:
  17.     double R, L, C, currentT0;
  18.     double alpha;
  19.     complex omegad, a1, b1, a2, b2, s1, s2;
  20. private:
  21.     SeriesRLCStepResponse *object;
  22. };
  23.  
  24. class UnderDampedResponse: public SeriesRLCStepResponse {
  25. public:
  26.     UnderDampedResponse(double r, double l, double c, double i) :
  27.     SeriesRLCStepResponse(r,l,c,i,0) { }
  28.     complex current(time t) {
  29.         return exp(-alpha * t) * (b1 * cos(omegad * t) +
  30.                b2 * sin(omegad * t));
  31.     }
  32. };
  33.  
  34. class OverDampedResponse: public SeriesRLCStepResponse {
  35. public:
  36.     OverDampedResponse(double r, double l, double c, double i) :
  37.     SeriesRLCStepResponse(r,l,c,i,0) { }
  38.     complex current(time t) {
  39.         return a1 * exp(s1 * t) + a2 * exp(s2 * t);
  40.     }
  41. };
  42.  
  43. class CriticallyDampedResponse: public SeriesRLCStepResponse {
  44. public:
  45.     CriticallyDampedResponse(double r, double l, double c, double i) :
  46.     SeriesRLCStepResponse(r,l,c,i,0) { }
  47.     complex criticallyDampedResponse(time t) {
  48.         return exp(-alpha * t) * (a1 * t + a2);
  49.     }
  50. };
  51.  
  52. SeriesRLCStepResponse::SeriesRLCStepResponse(
  53.         double r, double l, double c, double initialCurrent,
  54.     short isenvelope) {
  55.     R = r; L = l; C = c; currentT0 = initialCurrent;
  56.     alpha = R / (L + L);
  57.     // calculation of a1, b1, a2, b2, etc
  58.     if (isenvelope) {
  59.         if (alpha < frequency()) {
  60.             object = new UnderDampedResponse(r, l, c, initialCurrent);
  61.         } else if (alpha > frequency()) {
  62.             object = new OverDampedResponse(r, l, c, initialCurrent);
  63.         } else {
  64.             object = new CriticallyDampedResponse(r, l, c, initialCurrent);
  65.         }
  66.     } else {
  67.         omegad = sqrt(frequency() * frequency() - alpha * alpha);
  68.     }
  69. }
  70.  
  71. int main() {
  72.     SeriesRLCStepResponse afilter(0.0, 0.0, 0.0, 0.0);
  73.     complex i10 = afilter.current(10.0);
  74. }
  75.