Spencer Bliven

Thoughts and Research

TV Oscilloscope Part 2

January 16, 2013 | Posted in Engineering,Technology

To further test out my TV oscilloscope, I hooked it up to the output from a small stereo. Changes in dynamics and instrumentation were very apparent in the songs I listened to.

Oscilloscope Music 1 Oscilloscope Music 2
Oscilloscope Music 3Oscilloscope Music 4


I find the blue start and red tail of the trace to be interesting (clearest in the upper right image). Perhaps the red phosphors energize slightly faster, while the blue phosphors persist slightly longer? It could also have something to due with slightly different path lengths from each electron beam to the peripheral pixels due to the way color TVs work.

Another cool trick is that you can calculate what aperture speed I shot these photos at. They all have about 7.5 scans visible in the photo, giving a shutter speed of 7.5 scans/60 Hz = 1/8 second. You can check that yourself against the EXIF data in the photos.

Finally, if you look close you can see that the traces are composed of alternating white and black dots. I think that the black parts represent the horizontal blanking interval, when the electron beam gets shut off between lines. If that is the case there should be 241 dots per trace, which doesn’t seem too far off from my quick estimate.

DNA font

February 10, 2012 | Posted in Science,Technology, Tagged , , , , , ,
ATGCXYR

A short DNA 'word' showing the four bases A, T, C, and G, the 'unknown' base X, and the 'pyrimidine' and 'purine' characters Y and R.

I recently downloaded the Deja-Vu font family and discovered the open-source typography community. I thought it would be fun to try to make a font myself. Since much of my time is spent looking at biological sequences, I thought a way to visualize DNA molecules in a text editor would be cool. The result: DNA Type. DNA only contains a few letters, so I’ve only made 7 characters so far. However, you can use it to write secret messages to your fellow bio-nerds, as long as the messages contain only A, T, G, C, Y, R, and X! The message is read off from the top strand.

Spencer, what’s your favorite instrumental electronic band?
RATATAT
What was the name of that movie which predicted the infringement of human rights based on genetic predisposition, a possibility which seems very plausible in todays high-throughput sequencing world?
GATACA

DNA is cool, but I’m really more of a protein guy. With a protein font you can make meaningful amino acid glyphs for the whole alphabet. However, polypeptides are usually displayed alternating up and down. I think this is possible with glyph variants, but I need to learn more about the TrueType format and the FontForge program before attempting something so complex.

Download

Version 0.
Download TrueType font
Download FontForge source file

Known bugs

  • Pretty much unreadable at smaller than 48pts.
  • Only 7 characters. Not even the lowercase works.
  • No hinting for small sizes beyond FontForge’s autohints.
  • Doesn’t work for RNA. No one cares about the difference between Uracil and Thymine, anyway.
  • Ugly glyphs. What are you, an artist? Go back to research.

Arduino IDE keywords

January 18, 2012 | Posted in Arduino,Technology, Tagged , ,

The other day I made my first library (a 7-segment display controller) for my new Arduino Uno, following two nice tutorials. They both mention that it’s a good idea to make a keywords.txt file for new libraries, which gives hints to the Arduino IDE’s syntax highlighter. However, neither gives a thorough explanation of format of that file. I thought I would document my findings.

The built-in keywords are defined an a simple text file. On my computer, this lives at /Applications/Arduino.app/Contents/Resources/Java/lib/keywords.txt. Here’s how it starts:

# LITERAL1 specifies constants

HIGH	LITERAL1	Constants
LOW 	LITERAL1	Constants

The interesting thing here is that there are three fields which get parsed. Only the first two are useful.

  1. The keyword to highlight
  2. The type of keyword it is. This really just determines the color, but most people seem to use the following convention:
    • KEYWORD1 Classes, datatypes, and C++ keywords
    • KEYWORD2 Methods and functions
    • KEYWORD3 setup and loop functions, as well as the Serial keywords
    • LITERAL1 Constants
    • LITERAL2 Built-in variables (unused by default)
  3. Documentation page. This is used by the ‘Help<Find in Reference’ menu item. For example, the reference for HIGH in the example above would be file:///Applications/Arduino.app/Contents/Resources/Java/reference/Constants.html.

