Ellipsoid into Sphere Optimization using Numpy and linalg

Last updated on Thu, 2012-11-15 09:30. Originally submitted by fabio on 2012-10-18 17:45.

I'm working on a user friendly calibration program, since I'm completely aware that what I released some time ago using a python script and Octave is very complex to use for FreeIMU library users.

So, I'm creating a new calibration software which will use Python, Numpy and its linalg component to provide an easy to use libre calibration software.

I started by trying what Nirav Patel suggested but wasn't much successful with it. So, I simply ported the Matlab/Octave code into Python+Numpy+linalg.

Below is the code for you to enjoy and here a sample input file.

""" - Ellipsoid into Sphere calibration library based upon numpy and linalg
Copyright (C) 2012 Fabio Varesano 

Development of this code has been supported by the Department of Computer Science,
Universita' degli Studi di Torino, Italy within the Piemonte Project

This program is free software: you can redistribute it and/or modify
it under the terms of the version 3 GNU General Public License as
published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see .


import numpy
from numpy import linalg

def calibrate(x, y, z):
  H = numpy.array([x, y, z, -y**2, -z**2, numpy.ones([len(x), 1])])
  H = numpy.transpose(H)
  w = x**2
  (X, residues, rank, shape) = linalg.lstsq(H, w)
  OSx = X[0] / 2
  OSy = X[1] / (2 * X[3])
  OSz = X[2] / (2 * X[4])
  A = X[5] + OSx**2 + X[3] * OSy**2 + X[4] * OSz**2
  B = A / X[3]
  C = A / X[4]
  SCx = numpy.sqrt(A)
  SCy = numpy.sqrt(B)
  SCz = numpy.sqrt(C)
  return ([OSx, OSy, OSz], [SCx, SCy, SCz])

if __name__ == "__main__":
  acc_f = open("acc.txt", 'r')
  acc_x = []
  acc_y = []
  acc_z = []
  for line in acc_f:
    reading = line.split()

  (offsets, scale) = calibrate(numpy.array(acc_x), numpy.array(acc_y), numpy.array(acc_z))
  print offsets
  print scale

Python: converting a string representing an hexadecimal integer representation into an int variable

Submitted by fabio on Wed, 2010-08-11 18:27.

Recently I had the need to convert a string containing the hexadecimal representation of an integer into an integer variable.

Eg, with something like FFAA, create an integer variable with the value of 65450.

In some programming languages there is an unhex() function but not in python.

In order to do so you'll need to use int(). If called with two arguments, int() expects the first to be a string representing a number in some base and the second to be the base.

Posted in:

Capturing a screen image with python and PIL on Windows

Submitted by fabio on Tue, 2009-03-17 12:11.

The more I use Python, the more I love it.

This programming language is simply awesome: with it's simple api and the huge amount of freely available additional modules creating complex applications is really easy and fast.

For example less than 15 minutes ago I wondered: how can I capture an image of the computer screen using Python?

If you don't know Python you might think that this could be pretty a complex task.

Posted in:

A simple django basename template filter tag

Last updated on Thu, 2009-04-16 13:52. Originally submitted by fabio on 2008-08-29 12:42.

I'm using Django, a python coded framework to develop web applications, to implement a project for the University.

During the development I needed to have a template tag which, given a path to a file, just returns its basename (just as it do the python os.path.basename).

So I implemented it. Following you have the code for it.

Python: call a function with more arguments on each element of a list

Last updated on Fri, 2008-06-27 18:12. Originally submitted by fabio on 2008-06-25 12:13.

I'm recently working a lot with Python because I understood it perfectly fits my needs being powerful, clean and fast to develop with it.

If you program with it there are good chances that you will end up using lot of lists which are pretty well designed and powerful in Python.

A really useful function in Python is map() which let you execute one function on each element of a list:

L = [0.1, 0.4, 0.7]
L2 = map(round, L)
print L2

#output [0.0, 0.0, 1.0]
Posted in:

Serial RS232 connections in Python

Last updated on Sun, 2010-03-28 19:10. Originally submitted by fabio on 2008-05-08 14:18.

Here at ULPGC I'm following a Robotics course. In the practical part of the exam we have to write some applications to operating some didactics robots we have (Rhino XR-4 and SCARA Robots).

Each robot is connected to a Controller (Rhino Mark-4) which can be connected to a PC to send commands to the Controller in order to let the robots do things.

The PC to Controller connection is a serial RS-232 connection.

Sudoku resolver in Python

Last updated on Fri, 2007-12-14 12:45. Originally submitted by fabio on 2007-12-08 15:15.

A simple Sudoku resolver written in Python.

Sorry for the Italian code comments. Please add a comment below if you don't get parts of the algorithm.