Mar 092013
 

False color (or more exactly pseudo color) can be useful when a device / detector records a grayscale image and a human has to interpret it. Fine differences are hard to see in grayscale images und a false color mapping allows us to map the 255 shades of gray of which ever pixel of any 8-bit grayscale image consists, to a full 3×8-bit color scale.

Here is a good mapping for scientific purposes, called Till’s Palette:

Oct 062012
 

Finding the value and the position of the maximum value in a 2D array (or the first one if there are more than one maximum values) can be done like this:

[v,ind]=max(X);
[v1,ind1]=max(max(X));
disp(sprintf('The largest element in this matrix is %f at (%d,%d).', v1, ind(ind1), ind1 ));

% or with less calculations (but not as evident):

[v,ind]=max(X(:));
[y,x] = ind2sub(size(X),ind);
disp(sprintf('The largest element in this matrix is %f at (%d,%d).', v1, y, x ));

References

VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)
Oct 062012
 

If you want to create a gaussian window with Matlab in three dimensions (as you can do in two dimensions with the built-in gausswin()) you can use the function gauss3D() found on MATLAB Central:

S = gauss3D(20,20,7);
slice(S, [], [], 1:size(S,3))
shading flat

The code produces this:

Gauss

References

VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)
Creating a Gaussian Window in 3D Using Matlab
Dec 272011
 

Calculate the Check Sum for a German Tax ID number using Python:

#!/usr/local/env python
# -*- coding: utf-8 -*-

class CalcCheckCipher(object):
    @staticmethod
    def getCheckCipher( idnrString):
        n = 11;
        m = 10;

        product = m;

        for cipher in idnrString:
            sum = (int(cipher) + product) % m
            if sum == 0: sum = m
            product = (2*sum) % n

        checkCipher = (n - product)%m
        return checkCipher


def main ():
    print("0107249563 -> %s" % CalcCheckCipher.getCheckCipher("0107249563"))

if __name__ == "__main__":
    main()

Resources

VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)
Aug 272011
 

ezyfit can prevent you from buying the Curve Fitting Tool that Matlab does not come with by default. This is how I use it to fit data according to a \sin(2 \theta)^2 function :

x=15:45
y = 30 + 40 * sin( (2*x) * pi / 180 ).^2 + 5*(rand(1, length(x)) - .5)
f = ezfit(x,y,'I(theta) = dC + I_0 * sin( (2 * theta) * pi / 180 )^2');
clf
plot(x,y,'r*');
showfit(f)
dispeqfit(f)

Custom functions can be defined and reused (to speed up) as follows:

Aug 252011
 

There seem to be quite a lot of problems with 16-bit grey scale TIFF images (especially with the Python Imaging Library – PIL). If you can, you may want to use FITS instead of TIFF. There are good and up-to-date libraries for Python: PyFITS.

For me, however, the FreeImage library works great to read 16bit TIFF images.

About the TIFF Format

Every TIFF begins with a 2-byte indicator of byte order: 0x490x49 (“II”) for little-endian Intel style and 0x4d0x4d (“MM”) for big-endian Motorola style byte order. More common is the Intel style.

Aug 252011
 

Hub is a useful extension to make git GitHub aware.

catch lines:

hub introduces git to GitHub
and
hub teaches git about GitHub

Installation

brew install hub

Usage

See hub’s webpage for introductory examples and its man page for further reference.

VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)
Aug 222011
 

You can use the following Python function to convert a photon energy (in electron volts) to the corresponding wavelength (in nano meters):

def wavelength_from_energy ( electron_volts):
    """Returns a photon wavelength in nm from a photon energy given in eV."""
    hc = 1239.841842144513
    return hc / E

Where the constant h*c is 1239.84 eV nm:

import physcon as pc
print "h*c in [eV  nm]: %.2f" % (pc.h * pc.c / pc.e * 10**9)
VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)
Aug 222011
 

SciPy (updated to CODATA 2010)

Installation (easiest using pip):

  1. Install Fortran as described for your OS on http://www.scipy.org/Installing_SciPy (or via brew install gfortran on Max OS X)
  2. Install SciPy itself: pip install scipy

Usage:

from scipy.constants import *
print "The Planck constant h:", h
print "The Avogadro constant N_A:", N_A
print "The muon mass in u:", physical_constants['muon mass in u']

Physcon (updated to CODATA 2006)

Installation:

Download the Python module http://www.hjcb.nl/python/physcon.txt and save it as physcon.py.

Usage:

Aug 222011
 

Installation of the pre-built binaries for OS X

Read Installing OSX binaries in the matplotlib Installation FAQ.

Installation via pip

This is a good way if you have an up-to-date Python installation on your computer and pip installed. It takes, however, some time (~ 20 mins for me) to install as it builds the stuff from source:

pip install numpy
pip install matplotlib

Usage