Thursday, September 8, 2022

Trondheim's air

 Trondheim's air

Norway spends millions in incentives for electric cars that are supposed to reduce the emissions in highly populated areas. At the same time, they allow wood burning for heating in private apartments and houses (which is active at least 9 months a year). In Trondheim, during the warmer months, instead, there are cruise ships docking every day. From morning to late afternoon, they create clouds of smoke over the city. What is the point of all this?


 

Saturday, June 20, 2020

reMarkable "real" landscape mode (feature request)

I recently bought a reMarkable tablet, that I mainly use to review PDF reports from students and research papers. The tablet works very well, and allows me to carry out these very time consuming tasks outside in the sun when weather permits. The 10.3 inches display makes it very portable compared to the bigger 13.3 inches screen options, and the screen size is fine for one-column documents, such as masters theses, books, or some research articles. However, most research articles I review are in two-column format, which makes the text too small to read on this tablet, at least for my eyes. The zoom option is not optimal because it has to be tweaked manually every time the portion of the page to be viewed is changed, and this needs to be done at least four times per page (in most cases it is not enough to push the view rectangle to the four corners of the page).

A very simple solution, that I hope reMarkable will add to future releases of their software, would be to have a "real" landscape mode, as illustrated in the example below. When changing to landscape mode, it would be great if not only the controls would change but also the PDF view. This way, the text would become big enough, and the only change of view necessary for each page would be to show the top or bottom part of the page (no fine tweaking needed). As a bonus, it would be great if there were gestures associated with moving up and down the page, for example swiping up and down, whereas swiping left and right will change page as usual.

This simple functionality, that is unfortunately not available as of version 2.2.0.48 of the software, would make the reMarkable even more competitive compared to the 13.3 inches tablets such as Sony Digital Paper, Onyx Boox, or the GoodEReader just to make some examples.


Wednesday, July 22, 2015

Building Hardware optimized ATLAS on Ubuntu 14.04

The Automatically Tuned Linear Algebra Software (ATLAS) is used by many scientific software packages such as Mathworks Matlab, Julia, Kaldi (speech recognition), and many more. ATLAS can be optimized to the current hardware with a few simple steps, giving much higher performance than the default available packages.

This guide helps you create and install an optimized Debian package on Ubuntu 14.04, but it might work similarly on other GNU Linux distributions. One problem with Ubuntu 14.04 on Intel processors is that it uses a new kernel module called intel_pstate for managing the CPU frequency. This driver will lower the CPU frequency for the idle CPUs (throttling) even if the profile is set to "performance". Unfortunately ATLAS will not build if the CPU frequency is not fixed to the maximum allowed. The workaround is to disable this module in the boot loader (GRUB) temporarily for the build. Here are the steps to follow:

  1. Configure the boot loader to disable the intel_pstate module:
    • edit /etc/default/grub and add the intel_pstate=disable option to the GRUB_CMDLINE_LINUX_DEFAULT variable. In my case the line before and after the change looked like this:
      before:
      GRUB_CMDLINE_LINUX_DEFAULT="quiet splash nomdmonddf nomdmonisw"
      after:
      GRUB_CMDLINE_LINUX_DEFAULT="quiet splash nomdmonddf nomdmonisw intel_pstate=disable"
    • make the change active by running:
      sudo grub-mkconfig -o /boot/grub/grub.cfg
    • reboot
  2. run the following commands:
    sudo apt-get source atlas
    sudo apt-get build-dep atlas
    sudo apt-get install devscripts gfortran f2c libcnf-dev
    cd atlas-*/
    sudo fakeroot debian/rules custom
  3. the last step (that can take a long time to finish) will build ATLAS and create a Debian package file one level up. Install the package with:
    sudo dpkg -i ../libatlas3-base_*.deb
  4. restore the /etc/default/grub file to the original, run the sudo grub-mkconfig -o /boot/grub/grub.cfg command and reboot.
