Guest User

RC Filter using Composition

a guest
Aug 1st, 2012
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.55 KB | None | 0 0
  1. [code]
  2. #include "Capacitor.h"
  3. #include <conio.h>
  4. #include "Filter.h"
  5. #include <iostream>
  6. #include "Resistor.h"
  7. #include <windows.h>
  8. #include <iomanip>
  9.  
  10. using namespace std;
  11.  
  12. void clear_screen(void);
  13.  
  14. void main(void)
  15. {
  16.  // Local variable for text data entry
  17.  char tempName[16];
  18.  // Local variable for component value data entry
  19.  double tempValue;
  20.  
  21.  cout << "Instantiate one object of class Filter" << endl << endl;
  22.  
  23.  cout << "Enter a name for the new filter object:  ";
  24.  cin.getline(tempName,15,'\n');
  25.  
  26.  Filter filt1;
  27.  
  28.  // Display filter values
  29.  cout << setiosflags(ios::fixed)
  30.   << setiosflags(ios::right)
  31.   << setiosflags(ios::showpoint)
  32.   << setprecision(3);
  33.  cout << endl << endl;
  34.  cout << "Nominal Filter Frequency =   " << setw(10) << filt1.getFilter() << " Hertz" << endl;
  35.  cout << "Minimum Filter Frequency =   " << setw(10) << filt1.getMaxFilter() << " Hertz" << endl;
  36.  cout << "Maximum Filter Frequency =   " << setw(10) << filt1.getMinFilter() << " Hertz" << endl;
  37.  cout << "Filter Bandwidth Frequency = " << setw(10) << filt1.getFilterTol() << " Hertz" << endl;
  38.  
  39.  // Display resistor values
  40.  cout << setiosflags(ios::fixed)
  41.    << setiosflags(ios::right)
  42.    << setiosflags(ios::showpoint)
  43.    << setprecision(3);
  44.  cout << endl << endl;
  45.  cout << "Nominal Resistance Value =   " << setw(10) << filt1.getResistance() << " ohms" << endl;
  46.  cout << "Resistor Tolerance Value =   " << setw(10) << filt1.getResTolerance()*100 << " Percent" << endl;
  47.  cout << "Maximum Resistance Value =   " << setw(10) << filt1.getMaxResistance() << " ohms" << endl;
  48.  cout << "Minimum Resistance Value =   " << setw(10) << filt1.getMinResistance() << " ohms" << endl;
  49.  cout << endl << endl;
  50.  
  51.  // Display capacitor values
  52.  cout << setiosflags(ios::fixed)
  53.    << setiosflags(ios::showpoint)
  54.    << setiosflags(ios::left)
  55.    << setprecision(3);
  56.  cout << "Nominal Capacitance Value =  " << setw(10) << filt1.getCapacitance() *1000000<< " micro Farads" << endl;
  57.  cout << "Capacitor Tolerance Value =  " << setw(10) << filt1.getCapTolerance()*100 << " Percent" << endl;
  58.  cout << "Maximum Capacitance Value =  " << setw(10) << filt1.getMaxCapacitance() * 1000000 << " micro Farads" << endl;
  59.  cout << "Minimum Capacitance Value =  " << setw(10) << filt1.getMinCapacitance() * 1000000<< " micro Farads" << endl;
  60.  cout << endl << endl;
  61.  
  62.  //filt1.FilterSave();
  63.  
  64.  cout << "Enter new nominal resistance value for filter:  ";
  65.  cin >> tempValue;
  66.  filt1.setResistance(tempValue);
  67.  
  68.  cout << "Enter new resistance tolerance value for filter:  ";
  69.  cin >> tempValue;
  70.  filt1.setResTolerance(tempValue/100.0);
  71.  
  72.  cout << "Enter new nominal micro Farad capacitance value for filter:  ";
  73.  cin >> tempValue;
  74.  filt1.setCapacitance(tempValue/1000000.0);
  75.  
  76.  cout << "Enter new capacitance tolerance value for filter:  ";
  77.  cin >> tempValue;
  78.  filt1.setResTolerance(tempValue/100.0);
  79.  
  80.  // Calculate filter values based on new resistance and capacitance values
  81.  filt1.calculateFilter();
  82.  
  83.  // Display filter values
  84.  cout << setiosflags(ios::fixed)
  85.   << setiosflags(ios::right)
  86.   << setiosflags(ios::showpoint)
  87.   << setprecision(3);
  88.  cout << endl << endl;
  89.  cout << "Nominal Filter Frequency =   " << setw(10) << filt1.getFilter() << " Hertz" << endl;
  90.  cout << "Minimum Filter Frequency =   " << setw(10) << filt1.getMaxFilter() << " Hertz" << endl;
  91.  cout << "Maximum Filter Frequency =   " << setw(10) << filt1.getMinFilter() << " Hertz" << endl;
  92.  cout << "Filter Bandwidth Frequency = " << setw(10) << filt1.getFilterTol() << " Hertz" << endl;
  93.  
  94.  // Display resistor values
  95.  cout << setiosflags(ios::fixed)
  96.    << resetiosflags(ios::left)
  97.    << setiosflags(ios::right)
  98.    << setiosflags(ios::showpoint)
  99.    << setprecision(3);
  100.  cout << endl << endl;
  101.  cout << "Nominal Resistance Value =   " << setw(10) << filt1.getResistance() << " ohms" << endl;
  102.  cout << "Resistor Tolerance Value =   " << setw(10) << filt1.getResTolerance()*100 << " Percent" << endl;
  103.  cout << "Maximum Resistance Value =   " << setw(10) << filt1.getMaxResistance() << " ohms" << endl;
  104.  cout << "Minimum Resistance Value =   " << setw(10) << filt1.getMinResistance() << " ohms" << endl;
  105.  cout << endl << endl;
  106.  
  107.  cout << setiosflags(ios::fixed)
  108.    << setiosflags(ios::showpoint)
  109.    << setiosflags(ios::left)
  110.    << setprecision(3);
  111.  cout << "Nominal Capacitance Value =  " << setw(10) << filt1.getCapacitance() *1000000<< " micro Farads" << endl;
  112.  cout << "Capacitor Tolerance Value =  " << setw(10) << filt1.getCapTolerance()*100 << " Percent" << endl;
  113.  cout << "Maximum Capacitance Value =  " << setw(10) << filt1.getMaxCapacitance() * 1000000 << " micro Farads" << endl;
  114.  cout << "Minimum Capacitance Value =  " << setw(10) << filt1.getMinCapacitance() * 1000000<< " micro Farads" << endl;
  115.  cout << endl << endl;
  116.  
  117.  
  118.  filt1.FilterRead();
  119.  //filt1.calculateFilter();
  120. *****************************************************************************************************
  121. #define PI 3.141592654
  122. #include "Capacitor.h"
  123. #include <conio.h>
  124. #include "Filter.h"
  125. #include <iomanip>
  126. #include <iostream>
  127. #include "Resistor.h"
  128. #include <stdio.h>
  129. #include <windows.h>
  130. #include <fstream>
  131. using namespace std;
  132.  
  133.  
  134.  
  135. Filter::Filter(void)
  136. {
  137. }
  138.  
  139. Filter::~Filter(void)
  140. {
  141. }
  142.  
  143.  
  144. void Filter::getResistor()
  145. {
  146.      double res;
  147.      double tol;
  148.      cout<<endl<<"Enter resistor's resistance:  ";
  149.      cin>>res;
  150.      cout<<endl<<"Enter resistor tolerance : ";
  151.      cin>>tol;
  152.      resistor.setResistance(res);
  153.      resistor.setResTolerance(tol);
  154. }
  155.  
  156. void Filter::getCapacitor()
  157. {
  158.      double cap;
  159.      double tol;
  160.      cout<<endl<<"Enter capacitor capacitance:  ";
  161.      cin>>cap;
  162.      cout<<endl<<"Enter capacitor tolerance : ";
  163.      cin>>tol;
  164.      capacitor.setCapacitance(cap);
  165.      capacitor.setCapTolerance(tol);
  166. }
  167. void Filter::getFilterType()
  168. {
  169.      string type;
  170.      cout<<endl<<"Enter Filter type(Low, High) : ";
  171.      cin>>type;
  172.      filterType=type;
  173. }
  174.  
  175. void Filter::calculateFilter()
  176. {
  177.     cutOffFrequency=1/(2*3.14*(resistor.getResistance()*capacitor.getCapacitance()));
  178.     maxFrequency=1/(2*3.14*(resistor.getResistance()+resistor.getResTolerance())*(capacitor.getCapacitance()+capacitor.getCapTolerance()));
  179.     minFrequency=1/(2*3.14*(resistor.getResistance()-resistor.getResTolerance())*(capacitor.getCapacitance()-capacitor.getCapTolerance()));
  180. }
  181.  
  182. double Filter::getFilter()
  183. {
  184.     return cutOffFrequency;
  185. }
  186.  
  187. double Filter::getMaxFilter()
  188. {
  189.     return maxFrequency;
  190. }
  191.  
  192. double Filter::getMinFilter()
  193. {
  194.     return minFrequency;
  195. }
  196.  
  197. void Filter::writeToFile()
  198. {
  199.     string filename;
  200.     cout<<"Enter file name: ";
  201.     cin>>filename;
  202.     ofstream fout;
  203.     fout.open(filename);
  204.     fout<<"Resistor resistance: "<<resistor.getResistance();
  205.     fout<<endl<<"Resistor tolerance: "<<resistor.getResTolerance();
  206.     fout<<endl<<"Resistor Min resistance: "<<resistor.getMinResistance();
  207.     fout<<endl<<"Resistor Max resistance: "<<resistor.getMaxResistance();
  208.     fout<<endl<<"Capacitor capacitance: "<<capacitor.getCapacitance();
  209.     fout<<endl<<"Capacitor tolerance: "<<capacitor.getCapTolerance();
  210.     fout<<endl<<"Capacitor max capacitance: "<<capacitor.getMaxCapacitance();
  211.     fout<<endl<<"Capacitor min capacitance: "<<capacitor.getMinCapacitance();
  212.     fout<<endl<<"Cut off frequency: "<<cutOffFrequency;
  213.     fout<<endl<<"Max frequency: "<<maxFrequency;
  214.     fout<<endl<<"Min frequency: "<<minFrequency;
  215.     fout<<endl<<"Filter Type: "<<filterType;
  216.     fout.close();
  217. }
  218.  
  219. void Filter::FilterRead(string filename)
  220. {
  221.     ifstream fin;
  222.     fin.open(filename);
  223.     if(!fin)
  224.     {
  225.         cout<<"File do not exist.";
  226.         return;
  227.     }
  228.     else
  229.     {
  230.         string line;
  231.         while(fin)
  232.         {
  233.             getline(fin,line);
  234.             cout<<line<<endl;
  235.         }
  236.     }
  237.     fin.close();
  238. }
  239.  // Display filter values
  240.  cout << setiosflags(ios::fixed)
  241.   << setiosflags(ios::right)
  242.   << setiosflags(ios::showpoint)
  243.   << setprecision(3);
  244.  cout << endl << endl;
  245.  cout << "Nominal Filter Frequency =   " << setw(10) << filt1.getFilter() << " Hertz" << endl;
  246.  cout << "Minimum Filter Frequency =   " << setw(10) << filt1.getMaxFilter() << " Hertz" << endl;
  247.  cout << "Maximum Filter Frequency =   " << setw(10) << filt1.getMinFilter() << " Hertz" << endl;
  248.  cout << "Filter Bandwidth Frequency = " << setw(10) << filt1.getFilterTol() << " Hertz" << endl;
  249.  
  250.  // Display resistor values
  251.  cout << setiosflags(ios::fixed)
  252.    << resetiosflags(ios::left)
  253.    << setiosflags(ios::right)
  254.    << setiosflags(ios::showpoint)
  255.    << setprecision(3);
  256.  cout << endl << endl;
  257.  cout << "Nominal Resistance Value =   " << setw(10) << filt1.getResistance() << " ohms" << endl;
  258.  cout << "Resistor Tolerance Value =   " << setw(10) << filt1.getResTolerance()*100 << " Percent" << endl;
  259.  cout << "Maximum Resistance Value =   " << setw(10) << filt1.getMaxResistance() << " ohms" << endl;
  260.  cout << "Minimum Resistance Value =   " << setw(10) << filt1.getMinResistance() << " ohms" << endl;
  261.  cout << endl << endl;
  262.  
  263.  cout << setiosflags(ios::fixed)
  264.    << setiosflags(ios::showpoint)
  265.    << setiosflags(ios::left)
  266.    << setprecision(3);
  267.  cout << "Nominal Capacitance Value =  " << setw(10) << filt1.getCapacitance() *1000000<< " micro Farads" << endl;
  268.  cout << "Capacitor Tolerance Value =  " << setw(10) << filt1.getCapTolerance()*100 << " Percent" << endl;
  269.  cout << "Maximum Capacitance Value =  " << setw(10) << filt1.getMaxCapacitance() * 1000000 << " micro Farads" << endl;
  270.  cout << "Minimum Capacitance Value =  " << setw(10) << filt1.getMinCapacitance() * 1000000<< " micro Farads" << endl;
  271.  cout << endl << endl;
  272. }
  273. *******************************************************************************************************
  274. //Filter.h
  275.  
  276. #pragma
  277. #include "Capacitor.h"
  278. #include "Resistor.h"
  279.  
  280. class Filter
  281. {
  282.     Resistor Resistor;
  283.     Capacitor capacitor;
  284. private:
  285.    
  286.     double cutOffFrequency;
  287.     double maxFrequency;
  288.     double minFrequency;
  289.     string filterType;
  290. public:
  291.     Filter(void);
  292.     ~Filter(void);
  293.      void getResistor();
  294.      void getCapacitor();
  295.     void getFilterType();
  296.     void calculateFilter();
  297.     double getFilter();
  298.     double getMinFilter();
  299.     double getMaxFilter();
  300.     double getFilterTol();
  301.     double getCapacitance;
  302.     double getCapTolerance;
  303.     double getMaxCapacitance;
  304.     double getMinCapacitance;
  305.     double getResistance;
  306.     double getResTolerance;
  307.     double getMaxResistance;
  308.     double getMinResistance;
  309.     double setResistance;
  310.     double setResTolerance;
  311.     double setCapacitance;
  312.     void writeToFile();
  313.     void FilterRead(string);
  314. };
  315. ******************************************************************************************************
  316. #pragma
  317.  
  318.  
  319. //The Class Declaration
  320.  
  321. class Resistor
  322. {
  323.    
  324. private:
  325.     double m_dResValue;
  326.     double m_dTolerance;
  327.     double m_dMinRResistance;
  328.     double m_dMaxResistance;
  329. protected:
  330.     double resValues[4];
  331. public:
  332.     Resistor();
  333.     Resistor(string Name, double resistorValue, double resistorTolerance);
  334.     Resistor(const Resistor &resistorObject);
  335.     string m_cResistorName;
  336.     void DisplayResistor(void);
  337.     void EnterResistance (void);
  338.     // Returns minimum  tolerance resistance
  339.     double getMinResistance(void);
  340.     // Set FiltRes nominal resistance
  341.     void setResistance(double);
  342.     void setResTolerance(double);
  343.     Resistor(double nom = 1000.0, double tol = 0.10);
  344.     ~Resistor();
  345.     // Returns resistance value
  346.     double getResistance(void);
  347.     // Returns maximum tolerance resistance
  348.     double getMaxResistance(void);
  349.     // Returns resistor tolerance
  350.     double getResTolerance(void);
  351.  
  352. };
  353. *************************************************************************************************
  354. #pragma
  355. #include <conio.h>
  356. #include <iostream>
  357. #include <iomanip>
  358. #include "Resistor.h"
  359. #include <windows.h>
  360. using namespace std;
  361.  
  362. Resistor::Resistor(double nom, double tol)
  363. {
  364.     resValues[0] = nom;
  365.     resValues[1] = tol;
  366.     resValues[2] = nom * (1.0 + tol);
  367.     resValues[3] = nom * (1.0 - tol);
  368. }
  369.  
  370. Resistor::~Resistor()
  371. {
  372. }
  373.  
  374. void Resistor::setResistance(double newResistance)
  375. {
  376.     resValues[0] = newResistance;
  377.     resValues[2] = resValues[0] * (1.0 - resValues[1]);
  378.     resValues[3] = resValues[0] * (1.0 + resValues[1]);
  379. }
  380.  
  381. void Resistor::setResTolerance(double newResTolerance)
  382. {
  383.     resValues[1] = newResTolerance;
  384.     resValues[2] = resValues[0] * (1.0 - resValues[1]);
  385.     resValues[3] = resValues[0] * (1.0 + resValues[1]);
  386. }
  387.  
  388. // Returns resistance value
  389. double Resistor::getResistance(void)
  390. {
  391.     return resValues[0];
  392. }
  393.  
  394. // Returns minimum  tolerance resistance
  395. double Resistor::getMinResistance(void)
  396. {
  397.     return resValues[3];
  398. }
  399.  
  400. // Returns maximum tolerance resistance
  401. double Resistor::getMaxResistance(void)
  402. {
  403.     return resValues[2];
  404. }
  405.  
  406. // Returns resistor tolerance
  407. double Resistor::getResTolerance(void)
  408. {
  409.     return resValues[1];
  410. }
  411. ***************************************************************************************************
  412. #pragma
  413.  
  414. class Capacitor  
  415. {
  416. public:
  417.     // Returns capacitance value
  418.     double getCapacitance(void);
  419.     // Returns minimum tolerance capacitance
  420.     double getMinCapacitance(void);
  421.     // Returns max tolerance capacitance
  422.     double getMaxCapacitance(void);
  423.     // Returns capacitor tolerance
  424.     double getCapTolerance(void);
  425.     double getFilter;
  426.     double getMinFilter();
  427.     double getMaxFilter();
  428.     double getFilterTol();
  429.     void calculateFilter();
  430.     int FilterRead(void);
  431.     void setCapacitance(double);
  432.     void setCapTolerance(double);
  433.     Capacitor(double nom = 0.000001, double tol = 0.20);
  434.     ~Capacitor();
  435.    
  436.  
  437. private:
  438.     double capValues[4];
  439.  
  440. };
  441. ***************************************************************************************************
  442. #include "Capacitor.h"
  443. #include <conio.h>
  444. #include <iostream>
  445. #include <iomanip>
  446. #include <windows.h>
  447. using namespace std;
  448.  
  449.  
  450. /**************************Construction/Destruction***********************************/
  451.  
  452. Capacitor::Capacitor(double nom, double tol)
  453. {
  454.     capValues[0] = nom;
  455.     capValues[1] = tol;
  456.     capValues[2] = nom * (1.0 + tol);
  457.     capValues[3] = nom * (1.0 - tol);
  458. }
  459.  
  460. Capacitor::~Capacitor()
  461. {
  462. }
  463.  
  464. void Capacitor::setCapacitance(double newCapacitance)
  465. {
  466.     capValues[0] = newCapacitance;
  467.     capValues[2] = capValues[0] * (1.0 + capValues[1]);
  468.     capValues[3] = capValues[0] * (1.0 - capValues[1]);
  469. }
  470.  
  471. void Capacitor::setCapTolerance(double newCapTolerance)
  472. {
  473.     capValues[1] = newCapTolerance;
  474.     capValues[2] = capValues[0] * (1.0 + capValues[1]);
  475.     capValues[3] = capValues[0] * (1.0 - capValues[1]);
  476. }
  477.  
  478. // Returns capacitance value
  479. double Capacitor::getCapacitance(void)
  480. {
  481.     return capValues[0];
  482. }
  483.  
  484. // Returns minimum tolerance capacitance
  485. double Capacitor::getMinCapacitance(void)
  486. {
  487.     return capValues[3];
  488. }
  489.  
  490. // Returns max tolerance capacitance
  491. double Capacitor::getMaxCapacitance(void)
  492. {
  493.     return capValues[2];
  494. }
  495.  
  496. // Returns capacitor tolerance
  497. double Capacitor::getCapTolerance(void)
  498. {
  499.     return capValues[1];
  500. }
  501. [/code]
Advertisement
Add Comment
Please, Sign In to add comment