Thursday, August 8, 2013

WxService Update Available

WxMonitor ow4j130807

  • Made the WxMonitor animation speed configurable. The animation speed can be controlled by the configuration property wxmonitor.sensor.event.animation.interval. It is normally set for 38, or about one sensor event per minute (fast). You can set it as low as 1, or about 38 sensor events per minute (slow). 
  • Made the WxMonitor animation length configurable. The animation length can be set from 0 milliseconds (no event backlog) to 86,400,000 milliseconds (24 hours) as the last argument on the command line.

WxService ow4j130807

Consensus Averaging vs. Vector Averaging

One Wire Weather Service for Java has been using Consensus Averaging to smooth wind vane data and increase device resolution, from its initial implementation several years ago. I thought it gave good results, although the algorithm proved difficult for me to analyze clearly.

I was reviewing some weather data recently, and I noticed that the wind direction seemed to have slight but noticeable affinity for the sixteen primary compass points. Since nature doesn't do that, I had to suspect a problem with the hardware or software. I decided to start with the software, since that's easier to tweak. I began looking for angle averaging algorithms on the web, simply to get an idea of what kinds of solutions other people had come up with. There are several, some of them quite convoluted, home-brew algorithms. However, I found several similar examples of vector averaging. 

Vector averaging entails converting each angle (wind compass heading from the wind vane) to a vector on the unit circle, and finding the x and y coordinates, averaging the coordinates, and taking the arc tangent, converting them back to an angle. The code to do this is actually very simple.

    /**
     * Calculates the average value by converting the angles to 
     * vectors on the unit circle and averaging the Cartesian 
     * coordinates. This avoids the 360~0 wrap-around at North. 
     * Math.atan2 returns the angle between -pi and pi. Therefore, 
     * we add tau to the result to force a positive angle, mod by
     * tau and convert to degrees.
     * @param data array of sensor data to average.
     * @return average directional value.
     */
    public double averageValue(double[] data) {
        int length = data.length;
        double sin = 0, cos = 0;

        for (int ii = 0; ii < length; ii++) {
            sin += Math.sin(data[ii]);
            cos += Math.cos(data[ii]);
        }
        double theta = Math.atan2(sin / length, cos / length);

        return Math.toDegrees((tau + theta) % tau);
    }

where tau = 2π.

One of the main reasons for averaging wind vane data is that the resolution of the wind vane itself is very coarse - only 16 compass points. But wind is turbulent, so the wind vane swings back and forth over several points. This turbulence dithers the data, so the average has a much higher resolution.

I mentioned earlier that consensus averaging did increase the wind vane resolution, but some affinity remained. I had difficulty analyzing the algorithm, so it wasn't obvious to me how to modify it. But the vector averaging algorithm appears to exhibit no affinity; the output is completely de-correlated, without requiring any tweaks or tuning. It "just works".

Vector averaging does have one potential problem: if two vectors are 180 degrees out of phase, they will cancel out, yielding an undefined angle. Fortunately, we have about 40 data points for each average sample. In order for the average to be zero, all of the vectors would have to be in opposition. That would be exceedingly unlikely for a wind vane that is supposed to point into the wind, even with very low air motion. The widest wind vane swing is about 90 degrees over relatively short time periods (e.g., a two minute averaging interval).

Update: I found out from the authors of consensus averaging that the original implementation did not have the benefit of a software math library with floating point arithmetic and trigonometric functions. That makes all the difference in the world. Necessity is a mother ... ~ KU

Sunday, July 28, 2013

WxService Update Available

WxMonitor ow4j130728

  • Made WxMonitor animate the backlog of sensor events missed since the last update. In the case of WxMonitor initial start-up, if WxService has been running for 24 hours or more, the past 24 hours' events will be animated. If the computer running WxMonitor has been sleeping or hibernating, the animation will cover the duration that the computer was offline, or 24 hours, whichever is lesser. It takes about 30 seconds to animate 24 hours worth of data, and it's entertaining and informative to watch.
  • Widened the buttons on the Configure tab and the Add Property dialog to provide more room when running on Open JDK under Linux, which uses boldface for the dialog font button labels. They didn't fit in the allotted space. A future update will also address a similar problem with the text field labels.