This procedure was tested with ATLAS 3.10.1. Hope this works for you as well. Let me know how it goes in the comments.

NOTES:

  1. the building process took between 6 and 7 hours to complete on an Intel(R) Core(TM) i7-4930K CPU @ 3.40GHz.
  2. Although there were 12 such cores, setting the environmental variable DEB_BUILD_OPTIONS=parallel=12 has no effect on the building process or speed
  3. there are 6 deb files generated by the building process:
    libatlas3-base_3.10.1-4+custom13_amd64.deb
    libatlas3gf-base_3.10.1-4+custom13_all.deb
    libatlas-base-dev_3.10.1-4+custom13_amd64.deb
    libatlas-dev_3.10.1-4+custom13_all.deb
    libatlas-doc_3.10.1-4+custom13_all.deb
    libatlas-test_3.10.1-4+custom13_amd64.deb
  4. when installing the libatlas-doc package I got the following errors:
    Processing triggers for doc-base (0.10.5) …
    Processing 3 added doc-base files…
    Error in `/usr/share/doc-base/atlas-install', line 14: all `Format' sections are invalid.
    Error in `/usr/share/doc-base/atlas-devel', line 10: all `Format' sections are invalid.
    Error in `/usr/share/doc-base/atlas-contrib', line 14: all `Format' sections are invalid.
    Note: `install-docs --verbose --check file_name' may give more details about the above errors.

Monday, September 29, 2014

"La variante di Lüneburg" and China's rice production

In 1993, Paolo Maurensig, an Italian writer from Gorizia, wrote a novel entitled "La variante di Lüneburg" (Adelphi, 1995, pp. 164, ISBN 88-459-0984-0). The novel is set in Nazi Germany during World War II and the main theme is the game of chess.

At the beginning of the story, Maurensig tells a legend according to which the game of chess was invented by a Chinese peasant with a formidable gift for mathematics. (There are different versions of this story, as far as I understand, the earliest written record is contained in the Shahnameh and takes place in India, instead.) The peasant asks the king, in exchange for the game he invented, for a quantity of rice equal to that obtained with the following procedure: First one grain of rice should be placed on the first square on the chess board, then two grains on the second square, than four on the third and so on, every time doubling the number of rice grains. The king accepts not realizing what he is agreeing on.

Let's try to calculate how much rice that would be. The series implied by the peasant has a more general form called geometric series and is well known in mathematics:
$$
  s_m = \sum_{k=0}^m x^k
$$
If we calculate the first steps in the sum we obtain:
\begin{eqnarray*}
  s_0 &=& 1 \\
  s_1 &=& 1+x \\
  s_2 &=& 1+x+x^2 \\
  \dots && \\
\end{eqnarray*}
To see how this relates to our problem, we can set $x=2$ and see that the sum will be: $1+ 2+ 4+ 8+ \dots $

If we observe $s_1$ and $s_2$ in the previous equations, we see that we can write the second in terms of the first in two different ways:
\begin{eqnarray*}
  s_2 &=& s_1+x^2 \\
  s_2 &=& 1 + x (1+x) = 1 + x s_1.\\
\end{eqnarray*}

In the first case, we grouped the first two terms in $s_2$, whereas in the second case we group the last two terms and realized that they shared a common factor $x$.

If we continue writing the terms of the sum for higher orders, we realize that what we obtained above is true in general:
\begin{eqnarray*}
  s_{m}   &=& 1+x+\dots+x^m \\
  s_{m+1} &=& 1+x+\dots+x^m+x^{m+1} = s_m+x^{m+1} \\
         &=& 1 + x (1+\dots+x^{m-1}+x^m) = 1 + x s_m, \\
\end{eqnarray*}
which also means that the right-hand side of the last two equations above must be equal:
\begin{eqnarray*}
  s_m+x^{m+1} &=& 1 + x s_m,
\end{eqnarray*}
and, therefore, rearranging:
\begin{eqnarray*}
  s_m &=& \frac{x^{m+1}-1}{x-1}.
\end{eqnarray*}
This is the general solution for the sum of the geometric series. If we want to know how many grains of rice the king will have to give to the peasant, we need to substitute the values of $x$ and $m$. We already saw above that $x$ should be equal to 2. We also know that the chess board has 8 rows and 8 columns, giving 64 squares. Because we start with $s_0$ in the first square, we need to calculate the series for $m=63$, that will correspond to the last square:
$$ s_{63} = \frac{2^{64}-1}{2-1} = 18\,446\,744\,073\,709\,551\,615, $$
which in words would sound something like eighteen quintillions...

In 1999, China produced approximately 198 million tons of rice. Which corresponds to $198\,000\,000\,000\,000$ grams. If we assume for simplicity that the production is constant over the years and that one gram of rice is approximately 50 grains, the king will have to give the peasant the entire rice production in China for more than 18 hundred years.

Needless to say, when the king realized the mistake, he killed the peasant.

Wednesday, December 18, 2013

MS Kinect microphone array geometry

This post provides the geometrical parameters of the MS Kinect microphone array that are necessary if you want to do acoustic beamforming from the 4 channels without using the Microsoft SDK (for example if you are using Linux or libfreenect or OpenNI).

The image below is a drawing of the internal parts of the Kinect including the microphones in purple:
As you can see, the microphones are not spaced at regular intervals. It is possible to see the exact position of the microphones without disassembling the Kinect by simply opening the plastic covers at the bottom of the device with a flat screwdriver (see next pictures).
The microphones are sitting in the small rubber cubes highlighted in the following picture and that provide some mechanical isolation from the body of the device:
Finally, the next drawing shows the position of the microphones as measured with respect to a reference system with origin in the device's middle point, x-axis along the devices length and y-axis oriented as the perspective of the cameras. The drawing should be interpreted looking at the device from above (I used an XBox 360 Kinect MODEL 1473, the product codes returned by lsusb on linux are the following: 045e:02ae Camera, 045e:02ad Audio, 045e:02c2 Motor):

In text form, the coordinates (x,y) of each microphone in cm are:
channel 1: (11.3, 2)
channel 2: (-3.6, 2)
channel 3: (-7.6, 2)
channel 4: (-11.3, 2)
The assignment between microphones and channels in the audio device was obtained by playing a tone with my telephone next to each microphone and using libfreenect's wavrecord tool to record the audio. I then picked the channel with the highest sound intensity with the help of audacity (see screendump below)


Appendix

some useful sox commands to handle the files returned by wavrecord:
1) merge the four channels in a single wav file (note however that the program you use to reproduce the resulting file might interpret some channels as surround or low frequency channels):
sox channel1.wav channel2.wav channel3.wav channel4.wav --channels 4 -M output_4ch.wav

2) create a stereo file by selecting only the extreme microphones (right and left channels correspond to right and left in the video from the camera):
sox channel1.wav channel4.wav -M --channels 2 output_stereo.wav

3) create a stereo file by mixing all four channels with weights determined by microphone position. Note that this not necessarily better than 2. If we call x the set of x coordinates for the mics, we have (in order of channels)
x = [11.3 -3.6 -7.6 -11.3]
if we call ldx the distance from the left microphone (ch1) normalized by the distance between the extreme microphones:
ldx = [0.0 0.6593 0.8363 1.0]
we can define, somewhat arbitrarily, the left weights lw as (1-ldx)^2 and additionally impose that the weights should sum to 1:
lw = [0.875 0.1016 0.0235 0.0]
If we repeat the same procedure for the right channel we obtain:
rw = [0.0 0.2037 0.3277 0.4686]
Then we can use sox to produce the mixture with the following command

sox channel1.wav channel2.wav channel3.wav channel4.wav -M --channels 2 output_stereo_mixture.wav remix 4v0.875,3v0.1016,2v0.0235 2v0.2037,3v0.3277,1v0.4686

Wednesday, August 11, 2010

LaTeX formulas in XFig figures using PDFLaTeX

Hi,
this post is to explain how to insert mathematical formulas in figures created with XFig when using PDFLaTeX (and therefore when PSFrags is not an option). The procedure is actually so painless that I would recommend it even when PSFrags is available.
  1. when inserting the text in XFig, make sure you set the "Special text" flag. This can be done even after inserting the text by opening the Edit dialog window. You can mix normal text and math with the usual $ symbol.
  2. instead of exporting to PDF, export as "Combined PDF/LaTeX (both parts)". This will create two files figure00.pdf and figure00.pdf_t (assuming the name of the XFig file is figure00.fig).
  3. In your document, instead of using \includegraphics{figure00.pdf} include the _t file with \input{figure00.pdf_t}. If you want to resize the picture, you can use the command \resizebox as shown in the example below
  4. run pdflatex as usual.


Here are example files:
test.tex:

\documentclass{article}
\usepackage{graphicx}
\usepackage{color}
\begin{document}
Some text before the figure.
\begin{figure}
\resizebox{.5\textwidth}{!}{
\input{figure00.pdf_t}
}
\end{figure}
\end{document}

figure00.fig:

#FIG 3.2 Produced by xfig version 3.2.5b
Landscape
Center
Metric
A4
100.00
Single
-2
1200 2
2 1 0 1 0 7 50 -1 -1 0.000 0 0 -1 1 0 2
0 0 1.00 60.00 120.00
1800 5490 1800 1395
2 1 0 1 0 7 50 -1 -1 0.000 0 0 -1 1 0 2
0 0 1.00 60.00 120.00
315 5310 5535 5310
3 0 0 1 0 7 50 -1 -1 0.000 0 0 0 4
585 5220 1980 5130 3195 4320 4545 1440
0.000 1.000 1.000 0.000
4 0 0 50 -1 0 12 0.0000 6 210 735 5040 5625 $\\alpha$\001
4 0 0 50 -1 0 12 0.0000 6 210 945 1935 1665 $e^\\alpha$\001

Enjoy!

Friday, August 6, 2010

TCP Window scaling may cause transfer problems

I noticed this problem first in Italy with an Alice Gate 2 Plus Wi-Fi modem and now I see a similar behaviour in Sweden with a NETGEAR Wireless Cable Voice Gateway modem used by ComHem. This made me think it was a good idea to rewrite in English my original post which was in Italian.

The symptoms (Alice Gate 2 Plus Wi-Fi):


- In GNU Linux this modem/router only works in modem modality, that is, by manually creating a pppoe connection with pppoeconf/pon/poff. If used as a router (automatic connection) web pages are only loaded partially or not loaded at all. Skype works in any case.
- In Windows 7, the router modality only works in combination with MS Internet Explorer (other browsers, such as Firefox and Chrome, show the same problem as in Linux)

The symptoms (NETGEAR Wireless Cable Voice Gateway):


- In GNU Linux the NETGEAR gateway works fine as long as browsing is concerned. When using SSH and many services using SSH, such as SVN, rsync, etc, sometimes the connection stalls and eventually times out
- I do not have a Windows computer in Sweden and could not check if the problem affects Windows as well, but, given the problems in saw in Italy, I would guess it does at least starting with Vista and newer versions.

The problem:


It seems that in both cases the routers do not handle properly the TCP option "window scaling" also called "receive window auto-tuning". This option is implemented in Linux and in Windows starting with Vista, and that is why the problem never occurred with Window XP or older.

The solution:


A solution is to disable window scaling in the TCP options, that is
- in GNU Linux open a terminal and run:
sudo sysctl net.ipv4.tcp_window_scaling=0
To make the option permanent, you have to add the line net.ipv4.tcp_window_scaling=0 in the /etc/sysctl.conf file.
- in Windows, start cmd as administrator and run:
netsh int tcp set global autotuninglevel=disabled

This may make the TCP protocol less efficient, so do this only if you encounter the problems described above.