Tuesday, July 3, 2012

Floating point accuracy

Here are two pieces of code that do that same thing, but produce different results

This one doesn't work


            double majorTickStart = majorStep * Math.Floor(min / majorStep);
            double majorTickEnd = majorStep * Math.Floor(max / majorStep);
            for (double x = majorTickStart; x <= majorTickEnd; x += majorStep)
            {
                Point p1 = new Point((x - min) * scale, 0);
                Point p2 = new Point((x - min) * scale, 16);
                drawingContext.DrawLine(majorTickPen, p1, p2);
            }


This one does work


            double majorTickStart = majorStep * Math.Floor(min / majorStep);
            double majorTickEnd = majorStep * Math.Floor(max / majorStep);
            double majorSteps = (majorTickEnd - majorTickStart);
            for (double x = 0; x <= majorSteps; x += majorStep)
            {
                double _x = (x + (majorTickStart - min)) * scale;
                Point p1 = new Point(_x, 0);
                Point p2 = new Point(_x, 16);
                drawingContext.DrawLine(majorTickPen, p1, p2);
            }


If you look closely that math turns out to be the same on both of them.  What this is meant to do is draw the tick marks you would see on a graph along the axis.   The first two lines compute the offsets of where to start and stop drawing tick marks, since the beginning value, "min", doesn't necessarily start on a boundary.   Next the for loop will iterate between those two values with a step size of, "majorStep".  This would normally be a power of 10.  
The problem the happens is the first set of code turns into an infinite loop under certain conditions.  If "majorsStep" is small compared to the start and end values, x will never increase.  "small" means that there is more than 52bits of precision between the two of them.  In other words a factor of more that 4503599627370496.   This has to do with how a computer stores an IEEE754 (64 bit double) number.  It stores it using 1 bit sign, 11bit exponent , and 52bit fractional.   To get the number it represents 2 raise to the power of the exponent times the fractional bits.   Stated another way the exponent is the number of bits that are ignored.  If it takes more than 52 bits to represent the number, you will lose the lower E bits of accuracy.    Adding 1 to your 53 bit number will result in the lowest bit being ignore, and this the 1 turns into a 0.

How do you fix this?  Make sure you do all your math with numbers that are close to each other.  The second one works because we start "x" at 0 and add the small value of "majorStep" to it.  We also subtract "min" from "majorTickStart" to get a small value before we add it to x, which will also be small.  If we tried to add "x" to "majorTickStart" and then subtract "min" we would end up with just "majorTickStart - min", because the value in x would be ignored.

Sunday, June 24, 2012

CableCards and Tuning Adapters

I've been using my Ceton turner card for a while now and its been pretty nice.  Being able to DVR multiple shows and once is very nice.  The only problem I've had so far, is that some channels will not tune some times.

tl;dr - Tuning adapters case 99% of the problems

I wake up in the morning with a notice that it wasn't able to record my sons cartoon because the channel couldn't be found.  To Cetons credit it is NOT their fault.  Its actually Time Warner's fault.  They use SDV (switch digital video) on a lot of their channels.  To receive SDV channels Time Warner gives you a "tuning adapter".  Its a small box that plugs into your computer through USB and your cable line.  The computer queries the tuner adapter for the frequency of a given channel and the tuning adapter does it magic and tells the computer where it can find the channel.  The problem is these adapters seem to have MANY, MANY problems.  Some times they don'y sync up to the cable company and suddenly you can not use half of your channels.


Raspberry Pi

After many months I got my Raspberry Pi in the mail.  Even though I've seen many pictures of it, it still looks much smaller than I thought.  If you don't know what a Raspberry Pi is, its a tiny computer about the size of a deck of cards.   Its just the board through, no fancy case.  It also has a bunch of extra pins on it so you can do some I/O, like reading switches and setting LEDs.  You also need to supply your own power supply and memory card.

