Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <bitset>
- #include <string>
- #include <sstream>
- using namespace std;
- string DtoAeqn(int bits, double& maxNoGain);
- void renderTable(int bits, double gain);
- double voltageOut(bitset<128> bits, int length, double gain = 1);
- double nthBitContrib(int bit, bool high, int length);
- int main(void)
- {
- bool run = 1;
- while(run)
- {
- cout << "Enter the full scale voltage of the DtoA device: ";
- double fs;
- cin >> fs;
- cout << "Enter the number of resolution bits (max 128): ";
- int bits = 129;
- while(bits > 128)
- {
- cin >> bits;
- }
- cout << "\nThe equation for the output voltage is: \nVo = G(";
- double maxNoGain = 0;
- string eqn(DtoAeqn(bits, maxNoGain));
- cout << eqn;
- double gain = fs/maxNoGain;
- cout << "Where G is the gain " << fs/maxNoGain << "\n\n";
- renderTable(bits, gain);
- cout << "\nRun again?(y/n): ";
- char runChar;
- cin >> runChar;
- if(runChar == 'n' || runChar == 'N')
- {
- run = 0;
- }
- cout << "\n";
- }
- }
- string DtoAeqn(int bits, double& maxNoGain)
- {
- ostringstream eqn;
- for(int i = 0; i < bits; i++)
- {
- double A = 1/pow(2.0,i);
- maxNoGain += A;
- eqn << "(" << A << "*" << "b" << bits-i-1 << ")";
- if(i != bits-1)
- {
- eqn << "+";
- }
- else
- {
- eqn << ")\n\n";
- }
- }
- return eqn.str();
- }
- void renderTable(int bits, double gain)
- {
- for(int i = 0; i < pow(2.0,bits); i++)
- {
- for(int k = 0; k < bits; k++)
- {
- cout << "b" << bits-k-1 << " ";
- }
- cout << " Vo";
- cout << "\n";
- bitset<128> binNumber(i);
- string binString(binNumber.to_string().substr(128-bits,bits));
- for(int k = 0; k < bits; k++)
- {
- cout << binString[k] << " ";
- }
- cout << " " << gain*voltageOut(binNumber, bits, gain);
- cout << "\n";
- }
- }
- double voltageOut(bitset<128> bits, int length, double gain)
- {
- double totalVolts = 0;
- for(int i = 0; i <= length; i++)
- {
- totalVolts += nthBitContrib(i, bits[i], length);
- }
- return totalVolts;
- }
- double nthBitContrib(int bit, bool high, int length)
- {
- return high*(1.0/pow(2.0,length-bit-1));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement