The Fugue Counterpoint by Hans Fugal

30Jan/127

Motorcycle Wheel Alignment

An important part of maintaining a motorcycle is keeping the correct slack on the chain. Usually this involves loosening the rear wheel nut and using two adjustment bolts (one on each side of the wheel) to set the correct tension. Care must be taken to ensure that the front and rear wheels are aligned, or you get bad wear and handling. When you know they are aligned already, you can just count the "flats" and turn the bolts the same amount. But from time to time, or whenever you're uncertain, you have to align them.

The simplest method is just using the marks on the swingarm. This is what I did the first time, but it felt like the rear tire was out of alignment. A little searching of the web confirmed that these marks are both too imprecise and often just plain wrong. What really matters is not the marks the manufacturer put on, but whether the two wheels are actually aligned.

The most common DIY method is to take a string, hold it taught so that it just touches both edges of the rear tire and extends to the ground at the front tire. Carefully measure on both sides, and adjust until the front tire is perfectly centered. I tried this method after I decided I wasn't sure my wheels were aligned. It was a big hassle, especially with just one person, but eventually I got the hang of it. It felt more accurate than the marks but it still felt wildly inaccurate. Something more rigid than string but still nice and straight may have given me more confidence, but I didn't have anything long and straight enough. It could have been placebo, but it rode a lot better after aligning this way.

By this point it had turned quixotic and I would not rest until I found an easy and accurate way to align my wheels. One search led to another and I eventually stumbled across the ProAligner. I was intrigued—how could a ruler be more accurate? Even one with "scientifically designed" red marks. Being the cheapskate engineer that I am I wasn't gong to shell out even $30 unless I knew it would work, and not if I could make my own.

If you search for more details on the ProAligner, you get not a lot of results that fall in two categories: gushing reviews that may or may not be shills, and skeptical curmudgeons that scream SPAM or dismiss even the idea of aligning your wheels. Nobody bridges the two with important details like what the theory is and why it may or may not work, or how it is even used.

Luckily for you anxious wheel aligners (Yes, I realize it is January. I live in California—deal with it! ;-), I have put the jigsaw together and will now help you decide whether to buy the ProAligner.

First, let's discuss the theory on which it works. It's simple geometry. Center front wheel relative to rear tireAlign rear wheel
You are making right triangles and adjusting the wheels until the opposite sides are the same. First you center the front wheel on the rear tire by sighting down each edge of the front tire, then you align the rear wheel by sighting down each edge of the rear tire. Adjust and repeat as necessary until the rear wheel is aligned when the front wheel is centered. Refer to the figure, and reread this paragraph until you get it because it is the key to the whole method, and the thing that ProAligner hopes to hide from all but those who have purchased their ruler. Once you know this tidbit and how it works, it's a slippery slope to realizing you don't need their product to replicate it.

I approximated their design in CAD (the exact spacing is irrelevant, I think I used centimeters and didn't fret whether my printer scaled it slightly), printed it out, and taped it onto some cardboard. Since I don't have a center stand, and my high-quality bike lift is opaque, I needed to do it on the sidestand, so I cut the cardboard to the appropriate angle.

The first time I tried to use it, I put the ruler at the rear wheel and didn't have the whole process figured out. I thought you just sighted along the front tires and soon realized that this alone was super inaccurate unless you could be sure the front wheel was centered perfectly. With the ruler at the rear wheel, it was impossible to read, even with the "scientifically designed" markings.

The second time, I put the ruler just behind the front wheel and had figured out the key idea of centering the front tire relative to the rear wheel, then sighting the rear wheel to read the adjustment. The ruler was easy to read at this distance. I think even a regular school ruler would be—it was 2–3 feet from my eye. The shadow side of my rear tire was hard to sight because it was all just dark. It would have been easier with better lighting, but it was easily remedied with a piece of masking tape on the rear edge to provide contrast. I was pleasantly surprised that it was not only easy to sight and read (aside from having to literally lie on my stomach), but the readings were consistent and reproduceable. It was fairly close to aligned (from the string method) but needed slight adjustment—it turned out I could distinguish one "flat" of the adjusting bolt. This is quite accurate, and easier than futzing about with strings.

As I see it, ProAligner is clever on several fronts. First, coming up with the idea of sighting down the edges of the tires and realizing that this is quite accurate (like sighting a rifle), that was pretty clever. Then, the folding ruler while hardly novel does make positioning it stable and flexible. Finally, I'll concede that the "scientifically designed" markings are easier to read than a regular ruler. So if you're into supporting clever independent companies or would rather spend some money than spend time making your own, then go buy yourself a ProAligner and rest assured that it is accurate and easy.

I have some other ideas for accurate measuring of alignment if lying on the ground isn't your thing. First, a string-based method that I hope would take away most of the frustration: Get a dowel or 2x4 and accurately notch the width of your rear wheel and the center point between the notches. Run the string from the rear tire (hint, wrap around the tire and anchor on a spoke—tape on the tire doesn't stick well) to the notches and wedge it under the centered front wheel. Now you just turn the bolts until the sides of the string just barely touch on both sides.