WxService ow4j130728

  • Updated WxService to use OWAPI library version 1.11, which includes support for 64-bit 1-Wire drivers when installed on a 64-bit operating system. (Note: for some reason, the Dallas/Maxim OWAPI library is still tagged with version 1.11, so I'm not sure how one can tell which library is actually present by looking at the WxService logs.)

Thursday, July 18, 2013

WxService Update Available

WxMonitor ow4j130713

  • Added a status bar message string "Downloading 24 hour sensor backlog from %s.", where %s is the URL of the WxService. This provides an indication that the initial backlog of sensor data has not yet been received. On a fast network, you may not get a chance to view this message. On a slow network (e.g., being served from a host that is running behind a DSL connection), it could take up to a minute to catch up.

WxService ow4j130713

  • Changed getSensorData to return all data for the last 24 hours, if the time argument is 0. This is the case when WxMonitor has not received any prior data (as on startup). This change allows WxMonitor to accurately reflect the minimum and maximum readings, and fill all of the averaging buffers on startup. 
This may seem like a step backwards in performance, but it is a major improvement in accuracy. I think accuracy beats the illusion of performance without the accuracy, every time.

(Download...)

Friday, July 5, 2013

Electric Vehicles -- Growing Pains

I have been intrigued by electrically powered vehicles. I know they're more efficient than Carnot-cycle internal combustion engines, and I know that electric motors have a much more user-friendly power curve than IC engines do. Electromotive propulsion is so desirable that diesel-electric railroad locomotives carry their own power generation plants on their backs wherever they go.

I just don't want to ignore practical problems that may also exist. I believe humans (and their media) are overstating the problems of fossil energy. It's because we're familiar with them: They have been in use for a long time, and the nearly universal application by millions of consumers brings the drawbacks up above the noise level. They rise to the level of measurable significance. Meanwhile, the problems associated with electrically powered vehicles are still too small to be obvious because the adoption rate is still negligible, so the media, the environmentalists, the government regulators, the road tax structure, have not picked up on it.

Now, if the illiberal control freaks in government would leave the free market to work these things out (save for the road tax angle), we humans - voting with our hard-earned dollars - would naturally gravitate towards the most practical (or at least cost-effective) solutions without a bunch of top-down central planners making all the wrong decisions for us, based on politics, junk science and poor engineering. If individuals make the wrong decisions, it doesn't require an act of congress to correct them. We just sell or scrap our bad decisions, and buy better ones. It's an evolutionary process.

We still need to solve the problem of how to pay for roads, when electric vehicles are all mains-powered. I don't like the idea of government tracking our position by GPS. Commercial charging stations and battery-swap stations would be a reliable and convenient choke-point for road taxes. I assume that domestic charging will not be the dominant go-forward mechanism for reforming the batteries. I actually think battery-swap stations would be more practical, unless we can dramatically increase the charging rates, or switch to ultracapacitors or some other technology.

The nice thing about charging stations or swap stations, is that they are responsible for their own power supply - generation or connection to the power grid, where it can be taxed at the point of sale. Battery swap stations can have battery banks in stock, so the charging demand could be moved to off-peak times. They might be able to make some use of solar or wind power (although I'm skeptical of that - I think it would be partial at best). Maybe they could even use Crower-cycle diesel generators. The Crower-cycle design would be extremely beneficial in this application. Of course, an intrinsically safe, 20-year pebble bed nuclear reactor buried onsite would be awesome!

Finally, I am very concerned that batteries, especially Li-ion, are not environmentally friendly to make. Even with recycling, which isn't 100% efficient at reclaiming the components, the demand would increase dramatically, and Lithium mines and the manufacture of other components will bring down the wrath of NIMBYs and environmental extremists, dramatically driving up costs, and curtailing the availability (which also drives up prices).

So, lots of things to think about. Left to individual ingenuity, I think we humans would work them out naturally. What we end up with might not look anything like the electric car as we envision them today. But if we allow bureaucrats to regulate the snot out of it, we'll be driving gasoline powered cars up until the point where cost of obtaining gasoline becomes prohibitive.

In case you haven't guessed, the thing that bothers me the most are the agenda-driven regulators, bureaucrats, NIMBYs and environmental extremists. Illiberal control freaks, all of them.

Friday, June 28, 2013

PI is Wrong!

PI isn't wrong in the sense that it is inaccurate, but it was the wrong choice for the circle constant. So many equations have terms that include 2π, if we had chosen a different constant, equal to 2π...



It's a long video, but well worth the time to watch.

Tuesday, April 23, 2013

Lousy Sound -- Capacitors

I am an audiophile, but not an audiophool. I don't buy magic elixirs and oxygen-free copper, especially not in mains cords and speaker wire. The difference between capacitor sounds and amplifier sounds, tubes and transistors, digital vs. analog, have to have some basis in physics, and be demonstrable in a blind A/B comparison, or it doesn't exist. If a blind A/B difference can be detected, then we need a scientific hypothesis, and an explanation with reproducible results, or it's mere superstition.

There are some differences that are so gross, so obnoxious that you don't need a blind test to detect. We all know that. It sometimes happens when a component fails, and all of a sudden there's a hum that wasn't there before, or some kind of distortion that is just painfully obvious, and it goes away when you fix the problem.

Case in point: I have a pair of JBL 500 bookshelf speakers that I purchased for my office about 15 years ago. They're not what I would call hi-fi, but they are adequate for use with my computer sound system, playing the various bleeps and boops, and sometimes an AAC or MP3 here and there. They sounded OK for the past decade or so, but lately they have just started sounding obnoxious. I simply could not stand to listen to music through them. I would have to turn them off. "Obnoxious" was definitely the operative word here. And it wasn't just while I was thinking about it. Background music was distracting me from my other work. It wasn't subliminal.

I was able to rule out the source and most of the amplifier by switching to my AKG K-501 headphones. I still wondered if the amplifier was behaving badly, maybe oscillating, or under-biased with nasty crossover distortion (I usually listen at low volume). But no, the amplifier bench tested well within spec, and all the voltages and currents were normal. It was definitely the speakers -- or my hearing. But no, the headphones sounded OK. It was the speakers. But how? I've never experienced speakers going bad before.

I finally popped the back off the speakers, and at one glance, I had my suspicions. The crossover. It is the simplest kind of crossover that would possibly work, using the cheapest components possible: one (ferrite core) inductor and one (electrolytic) capacitor. The capacitors had gone bad in both channels. They were five microfarad bipolar electrolytics, and if their values changed, if they shorted in one direction, they could become quite non-linear. And of course, a change in value would make the crossover operate at the wrong frequency, and hence, change the frequency response and distortion characteristics of the speakers. I had to replace the caps.

So, what to replace them with? Electrolytics? Well no, but not with audiophool caps either. Electrolytics can deteriorate over time. Film caps generally do not. Five microfarad film caps are available for building crossovers. They're more expensive, and they're physically larger than electrolytics, which is why JBL didn't use them on entry-level bookshelf speakers, which outlived their warranty by some 14 years anyway.

So I paid my three dollars each, and put them in. The main problem was, they wouldn't fit on the crossover circuit board. I had to wire them on, and then use hot glue to secure them in place so they wouldn't vibrate and rattle around. But I got them installed.

The speakers sound much better now, thank you very much. Not obnoxious. I can work once again, undistracted, with music playing in the background. There is definitely a difference when a component is malfunctioning or operating out of spec. Capacitors can have a "sound", especially when they're used as a frequency-discriminating element in a filter, such as a speaker crossover, where voltages are gyrating all over the place. Bypass and coupling capacitors most likely do not have a "sound", unless they're inadequately specified, or if they're malfunctioning.