By default, Arduino 1.0 colors all the KEYWORD types orange, and all the LITERAL types blue. These defaults are set in the /Applications/Arduino.app/Contents/Resources/Java/lib/theme/theme.txt. Here’s the relevant snippet (the comments seem to be inaccurate or outdated):

# TEXT - KEYWORDS

# e.g abstract, final, private
editor.keyword1.style = #cc6600,plain

# e.g. beginShape, point, line
editor.keyword2.style = #cc6600,plain

# e.g. byte, char, short, color
editor.keyword3.style = #cc6600,bold


# TEXT - LITERALS

# constants: e.g. null, true, this, RGB, TWO_PI
editor.literal1.style = #006699,plain

# p5 built in variables: e.g. mouseX, width, pixels
editor.literal2.style = #006699,plain

Just change any of the hexadecimal colors. I like the following:

editor.keyword1.style = #cc6600,plain
editor.keyword2.style = #993300,plain
editor.keyword3.style = #993300,bold
editor.literal1.style = #006699,plain
editor.literal2.style = #0099CC,plain

If loop and setup aren’t showing up bold, you may be using Monaco, which doesn’t have a bold style. I recommend using another fixed-width font which does have a bold style, such as DejaVu Sans Mono. This can be set in the Arduino preferences file, ~/Library/Arduino/preferences.txt:

editor.font=DejaVu Sans Mono,plain,10
editor.antialias=true

Make sure the Arduino IDE is not running, as it overwrites the preferences file upon exit.

Java Interpreters

April 29, 2011 | Posted in Technology, Tagged , ,

I’m a big fan of the iPython interpreter. I like having an interpreter running while I develop for prototyping and debugging. Since I currently develop in java primarily, I thought I’d take a look at what java interpreters are available. I had three main features which I wanted. In order of importance:

  1. Basic history and command editing, at least as good as bash.
  2. Autocompletion. At a minimum, autocomplete built-in commands and previously seen code. Ideally, autocomplete instance methods from loaded libraries.
  3. Eclipse compatibility. Ideally, it should run as a eclipse plugin. Baring that, it should be able to find the most recently compiled version of class files (for instance, through a local maven repo).

Sadly, none of the solutions I found fulfilled all three of my requirements.

1. Groovy

Groovy is a dynamic language that runs on the JVM. Java code is valid Groovy code, but groovy includes a lot of nice dynamic features ala python or ruby, such as dynamic typing. The community feels very rails-like, with a popular agile web server (Grails) which holds most of the die-hard interest, and plenty of hip conferences.

Pros: Dynamic language, fully compatible with java. Comes with an interpreter. Active development, including a MacPorts installer. Strong community. Bash-like history feature.
Cons: Hard to configure correctly (classpaths, maven integration, etc). No autocompletion, no eclipse integration.

2. BeanShell

BeanShell is a java interpreter. It’s actually quite similar to Groovy, but positions itself as a dev tool rather than a new language. The documentation refers to autocompletion features, but they didn’t work for me. It only seems to have been developed for 6 months in 2005 by two developers, so that’s not surprising.

Pros: Bash-like history feature. Embeddable!!!! (<--this is useless to me.)
Cons: No development since 2005, no autocompletion, no eclipse integration, tricky to get classpath right.

No autocompletion? Did you try jLine, you ask? Yes, I did find and follow those arcane instructions for wrapping BeanShell with the java version of readline. It was a pain, and it didn’t even autocomplete words from my history. FAIL.

3. EclipseShell

The command-line tools didn’t seem to be cutting it, so I checked out EclipseShell, which is an eclipse wrapper for BeanShell. This one almost worked, but feels like a beta or first release. Screenshots show autocompletion, but it sure doesn’t work on current versions of eclipse. The interface tries to be this matlab-style cell format, but just looks like a text editor. In short, good idea but no followthrough.

Pros: Edit like a text file, eclipse integration, easy installation.
Cons: broken autocompletion, no recent development.

My location over the past year

April 22, 2011 | Posted in Technology, Tagged , ,

