copy of JR King's Matlab code