Second, a laser-pointer method. This isn't unlike this post which was my original tip on the ProAligner, but I think it's easier than his contraption. Attach two laser pointers to straight 2x4s such that they span the rear wheel and point true and level. Bungee them to the rear wheel such that the lasers hit down by the front wheel. Adjust until the front wheel is centered between the laser points. The success of this depends on how accurately you can true the pointers and whether you can keep them on (or have a friend), etc. You could also forego the boards if you have a friend and just sight the laser down the side of the rear tire (a little red on both edges, and also down on the ground by the front tire), have your friend mark with chalk, then measure.

Well, now you have all my thoughts on wheel alignment. My favorite is the ProAligner method—it's faster, simpler, feels most consistent and reliable, and you can even DIY with a ruler, some cardboard, a utility knife, and some tape.

15Jan/092

Mental UTC Conversion

I do a lot of UTC conversions, more than most, and I am dismayed that after perhaps a year of relatively frequent mental UTC conversions it's still no faster than firing up a terminal and typing date -u. Even with Quicksilver and fleet fingers my mind should win that race every time.

No one step is difficult, but there are too many steps. First you have to load in the reference time (might be a UTC time or local time) then maybe convert it to 24-hour time then remember whether DST is in effect or not and whether that means -7 or -6, then figure out whether to add or subtract that 7 or 6 hours, then do the actual subtraction, then wonder if you did that right and possibly wonder whether that's the same date as today or not… Clearly the brute force method is not the way to go.

If Richard Feynman can calculate square roots in his head instantly, I'm sure I can convert time zones. Anyone have any suggestions on how to gain this skill? I think I'll add Mental Math for Pilots to my wishlist.

28Jul/080

%BF Nomogram

Remember that system I came up with for calculating body fat percentage using a gallon jug in a swimming pool? I always let the computer do the calculations for me—I have a little script that I run that updates my weight graph. But not everyone is as geeky as that, and formula is not that simple, and when you add units conversion in it gets downright hairy.

I finally figured out how to generate a nomogram. Now you have no excuses.

20Aug/077

Measure Your Body Density

When you try to lose weight, what you are really trying to do is lose fat.
Weighing yourself is a first approximation of your progress, but a better
indicator is your body fat percentage (%BF). Unfortunately, measuring %BF can
be expensive and/or difficult. It doesn't need to be so. All you need is a body
of water (e.g. a swimming pool), a gallon jug, and your bathroom scale.

One of the most accurate ways to measure body fat is hydrostatic weighing. You are weighed underwater and on land, and your body's density is determined. Then body fat is estimated from the measured density. This is the same basic technique that we will use, but we don't require an underwater scale or special tank.

First the how, then I'll give you the physics. Get in the pool and exhale all
the air you can, and allow yourself to sink. You will sink unless you're
particularly obese. Take note of the sensation of sinking. Then do the same
thing but with your lungs full. Take note of the sensation of floating. Now, we
want to reach the point of neutral buoyancy when your lungs are empty, where
you are neither sinking nor floating. You will be weightless under the water.
Take the gallon jug and hold it under the water, then exhale completely. If the
jug is full of air, you will probably float (unless you are quite lean, in
which case you'll need two jugs). Keep adding water and repeating until you
reach neutral buoyancy. If you sink, add air (pour out some water). If you
float, add water. Once you've found the magic amount of water, use this
equation to calculate your body density (ρ):

rho=m/(m liters/kg - v_\text{air}))

where m is your mass (what the scale tells
you), v is the volume of you and your buoy combined, and vair is the
volume of air in your buoy. If you have ¾ gallon of water in your gallon jug,
then vair is ¼ gallon. ρw=1 kg/liter for all the
precision we need.

Once you have density, you may like to estimate your body fat. The equation for that is Siri's equation, which says

This equation assumes your lungs are completely empty, which they can't be, so we need to introduce a term for the residual volume of your lungs. This is about ¼ of your total lung capacity, or ⅓ of your vital lung capacity. You can measure your vital lung capacity with a balloon or a by blowing air through a straw into an inverted container filled with water. The average residual lung capacity for an american male is 1.2 liters; mine is about 1.9 liters. So we can adjust the formula for density as follows:

rho=m/(m/rho w - v a - v_r)

If you do this experiment you will probably find that your estimated %BF is not too far from your BMI, which is a statistical tool for estimating %BF. It can be wildly inaccurate for statistical outliers (e.g. people who are actually in shape), but it's easy to calculate and a decent sanity check in this case.

