Writing C3D files

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/b3d1e10fd4431f5282672afc149550fed27de53d7e88229104b33e1ef73f7bc1.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/f4268e936d14990299916183b6e61f98ef959de3d9c8fea863d0fdc64e3014c8.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)