I used my USB phone charger cable to power it.  I've read some phone cables don't work, but this one seems to work, so far.  I copied the Debian image from the Raspberry Pi website on to the memory card and booted up with no problem.  Next I looked for something interesting to try out.  I found this post about streaming Pandora music.  I followed it and was able to connect to Pandora, but the music would stop after a second or two.  Turns out there was a bug in the ALSA (sound) drivers.   If you background the task (Ctrl-Z) and then foreground it again (fg), it would play for a few more seconds.  I next tried the beta Debian image on the Raspberry Pi website, since it was newer.  It still had the problem.  After a a little google search I found this.  Apparently they just fixed the driver a few days ago.  To update the firmware I used Hexxah's raspi-update tool.  Just had to run it and reboot when it finishes.  I tried pianobar and it work that time.

Next was the GPIO pins.  They are perfectly spaced out for an old ribbon cable.  I first tried and IDE cable but realized it was one of the UMA 66 with extra conductors in it.  Rather than chance anything funny with that cable I found an old floppy drive cable.  It fits over the pins with the last 4 rows sticking out.  I then wired an old bread board I had to an LED and switch using this diagram.  I next used the RPi.GPIO library and wrote a script to toggle the LED on the switch.  The LED was a little dim the for the first test, so I swapped the resistor for a smaller one and its much brighter now.

Something of note, I had used the wrong pin diagram incorrectly wired the wrong GPIO to the switch, and the Pi instantly turned off.  Thought I had blown something, but after only minute of being off it came back up fine.  Another thing to know is the GPIO to turn a LED on is TRUE and FALSE to turn it off, while reading a switch is TRUE when its open and FALSE when its closed.

If any cares I can post pictures.

Friday, April 13, 2012

Old server is gone

I used to have a CoLocated server.  Its no longer co-located anywhere with internet, so its doing me no good.  It used to run my blog, but I have since switch over to here.  One of the popular posts on it was about my old Acer laptop having a WHITE SCREEN OF DEATH.  Basically the computer would only show a White screen when you powered it on.

The solution is to open the panel on the top right side above the keyboard.  This is where the LCD screen connects to the mother board.  The ribbon cable is only held in with some tape and can easily become loose.  When it does, the screen will go white.   Just push the connection all the way back on and secure it with something better.  I used some blue painter tape.  Some people have noticed the round toroid on the ribbon cable.  This is supposed to be there.  It is to filter the signal LCD screen.  Do not remove it. 

4 HD Tuners

Months ago I bought a ceton 4 tuner HD card.  Its been sitting in my computer picking up over the air HD channels.  Though nice, it wasn't using the full potential of the card.  I finally call the cable company to get someone out to install a CableCard and Network tuner.  Technically, they are required to let me do this for free, but insisted  on sending someone out and charger us for it.  Mainly because they have issues with CableCards not working the first time.

The CableCard worked the first time and I was able to pick up cable channels on the card.  A small issue happened when tried to watch the HD channels.  Seems all the HD channels use SDV (switched digital video). To watch SDV channels, you need a tuner adapter.  Because there are so many cable channels now, they can't send them all down the pipe at the same time.  The tuning adapter is an extra box that connects via USB to the computer.  The computer tells it which channel you want to watch and it reports back what frequency that channel will show up on.  

The tuner adapter did not connect to the computer the first day, or the second.  I opened a ticket with Ceton about it, they were quick to respond and had me send a few diagnostic logs.  Then, a few days later it just started working on its own.  Might have had to do with the fact the computer finally auto installed SP1 on it.

Now that its working correctly, I have to say its pretty nice.  I'm able to watch 1 channel and record 3 others at the same time.  I can even share the tuners over the network so other computers can watch tv too.

Some things I'm sad about.  Media Center will not allow you to watch 2 channels at the same time, ala Picture in Picture.  The hard drive in the computer is TOO small.  I used an old 80gb laptop one I had laying around.  This is not even close to what you need when recording HD video.