(I know the post is old) A A better way of doing it can be found at the end of this document: https://hal.archives-ouvertes.fr/hal-01704943v2/document
inIn this document, it's explained how to do it properly and including a C code so it's easy to copy and paste to see if the solution works.