Wednesday, 18 May 2011

Removing N first characters from each line in a file

I recently had to remove the 5 first characters from each line in a text file; it was a source code file copied from the internet with the line numbers included. I found a nice way to do this in a forum post:

sed -i 's/\(.\{5\}\)//' file.m

Here, the "sed" function of the Linux command line is used with the -i (for "in place") switch to remove the 5 first characters from each line in the file "file.m". It looks a bit messy, but it works.

Wednesday, 4 May 2011

Using LaTeX in Inkscape Drawings

I use Inkscape to draw all my vector graphics. I often use these graphics in combination with PdfLaTeX to write papers and Beamer presentations. However, one of the things I have been struggling with is how to include greek letters and mathematical formulas in the graphics. One simple solution for displaying Greek letters is to use the corresponding unicode. For example, to display the letter gamma, create a text box, press Ctrl+U, and enter <03B3>. However, the character will (usually) not be available in the same font as the LaTeX document, and it is also hard to write anything more complex than single characters.

It was not until today that I found out that Inkscape actually has a export option that solves this problem quite nicely. This is documented very nicely here. To create a drawing with LaTeX elements, simply enter the LaTeX code in a text box, export as PDF, and cross the box in the PDF export options. This will export the graphics to a PDF file and the text to a separate .tex file, which can then be included in the LaTeX document using this syntax (or similar):

\begin{figure}
\centering
\def\svgwidth{\columnwidth}
\includesvg{image}
\end{figure}

where the file name of the exported graphic is "image". Note that no additional packages are needed. Consult the documentation for more details.

Tuesday, 3 May 2011

Spell checking LaTeX documents

Spell checking a LaTeX document is different from spell checking a plain text document, since the LaTeX document contains commands that should be ignored by the spell checker. For example, consider this command for including a figure with a caption:

\begin{figure}
\centering
\includegraphics[width = 7cm]{image1}
\caption{Focused image of piont scatterer.}
\end{figure}

Here, a spell checker should not suggest changing "\includegraphics" to "include graphics", but it should suggest changing the misspelled "piont" in the caption to "point". Luckily, there are spell checkers that interpret LaTeX commands, and I am currently using one of them, Aspell. In Linux, Aspell is easily installed by the following commands:

apt-get install aspell
apt-get install aspell-en

which installs both the program and the english dictionary. To spell check a LaTeX file called paper.tex on the command line, type

aspell -c paper.tex

Note that Aspell (and its relative Ispell) can also be used at runtime by text processing programs like Kile.

Thursday, 28 April 2011

Concatenating binary raw data files

At my company, Breivoll Inspection Technologies, we perform inline ultrasound scans of water pipelines, and the resulting raw data is stored in a number of binary files for postprocessing. I was looking for a simple way to combine these files, and found out that the familiar cat command (on Linux) does the trick:

cat *.dat > mergedFile.dat

This concatenates all the .dat files in the current directory and outputs them into mergedFile.dat. Note that although cat is often used for text files, it works just as well for binary files.

Another note: In some cases, a raw data file may contain headers or footers that should not be included when the files are concatenated. I found a simple solution in this forum post. For binary files, a number of bytes can easily be stripped away from the beginning and the end of the file by using the dd command, for example:

dd if=input.dat of=output.dat bs=1 skip=X count=Y

This skips the X first bytes of the input file, and writes the following Y bytes to the output file. See the help section on the dd command for more options.

Sunday, 3 April 2011

The Acoustic Ghost in the Machine

I came across this awesome article when doing research on another topic. Haunting "supernatural" events can be explained by simple acoustics - infrasonic waves are the Ghost In The Machine! :D

Abstract: In this paper we outline an as yet undocumented natural cause for some cases of ostensible haunting. Using the first author’s own experience as an example, we show how a 19hz standing air wave may under certain conditions create sensory phenomena suggestive of a ghost. The mechanics and physiology of this "ghost in the machine" effect is outlined. Spontaneous case researchers are encouraged to rule out this potential natural explanation for paranormal experience in future cases of the haunting or poltergeistic type.

Wednesday, 19 January 2011

Inline comments in LaTeX

I recently got some feedback on an article that I've submitted, and when I started including the reviewer's comments in the revised version, I started wondering if there is a way to make an "in-line" comment in LaTeX documents. I often use the percent sign, %, to comment out everything after a line, like this:

... and thus we see that these methods are equvivalent. % Or are they?

After some searching I found an interesting post on Eric Rasmussen's blog, and it seems that the easiest way to do this is to define a "macro" that doesn't do anything, in the document preamble:

\newcommand{\comment}[1]{}

and then I can make an inline comment using the \comment{} command, like this:

... these methods are equvalent. \comment{Or are they?} Luckily, it doesn't matter...

Thursday, 13 January 2011

Compressing Matlab AVI videos in Linux

Recently I've started making a few movies in Matlab. This is actually quite easy - for an example see this description from the Matlab documentation. Unfortunately, the movie2avi() function does not apply any compression when writing the movie to file (not in Linux, anyway), and this results in very large movies (on the order of 100 MB for a few seconds).

However, I found a solution. I originally posted it as a reply in the MATLAB Newsreader, but I thought I'd share it here as well:

You can transcode Matlabs AVI movies using FFmpeg (www.ffmpeg.org). If you don't have the package, you can install it using the terminal:

sudo apt-get install ffmpeg

If you have only one file, let's call it inputFile, you can transcode it using something like this:

ffmpeg -i inputFile.avi -sameq outputFile.avi

The "-sameq" option is used to preserve video quality. I tested this on a 80 MB file produced by Matlab, and got a 5 MB file without any visible loss in quality. If you want to do it within your matlab script, use the system() command:

system('ffmpeg -i file1.avi -sameq outputFile.avi')

If you have a problem with the Matlab AVI file growing too big, you can split it into several smaller files and compress them as you go, and finally combine them after your iteration has finished. A way of doing this is described here in the FFmpeg FAQ. Note that you will have to transcode your intermediate files to MPEG format. .