<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Algorithm-Forge</title>
	<atom:link href="http://www.algorithm-forge.com/techblog/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.algorithm-forge.com/techblog</link>
	<description>Java, R, Statistics, Algorithms and other stuff</description>
	<lastBuildDate>Sun, 18 Jul 2010 16:53:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Using JavaGD in Java applications</title>
		<link>http://www.algorithm-forge.com/techblog/2010/07/using-javagd-in-java-applications/</link>
		<comments>http://www.algorithm-forge.com/techblog/2010/07/using-javagd-in-java-applications/#comments</comments>
		<pubDate>Fri, 09 Jul 2010 21:54:25 +0000</pubDate>
		<dc:creator>Kornelius Rohmeyer</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[R]]></category>

		<guid isPermaLink="false">http://www.algorithm-forge.com/techblog/?p=316</guid>
		<description><![CDATA[Goal: Create a Java application with a JFrame that contains a JGDPanel from JavaGD and plot something via JRI. Solution: The following code is a derivate of JGR code snipplets under GPL-2: import java.awt.Component; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import javax.swing.JFrame; &#8230; <a href="http://www.algorithm-forge.com/techblog/2010/07/using-javagd-in-java-applications/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><strong>Goal:</strong> Create a Java application with a <tt>JFrame</tt> that contains a <tt>JGDPanel</tt> from <tt>JavaGD</tt> and plot something via <tt>JRI</tt>.</p>
<p><strong>Solution:</strong> The following code is a derivate of <a href="http://rforge.net/JGR/">JGR</a> code snipplets under GPL-2:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.Component</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.event.WindowEvent</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.event.WindowListener</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.swing.JFrame</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.rosuda.JRI.Rengine</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.rosuda.javaGD.GDInterface</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.rosuda.javaGD.JGDBufferedPanel</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> JavaGDExample <span style="color: #000000; font-weight: bold;">extends</span> GDInterface <span style="color: #000000; font-weight: bold;">implements</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Awindowlistener+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">WindowListener</span></a> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajframe+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JFrame</span></a> f<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> gdOpen<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">double</span> w, <span style="color: #000066; font-weight: bold;">double</span> h<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>f<span style="color: #339933;">!=</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> gdClose<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; f <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajframe+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JFrame</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;JavaGD Example&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; f.<span style="color: #006633;">addWindowListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; c <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JGDBufferedPanel<span style="color: #009900;">&#40;</span>w, h<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; f.<span style="color: #006633;">getContentPane</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Acomponent+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Component</span></a><span style="color: #009900;">&#41;</span> c<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; f.<span style="color: #006633;">pack</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; f.<span style="color: #006633;">setVisible</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> gdClose<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">gdClose</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>f<span style="color: #339933;">!=</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c<span style="color: #339933;">=</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f.<span style="color: #006633;">removeAll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f.<span style="color: #006633;">dispose</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f<span style="color: #339933;">=</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Rengine engine <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Rengine<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;--vanilla&quot;</span><span style="color: #009900;">&#125;</span>, <span style="color: #000066; font-weight: bold;">true</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; engine.<span style="color: #006633;">eval</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;.setenv &lt;- if (exists(<span style="color: #000099; font-weight: bold;">\&quot;</span>Sys.setenv<span style="color: #000099; font-weight: bold;">\&quot;</span>)) Sys.setenv else Sys.putenv&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; engine.<span style="color: #006633;">eval</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;.setenv(<span style="color: #000099; font-weight: bold;">\&quot;</span>JAVAGD_CLASS_NAME<span style="color: #000099; font-weight: bold;">\&quot;</span>=<span style="color: #000099; font-weight: bold;">\&quot;</span>JavaGDExample<span style="color: #000099; font-weight: bold;">\&quot;</span>)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; engine.<span style="color: #006633;">eval</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;library(JavaGD)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; engine.<span style="color: #006633;">eval</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;JavaGD()&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; engine.<span style="color: #006633;">eval</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;plot(rnorm(100))&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/** listener response to &quot;Close&quot; - effectively invokes &lt;code&gt;dev.off()&lt;/code&gt; on the device */</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> windowClosing<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Awindowevent+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">WindowEvent</span></a> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>c<span style="color: #339933;">!=</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> executeDevOff<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> windowClosed<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Awindowevent+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">WindowEvent</span></a> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> windowOpened<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Awindowevent+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">WindowEvent</span></a> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> windowIconified<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Awindowevent+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">WindowEvent</span></a> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> windowDeiconified<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Awindowevent+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">WindowEvent</span></a> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> windowActivated<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Awindowevent+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">WindowEvent</span></a> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> windowDeactivated<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Awindowevent+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">WindowEvent</span></a> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Et voilà:</p>
<p><img src="http://www.algorithm-forge.com/techblog/wp-content/uploads/2010/07/JavaGDExample-300x241.png" alt="JavaGDExample" title="JavaGDExample" /></p>
<p>Of course you have to make sure that the JRI native library is in a directory listed in <tt>java.library.path</tt>, for example on my machine via:</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">java -Djava.library.path=<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>R<span style="color: #000000; font-weight: bold;">/</span>site-library<span style="color: #000000; font-weight: bold;">/</span>rJava<span style="color: #000000; font-weight: bold;">/</span>jri<span style="color: #000000; font-weight: bold;">/</span> JavaGDExample</div></div>
<p>Also you have to provide the correct path for the environment variable <tt>R_HOME</tt> such as:</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #007800;">R_HOME</span>=<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>lib64<span style="color: #000000; font-weight: bold;">/</span>R<span style="color: #000000; font-weight: bold;">/</span></div></div>
<p>And of course you need the two jars <tt>javaGD.jar</tt> and <tt>JRIEngine.jar</tt> in your <tt>CLASSPATH</tt>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm-forge.com/techblog/2010/07/using-javagd-in-java-applications/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Linux USB driver for the Arexx TL-500 – Part II</title>
		<link>http://www.algorithm-forge.com/techblog/2010/01/linux-usb-driver-for-the-arexx-tl-500-%e2%80%93-part-ii/</link>
		<comments>http://www.algorithm-forge.com/techblog/2010/01/linux-usb-driver-for-the-arexx-tl-500-%e2%80%93-part-ii/#comments</comments>
		<pubDate>Sun, 03 Jan 2010 22:51:10 +0000</pubDate>
		<dc:creator>Kornelius Rohmeyer</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Driver]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[USB]]></category>

		<guid isPermaLink="false">http://www.algorithm-forge.com/techblog/?p=259</guid>
		<description><![CDATA[Last time we started communicating with the TL-500. This time we want to find out the meaning of the data rows from the IN endpoint. Therefore we start the USB Sniffer SnoopyPro again and observe the data flow under Windows &#8230; <a href="http://www.algorithm-forge.com/techblog/2010/01/linux-usb-driver-for-the-arexx-tl-500-%e2%80%93-part-ii/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Last time we <a href="http://www.algorithm-forge.com/techblog/2009/12/using-libusb-to-write-a-linux-usb-driver-for-the-arexx-tl-500-part-i/">started communicating with the TL-500</a>. This time we want to find out the meaning of the data rows from the IN endpoint.</p>
<p>Therefore we start the USB Sniffer SnoopyPro again and observe the data flow under Windows while the logger is working. We then export the USB log as XML and grep for &#8220;000a&#8221;. Now we can compare the raw data from the USB device with the recorded measurements. The first surprising thing is that in some cases SnoopyPro seems to miss some incoming data events (ca. 5% of the total events). (The other possibility is that the Windows software is interpolating data points when you export them &#8211; but I disbelieve this, since this would be rather stupid, wouldn&#8217;t it?)</p>
<p>By only using one sensor at a time one is able to distinguish the data for each sensor. I have the following sensors and the respective data has the following values as hexadecimal digits in the 5th-8th position:</p>
<table border="1">
<tr>
<td>8818:</td>
<td> 72 22</td>
</tr>
<tr>
<td>8908:</td>
<td> cc 22</td>
</tr>
<tr>
<td>17882:</td>
<td> da 45</td>
</tr>
<tr>
<td>17882RH:</td>
<td> db 45</td>
</tr>
<tr>
<td>18438:</td>
<td> 06 48</td>
</tr>
<tr>
<td>18438RH:</td>
<td> 07 48</td>
</tr>
</table>
<p>After changing the order of the two halfs, the temperature sensors translate directly: 0&#215;2272=8818, 0x22CC=8908, 0x45DA=17882 and 0&#215;4806=18438. Also if the last bit of the first half is 0, we have a RH sensor, otherwise a temperature sensor. This would imply that all sensors have even numbers. RH sensors would have the same id incremented by 1. Is this always the case?</p>
<p>Just by looking at the raw data rows and exposing the sensors to different conditions, I suspect the 9-14 hexadecimal digits to contain the temperature and RH data.</p>
<p>Let&#8217;s look at a sample data set: <a href="http://www.algorithm-forge.com/data/tl500.csv">tl500.csv</a>. We load it into <a href="http://www.r-project.org/">R</a> and start investigating:</p>
<div class="codecolorer-container r default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="r codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">data <span style="color: #78aaac;">&lt;-</span> read.table<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;tl500.csv&quot;</span>, header<span style="color: #78aaac;">=</span><span style="color:#228b22;">TRUE</span><span style="color: #66cc66;">&#41;</span><br />
<br />
hex2int <span style="color: #78aaac;">&lt;-</span> Vectorize<span style="color: #66cc66;">&#40;</span><span style="color: #a020f0;">function</span><span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; v <span style="color: #78aaac;">&lt;-</span> 0<br />
&nbsp; <span style="color: #a020f0;">for</span> <span style="color: #66cc66;">&#40;</span>i <span style="color: #a020f0;">in</span> 1:nchar<span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; w <span style="color: #78aaac;">&lt;-</span> ifelse<span style="color: #66cc66;">&#40;</span>substr<span style="color: #66cc66;">&#40;</span>x,i,i<span style="color: #66cc66;">&#41;</span><span style="color: #78aaac;">==</span>c<span style="color: #66cc66;">&#40;</span>as.character<span style="color: #66cc66;">&#40;</span>0:9<span style="color: #66cc66;">&#41;</span>, letters<span style="color: #66cc66;">&#91;</span>1:6<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>, 0:15, <span style="color:#228b22;">NA</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; w <span style="color: #78aaac;">&lt;-</span> ifelse<span style="color: #66cc66;">&#40;</span><span style="color: #78aaac;">!</span>all<span style="color: #66cc66;">&#40;</span>is.na<span style="color: #66cc66;">&#40;</span>w<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>, w <span style="color: #78aaac;">&lt;-</span> w<span style="color: #66cc66;">&#91;</span><span style="color: #78aaac;">!</span>is.na<span style="color: #66cc66;">&#40;</span>w<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>, <span style="color:#228b22;">NA</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; v <span style="color: #78aaac;">&lt;-</span> v <span style="color: #78aaac;">+</span> w<span style="color: #78aaac;">*</span>16<span style="color: #78aaac;">**</span><span style="color: #66cc66;">&#40;</span>nchar<span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">&#41;</span><span style="color: #78aaac;">-</span>i<span style="color: #66cc66;">&#41;</span><br />
&nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; <span style="color: #a020f0;">return</span><span style="color: #66cc66;">&#40;</span>v<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><br />
<br />
data$Sensor <span style="color: #78aaac;">&lt;-</span> substr<span style="color: #66cc66;">&#40;</span>data$Rawdata,<span style="color: #cc66cc;">5</span>,<span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span><br />
data$RawValue <span style="color: #78aaac;">&lt;-</span> hex2int<span style="color: #66cc66;">&#40;</span>substr<span style="color: #66cc66;">&#40;</span>data$Rawdata,<span style="color: #cc66cc;">9</span>,<span style="color: #cc66cc;">12</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
plot<span style="color: #66cc66;">&#40;</span>data$RawValue, data$Value<span style="color: #66cc66;">&#41;</span></div></div>
<p><img src="http://www.algorithm-forge.com/techblog/wp-content/uploads/2010/01/plot1.png" alt="" title="plot" width="480" height="480" class="aligncenter size-full wp-image-289" /></p>
<p>There are obviously subgroups and in these groups a linear relationship exists between the 5th and 6th byte (9-12 hexadecimal digits) of the raw data and the measurements. Adding the 7th byte does not improve the linear correlation. (Weee! No floating point data!) Further investigation shows us that we can calculate the measurements from RH sensors by (0.6 + Rawdata * 0.03328) RH%, from temperature sensors TSN-TH70E by (-39.58 + Rawdata * 0.01) °C and from temperature sensors TL-3TSN by (Rawdata * 0.0078) °C. These coefficients are based on the the following linear regressions. With more data they will slightly change, but given the accuracy and that you need to calibrate your sensors either way, it&#8217;s a sufficient translation of uncalibrated sensor data.</p>
<div class="codecolorer-container r default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="r codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">rhdata <span style="color: #78aaac;">&lt;-</span> data<span style="color: #66cc66;">&#91;</span>data$Sensor <span style="color: #78aaac;">%</span>in<span style="color: #78aaac;">%</span> c<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;db45&quot;</span>, <span style="color: #ff0000;">&quot;0748&quot;</span><span style="color: #66cc66;">&#41;</span>,<span style="color: #66cc66;">&#93;</span><br />
t1data <span style="color: #78aaac;">&lt;-</span> data<span style="color: #66cc66;">&#91;</span>data$Sensor <span style="color: #78aaac;">%</span>in<span style="color: #78aaac;">%</span> c<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;da45&quot;</span>, <span style="color: #ff0000;">&quot;0648&quot;</span><span style="color: #66cc66;">&#41;</span>,<span style="color: #66cc66;">&#93;</span><br />
t2data <span style="color: #78aaac;">&lt;-</span> data<span style="color: #66cc66;">&#91;</span>data$Sensor <span style="color: #78aaac;">%</span>in<span style="color: #78aaac;">%</span> c<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;7222&quot;</span>, <span style="color: #ff0000;">&quot;cc22&quot;</span><span style="color: #66cc66;">&#41;</span>,<span style="color: #66cc66;">&#93;</span><br />
<br />
lm<span style="color: #66cc66;">&#40;</span>Value~RawValue,data<span style="color: #78aaac;">=</span>rhdata<span style="color: #66cc66;">&#41;</span><br />
lm<span style="color: #66cc66;">&#40;</span>Value~RawValue,data<span style="color: #78aaac;">=</span>t1data<span style="color: #66cc66;">&#41;</span><br />
lm<span style="color: #66cc66;">&#40;</span>Value~RawValue,data<span style="color: #78aaac;">=</span>t2data<span style="color: #66cc66;">&#41;</span></div></div>
<p>Let&#8217;s update the C program from the last time with the new results:</p>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333;">static</span> <span style="color: #993333;">int</span> get_sensor<span style="color: #009900;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> data<span style="color: #009900;">&#91;</span>64<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#91;</span>3<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>256<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#91;</span>2<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #993333;">static</span> <span style="color: #993333;">int</span> get_value<span style="color: #009900;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> data<span style="color: #009900;">&#91;</span>64<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> value <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// We use a loop since it is not clear whether we want to use also data[6] someday.</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> i<span style="color: #339933;">=</span><span style="color: #0000dd;">4</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;=</span><span style="color: #0000dd;">5</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; value <span style="color: #339933;">+=</span> data<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span>pow<span style="color: #009900;">&#40;</span>16<span style="color: #339933;">,</span>2<span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>5<span style="color: #339933;">-</span>i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> value<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #808080; font-style: italic;">/**<br />
&nbsp;* We assume that all TSN-TH70E sensors have ids bigger than 10000.<br />
&nbsp;* If the id is then odd we have a humidity sensor.<br />
&nbsp;* Has anyone more information about this?<br />
&nbsp;*/</span><br />
<span style="color: #993333;">static</span> <span style="color: #993333;">double</span> get_measurement<span style="color: #009900;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> data<span style="color: #009900;">&#91;</span>64<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">double</span> value <span style="color: #339933;">=</span> get_value<span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> sensor <span style="color: #339933;">=</span> get_sensor<span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>sensor <span style="color: #339933;">&lt;</span> 10000<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> value <span style="color: #339933;">*</span> <span style="color:#800080;">0.0078</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>sensor<span style="color: #339933;">%</span>2<span style="color: #339933;">==</span>0<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">return</span> <span style="color: #339933;">-</span>39.58 <span style="color: #339933;">+</span> value <span style="color: #339933;">*</span> <span style="color:#800080;">0.01</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">return</span> 0.6 <span style="color: #339933;">+</span> value <span style="color: #339933;">*</span> <span style="color:#800080;">0.03328</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #808080; font-style: italic;">/**<br />
&nbsp;* We assume that all TSN-TH70E sensors have ids bigger than 10000.<br />
&nbsp;* If the id is then odd we have a humidity sensor.<br />
&nbsp;* Has anyone more information about this?<br />
&nbsp;*/</span><br />
<span style="color: #993333;">const</span> <span style="color: #993333;">char</span><span style="color: #339933;">*</span> get_unit<span style="color: #009900;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> data<span style="color: #009900;">&#91;</span>64<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> sensor <span style="color: #339933;">=</span> get_sensor<span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>sensor <span style="color: #339933;">&gt;</span> 10000 <span style="color: #339933;">&amp;&amp;</span> sensor<span style="color: #339933;">%</span>2<span style="color: #339933;">!=</span>0<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;%RH&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;°C&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>If we now add the following line into the data reading loop we get a usefull result:</p>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;From sensor %d we get a raw value %d. We guess this means %3.2f %s. <span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;get_sensor<span style="color: #009900;">&#40;</span>dataUp<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> get_value<span style="color: #009900;">&#40;</span>dataUp<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> get_measurement<span style="color: #009900;">&#40;</span>dataUp<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> get_unit<span style="color: #009900;">&#40;</span>dataUp<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Found Arexx TL-500.<br />
Data: 00 0a 72 22 0c 17 05 00 00 00 15 00 ff 04 ed 1e 06 00 00 00 ff ff ff ff ff ff ff ff ff ff ff 0b ff af 05 00 00 09 07 48 04 ec c9 05 00 00 09 72 22 0b ff df 05 00 00 00 06 48 48 ed 31 2e 3f 09 <br />
From sensor 8818 we get a raw value 3095. We guess this means 24.14 °C. <br />
Data: 00 0a 07 48 04 f5 17 00 00 00 06 00 ff 04 ed 1e 06 00 00 00 ff ff ff ff ff ff ff ff ff ff ff 0b ff af 05 00 00 09 07 48 04 ec c9 05 00 00 09 72 22 0b ff df 05 00 00 00 06 48 48 ed 31 2e 3f 09 <br />
From sensor 18439 we get a raw value 1269. We guess this means 42.83 %RH. <br />
Data: 00 0a 72 22 0c 0f 39 00 00 00 0c 00 ff 04 ed 1e 06 00 00 00 ff ff ff ff ff ff ff ff ff ff ff 0b ff af 05 00 00 09 07 48 04 ec c9 05 00 00 09 72 22 0b ff df 05 00 00 00 06 48 48 ed 31 2e 3f 09 <br />
From sensor 8818 we get a raw value 3087. We guess this means 24.08 °C. <br />
Data: 00 0a 06 48 18 dd 51 00 00 00 06 00 ff 04 ed 1e 06 00 00 00 ff ff ff ff ff ff ff ff ff ff ff 0b ff af 05 00 00 09 07 48 04 ec c9 05 00 00 09 72 22 0b ff df 05 00 00 00 06 48 48 ed 31 2e 3f 09 <br />
From sensor 18438 we get a raw value 6365. We guess this means 24.07 °C.</div></div>
<p>You can download the <a href="http://www.algorithm-forge.com/techblog/wp-content/uploads/2010/01/tl500v2.cpp">updated C file here</a>. Now we can create our first applications for Linux.</p>
<p>P.S.: The connection quality is in the 21th and 22th hexadecimal digits and further blog posts will follow&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm-forge.com/techblog/2010/01/linux-usb-driver-for-the-arexx-tl-500-%e2%80%93-part-ii/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Using libusb to write a Linux USB driver for the Arexx TL-500 &#8211; Part I</title>
		<link>http://www.algorithm-forge.com/techblog/2009/12/using-libusb-to-write-a-linux-usb-driver-for-the-arexx-tl-500-part-i/</link>
		<comments>http://www.algorithm-forge.com/techblog/2009/12/using-libusb-to-write-a-linux-usb-driver-for-the-arexx-tl-500-part-i/#comments</comments>
		<pubDate>Mon, 21 Dec 2009 16:15:17 +0000</pubDate>
		<dc:creator>Kornelius Rohmeyer</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Driver]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[USB]]></category>

		<guid isPermaLink="false">http://www.algorithm-forge.com/techblog/?p=221</guid>
		<description><![CDATA[The Arexx TL-500 is a well-priced temperature and humidity logging system. But with only twelve direct requests for a Linux USB drivers, Arexx recently decided to abandon their plans creating a Linux driver for this device (I think there is &#8230; <a href="http://www.algorithm-forge.com/techblog/2009/12/using-libusb-to-write-a-linux-usb-driver-for-the-arexx-tl-500-part-i/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://www.arexx.com/arexx.php?cmd=goto&#038;cparam=p_templogger_bs500">Arexx TL-500</a> is a well-priced temperature and humidity logging system.</p>
<div id="attachment_251" class="wp-caption aligncenter" style="width: 460px"><a href="http://www.algorithm-forge.com/techblog/wp-content/uploads/2009/12/arexx_tl_500.jpg"><img src="http://www.algorithm-forge.com/techblog/wp-content/uploads/2009/12/arexx_tl_500.jpg" alt="TL-500" title="TL-500" width="450" height="407" class="size-full wp-image-251" /></a><p class="wp-caption-text">TL-500</p></div>
<p>But with only twelve direct requests for a Linux USB drivers, Arexx recently decided to abandon their plans creating a Linux driver for this device (I think there is a much higher demand, but only 12 people directly asked for Linux support). Since my TL-500 is connected to a Linux server and I don&#8217;t want to have Windows running in a virtual machine all the time, I decided to write a Linux driver for the TL-500 myself.</p>
<p>Let&#8217;s connect the TL-500 to a Linux box and take a look at the device and it&#8217;s endpoints:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">kornel@kornel-desktop:~$ lsusb -v<br />
[...]<br />
Bus 003 Device 002: ID 0451:3211 Texas Instruments, Inc. <br />
Device Descriptor:<br />
&nbsp; bLength &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;18<br />
&nbsp; bDescriptorType &nbsp; &nbsp; &nbsp; &nbsp; 1<br />
&nbsp; bcdUSB &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1.10<br />
&nbsp; bDeviceClass &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;255 Vendor Specific Class<br />
&nbsp; bDeviceSubClass &nbsp; &nbsp; &nbsp; &nbsp; 0 <br />
&nbsp; bDeviceProtocol &nbsp; &nbsp; &nbsp; &nbsp; 0 <br />
&nbsp; bMaxPacketSize0 &nbsp; &nbsp; &nbsp; &nbsp; 8<br />
&nbsp; idVendor &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x0451 Texas Instruments, Inc.<br />
&nbsp; idProduct &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0x3211 <br />
&nbsp; bcdDevice &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1.00<br />
&nbsp; iManufacturer &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 <br />
&nbsp; iProduct &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 <br />
&nbsp; iSerial &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 <br />
&nbsp; bNumConfigurations &nbsp; &nbsp; &nbsp;1<br />
&nbsp; Configuration Descriptor:<br />
&nbsp; &nbsp; bLength &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 9<br />
&nbsp; &nbsp; bDescriptorType &nbsp; &nbsp; &nbsp; &nbsp; 2<br />
&nbsp; &nbsp; wTotalLength &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 32<br />
&nbsp; &nbsp; bNumInterfaces &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1<br />
&nbsp; &nbsp; bConfigurationValue &nbsp; &nbsp; 1<br />
&nbsp; &nbsp; iConfiguration &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 <br />
&nbsp; &nbsp; bmAttributes &nbsp; &nbsp; &nbsp; &nbsp; 0x80<br />
&nbsp; &nbsp; &nbsp; (Bus Powered)<br />
&nbsp; &nbsp; MaxPower &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;100mA<br />
&nbsp; &nbsp; Interface Descriptor:<br />
&nbsp; &nbsp; &nbsp; bLength &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 9<br />
&nbsp; &nbsp; &nbsp; bDescriptorType &nbsp; &nbsp; &nbsp; &nbsp; 4<br />
&nbsp; &nbsp; &nbsp; bInterfaceNumber &nbsp; &nbsp; &nbsp; &nbsp;0<br />
&nbsp; &nbsp; &nbsp; bAlternateSetting &nbsp; &nbsp; &nbsp; 0<br />
&nbsp; &nbsp; &nbsp; bNumEndpoints &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2<br />
&nbsp; &nbsp; &nbsp; bInterfaceClass &nbsp; &nbsp; &nbsp; 255 Vendor Specific Class<br />
&nbsp; &nbsp; &nbsp; bInterfaceSubClass &nbsp; &nbsp; &nbsp;0 <br />
&nbsp; &nbsp; &nbsp; bInterfaceProtocol &nbsp; &nbsp; &nbsp;0 <br />
&nbsp; &nbsp; &nbsp; iInterface &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 <br />
&nbsp; &nbsp; &nbsp; Endpoint Descriptor:<br />
&nbsp; &nbsp; &nbsp; &nbsp; bLength &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 7<br />
&nbsp; &nbsp; &nbsp; &nbsp; bDescriptorType &nbsp; &nbsp; &nbsp; &nbsp; 5<br />
&nbsp; &nbsp; &nbsp; &nbsp; bEndpointAddress &nbsp; &nbsp; 0x01 &nbsp;EP 1 OUT<br />
&nbsp; &nbsp; &nbsp; &nbsp; bmAttributes &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Transfer Type &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Bulk<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Synch Type &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; None<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Usage Type &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Data<br />
&nbsp; &nbsp; &nbsp; &nbsp; wMaxPacketSize &nbsp; &nbsp; 0x0040 &nbsp;1x 64 bytes<br />
&nbsp; &nbsp; &nbsp; &nbsp; bInterval &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0<br />
&nbsp; &nbsp; &nbsp; Endpoint Descriptor:<br />
&nbsp; &nbsp; &nbsp; &nbsp; bLength &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 7<br />
&nbsp; &nbsp; &nbsp; &nbsp; bDescriptorType &nbsp; &nbsp; &nbsp; &nbsp; 5<br />
&nbsp; &nbsp; &nbsp; &nbsp; bEndpointAddress &nbsp; &nbsp; 0x81 &nbsp;EP 1 IN<br />
&nbsp; &nbsp; &nbsp; &nbsp; bmAttributes &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Transfer Type &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Bulk<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Synch Type &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; None<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Usage Type &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Data<br />
&nbsp; &nbsp; &nbsp; &nbsp; wMaxPacketSize &nbsp; &nbsp; 0x0040 &nbsp;1x 64 bytes<br />
&nbsp; &nbsp; &nbsp; &nbsp; bInterval &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0<br />
Device Status: &nbsp; &nbsp; 0x0000<br />
&nbsp; (Bus Powered)</div></div>
<p>Aside from the control endpoint there are two bulk endpoints. (If you are not that familiar with USB, you perhaps want to read some pages from <a href="http://lwn.net/images/pdf/LDD3/ch13.pdf">chapter 13 &#8220;USB Drivers&#8221;</a> from the book <a href="http://lwn.net/Kernel/LDD3/">Linux Device Drivers</a>, which is available from O&#8217;Reilly and also freely available as pdf under creative commons license.) The endpoint with address 0&#215;01 transfers data from the computer to the device (OUT endpoint &#8211; this direction is called &#8220;down&#8221;) and the endpoint with address 0&#215;81 transfers data from the device to the computer (IN endpoint &#8211; this direction is called &#8220;up&#8221;). </p>
<p>But how do we get the logging data from the device? Since Arexx neither answered my emails nor responded to the forum posts, I used the Windows USB Sniffer <a href="http://sourceforge.net/projects/usbsnoop/"><em>SnoopyPro</em></a> for unscrambling the communication.</p>
<p>If you have installed <a href="http://sourceforge.net/projects/usbsnoop/"><em>SnoopyPro</em></a> and started, first open &#8220;<em>View -> USB Devices</em>&#8220;, then select &#8220;<em>File -> Unpack drivers</em>&#8221; and after that &#8220;<em>File -> Install Service</em>&#8221; from the menu of the USB Devices window. Right-click on the appropriate device (the prior <tt>lsusb</tt> told us the ID 0451:3211) and select &#8220;<em>Install and Restart</em>&#8220;.</p>
<p><a href="http://www.algorithm-forge.com/techblog/wp-content/uploads/2009/12/SnoopyPro.png"><img src="http://www.algorithm-forge.com/techblog/wp-content/uploads/2009/12/SnoopyPro.png" alt="Start the USB Sniffer for the TL-500" title="Start the USB Sniffer for the TL-500" width="645" height="402" class="aligncenter size-full wp-image-230" /></a></p>
<p>An internal window &#8220;USBLog1&#8243; should open, counting the recorded packets. If you have recorded enough, stop the recording and take a look at the output:</p>
<div id="attachment_235" class="wp-caption aligncenter" style="width: 508px"><a href="http://www.algorithm-forge.com/techblog/wp-content/uploads/2009/12/log2.png"><img src="http://www.algorithm-forge.com/techblog/wp-content/uploads/2009/12/log2.png" alt="Watching the Windows driver communicating with the device" title="Watching the Windows driver communicating with the device" width="498" height="675" class="size-full wp-image-235" /></a><p class="wp-caption-text">Watching the Windows driver communicating with the device</p></div>
<p>After a few times repeating this process the following pattern emerges:</p>
<ul>
<li>The computer sends data starting with 0&#215;04.</li>
<li>After that he sends data starting with 0&#215;03. The TL-500 responds with data that starts with 0&#215;0000 or with data that actually contains logging data. This repeats ad infinitum.</li>
<li>Rarely the loop hangs and the computer resends again data starting with 0&#215;03.</li>
</ul>
<p>Fortunately with the library <a href="http://www.libusb.org/">libusb</a> nowadays writing a USB device driver is no big problem. (If you don&#8217;t want to use libusb but instead get involved with the kernel programming, take a look at the tutorial <a href="http://matthias.vallentin.cc/2007/04/writing-a-linux-kernel-driver-for-an-unknown-usb-device/">Writing a Linux kernel driver for an unknown USB device</a> from Matthias Vallentin.) But let&#8217;s start with libusb and <a href="http://www.libusb.org/browser/examples/lsusb.c">their example file</a> under <a href="http://www.gnu.org/licenses/lgpl.html">LGPL</a> and implement what we have discovered so far:</p>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">#include &lt;stdio.h&gt;</span><br />
<span style="color: #339933;">#include &lt;sys/types.h&gt;</span><br />
<span style="color: #339933;">#include &lt;unistd.h&gt;</span><br />
<span style="color: #339933;">#include &lt;libusb.h&gt;</span><br />
<br />
uint16_t VENDOR <span style="color: #339933;">=</span> <span style="color: #0000dd;">1105</span><span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/* =0x0451 */</span><br />
uint16_t PRODUCT <span style="color: #339933;">=</span> <span style="color: #0000dd;">12817</span><span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/* =0x3211 */</span><br />
<br />
libusb_device_handle<span style="color: #339933;">**</span> find_tl500<span style="color: #009900;">&#40;</span>libusb_device <span style="color: #339933;">**</span>devs<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; libusb_device <span style="color: #339933;">*</span>dev<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; libusb_device_handle <span style="color: #339933;">**</span>handle<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Trying to find Arexx logging system.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>dev <span style="color: #339933;">=</span> devs<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> NULL<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">struct</span> libusb_device_descriptor desc<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">int</span> r <span style="color: #339933;">=</span> libusb_get_device_descriptor<span style="color: #009900;">&#40;</span>dev<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>desc<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>r <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fprintf<span style="color: #009900;">&#40;</span>stderr<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;failed to get device descriptor&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> NULL<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%04x:%04x (bus %d, device %d)<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; desc.<span style="color: #202020;">idVendor</span><span style="color: #339933;">,</span> desc.<span style="color: #202020;">idProduct</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; libusb_get_bus_number<span style="color: #009900;">&#40;</span>dev<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> libusb_get_device_address<span style="color: #009900;">&#40;</span>dev<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>desc.<span style="color: #202020;">idVendor</span> <span style="color: #339933;">==</span> VENDOR <span style="color: #339933;">&amp;&amp;</span> desc.<span style="color: #202020;">idProduct</span> <span style="color: #339933;">==</span> PRODUCT<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Found Arexx TL-500.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">int</span> usb_open <span style="color: #339933;">=</span> libusb_open<span style="color: #009900;">&#40;</span>dev<span style="color: #339933;">,</span> handle<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>usb_open<span style="color: #339933;">==</span>0<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;libusb_open successful.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> handle<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fprintf<span style="color: #009900;">&#40;</span>stderr<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;libusb_open failed. Error code %d.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> usb_open<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> handle<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> NULL<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; libusb_device <span style="color: #339933;">**</span>devs<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> r<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; ssize_t cnt<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; r <span style="color: #339933;">=</span> libusb_init<span style="color: #009900;">&#40;</span>NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>r <span style="color: #339933;">&lt;</span> 0<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> r<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; cnt <span style="color: #339933;">=</span> libusb_get_device_list<span style="color: #009900;">&#40;</span>NULL<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>devs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>cnt <span style="color: #339933;">&lt;</span> 0<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span><span style="color: #009900;">&#41;</span> cnt<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; libusb_device_handle <span style="color: #339933;">**</span>handle <span style="color: #339933;">=</span> find_tl500<span style="color: #009900;">&#40;</span>devs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>handle<span style="color: #339933;">==</span>NULL<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; fprintf<span style="color: #009900;">&#40;</span>stderr<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;No logging system found.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; libusb_free_device_list<span style="color: #009900;">&#40;</span>devs<span style="color: #339933;">,</span> 1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; libusb_exit<span style="color: #009900;">&#40;</span>NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> ENDPOINT_DOWN <span style="color: #339933;">=</span> <span style="color: #208080;">0x1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> ENDPOINT_UP <span style="color: #339933;">=</span> <span style="color: #208080;">0x81</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> actual_length<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> dataUp<span style="color: #009900;">&#91;</span>64<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> dataDown<span style="color: #009900;">&#91;</span>64<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> j<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>j<span style="color: #339933;">&lt;</span><span style="color: #0000dd;">64</span><span style="color: #339933;">;</span>j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> dataDown<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; dataDown<span style="color: #009900;">&#91;</span>0<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">4</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; libusb_bulk_transfer<span style="color: #009900;">&#40;</span>handle<span style="color: #009900;">&#91;</span>0<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> ENDPOINT_DOWN<span style="color: #339933;">,</span> dataDown<span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>dataDown<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>actual_length<span style="color: #339933;">,</span> 0<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; dataDown<span style="color: #009900;">&#91;</span>0<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">3</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> i<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span><span style="color: #0000dd;">10000</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; r <span style="color: #339933;">=</span> libusb_bulk_transfer<span style="color: #009900;">&#40;</span>handle<span style="color: #009900;">&#91;</span>0<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> ENDPOINT_DOWN<span style="color: #339933;">,</span> dataDown<span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>dataDown<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>actual_length<span style="color: #339933;">,</span> 0<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; r <span style="color: #339933;">=</span> libusb_bulk_transfer<span style="color: #009900;">&#40;</span>handle<span style="color: #009900;">&#91;</span>0<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> ENDPOINT_UP<span style="color: #339933;">,</span> dataUp<span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>dataUp<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>actual_length<span style="color: #339933;">,</span> 1000<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>r <span style="color: #339933;">==</span> 0 <span style="color: #339933;">&amp;&amp;</span> actual_length <span style="color: #339933;">==</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>dataUp<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>dataUp<span style="color: #009900;">&#91;</span>0<span style="color: #009900;">&#93;</span><span style="color: #339933;">!=</span>0 <span style="color: #339933;">||</span> dataUp<span style="color: #009900;">&#91;</span>1<span style="color: #009900;">&#93;</span><span style="color: #339933;">!=</span>0<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Data: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> j<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>j<span style="color: #339933;">&lt;</span><span style="color: #0000dd;">64</span><span style="color: #339933;">;</span>j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%02x &quot;</span><span style="color: #339933;">,</span>dataUp<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sleep<span style="color: #009900;">&#40;</span>1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Something went wrong (r == %i, actual_length == %i , sizeof(data) == %lu ).<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;r<span style="color: #339933;">,</span> actual_length<span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>dataUp<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; libusb_free_device_list<span style="color: #009900;">&#40;</span>devs<span style="color: #339933;">,</span> 1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; libusb_exit<span style="color: #009900;">&#40;</span>NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>If you want to test it, download <a href="http://www.algorithm-forge.com/techblog/wp-content/uploads/2009/12/tl500.cpp">this file</a> with the code above and compile it (make sure you have the libusb development files installed &#8211; e.g. under Ubuntu the package libusb-dev):</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">g++</span> -I<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>include<span style="color: #000000; font-weight: bold;">/</span>libusb-1.0<span style="color: #000000; font-weight: bold;">/</span> <span style="color: #660033;">-c</span> tl500.cpp -otl500.o<br />
<span style="color: #c20cb9; font-weight: bold;">g++</span> <span style="color: #660033;">-otl500</span> tl500.o -lusb-<span style="color: #000000;">1.0</span></div></div>
<p>If you run the executable tl500, it should find the TL-500 and will request data from it:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Trying to find Arexx logging system.<br />
1d6b:0002 (bus 1, device 1)<br />
1d6b:0002 (bus 2, device 1)<br />
1d6b:0001 (bus 3, device 1)<br />
1d6b:0001 (bus 4, device 1)<br />
0451:3211 (bus 4, device 3)<br />
Found Arexx TL-500.<br />
Data: 00 0a 07 48 05 b4 04 00 00 00 0b 00 ff 01 00 00 00 01 09 02 19 00 01 01 00 80 32 09 04 00 00 01 ff 00 00 00 07 05 01 02 40 00 00 36 21 00 00 f1 b5 ad 76 af 7d c1 f2 47 6a 7f bf ed 31 2e 3f 09 <br />
Data: 00 0a cc 22 0a e7 1e 00 00 00 07 00 ff 01 00 00 00 01 09 02 19 00 01 01 00 80 32 09 04 00 00 01 ff 00 00 00 07 05 01 02 40 00 00 36 21 00 00 f1 b5 ad 76 af 7d c1 f2 47 6a 7f bf ed 31 2e 3f 09 <br />
Data: 00 0a 06 48 17 fe 2b 00 00 00 14 00 ff 01 00 00 00 01 09 02 19 00 01 01 00 80 32 09 04 00 00 01 ff 00 00 00 07 05 01 02 40 00 00 36 21 00 00 f1 b5 ad 76 af 7d c1 f2 47 6a 7f bf ed 31 2e 3f 09 <br />
Data: 00 0a db 45 05 67 3b 00 00 00 0c 0a 72 22 0b 07 3b 00 00 00 0a 00 01 01 00 80 32 09 04 00 00 01 ff 00 00 00 07 05 01 02 40 00 00 36 21 00 00 f1 b5 ad 76 af 7d c1 f2 47 6a 7f bf ed 31 2e 3f 09 <br />
Data: 00 0a cc 22 0a e7 50 00 00 00 07 00 ff 22 0b 07 3b 00 00 00 0a 00 01 01 00 80 32 09 04 00 00 01 ff 00 00 00 07 05 01 02 40 00 00 36 21 00 00 f1 b5 ad 76 af 7d c1 f2 47 6a 7f bf ed 31 2e 3f 09</div></div>
<p>How to decipher these data rows to meaningful logging data will be a blog post on its own.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm-forge.com/techblog/2009/12/using-libusb-to-write-a-linux-usb-driver-for-the-arexx-tl-500-part-i/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Because it is autumn</title>
		<link>http://www.algorithm-forge.com/techblog/2009/10/because-it-is-autumn/</link>
		<comments>http://www.algorithm-forge.com/techblog/2009/10/because-it-is-autumn/#comments</comments>
		<pubDate>Fri, 16 Oct 2009 14:19:26 +0000</pubDate>
		<dc:creator>Kornelius Rohmeyer</dc:creator>
				<category><![CDATA[Graphics]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[Fun]]></category>

		<guid isPermaLink="false">http://www.algorithm-forge.com/techblog/?p=189</guid>
		<description><![CDATA[library&#40;colorspace&#41; branchvar &#60;- 1 tree &#60;- function&#40;x, y, branch&#41; &#123; &#160; lines&#40;x,y,col=hex&#40;RGB&#40;0.1,0.4,0&#41;&#41;&#41; &#160; wadd &#60;- 0.7 &#160; if&#40;branch&#62;0&#41; &#123; &#160; &#160; alpha &#60;- atan2&#40;&#40;y&#91;2&#93;-y&#91;1&#93;&#41;,&#40;x&#91;2&#93;-x&#91;1&#93;&#41;&#41; &#160; &#160; len &#60;- sqrt&#40;&#40;y&#91;2&#93;-y&#91;1&#93;&#41;^2+&#40;x&#91;2&#93;-x&#91;1&#93;&#41;^2&#41;*0.6 &#160; &#160; tree&#40;c&#40;x&#91;2&#93;,x&#91;2&#93;+abs&#40;rnorm&#40;1,1,branchvar&#41;&#41;*len*cos&#40;alpha&#41;&#41;, &#160; &#160; &#160; &#160; &#160; c&#40;y&#91;2&#93;,y&#91;2&#93;+abs&#40;rnorm&#40;1,1,branchvar&#41;&#41;*len*sin&#40;alpha&#41;&#41;, &#160; &#160; &#8230; <a href="http://www.algorithm-forge.com/techblog/2009/10/because-it-is-autumn/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div id="attachment_194" class="wp-caption alignnone" style="width: 460px"><img src="http://www.algorithm-forge.com/techblog/wp-content/uploads/2009/10/autumn.png" alt="autumn" title="autumn" width="450" height="420" class="size-full wp-image-194" /><p class="wp-caption-text">autumn</p></div>
<div class="codecolorer-container r default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="r codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">library<span style="color: #66cc66;">&#40;</span>colorspace<span style="color: #66cc66;">&#41;</span><br />
branchvar <span style="color: #78aaac;">&lt;-</span> 1<br />
tree <span style="color: #78aaac;">&lt;-</span> <span style="color: #a020f0;">function</span><span style="color: #66cc66;">&#40;</span>x, y, branch<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; lines<span style="color: #66cc66;">&#40;</span>x,y,col<span style="color: #78aaac;">=</span>hex<span style="color: #66cc66;">&#40;</span>RGB<span style="color: #66cc66;">&#40;</span>0.1,0.4,0<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; wadd <span style="color: #78aaac;">&lt;-</span> 0.7<br />
&nbsp; <span style="color: #a020f0;">if</span><span style="color: #66cc66;">&#40;</span>branch<span style="color: #78aaac;">&gt;</span>0<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; alpha <span style="color: #78aaac;">&lt;-</span> atan2<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>y<span style="color: #66cc66;">&#91;</span>2<span style="color: #66cc66;">&#93;</span><span style="color: #78aaac;">-</span>y<span style="color: #66cc66;">&#91;</span>1<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>,<span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">&#91;</span>2<span style="color: #66cc66;">&#93;</span><span style="color: #78aaac;">-</span>x<span style="color: #66cc66;">&#91;</span>1<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; len <span style="color: #78aaac;">&lt;-</span> sqrt<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>y<span style="color: #66cc66;">&#91;</span>2<span style="color: #66cc66;">&#93;</span><span style="color: #78aaac;">-</span>y<span style="color: #66cc66;">&#91;</span>1<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #78aaac;">^</span>2<span style="color: #78aaac;">+</span><span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">&#91;</span>2<span style="color: #66cc66;">&#93;</span><span style="color: #78aaac;">-</span>x<span style="color: #66cc66;">&#91;</span>1<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #78aaac;">^</span>2<span style="color: #66cc66;">&#41;</span><span style="color: #78aaac;">*</span>0.6<br />
&nbsp; &nbsp; tree<span style="color: #66cc66;">&#40;</span>c<span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">&#91;</span>2<span style="color: #66cc66;">&#93;</span>,x<span style="color: #66cc66;">&#91;</span>2<span style="color: #66cc66;">&#93;</span><span style="color: #78aaac;">+</span>abs<span style="color: #66cc66;">&#40;</span>rnorm<span style="color: #66cc66;">&#40;</span>1,1,branchvar<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #78aaac;">*</span>len<span style="color: #78aaac;">*</span>cos<span style="color: #66cc66;">&#40;</span>alpha<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c<span style="color: #66cc66;">&#40;</span>y<span style="color: #66cc66;">&#91;</span>2<span style="color: #66cc66;">&#93;</span>,y<span style="color: #66cc66;">&#91;</span>2<span style="color: #66cc66;">&#93;</span><span style="color: #78aaac;">+</span>abs<span style="color: #66cc66;">&#40;</span>rnorm<span style="color: #66cc66;">&#40;</span>1,1,branchvar<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #78aaac;">*</span>len<span style="color: #78aaac;">*</span>sin<span style="color: #66cc66;">&#40;</span>alpha<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; branch<span style="color: #78aaac;">-</span>1<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; tree<span style="color: #66cc66;">&#40;</span>c<span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">&#91;</span>2<span style="color: #66cc66;">&#93;</span>,x<span style="color: #66cc66;">&#91;</span>2<span style="color: #66cc66;">&#93;</span><span style="color: #78aaac;">+</span>len<span style="color: #78aaac;">*</span>cos<span style="color: #66cc66;">&#40;</span>alpha<span style="color: #78aaac;">+</span>wadd<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c<span style="color: #66cc66;">&#40;</span>y<span style="color: #66cc66;">&#91;</span>2<span style="color: #66cc66;">&#93;</span>,y<span style="color: #66cc66;">&#91;</span>2<span style="color: #66cc66;">&#93;</span><span style="color: #78aaac;">+</span>len<span style="color: #78aaac;">*</span>sin<span style="color: #66cc66;">&#40;</span>alpha<span style="color: #78aaac;">+</span>wadd<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>,branch<span style="color: #78aaac;">-</span>1<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; tree<span style="color: #66cc66;">&#40;</span>c<span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">&#91;</span>2<span style="color: #66cc66;">&#93;</span>,x<span style="color: #66cc66;">&#91;</span>2<span style="color: #66cc66;">&#93;</span><span style="color: #78aaac;">+</span>len<span style="color: #78aaac;">*</span>cos<span style="color: #66cc66;">&#40;</span>alpha<span style="color: #78aaac;">-</span>wadd<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c<span style="color: #66cc66;">&#40;</span>y<span style="color: #66cc66;">&#91;</span>2<span style="color: #66cc66;">&#93;</span>,y<span style="color: #66cc66;">&#91;</span>2<span style="color: #66cc66;">&#93;</span><span style="color: #78aaac;">+</span>len<span style="color: #78aaac;">*</span>sin<span style="color: #66cc66;">&#40;</span>alpha<span style="color: #78aaac;">-</span>wadd<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>,branch<span style="color: #78aaac;">-</span>1<span style="color: #66cc66;">&#41;</span> &nbsp; &nbsp;<br />
&nbsp; <span style="color: #66cc66;">&#125;</span> <span style="color: #a020f0;">else</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; points<span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span>,y<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span>,pch<span style="color: #78aaac;">=</span><span style="color: #cc66cc;">16</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;col<span style="color: #78aaac;">=</span>hex<span style="color: #66cc66;">&#40;</span>RGB<span style="color: #66cc66;">&#40;</span>rbeta<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">2.6</span>,<span style="color: #cc66cc;">0.5</span><span style="color: #66cc66;">&#41;</span>,rbeta<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0.5</span>,<span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span>,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span><br />
<br />
plot.new<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><br />
tree<span style="color: #66cc66;">&#40;</span>c<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0.20</span><span style="color: #66cc66;">&#41;</span>,c<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0.20</span><span style="color: #66cc66;">&#41;</span>,<span style="color: #cc66cc;">7</span><span style="color: #66cc66;">&#41;</span></div></div>
<p>You need library colorspace.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm-forge.com/techblog/2009/10/because-it-is-autumn/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Shortcuts in R under Unix from the readline library</title>
		<link>http://www.algorithm-forge.com/techblog/2009/09/shortcuts-in-r-under-unix-from-the-readline-library/</link>
		<comments>http://www.algorithm-forge.com/techblog/2009/09/shortcuts-in-r-under-unix-from-the-readline-library/#comments</comments>
		<pubDate>Tue, 15 Sep 2009 13:49:45 +0000</pubDate>
		<dc:creator>Kornelius Rohmeyer</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[R]]></category>

		<guid isPermaLink="false">http://www.algorithm-forge.com/techblog/?p=121</guid>
		<description><![CDATA[Under Unix you can use in R the the advanced features for command editing and command history that the GNU Readline Library provides. Both Emacs and vi editing modes are available and the Emacs-like keybindings are installed by default. Here &#8230; <a href="http://www.algorithm-forge.com/techblog/2009/09/shortcuts-in-r-under-unix-from-the-readline-library/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Under Unix you can use in R the the advanced features for command editing and command history that <a href="http://tiswww.case.edu/php/chet/readline/rltop.html">the GNU Readline Library</a> provides.</p>
<p>Both Emacs and vi editing modes are available and the Emacs-like keybindings are installed by default. Here some Emacs keybindings that also work in R (from the <a href="http://tiswww.case.edu/php/chet/readline/readline.html">documentation of the GNU Readline Library</a>):</p>
<p><strong>Copy and Paste:</strong><br />
<em>Ctrl-u</em> 	Cut from the cursor to the beginning of the line.<br />
<em>Ctrl-k</em> 	Cut from the cursor to the end of the line.<br />
<em>Ctrl-w</em> 	Cut from the cursor to the start of the word.<br />
<em>Ctrl-y</em> 	Pastes text from the clipboard.<br />
<em>Alt-y</em>         Rotate the kill-ring, and paste the new top. You can only do this if the prior command was Ctrl-y or Alt-y.</p>
<p><strong>Moving:</strong><br />
<em>Ctrl-a</em> 	Move to the start of the line.<br />
<em>Ctrl-e</em> 	Move to the end of the line.<br />
<em>Alt-b</em>  	Move back one word.<br />
<em>Alt-f </em> 	Move forward one word.<br />
<em>Ctrl-b</em> 	Move back one character.<br />
<em>Ctrl-f</em> 	Move forward one character.</p>
<p><strong>Undo:</strong><br />
<em>Ctrl-_</em> 	Undo the last changes.<br />
<em>Alt-r</em>  	Undo all changes to the line.</p>
<p><strong>Miscellaneous:</strong><br />
<em>Ctrl-l</em>  	Clear the screen leaving the current line at the top of the screen.</p>
<p><strong>History:</strong><br />
<em>Ctrl-r</em> 	Incremental reverse search of history.<br />
<em>Alt-p</em>  	Non-incremental reverse search of history.</p>
<p>If you want to use the vi-mode just press Ctrl+Alt+j and you can use the usual vi modes and commands (for example take a look at this <a href="http://www.catonmat.net/download/bash-vi-editing-mode-cheat-sheet.pdf">vi-editing-mode-cheat-sheet</a>).</p>
<p>If you want to start in the vi mode by default, put the following line in your <tt>~/.inputrc</tt> (which of course will also effect your bash etc.):</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">set</span> editing-mode <span style="color: #c20cb9; font-weight: bold;">vi</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm-forge.com/techblog/2009/09/shortcuts-in-r-under-unix-from-the-readline-library/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OpenOffice advanced fill down</title>
		<link>http://www.algorithm-forge.com/techblog/2009/08/openoffice-advanced-fill-down/</link>
		<comments>http://www.algorithm-forge.com/techblog/2009/08/openoffice-advanced-fill-down/#comments</comments>
		<pubDate>Sun, 16 Aug 2009 21:30:12 +0000</pubDate>
		<dc:creator>Kornelius Rohmeyer</dc:creator>
				<category><![CDATA[OpenOffice]]></category>
		<category><![CDATA[Preprocessing]]></category>

		<guid isPermaLink="false">http://www.algorithm-forge.com/techblog/?p=29</guid>
		<description><![CDATA[When people come to me for statistical advise, they often bring Excel files containing data like the following, where values are only written down when they change. But often it is required for further analysis that each row is completely &#8230; <a href="http://www.algorithm-forge.com/techblog/2009/08/openoffice-advanced-fill-down/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>When people come to me for statistical advise, they often bring Excel files containing data like the following, where values are only written down when they change. But often it is required for further analysis that each row is completely filled with the correct values. There is a basic &#8220;<em>fill down</em>&#8221; option in OpenOffice (<tt>CTRL+D</tt>), but up to my knowledge there is unfortunately no extension for this case.</p>
<p align="center"><img class="alignnone size-full wp-image-31" title="filldown" src="http://www.algorithm-forge.com/techblog/wp-content/uploads/2009/07/filldown.gif" alt="filldown" width="351" height="348" /></p>
<p>Years ago I wrote the following lines in Visual Basic for a small company I was visiting that even employed a student assistant for filling out these missing values in enormous automatically generated Excel files (I did this in just a few minutes and of course free of charge, that&#8217;s why it is so basic &#8211; I just couldn&#8217;t see people doing such dreary tasks):</p>
<div class="codecolorer-container vb default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000080;">Sub</span> MakroAdvancedFillDown()<br />
&nbsp; <span style="color: #000080;">For</span> <span style="color: #000080;">Each</span> cell <span style="color: #000080;">In</span> Range(<span style="color: #800000;">&quot;h1:h13215&quot;</span>) <br />
&nbsp; &nbsp; <span style="color: #000080;">If</span> cell.Value = <span style="color: #800000;">&quot; &quot;</span> <span style="color: #000080;">Or</span> cell.Value = <span style="color: #800000;">&quot;&quot;</span> <span style="color: #000080;">Then</span> <br />
&nbsp; &nbsp; &nbsp; cell.Value = old <br />
&nbsp; &nbsp; <span style="color: #000080;">End</span> <span style="color: #000080;">If</span> <br />
&nbsp; &nbsp; old = cell.Value <br />
&nbsp; <span style="color: #000080;">Next</span> cell <br />
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span></div></div>
<p>And given that last week someone with a really large table with the same problem came into my office I took an hour to learn StarOffice Basic and to write the following Macro:</p>
<div class="codecolorer-container vb default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000080;">SUB</span> AdvancedFillDown<br />
&nbsp; oSel = ThisComponent.getCurrentSelection()<br />
&nbsp; oAdr = oSel.rangeAddress<br />
&nbsp; oSheet = ThisComponent.CurrentController.ActiveSheet &nbsp; <br />
&nbsp; <span style="color: #000080;">IF</span> <span style="color: #000080;">NOT</span> oSel.supportsService(<span style="color: #800000;">&quot;com.sun.star.sheet.SheetCellRange&quot;</span>) <span style="color: #000080;">then</span><br />
&nbsp; &nbsp; &nbsp; MsgBox <span style="color: #800000;">&quot;Error: Please select some cells for this makro.&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000080;">EXIT</span> <span style="color: #000080;">SUB</span><br />
&nbsp; <span style="color: #000080;">END</span> <span style="color: #000080;">IF</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; <br />
&nbsp; <span style="color: #000080;">FOR</span> j = oAdr.startColumn <span style="color: #000080;">TO</span> oAdr.EndColumn<br />
&nbsp; &nbsp; <span style="color: #000080;">FOR</span> i = oAdr.startRow <span style="color: #000080;">TO</span> oAdr.EndRow<br />
&nbsp; &nbsp; &nbsp; oCell=oSheet.getCellByPosition(j,i)<br />
&nbsp; &nbsp; &nbsp; <span style="color: #000080;">IF</span> oCell.<span style="color: #000080;">string</span> = <span style="color: #800000;">&quot;&quot;</span> <span style="color: #000080;">THEN</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; oSource = oSheet.getCellRangeByPosition(j,i-1,j,i-1)<br />
&nbsp; &nbsp; &nbsp; &nbsp; oSourceAdr = oSource.getRangeAddress<br />
&nbsp; &nbsp; &nbsp; &nbsp; oSheet.copyRange(oCell.getCellAddress,oSourceAdr)<br />
&nbsp; &nbsp; &nbsp; <span style="color: #000080;">END</span> <span style="color: #000080;">IF</span><br />
&nbsp; &nbsp; <span style="color: #000080;">NEXT</span><br />
&nbsp; <span style="color: #000080;">NEXT</span><br />
<span style="color: #000080;">END</span> <span style="color: #000080;">SUB</span></div></div>
<p>Now I can deal with this problem with just one click.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm-forge.com/techblog/2009/08/openoffice-advanced-fill-down/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Detexify</title>
		<link>http://www.algorithm-forge.com/techblog/2009/08/detexify/</link>
		<comments>http://www.algorithm-forge.com/techblog/2009/08/detexify/#comments</comments>
		<pubDate>Sun, 16 Aug 2009 20:11:57 +0000</pubDate>
		<dc:creator>Kornelius Rohmeyer</dc:creator>
				<category><![CDATA[LaTeX]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[Fun]]></category>

		<guid isPermaLink="false">http://www.algorithm-forge.com/techblog/?p=51</guid>
		<description><![CDATA[A useful link today: Detexify2 &#8211; LaTeX symbol classifier And don&#8217;t forget to take a look at Detexify explained. I love this tool! It&#8217;s useful and also really fun to train.]]></description>
			<content:encoded><![CDATA[<p>A useful link today: <a href="http://detexify.kirelabs.org/classify.html">Detexify2 &#8211; LaTeX symbol classifier</a></p>
<div id="attachment_92" class="wp-caption alignnone" style="width: 418px"><img src="http://www.algorithm-forge.com/techblog/wp-content/uploads/2009/08/Detexify.png" alt="Detexify in action" title="Detexify in action" width="408" height="389" class="size-full wp-image-92" /><p class="wp-caption-text">Detexify in action</p></div>
<p>And don&#8217;t forget to take a look at <a href="http://detexifyblog.kirelabs.org/past/2009/7/19/detexify_explained/">Detexify explained</a>.</p>
<p>I love this tool! It&#8217;s useful and also really fun to train. <img src='http://www.algorithm-forge.com/techblog/wp-includes/images/smilies/icon_biggrin.gif' alt=':-D' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm-forge.com/techblog/2009/08/detexify/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Random Correlation Matrices</title>
		<link>http://www.algorithm-forge.com/techblog/2009/08/random-correlation-matrices/</link>
		<comments>http://www.algorithm-forge.com/techblog/2009/08/random-correlation-matrices/#comments</comments>
		<pubDate>Fri, 14 Aug 2009 12:43:17 +0000</pubDate>
		<dc:creator>Kornelius Rohmeyer</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[Statistics]]></category>
		<category><![CDATA[Simulations]]></category>

		<guid isPermaLink="false">http://www.algorithm-forge.com/techblog/?p=56</guid>
		<description><![CDATA[Some time ago one of my colleagues with a new method for evaluating the cumulative distribution function of a multivariate normal distribution wanted to compare the speed of his method with that of randomized quasi-Monte Carlo methods. While we were &#8230; <a href="http://www.algorithm-forge.com/techblog/2009/08/random-correlation-matrices/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Some time ago one of my colleagues with a new method for evaluating the cumulative distribution function of a multivariate normal distribution wanted to compare the speed of his method with that of randomized quasi-Monte Carlo methods. While we were going to lunch, he asked me how to generate random correlation matrices, because the speed of his method depends strongly on the <a href="http://en.wikipedia.org/wiki/Correlation#Correlation_matrices">correlation matrix</a> and he wanted to have some sort of average.</p>
<p><strong>But what is a random correlation matrix?</strong></p>
<p>Let&#8217;s first give a characterization of correlation matrices.</p>
<p>It is well known that for a matrix <img src='http://s.wordpress.com/latex.php?latex=C%3A%3D%28c_%7Bi%2Cj%7D%29_%7B1%5Cleq%20i%2Cj%5Cleq%20n%7D%5Cin%5Cmathbb%7BR%7D%5E%7Bn%5Ctimes%20n%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='C:=(c_{i,j})_{1\leq i,j\leq n}\in\mathbb{R}^{n\times n}' title='C:=(c_{i,j})_{1\leq i,j\leq n}\in\mathbb{R}^{n\times n}' class='latex' /> there exist (multivariate normal distributed) random variables <img src='http://s.wordpress.com/latex.php?latex=X%2CY&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='X,Y' title='X,Y' class='latex' /> with <img src='http://s.wordpress.com/latex.php?latex=%5Cdisplaystyle%20%5Ctext%7BCor%7D%28X%2CY%29%3A%3D%5Cleft%28%5Cfrac%7B%5Ctext%7BCov%7D%28X_i%2CY_j%29%7D%7B%5Csqrt%7B%5Ctext%7BVar%7D%28X_i%29%5Ctext%7BVar%7D%28Y_j%29%7D%7D%5Cright%29_%7B1%5Cleq%20i%2Cj%5Cleq%20n%7D%3DC&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\displaystyle \text{Cor}(X,Y):=\left(\frac{\text{Cov}(X_i,Y_j)}{\sqrt{\text{Var}(X_i)\text{Var}(Y_j)}}\right)_{1\leq i,j\leq n}=C' title='\displaystyle \text{Cor}(X,Y):=\left(\frac{\text{Cov}(X_i,Y_j)}{\sqrt{\text{Var}(X_i)\text{Var}(Y_j)}}\right)_{1\leq i,j\leq n}=C' class='latex' /> if and only if </p>
<ol>
<li><img src='http://s.wordpress.com/latex.php?latex=-1%5Cleq%20c_%7Bi%2Cj%7D%5Cleq%201&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='-1\leq c_{i,j}\leq 1' title='-1\leq c_{i,j}\leq 1' class='latex' /> for all <img src='http://s.wordpress.com/latex.php?latex=i%2Cj%5Cin%5C%7B0%2C%5Cldots%2Cn%5C%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='i,j\in\{0,\ldots,n\}' title='i,j\in\{0,\ldots,n\}' class='latex' />,</li>
<li><img src='http://s.wordpress.com/latex.php?latex=c_%7Bi%2Ci%7D%3D%201&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='c_{i,i}= 1' title='c_{i,i}= 1' class='latex' /> for all <img src='http://s.wordpress.com/latex.php?latex=i%5Cin%5C%7B0%2C%5Cldots%2Cn%5C%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='i\in\{0,\ldots,n\}' title='i\in\{0,\ldots,n\}' class='latex' />,</li>
<li><img src='http://s.wordpress.com/latex.php?latex=C&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='C' title='C' class='latex' /> is symmetric (therefore all eigenvalues <img src='http://s.wordpress.com/latex.php?latex=%5Clambda_1%2C%5Cldots%2C%5Clambda_n&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\lambda_1,\ldots,\lambda_n' title='\lambda_1,\ldots,\lambda_n' class='latex' /> of <img src='http://s.wordpress.com/latex.php?latex=C&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='C' title='C' class='latex' /> are real)</li>
<li>and all eigenvalues of <img src='http://s.wordpress.com/latex.php?latex=C&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='C' title='C' class='latex' /> are greater or equal to zero.</li>
</ol>
<p>But what is the right notion of randomness for these matrices?<br />
For example let&#8217;s look at the orthogonal matrices. In many numerical applications we need uniformly distributed random orthogonal matrices in terms of the Haar measure (See <a href="http://en.wikipedia.org/wiki/Orthogonal_matrix#Randomization">http://en.wikipedia.org/wiki/Orthogonal_matrix#Randomization</a>).</p>
<p>Unfortunately in our case there is no clear, natural notion of randomness. <img src='http://www.algorithm-forge.com/techblog/wp-includes/images/smilies/icon_sad.gif' alt=':-(' class='wp-smiley' /> </p>
<p><strong>Method 1 &#8211; Try and Error:</strong> We generate a matrix fulfilling no. 1, 2 and 3 of the characterization (these matrices are called pseudo correlation matrices) by generating independent pseudo-random numbers uniformly distributed between -1 and 1 for the entries <img src='http://s.wordpress.com/latex.php?latex=c_%7Bi%2Cj%7D%3Dc_%7Bj%2Ci%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='c_{i,j}=c_{j,i}' title='c_{i,j}=c_{j,i}' class='latex' />, <img src='http://s.wordpress.com/latex.php?latex=1%5Cleq%20i%3Cj%5Cleq%20n&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='1\leq i&lt;j\leq n' title='1\leq i&lt;j\leq n' class='latex' />.</p>
<p>If this random symmetric matrix is positive semidefinite (i.e. all eigenvalues of <img src='http://s.wordpress.com/latex.php?latex=C&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='C' title='C' class='latex' /> are greater or equal to zero) than we have the desired result. Otherwise we try again. Here is the corresponding R code:</p>
<div class="codecolorer-container r default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="r codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">random.pseudo.correlation.matrix <span style="color: #78aaac;">&lt;-</span><br />
<span style="color: #a020f0;">function</span><span style="color: #66cc66;">&#40;</span>n<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; a <span style="color: #78aaac;">&lt;-</span> diag<span style="color: #66cc66;">&#40;</span>n<span style="color: #66cc66;">&#41;</span><br />
&nbsp; <span style="color: #a020f0;">for</span><span style="color: #66cc66;">&#40;</span>i <span style="color: #a020f0;">in</span> 1:<span style="color: #66cc66;">&#40;</span>n<span style="color: #78aaac;">-</span>1<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #a020f0;">for</span><span style="color: #66cc66;">&#40;</span>j <span style="color: #a020f0;">in</span> <span style="color: #66cc66;">&#40;</span>i<span style="color: #78aaac;">+</span>1<span style="color: #66cc66;">&#41;</span>:n<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; a<span style="color: #66cc66;">&#91;</span>i,j<span style="color: #66cc66;">&#93;</span> <span style="color: #78aaac;">&lt;-</span> a<span style="color: #66cc66;">&#91;</span>j,i<span style="color: #66cc66;">&#93;</span> <span style="color: #78aaac;">&lt;-</span> runif<span style="color: #66cc66;">&#40;</span>1,<span style="color: #78aaac;">-</span>1,1<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; <span style="color: #a020f0;">return</span><span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#125;</span><br />
<br />
random.correlation.matrix.try.and.error <span style="color: #78aaac;">&lt;-</span><br />
<span style="color: #a020f0;">function</span><span style="color: #66cc66;">&#40;</span>n<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; <span style="color: #a020f0;">repeat</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; a <span style="color: #78aaac;">&lt;-</span> random.pseudo.correlation.matrix<span style="color: #66cc66;">&#40;</span>n<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #a020f0;">if</span> <span style="color: #66cc66;">&#40;</span>min<span style="color: #66cc66;">&#40;</span>eigen<span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#41;</span>$values<span style="color: #66cc66;">&#41;</span><span style="color: #78aaac;">&gt;=</span>0<span style="color: #66cc66;">&#41;</span> <span style="color: #a020f0;">return</span><span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#41;</span><br />
&nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></div></div>
<p>This approach is only reasonable for very small dimensions (try it with <img src='http://s.wordpress.com/latex.php?latex=n%3D6%2C7%2C8&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='n=6,7,8' title='n=6,7,8' class='latex' />).</p>
<p><strong>Method 2 &#8211; Lift the Diagonal:</strong></p>
<p>We denote by <img src='http://s.wordpress.com/latex.php?latex=I&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='I' title='I' class='latex' /> the identity matrix. If <img src='http://s.wordpress.com/latex.php?latex=C&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='C' title='C' class='latex' /> has the eigenvalues <img src='http://s.wordpress.com/latex.php?latex=%5Clambda_1%5Cleq%5Cldots%5Cleq%5Clambda_n&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\lambda_1\leq\ldots\leq\lambda_n' title='\lambda_1\leq\ldots\leq\lambda_n' class='latex' /> then <img src='http://s.wordpress.com/latex.php?latex=%28C%2Ba%5Ccdot%20I%29&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='(C+a\cdot I)' title='(C+a\cdot I)' class='latex' /> has the eigenvalues <img src='http://s.wordpress.com/latex.php?latex=%5Clambda_1%2Ba%5Cleq%5Cldots%5Cleq%5Clambda_n%2Ba&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\lambda_1+a\leq\ldots\leq\lambda_n+a' title='\lambda_1+a\leq\ldots\leq\lambda_n+a' class='latex' /> since <img src='http://s.wordpress.com/latex.php?latex=x&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='x' title='x' class='latex' /> is a solution of <img src='http://s.wordpress.com/latex.php?latex=%5Cdet%28C-x%5Ccdot%20I%29%3D0&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\det(C-x\cdot I)=0' title='\det(C-x\cdot I)=0' class='latex' /> if and only if <img src='http://s.wordpress.com/latex.php?latex=x%2Ba&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='x+a' title='x+a' class='latex' /> is a solution of <img src='http://s.wordpress.com/latex.php?latex=%5Cdet%28C%2Ba%5Ccdot%20I-x%5Ccdot%20I%29%3D%5Cdet%28C-%28x-a%29%5Ccdot%20I%29%3D0&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\det(C+a\cdot I-x\cdot I)=\det(C-(x-a)\cdot I)=0' title='\det(C+a\cdot I-x\cdot I)=\det(C-(x-a)\cdot I)=0' class='latex' />.</p>
<p>So we start again with a pseudo correlation matrix <img src='http://s.wordpress.com/latex.php?latex=C&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='C' title='C' class='latex' />, but instead of retrying when <img src='http://s.wordpress.com/latex.php?latex=C&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='C' title='C' class='latex' /> has negative eigen values, we lift the diagonal by <img src='http://s.wordpress.com/latex.php?latex=%5Clambda_1&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\lambda_1' title='\lambda_1' class='latex' /> and obtain <img src='http://s.wordpress.com/latex.php?latex=C%2B%5Clambda_1%5Ccdot%20I&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='C+\lambda_1\cdot I' title='C+\lambda_1\cdot I' class='latex' />, which is always positive semidefinite. After dividing by <img src='http://s.wordpress.com/latex.php?latex=1%2B%5Clambda_1&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='1+\lambda_1' title='1+\lambda_1' class='latex' /> we have a correlation matrix which is &#8220;some kind of random&#8221;. <img src='http://www.algorithm-forge.com/techblog/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>Unfortunatly the diagonal is accentuated and the smallest eigen value is always zero. We could avoid the second problem by adding <img src='http://s.wordpress.com/latex.php?latex=%5Clambda_1%2Bb&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\lambda_1+b' title='\lambda_1+b' class='latex' /> where <img src='http://s.wordpress.com/latex.php?latex=b&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='b' title='b' class='latex' /> is some random number, but the first remains.</p>
<div class="codecolorer-container r default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="r codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">make.positive.semi.definite <span style="color: #78aaac;">&lt;-</span><br />
<span style="color: #a020f0;">function</span><span style="color: #66cc66;">&#40;</span>a, offset<span style="color: #78aaac;">=</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; <span style="color: #66cc66;">&#40;</span>a <span style="color: #78aaac;">+</span> <span style="color: #66cc66;">&#40;</span>diag<span style="color: #66cc66;">&#40;</span>dim<span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #78aaac;">*</span> <span style="color: #66cc66;">&#40;</span>abs<span style="color: #66cc66;">&#40;</span>min<span style="color: #66cc66;">&#40;</span>eigen<span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#41;</span>$values<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #78aaac;">+</span>offset<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #78aaac;">/</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #78aaac;">+</span><span style="color: #66cc66;">&#40;</span>abs<span style="color: #66cc66;">&#40;</span>min<span style="color: #66cc66;">&#40;</span>eigen<span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#41;</span>$values<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #78aaac;">+</span>offset<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#125;</span></div></div>
<div class="codecolorer-container r default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="r codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">random.correlation.matrix.lift.diagonal <span style="color: #78aaac;">&lt;-</span><br />
<span style="color: #a020f0;">function</span><span style="color: #66cc66;">&#40;</span>n, offset<span style="color: #78aaac;">=</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; a <span style="color: #78aaac;">&lt;-</span> random.pseudo.correlation.matrix<span style="color: #66cc66;">&#40;</span>n<span style="color: #66cc66;">&#41;</span><br />
&nbsp; make.positive.semi.definite<span style="color: #66cc66;">&#40;</span>offset<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#125;</span></div></div>
<p><strong>Method 3 &#8211; Gramian matrix &#8211; my favorite:</strong> Holmes <a href="#holmes">[1]</a> discusses two principal methods for generating random correlation matrices.<br />
One of them is to generate <img src='http://s.wordpress.com/latex.php?latex=n&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='n' title='n' class='latex' /> independent pseudo-random vectors <img src='http://s.wordpress.com/latex.php?latex=t_1%2C%5Cldots%2Ct_n&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='t_1,\ldots,t_n' title='t_1,\ldots,t_n' class='latex' /> distributed uniformly on the unit sphere <img src='http://s.wordpress.com/latex.php?latex=S%5E%7Bn-1%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='S^{n-1}' title='S^{n-1}' class='latex' /> in <img src='http://s.wordpress.com/latex.php?latex=%5Cmathbb%7BR%7D%5En&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\mathbb{R}^n' title='\mathbb{R}^n' class='latex' /> and to use the <a href="http://en.wikipedia.org/wiki/Gramian_matrix">Gram matrix</a> <img src='http://s.wordpress.com/latex.php?latex=T%5EtT&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='T^tT' title='T^tT' class='latex' />, where <img src='http://s.wordpress.com/latex.php?latex=T%3A%3D%28t_1%2C%5Cldots%2Ct_n%29&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='T:=(t_1,\ldots,t_n)' title='T:=(t_1,\ldots,t_n)' class='latex' /> has <img src='http://s.wordpress.com/latex.php?latex=t_i&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='t_i' title='t_i' class='latex' /> as <img src='http://s.wordpress.com/latex.php?latex=i&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='i' title='i' class='latex' />-th column and <img src='http://s.wordpress.com/latex.php?latex=T%5Et&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='T^t' title='T^t' class='latex' /> is the transpose of <img src='http://s.wordpress.com/latex.php?latex=T&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='T' title='T' class='latex' />.</p>
<p>To create the <img src='http://s.wordpress.com/latex.php?latex=t_i&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='t_i' title='t_i' class='latex' /> in R, we load the package <a href="http://cran.r-project.org/web/packages/mvtnorm/index.html">mvtnorm</a>, generate <img src='http://s.wordpress.com/latex.php?latex=%5Ctau_i%5Csim%5Cmathcal%7BN%7D%280%2CI%29&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\tau_i\sim\mathcal{N}(0,I)' title='\tau_i\sim\mathcal{N}(0,I)' class='latex' /> and set <img src='http://s.wordpress.com/latex.php?latex=t_i%3A%3D%5Ctau_i%2F%7C%7C%5Ctau_i%7C%7C&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='t_i:=\tau_i/||\tau_i||' title='t_i:=\tau_i/||\tau_i||' class='latex' />:</p>
<div class="codecolorer-container r default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="r codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">random.correlation.matrix.sphere <span style="color: #78aaac;">&lt;-</span><br />
<span style="color: #a020f0;">function</span><span style="color: #66cc66;">&#40;</span>n<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; require<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;mvtnorm&quot;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; t <span style="color: #78aaac;">&lt;-</span> rmvnorm<span style="color: #66cc66;">&#40;</span>n,rep<span style="color: #66cc66;">&#40;</span>0,n<span style="color: #66cc66;">&#41;</span>,diag<span style="color: #66cc66;">&#40;</span>n<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; <span style="color: #a020f0;">for</span> <span style="color: #66cc66;">&#40;</span>i <span style="color: #a020f0;">in</span> <span style="color: #cc66cc;">1</span>:n<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; t<span style="color: #66cc66;">&#91;</span>i,<span style="color: #66cc66;">&#93;</span> <span style="color: #78aaac;">&lt;-</span> t<span style="color: #66cc66;">&#91;</span>i,<span style="color: #66cc66;">&#93;</span><span style="color: #78aaac;">/</span>sqrt<span style="color: #66cc66;">&#40;</span>t<span style="color: #66cc66;">&#40;</span>t<span style="color: #66cc66;">&#91;</span>i,<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #78aaac;">%*%</span>t<span style="color: #66cc66;">&#91;</span>i,<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; t<span style="color: #78aaac;">%*%</span>t<span style="color: #66cc66;">&#40;</span>t<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#125;</span></div></div>
<p><strong>Conclusion:</strong> There are futher methods (like e.g. to generate a random spectrum and then construct the correlation matrix), which are not all so easy to implement. But as much as the three given methods they all are unsatisfactory in some way because we don&#8217;t really know how random correlation matrices should be distributed.</p>
<p>For my colleague an average of calculation time does only make sense when he knows which kinds of correlation matrices occur in the applications. He decided to describe and compare the different cases individually.</p>
<p>But does it perhaps make sense to use random correlation matrices as test cases or are the special cases more important? For example random correlation matrices generated with method 1 and 3 are only singular with probability zero.</p>
<p>Any critique, comments, suggestions or questions are welcome!</p>
<p><strong>And for the next time:</strong> Given a correlation matrix C. How do we generate tuples of pseudo-random numbers following a given multivariate distribution with correlation matrix C?</p>
<p><strong>Literature:</strong><br />
<a name="holmes">Holmes, R. B. 1991.</a><br />
<em>On random correlation matrices.</em><br />
Siam J. Matrix Anal. Appl., Vol. 12 No. 2: 239-272.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm-forge.com/techblog/2009/08/random-correlation-matrices/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Executable application launcher</title>
		<link>http://www.algorithm-forge.com/techblog/2009/07/executable-application-launcher/</link>
		<comments>http://www.algorithm-forge.com/techblog/2009/07/executable-application-launcher/#comments</comments>
		<pubDate>Thu, 23 Jul 2009 13:44:13 +0000</pubDate>
		<dc:creator>Kornelius Rohmeyer</dc:creator>
				<category><![CDATA[Desktop]]></category>
		<category><![CDATA[GNOME]]></category>
		<category><![CDATA[KDE]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://www.algorithm-forge.com/techblog/?p=14</guid>
		<description><![CDATA[Under Linux we are creating launchers for our applications according to the Freedesktop Desktop Entry Specification. With recent versions of Gnome or KDE we get the following launcher: When the launcher is clicked, it is asked whether the application should &#8230; <a href="http://www.algorithm-forge.com/techblog/2009/07/executable-application-launcher/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Under Linux we are creating launchers for our applications according to the <a href="http://www.freedesktop.org/wiki/Specifications/desktop-entry-spec">Freedesktop Desktop Entry Specification</a>. With recent versions of Gnome or KDE we get the following launcher:</p>
<div id="attachment_12" class="wp-caption alignnone" style="width: 167px"><img class="size-full wp-image-12" title="Untrusted application launcher" src="http://www.algorithm-forge.com/techblog/wp-content/uploads/2009/07/starter1.png" alt="Untrusted application launcher" width="157" height="152" /><p class="wp-caption-text">Untrusted application launcher</p></div>
<p>When the launcher is clicked, it is asked whether the application should be trusted.</p>
<p>Why? Because recently the desktop environment developers became aware of the following problem:</p>
<p>Some browser like Firefox are saving files to the desktop. When someone is now providing a link to a file with a <tt>.desktop</tt> extension, it would appear as a desktop launcher on the desktop. Without any problem it could look like a arbitrary starter e.g. for Firefox (provided the according image like <tt>/usr/share/pixmaps/firefox-3.0.png</tt> can be correctly guessed) but do something completely different and possibly malignant.</p>
<p>Therefore in recent version of GNOME and KDE the desktop launchers need to have execute permissions (which is not preserved for downloaded files). Then they look and act again as normal:</p>
<div id="attachment_13" class="wp-caption alignnone" style="width: 167px"><img class="size-full wp-image-13" title="Executable application launcher" src="http://www.algorithm-forge.com/techblog/wp-content/uploads/2009/07/starter2.png" alt="Executable application launcher" width="157" height="152" /><p class="wp-caption-text">Executable application launcher</p></div>
<p>So don&#8217;t forget to give your desktop launchers execute permissions! <img src='http://www.algorithm-forge.com/techblog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm-forge.com/techblog/2009/07/executable-application-launcher/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hello world!</title>
		<link>http://www.algorithm-forge.com/techblog/2009/06/hello-world/</link>
		<comments>http://www.algorithm-forge.com/techblog/2009/06/hello-world/#comments</comments>
		<pubDate>Wed, 17 Jun 2009 09:26:07 +0000</pubDate>
		<dc:creator>Kornelius Rohmeyer</dc:creator>
				<category><![CDATA[Algorithm-Forge]]></category>

		<guid isPermaLink="false">http://www.algorithm-forge.com/techblog/?p=1</guid>
		<description><![CDATA[This is the blog of algorithm-forge! I hope content will appear soon.]]></description>
			<content:encoded><![CDATA[<p>This is the blog of algorithm-forge!</p>
<p>I hope content will appear soon. <img src='http://www.algorithm-forge.com/techblog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm-forge.com/techblog/2009/06/hello-world/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
