Hello Elisa,
Thank you very much for writing.
In this case I would like to recommend using the Sensor Fusion Library. You may find more information at:
NXP Sensor Fusion
Within the Sensor Fusion Library, you may find advanced functions for computation of device orientation, linear acceleration, gyro offset and magnetic interference based on the outputs of NXP inertial and magnetic sensors.
More specifically, I recommend using the AN5021 Orientation Matrices mentioned on the documentation.
Please let me know if this information helps.
Best Regards,
David