8.2. Writing C3D files#

Writing a new C3D file is done using ktk.write_c3d. In this example, suppose that we forgot to zero the force plates before recording, and our workflow requires that the input C3D file has zeroed vertical forces. We will open this C3D file as in the previous section, correct the analog signals, and then save a new C3D file.

import kineticstoolkit.lab as ktk
import numpy as np

filename = ktk.doc.download("c3d_test_suite_sample.c3d")
c3d_contents = ktk.read_c3d(filename, convert_point_unit=True)

points = c3d_contents["Points"]
analogs = c3d_contents["Analogs"]

Let’s take a look at the forces:

analogs.plot(["FZ1", "FZ2"])
_images/920f842bff741a2229b3c65590f01d7cda78286654e15bb28807756dcb52c0ba.png

There are some offsets in these signals, that could be corrected by subtracting the median of the signal:

analogs.data["FZ1"] -= np.median(analogs.data["FZ1"])
analogs.data["FZ2"] -= np.median(analogs.data["FZ2"])

analogs.plot(["FZ1", "FZ2"])
_images/ac013fc5f358a9a9bc221dd99354c808805909966e22602b007249726a37b7fe.png

Much better. To save these corrected data as a new C3D file, we use ktk.write_c3d:

ktk.write_c3d("corrected.c3d", points=points, analogs=analogs)