Archive

Posts Tagged ‘Programming’

Convert Gray-Code to decimal/binary and back

February 16th, 2009 admin No comments

Gray-Code is a binary code developed by Frank Gray in 1947. It is used by most absolute incremental encoders and some favor it's use for fringe projection systems that have to encode the pixel data in a series of image patterns (mostly fringes with differing widths). To convert Gray-Code from and to Decimal numbers using c++ i had to look up different approaches with varying difficulty levels. I then compiled the following algorithms, that i will be using for my own fringe projection system (keep tuned).

It's rather easy if you know how to do it. I had to find out the hard way, so here's for the rest of the world:

First the easy part: transforming a integer number dec into a Gray-code _code:

C++:
  1. void _calculateGrayCode () {
  2.     bitset<numeric_limits<int>::digits + 1> bs(dec);
  3.     bitset<numeric_limits<int>::digits + 1> gs(dec);
  4.     bitset<numeric_limits<int>::digits + 1> _code(0);
  5.     bs>>= 1;
  6.     gs ^= bs;
  7.     for (unsigned i=0; i<_code.size(); i++){
  8.         _code[i] = gs[i];
  9.         gs[i]=0;
  10.     }
  11. }

Now to the part of converting Gray-Code to binary and then decimal number: Suppose our Gray-code is inside the bitset _code.

C++:
  1. void _calculateNumber () {
  2.     bitset<numeric_limits<int>::digits + 1> gs(0);
  3.     bitset<numeric_limits<int>::digits + 1> bs(0);
  4.     for (unsigned i=0; i<_code.size(); i++){
  5.         gs[i] = _code[i];
  6.     }
  7.     bs[gs.size()-1] = gs[gs.size()-1];
  8.     for (int i=gs.size()-2; i>=0;i--){
  9.         bs[i]=bs[i+1];
  10.         bs[i]=bs[i]^gs[i];
  11.     }
  12.     _number = static_cast<int>(bs.to_ulong());
  13. }

bs holds the binary representation of the Gray-Code and _number is the decimal respresentation.

References:

First version of wp-stattraq is released

August 11th, 2008 admin 2 comments

Some seconds ago i've uploaded the first new version of wp-stattraq to the wordpress plugin directory. I've changed the plugin to be incooperated into the administration panel and work with version 2.6 of wordpress (which it didn't up until now).

I'm planning to put more features into the plugin and squash more bugs, but right now, i'm concentrating on releasing a stable version (as in 2.6-stable) as fast as possiblem, that can be easily installed. This first version is mainly meant for those, who were running wordpress already and need this update to be able to work with wordpress 2.6.

You can follow the updates and file tickets by visiting the trac-system i set up:

http://trac.toomuchcookies.net/trac/wiki

Also, i'm planning on releasing more plugins related to wp-stattraq that contribute to seo-work.. Look forward for more to come..