Code for polarization analyzer. See http://asaf.pm/wordpress/?p=198