Advertisement
Guest User

DtoAtable

a guest
Feb 22nd, 2011
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <bitset>
  4. #include <string>
  5. #include <sstream>
  6.  
  7. using namespace std;
  8.  
  9. string DtoAeqn(int bits, double& maxNoGain);
  10. void renderTable(int bits, double gain);
  11. double voltageOut(bitset<128> bits, int length, double gain = 1);
  12. double nthBitContrib(int bit, bool high, int length);
  13.  
  14. int main(void)
  15. {
  16.     bool run = 1;
  17.     while(run)
  18.     {
  19.         cout << "Enter the full scale voltage of the DtoA device: ";
  20.         double fs;
  21.         cin >> fs;
  22.  
  23.         cout << "Enter the number of resolution bits (max 128): ";
  24.         int bits = 129;
  25.         while(bits > 128)
  26.         {
  27.             cin >> bits;
  28.         }
  29.  
  30.         cout << "\nThe equation for the output voltage is: \nVo = G(";
  31.         double maxNoGain = 0;
  32.         string eqn(DtoAeqn(bits, maxNoGain));
  33.         cout << eqn;
  34.    
  35.         double gain = fs/maxNoGain;
  36.         cout << "Where G is the gain " << fs/maxNoGain << "\n\n";
  37.  
  38.         renderTable(bits, gain);
  39.  
  40.         cout << "\nRun again?(y/n): ";
  41.         char runChar;
  42.         cin >> runChar;
  43.         if(runChar == 'n' || runChar == 'N')
  44.         {
  45.             run = 0;
  46.         }
  47.         cout << "\n";
  48.     }
  49. }
  50.  
  51. string DtoAeqn(int bits, double& maxNoGain)
  52. {
  53.     ostringstream eqn;
  54.     for(int i = 0; i < bits; i++)
  55.     {
  56.         double A = 1/pow(2.0,i);
  57.         maxNoGain += A;
  58.         eqn << "(" << A << "*" << "b" << bits-i-1 << ")";
  59.         if(i != bits-1)
  60.         {
  61.             eqn << "+";
  62.         }
  63.         else
  64.         {
  65.             eqn << ")\n\n";
  66.         }
  67.     }
  68.  
  69.     return eqn.str();
  70. }
  71.  
  72. void renderTable(int bits, double gain)
  73. {
  74.     for(int i = 0; i < pow(2.0,bits); i++)
  75.     {
  76.         for(int k = 0; k < bits; k++)
  77.         {
  78.             cout << "b" << bits-k-1 << " ";
  79.         }
  80.         cout << " Vo";
  81.         cout << "\n";
  82.        
  83.         bitset<128> binNumber(i);
  84.         string binString(binNumber.to_string().substr(128-bits,bits));
  85.         for(int k = 0; k < bits; k++)
  86.         {
  87.             cout << binString[k] << "  ";
  88.         }
  89.         cout << " " << gain*voltageOut(binNumber, bits, gain);
  90.        
  91.         cout << "\n";
  92.     }
  93. }
  94.  
  95. double voltageOut(bitset<128> bits, int length, double gain)
  96. {
  97.     double totalVolts = 0;
  98.     for(int i = 0; i <= length; i++)
  99.     {
  100.         totalVolts += nthBitContrib(i, bits[i], length);
  101.     }
  102.     return totalVolts;
  103. }
  104.  
  105. double nthBitContrib(int bit, bool high, int length)
  106. {
  107.     return high*(1.0/pow(2.0,length-bit-1));
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement