check out my blog engrsajidzaman.blogspot.com and for understanding read this
http://www.google.com.pk/url?sa=t&rct=j&q=sebastian%20o.h.%20madgwick&source=web&cd=2&sqi=2&ved=0CCQQFjAB&url=http%3A%2F%2Fsharenet-wii-motion-trac.googlecode.com%2Ffiles%2FAn_efficient_orientation_filter_for_inertial_and_inertialmagnetic_sensor_arrays.pdf&ei=6MNCUNBjhtSyBsiXgXg&usg=AFQjCNEBfCip2CAVZhCKjxP1HYvVvWruYw