Recently there has been much ado over the discovery that the iPhone keeps a log of everywhere you’ve been. I choose to push my paranoia aside and focus on the benefits of this: a cool app that lets you visualize your travels.

Here’s my map, from last june through the present. You can see my route on 8/26-27/2010 when I drove from Seattle to San Diego. And there’s a video below! A few notes:

  • Observations are binned into rectangles of 1/100 of a degree. The size of the circles represents the number of observations in that square centidegree over the time period.
  • Location is calculated by cell towers rather than GPS, so it’s not very accurate. For instance, I have never been to Eureka, CA, despite a number of observations to the contrary.
  • The phone doesn’t seem to log locations unless you switch towers, so the movie skips over periods of time where I stayed in one place
  • I had to modify the source code slightly to get the movie below to progress in 1-hour intervals. The original showed 1-week timesteps in a half-hearted attempt to prevent housewives from using it to spy on their husbands.

Secure Synergy

April 15, 2011 | Posted in Technology, Tagged , ,

Synergy is a really cool little program that allows one to share a keyboard, mouse, and clipboard seamlessly between multiple computers. I have it set up at work so that I can use my desktop keyboard and mouse to control my laptop.

I’ve been happy with it, but this morning it occurred to me that anyone on my work network could theoretically view all my keystrokes. So today I implemented a script to securely connect to the synergy server from my laptop. It is based on a suggestion from the synergy FAQ.

#!/bin/bash
# Opens a secure synergyc connection
# 
# usage: synergyc_secure server [synergyc options]
#
# Author: Spencer Bliven

SERVER="${1:-desktop}"
shift
LPORT=24800
RPORT=24800

ssh -x -f -L $LPORT:localhost:$RPORT -o ExitOnForwardFailure=yes \
    "$SERVER" 'sleep 10' &&
synergyc "$@" localhost:$LPORT

Surprises with floating point operations

December 17, 2010 | Posted in Programming,Technology, Tagged , , , , , , , ,

At work I am currently writing software that calculates some thermodynamic properties of proteins. I recently refactored some of the code and I wanted to make sure that I didn’t screw something up and change the output. So I was concerned when I compared the old and new output and discovered some differences:

Corex output before and after the refactor. The second column of numbers in each file gives the conformational entropy (Sconf).

Looking into the difference further, I finally tracked it back to a single change in the C source code. The original programmer liked to write out additions explicitly: Sconf=Sconf+backboneEntropy+sidechainEntropy; During the refactor I changed this to the more readable (IMHO) Sconf += backboneEntropy + sidechainEntropy;

This small change resulted in all the numerical differences I was seeing. To better understand the reason for the difference, here’s a simple C program:

#include <stdio.h>
int main(int args, char *argv[]) {
    float a, b, sum1, sum2;

    a= 4.1;
    b = -0.12;
    sum1 = sum2 = 11.9;

    sum1 = sum1 + a + b;
    sum2 += a + b;

    printf("=+ %f\n+= %f",sum1,sum2);
    return 0;
}

This yields the output =+ 15.880000 += 15.879999

Here is the unoptimized assembly code for lines 9-10, commented with “pseudo-c” descriptions of what’s happening. XMM0 and XMM1 are two 128-bit registers used by 64-bit processors (like my Intel Core 2 duo) for floating point operations. However, the ‘ss’ at the end of all the operations means that only the lowest 32 bits are used in the computation, with overflow being discarded.

	.loc 1 9 0    ;line 9
	movss	-12(%rbp), %xmm0  ; XMM0 = sum1
	addss	-4(%rbp), %xmm0  ; XMM0 += a
	addss	-8(%rbp), %xmm0  ; XMM0 += b
	movss	%xmm0, -12(%rbp)  ; sum1 = XMM0
	.loc 1 10 0    ;line 10
	movss	-4(%rbp), %xmm0  ; XMM0 = a
	movaps	%xmm0, %xmm1  ; XMM1 = XMM0
	addss	-8(%rbp), %xmm1  ; XMM1 += b
	movss	-16(%rbp), %xmm0  ; XMM0 = sum2
	addss	%xmm1, %xmm0  ; XMM0 += XMM1
	movss	%xmm0, -16(%rbp)  ; sum2 = XMM0

