Calibrating a Compass module

Hello community..
I need to calibrate a 3 axis compass module for my project. It is enough to calibrate x and y axis. Can you help me in that problem. Any code?
I herd there is a matrix method to calibrate the compass, but I cannot find it

The best reference is Tutorial: How to calibrate a compass (and accelerometer) with Arduino | Underwater Arduino Data Loggers

Thank you jremington