<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[Random Access Science]]></title>
  <link href="https://fredborg-braedstrup.dk//atom.xml" rel="self"/>
  <link href="https://fredborg-braedstrup.dk//"/>
  <updated>2015-02-03T14:44:21+01:00</updated>
  <id>https://fredborg-braedstrup.dk//</id>
  <author>
    <name><![CDATA[Christian Brædstrup]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[XBMC Kitchen Radio]]></title>
    <link href="https://fredborg-braedstrup.dk//blog/2015/02/03/xbmc-kitchen-radio/"/>
    <updated>2015-02-03T14:06:57+01:00</updated>
    <id>https://fredborg-braedstrup.dk//blog/2015/02/03/xbmc-kitchen-radio</id>
    <content type="html"><![CDATA[<p>A while back I decided to turn a old <a href="http://www.dell.com/us/dfh/p/inspiron-mini10/pd">Dell Inspiron Mini 10 netbook</a> into a XBMC radio for the kitchen.
My requirements where,</p>

<ul>
<li>Stream music from my server</li>
<li>Stream online radio</li>
<li>Remote control from my other devices</li>
<li>Push media to device</li>
<li>Low power consumption</li>
</ul>


<h2>Installing XBMC from ppa</h2>

<p>First add the XBMC ppa repository to your apt-get list and then install XBMC</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'> sudo add-apt-repository ppa:team-xbmc
</span><span class='line'> sudo apt-get install xbmc xbmc-eventclients-xbmc-send
</span></code></pre></td></tr></table></div></figure>


<h2>Configure for low power consumption</h2>

<p>I am a big fan of turning off my devices when I do not use them. I am going to use the laptop for less than a hour on average each day so I do not want it running 24/7. In Linux there are two powersaving modes: Suspension and Hibernation. During suspension the machine is still running and uses a bit of power. In hibernation the RAM is dumped to the harddrive and no power is used.</p>

<p>When you use a normal kitchen radio you just flick the power switch when you leave. I really wanted that feature in this device. Since I am using a laptop it can survive on battery power for a couple of hours. So I wrote a small script that detects if there is a active external power connection to the netbook and shutdown if the user turns of the power (on the wall socket).</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="c">#!/bin/bash</span>
</span><span class='line'><span class="nv">PowerStatus</span><span class="o">=</span>yes <span class="c"># Is the power on?</span>
</span><span class='line'><span class="nv">LinePower_Path</span><span class="o">=</span>/org/freedesktop/UPower/devices/line_power_ACAD <span class="c"># Found using upower -e</span>
</span><span class='line'>
</span><span class='line'><span class="k">while</span> <span class="o">[</span> <span class="m">1</span> <span class="o">]</span>
</span><span class='line'><span class="k">do</span>
</span><span class='line'><span class="c"># Check current power status</span>
</span><span class='line'><span class="nv">PowerStatus</span><span class="o">=</span><span class="k">$(</span>upower -i <span class="nv">$LinePower_Path</span> <span class="p">|</span> grep online <span class="p">|</span> awk <span class="s1">&#39;{ print $2}&#39;</span><span class="k">)</span>
</span><span class='line'>
</span><span class='line'><span class="k">if</span> <span class="o">[</span> <span class="s2">&quot;$PowerStatus&quot;</span> <span class="o">==</span> <span class="s2">&quot;no&quot;</span> <span class="o">]</span><span class="p">;</span> <span class="k">then</span>
</span><span class='line'>
</span><span class='line'><span class="c"># Test by sending a message?</span>
</span><span class='line'><span class="c">#    notify-send &quot;Power status: $PowerStatus&quot;</span>
</span><span class='line'>
</span><span class='line'><span class="c"># Mute sound to smooth things</span>
</span><span class='line'>xbmc-send --host<span class="o">=</span>127.0.0.1 --port<span class="o">=</span><span class="m">9777</span> --action<span class="o">=</span><span class="s2">&quot;PlayerControl(Stop)&quot;</span>
</span><span class='line'>
</span><span class='line'><span class="c"># Put into hibernation</span>
</span><span class='line'>sleep 2
</span><span class='line'>sudo pm-hibernate
</span><span class='line'>
</span><span class='line'><span class="k">fi</span>
</span><span class='line'>
</span><span class='line'><span class="c"># Sleep for 10 seconds and check status again</span>
</span><span class='line'>sleep 10
</span><span class='line'>
</span><span class='line'><span class="k">done</span>
</span></code></pre></td></tr></table></div></figure>


<p>Here i use the <strong>upower</strong> tool in Ubuntu to check current power status. The path (/org/freedesktop/UPower/devices/line_power_ACAD) may vary between systems. You can determine it by running the UPower command,</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>   upower -e
</span></code></pre></td></tr></table></div></figure>


<p>When the power is cut it will stop whatever is playing using the xbmc-send command (<a href="http://kodi.wiki/view/List_of_built-in_functions">Full command list</a>) and put the system into hibernation. The script is run in a while loop so the status is checked every 10 seconds. To run the script on boot save it to disk and edit /etc/rc.local (works on Ubuntu). The file should look something like this,</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>   <span class="c">#!/bin/sh -e</span>
</span><span class='line'>
</span><span class='line'>   ./home/christian/detect_linepower.sh <span class="p">&amp;</span>
</span><span class='line'>   sudo killall NetworkManager
</span><span class='line'>   <span class="nb">exit </span>0
</span></code></pre></td></tr></table></div></figure>


<h2>Setting up streaming and media push functions</h2>

<p>Music streaming works out out-of-the-box in XBMC. Just hook-up your server via a NFS or SMB connection to the client. To get streaming Danish TV/radio I normally install the addons developed by <a href="http://tommy.winther.nu/wordpress/">Tommy Winther</a>.</p>

<p>If you have several devices you can push content using the UPnP features build into XBMC settings (<a href="http://kodi.wiki/view/UPnP/Client">UPnP/Client</a>). With this I can push music from my laptop/server to the radio with zero setup. I do however need XBMC installed on the device that sends contents.</p>

<p>If you want complete control of the radio without having a monitor attached I suggest using the web interface build into XBMC. There are some nice addons available to improve on the web experience (<a href="http://kodi.wiki/view/Add-on:Chorus">Chorus</a>).</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Saving Matplotlib Figures Using Pickle]]></title>
    <link href="https://fredborg-braedstrup.dk//blog/2014/10/10/saving-mpl-figures-using-pickle/"/>
    <updated>2014-10-10T14:25:54+02:00</updated>
    <id>https://fredborg-braedstrup.dk//blog/2014/10/10/saving-mpl-figures-using-pickle</id>
    <content type="html"><![CDATA[<p><a href="http://www.matplotlib.org">Matplotlib</a> is a external library that ships with most bundles of the Python programming language. Providing a huge array of plotting functions is has completely replaced my previous workflow in <a href="http://www.mathworks.com">MATLAB</a>. I have how always missed the *.fig format that exists in MATLAB allowing user to save figures in a native format.</p>

<p>Since version 1.2 matplotlib now allows user to save figures using pickle (<a href="http://matplotlib.org/users/whats_new.html#figures-are-picklable">Matplotlib: Whats new</a>). <a href="https://wiki.python.org/moin/UsingPickle">Pickle</a> allows python to store most variables directly to a file while retraining the structure. It is similar to the <strong>save</strong> function in numpy, but more general. It is essentially a dump of python code that can store any kind of information. In this way you can dump the whole matplotlib figure handle into a file and load it on demand. I often work with large datasets that takes time to process. By dumping the figure handle I can quickly make small changes to a figure without processing the raw data behind it.</p>

<h3>A Example</h3>

<p>The code below illustrates how pickle can be used with python. Using matplotlib a simple sinus function is plotted. Then calling the pickle function <strong>dump</strong> we save the figure handle to a file called sinus.pickle.</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
</span><span class='line'><span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="kn">as</span> <span class="nn">plt</span>
</span><span class='line'><span class="kn">import</span> <span class="nn">pickle</span> <span class="kn">as</span> <span class="nn">pl</span>
</span><span class='line'>
</span><span class='line'><span class="c"># Plot simple sinus function</span>
</span><span class='line'><span class="n">fig_handle</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">()</span>
</span><span class='line'><span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">2</span><span class="o">*</span><span class="n">np</span><span class="o">.</span><span class="n">pi</span><span class="p">)</span>
</span><span class='line'><span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
</span><span class='line'><span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="c"># Save figure handle to disk</span>
</span><span class='line'><span class="n">pl</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">fig_handle</span><span class="p">,</span><span class="nb">file</span><span class="p">(</span><span class="s">&#39;sinus.pickle&#39;</span><span class="p">,</span><span class="s">&#39;w&#39;</span><span class="p">))</span>
</span></code></pre></td></tr></table></div></figure>


<p>In a different python script we can load the figure back into python using the <strong>load</strong> function.</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="kn">as</span> <span class="nn">plt</span>
</span><span class='line'><span class="kn">import</span> <span class="nn">pickle</span> <span class="kn">as</span> <span class="nn">pl</span>
</span><span class='line'><span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
</span><span class='line'>
</span><span class='line'><span class="c"># Load figure from disk and display</span>
</span><span class='line'><span class="n">fig_handle</span> <span class="o">=</span> <span class="n">pl</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="nb">open</span><span class="p">(</span><span class="s">&#39;sinus.pickle&#39;</span><span class="p">,</span><span class="s">&#39;rb&#39;</span><span class="p">))</span>
</span><span class='line'><span class="n">fig_handle</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
</span></code></pre></td></tr></table></div></figure>


<p>Running the above codeblock through iPython we can make a completely interactive workflow and just save our figures for later if need be. You can even extract the data behind figures,</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">fig_handle</span><span class="o">.</span><span class="n">axes</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">lines</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">get_data</span><span class="p">()</span>
</span></code></pre></td></tr></table></div></figure>


<p>This will return x- and y-coordinates for the sinus function. This also works for 2D plots create with <strong>imshow</strong> or <strong>pcolormesh</strong>.</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">fig_handle</span><span class="o">.</span><span class="n">axes</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">images</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">get_data</span><span class="p">()</span>
</span></code></pre></td></tr></table></div></figure>


<p> I use this feature all the time to combine existing plots and store subsets for my data for later processing. Most figures for publication are very different and using pickle I can maintain general plotting scripts and then later combine the data I need.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Extract Audio From DVD With VLC on Windows]]></title>
    <link href="https://fredborg-braedstrup.dk//blog/2013/12/24/extract-audio-from-dvd-with-vlc-on-windows/"/>
    <updated>2013-12-24T00:00:00+01:00</updated>
    <id>https://fredborg-braedstrup.dk//blog/2013/12/24/extract-audio-from-dvd-with-vlc-on-windows</id>
    <content type="html"><![CDATA[<p>This December my dad wanted to copy the sound from a couple of DVD concerts he had. Then he could hear it in his office or in the car. After a bit googling around I saw that VLC (Videolan.org) is able to extract the data. The problem was that VLC is only able to extract all the audio in one go. So all music is in one file… No way to skip from one track to the next. Not really what I wanted… The way out of this is to call VLC from the commandline and tell it to only play one chapter.</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>vlc dvd:///O:/1:2-1:2</span></code></pre></td></tr></table></div></figure>


<p>This will play from title 1 and chapter 2 to title 1 and chapter 2. In this way you are able to open a single track at a time. But going in and using the GUI to record one track at a time is a bit cumbersome on a DVD with 32 tracks. So I ended up writing a script. My dad uses windows so it is in batch (sorry), but POSIX should work in a similar way,</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>set title=%1
</span><span class='line'>set chapters=%2
</span><span class='line'>set name=%3
</span><span class='line'>
</span><span class='line'>FOR /L %%G in (1,1,%chapters%) DO CALL "C:\Programmer\VideoLAN\VLC\vlc" -I dummy -vvv dvd:///O:/#%title%:%%G-%title%:%%G --sout=#transcode{vcodec=none,acodec=mp3,ab=192,channels=2}:std{access="file",mux=raw,dst="g:\RIPDVD\%name%_%%G.mp3"}} vlc://quit</span></code></pre></td></tr></table></div></figure>


<p>The above script assumes that the DVD is in the drive with letter O, the file is saved to “g:\RIPDVD\”, the output file is a mp3 file with bitrate 192.</p>

<p>To use the script edit the above parts to work with your system. When calling the script you must give it a couple of commandline arguments,</p>

<ol>
<li>The title to use (this can be found from a program like Handbrake)</li>
<li>Number of chapters to extract (i.e. number of tracks in the concert. Can also be found using Handbrake)</li>
<li>The base filename of the output file (i.e. filename_trackNumber.mp3)</li>
</ol>


<p>If you decide to call the script <strong>rip-audioDVD.bat</strong> then you would call the script from the Windows commandline (cmd.exe) with</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>rip-audioDVD.bat 1 10 mymusic</span></code></pre></td></tr></table></div></figure>


<p>This would rip tracks 1-10 from title 1 on the current DVD. The output would be
call mymusic_1.mp3, mymusic_2.mp3, …, mymusic_10mp3.</p>

<p>So the script will run through all the chapters in the title and create one .mp3 file per track in the folder given. There may be a easy way to do this without scripting but this was the quickest way I could find.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Trying Out the Alfa AWUS036NH on Crunchbang Linux]]></title>
    <link href="https://fredborg-braedstrup.dk//blog/2013/10/26/trying-out-the-alfa-awus036nh-on-crunchbang-linux/"/>
    <updated>2013-10-26T09:42:10+02:00</updated>
    <id>https://fredborg-braedstrup.dk//blog/2013/10/26/trying-out-the-alfa-awus036nh-on-crunchbang-linux</id>
    <content type="html"><![CDATA[<h2>Background</h2>

<p>I travel a lot for conferences and workshops as part of my PhD. Most hotels claim to have “High-speed internet” and WIFI but most of the time that is a overstatement. So recently I bought the Alfa AWUS036NH long-range USB adapter. It is simply a USB network card with a 5dB antenna that will boost the number of networks you can access. So now even if you get the room farthest away from the hotel access point you may still have a change to connect.</p>

<h2>It’s plug and play</h2>

<p>On the website (bought if off amazon.co.uk) they claimed it would work on Linux 2.4/2.6. My current kernel is 3.10 so I was a bit afraid it would not work. Plugged it in and it was ready… That was a first.
Depending on your system you may need to install the Ralink firmware (on debian: apt-get install firmware-ralink). I did have some problems getting access to networks on kernel 3.2.0 but after the switch to 3.10 everything was fine.</p>

<p>The card takes a minute or so to “warm up”. After that I was able to see about three times the number of networks with the 5 dB antenna. I also bought a 9.5 dB antenna just because I had to try it out. It is massive (There wasn’t any scale on the promotion pictures). It is massive! About the same length as my 13” macbook air. If you didn’t look like someone trying to crack WIFI passwords before you will with this.</p>

<h2>Does it work?</h2>

<p>I am quite impressed with the device. Allowing the adapter to be on for about 5 minutes I got the following statistics using the list in the network manager.</p>

<ul>
<li>build-in Macbook Air WIFI: 7 network,</li>
<li>Using the 5 dB antenna about 31 network.</li>
<li>Using the 9.5dB antenna: 39 networks</li>
</ul>


<p>So not that big a difference with the large antenna but it still makes a difference. The list of available networks does seem a bit unstable. When connected to a network the list shrinks but that is perhaps because some power is used on the current connection.</p>

<h2>Conclusion</h2>

<p>Do I recommend the Alfa AWUS036NH for linux users? YES, it great. Do I recommend the 9.5dB antenna? Well if you think you need it then yes otherwise it is quite large and removed a lot of the portability of the device. Also with the large antenna the device will tip over because of the weight. You can find a “stable” position where it will stand but the smallest push will tip it. But that is a small price to pay and can be fixed with a piece of paper under the antenna.
I also do not quite know how it is to fly with the device. I am sure it will not be a problem but you may get pulled over in security to explain the large antenna you have in your carry-in :)</p>

<p>Update: I have now tried the device a couple of places. At a random coffee house in Helsinki (Finland) I got 14 networks using the build-in device and 27 networks using the 5dB antenna. So quite a difference. I also tried putting the device in my carry-on and did not have any problems. I also experinced that the device uses a lot of power. So if you run in on a laptop expect that you battery life will decrease by perhaps 30% – 50%.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[VIDEO: How to Steal a Botnet]]></title>
    <link href="https://fredborg-braedstrup.dk//blog/2013/10/23/video-how-to-steal-a-botnet/"/>
    <updated>2013-10-23T00:00:00+02:00</updated>
    <id>https://fredborg-braedstrup.dk//blog/2013/10/23/video-how-to-steal-a-botnet</id>
    <content type="html"><![CDATA[<p>I found this great google talk by Richard A. Kemmerer about how his research team captured control of a large botnet for 10 days. It’s worth a view.</p>

<p><a href="https://www.youtube.com/watch?v=2GdqoQJa6r4">VIDEO: How to steal a botnet</a></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[NFS on Debian]]></title>
    <link href="https://fredborg-braedstrup.dk//blog/2013/10/20/nfs-on-debian/"/>
    <updated>2013-10-20T09:29:57+02:00</updated>
    <id>https://fredborg-braedstrup.dk//blog/2013/10/20/nfs-on-debian</id>
    <content type="html"><![CDATA[<p>There are plenty of guides on the internet about NFS. Still it took me ~2 hour and 15 different website to get NFS working on my home server. The reason? Well firstly, a lot of NFS posts are outdated because of a switch from portmap to rpcbind. Secondly, NFS was never properly installed. Because NFS is part of the kernel the nfs-kernel-server and nfs-common files can still be on the system even without the packages being installed.</p>

<p>The best source I found to setup NFS on debian is: <a href="http://debian-handbook.info/browse/stable/sect.nfs-file-server.html">http://debian-handbook.info/browse/stable/sect.nfs-file-server.html</a></p>
]]></content>
  </entry>
  
</feed>