So basically, the difference between “=…+” and “+=” is just the order of operations; the former does (sum1 + a) + b while the latter does sum1 + (a + b). The small differences in rounding behavior between these two variants add up to account for the 0.01 differences I was observing in my program.

Executing commands from TextWrangler/BBEdit

December 6, 2010 | Posted in Technology, Tagged , , , , , , , ,

I use TextWrangler occasionally for editing code. It has a well-developed applescript dictionary, so I decided to write a script to execute commands directly from TextWrangler.

The following Applescript copies either the selected text or the current line into your application of choice. For instance, “iTerm” is a reasonable choice, as that would allow you to start a command (ipython, for instance), and then quickly modify and execute (python) statements without overwriting your clipboard. The pastebin is configured for R code since I originally wrote it in response to a request by an R user.

EDIT: I also uploaded a pre-compiled version of the script (using Terminal).

Daily WTF

December 1, 2010 | Posted in Technology, Tagged , , , , , ,

Whenever I’m writing C code I am amazed that we have to keep track of things like array lengths and string terminators. It’s no wonder poor coders create weird bugs, like this one I found in my work code for storing atom names (like chlorine-1, carbon-2, etc).

char names[MAX_NAMES][6];
int num_names, i;
for(i=0; i < num_names; i++) {
    printf("(%d) %s\n", i, names[i]);
}

This is what I see:

(0)  CL1   CL2   C2 
(1)  CL2   C2 
(2)  C2 
(3)  CD2   N2 
(4)  N2 
...

WTF? Turns out there are exactly three spaces around each of those strings, making the 3-character names overflow. That’s what happens when you use strcpy on strings that are too short to contain the result.

Connecting through firewalls

June 23, 2010 | Posted in Technology, Tagged , , , , , ,

I have access to a number of servers which are behind firewalls. To access them I generally create a SSH tunnel through the firewall and then connect to the server through the tunnel. To speed up this process I made the following bash function (copy it into your .bashrc): #Forward ssh connections through a firewall function fw { USAGE=$(cat<<'END' fw firewall destination [port] fw alias Generate an ssh tunnel through a firewall & connect through it to the destination. Port refers the local port devoted to the tunnel. Only one tunnel may be generated per port. Alias refers to a named tunnel, specified in ~/.ssh/tunnels. END ) if [[ ! "$#" =~ [1-3] || "$1" == "-h" || "$1" == "--help" ]]; then echo "$USAGE" >&2 return 70 fi TUNNELS=~/.ssh/tunnels if [[ "$#" == 1 ]]; then #alias mode TUNNEL=$(egrep -i "^$1 " $TUNNELS 2>/dev/null || { echo "Tunnel $1 not found in $TUNNELS." >&2; return 2; } ) FW=$(echo $TUNNEL|awk '{print $2}') DEST=$(echo $TUNNEL|awk '{print $3}') PORT=$(echo $TUNNEL|awk '{print $4}') else FW="$1" DEST="$2" PORT="${3:-2222}" fi # Create tunnel & establish a connection through. # Tunnel will close when the last connection through it closes. ssh -f -L $PORT:${DEST##*@}:22 $FW 'sleep 10' && ssh -p $PORT -l "${DEST%%@*}" 127.0.0.1 }

Now, to ssh into ‘hotstuff.ucsd.edu’ through firewall ‘kerberos.ucsd.edu’, just run $ fw sbliven@kerberos.ucsd.edu sbliven@hotstuff.ucsd.edu 2200 You can also make additional connections via ssh/sftp/sshfs on port 2200.

Frequent connections can be stored in a configuration file. Put a line in ~/.ssh/tunnels for each connection with an alias, the firewall, the destination, and the port: # ~/.ssh/tunnels # Alias fwuser@Firewall destuser@Destination Port hotstuff sbliven@kerberos.ucsd.edu sbliven@hotstuff.ucsd.edu 2200 Now, just write fw hotstuff and everything will connect.