Here's what's going on. We're using Archimedes' principle: the buoyant force on a submerged object is equal to the weight of the fluid displaced. When the buoyant force balances the force of gravity, we have neutral buoyancy. The buoyant force is expressed as F_b = -rho_w v g. Substitute weight for the buoyant force and solve for the volume of the body (v = vbody + vair), then substitute that into the definition of density (m/v), and you get the formula I gave you above (if you consider the mass of air and the gallon jug as negligible). I glossed over that—if you'd like me to go into more detail say so in the comments.

If you're particularly obese and don't sink when you exhale completely, then all is not lost. You just need some counterbalance. The modified equation is:

rho = m<em>b / ((m</em>b + m<em>c)/rho</em>w - v<em>a - v</em>c).

You can find the volume of your counterbalance by taking a cue from Archimedes and measuring displacement.

About accuracy: the biggest variable in this process is how much air is left in your lungs. You will find with practice that you are able to exhale more air, which will lower your %BF estimation, as if by magic. However it always overestimates and once you figure out how to completely exhale will be very consistent. Siri's equation is the next place to look—it basically takes the density of fat and the density of muscle and ignores bone mass and density, what you ate for lunch, etc. It will also almost certainly overestimate %BF. The astute reader will wonder about air compression in the milk jug. I measured this and found that when the jug is held within a foot or so from the surface, it does compress. However, the amount it compresses conveniently offsets the extra capacity of the jug (they don't pack milk spilling over the brim of the jug, after all). All in all I think it's accurate within a few percentage points for %BF, gives you an upper bound (i.e. you are free to brag about the number you get, even if it may be slightly high), and is more accurate than BMI.

13Feb/070

align Environment

How I managed to get along for so long without knowing this I don't know.
The align environment in LaTeX is very nice for typesetting a series of equations, e.g. in a proof. Here is an example:

\begin{align*}
P(A)\text{ and }P(\overline A\cap B)&\text{ are mutually exclusive.}\
P(\overline A\cap B)+P(A\cap B) &= P(B)\
P(A\cup(\overline A\cap B)) &= P(A)+P(\overline A\cap B) &\because           \text{Third axiom of probability}\
P(A)+P(\overline A\cap B) &\le 1 &\because \text{First axiom of probability}\
P(A)+P(\overline A\cap B)+P(A\cap B)-1 &\le P(A\cap B)\
P(A)+P(B)-1 &\le P(A\cap B)
\end{align*}

That will typeset a series of equations aligned by the &s.

5Apr/063

% for Remainder

I had my lunch handed to me today because some C DSP code I had written was
wrong:

/* M is the size of the buffer,
 * w is the base pointer,
 * p is the pointer into the buffer */
void wrap(short M, short *w, short **p)
{
if (*p < w || *p >= (w + M))
    *p = w + (*p - w) % M;
}

What I did not realize is that the % operator in C does not wrap negative
numbers into the positive range like you would expect if you were a
mathemtician. i.e. -7 % 8 == -7 in C, where in mathematics -7 = 1 mod 8.

I can hear you now: "Didn't you test it, you fool?" Well, yes, I tested the
algorithm in Ruby, where mathematics hold true:

rb(main):001:0> -7 % 8
=> 1

How was I to know the C version was braindead?

So which is right? Well you can probably guess my bias by now. But inquiring
minds want to know, and no other type reads this blog so I did some research.

First, the mathematics. Wolfram has a dizzying
explanation
. Search for
modular
arithmetic

for any number of treatments of the subject. Too lazy for that? Fine, look at
your watch. If it's 10:05 and you go back in time 10 minutes, is it -5 past
10? Arguably so ("5 to"), but most people would agree that it's actually 9:55
by canon.

Now for the C argument. The 1999 ISO C Standard says:

If the quotient a/b is representable, the expression (a/b)*b + a%b shall
equal a.

So my compiler is fine. It's C that's broken. Before C99 the use of % on
negative numbers was IMPLEMENTATION DEPENDENT, which if you know anything about
C history means they didn't think it through well enough, or they made a
decision based on speed or ease of implementation. The C99 definition was
probably chosen either for ease of implementation or for the most common case.
Not exactly good enough to convince me.

Naturally, there's no going back now, so if you find yourself possibly needing
to do modular arithmetic on negative numbers in C, be sure to add again if
negative:

/* M is the size of the buffer,
 * w is the base pointer,
 * p is the pointer into the buffer */
void wrap(short M, short *w, short **p)
{
if (*p < w || *p >= (w + M))
    *p = w + (*p - w) % M;

if (*p - w < 0)
    *p += M;
}

Here's a coherent post
to the gcc-help list about the subject. Now, I don't want to hear anyone saying
that % is the modulus operator from now on. It's the remainder operator.