Skip to content

Removing Heart Rate Data from Garmin TCX

Wed 23rd January 2013

This evening I forgot my Garmin GPS watch for training. Luckily I was running with Huw who had remembered his! Therefore I thought it would be smart to steal Huw’s TCX (the XML file format for storing the run data) file from his Garmin Connect, and upload it to Garmin Connect and FetchEveryone for my records.

However, while our paces, route and distances were basically the same, Huw’s data included heart rate which is obviously unique to Huw and not representative of me! To fix this, I had to strip out all the heart rate data in the TCX file, before uploading it.

What to get rid of?

In the TCX file there are a few summary tags scattered around (at the start of an activity and the start of each lap) and then for each point that it has recorded (which will typically also record GPS location and pace). These tags are:

  • <AverageHeartRateBpm>
  • <HeartRateBpm>
  • <MaximumHeartRateBpm>

(retrieved with grep HeartRate original.tcx | tr -d ' ' | sort | uniq)

Between these and their closing tags is usually a <Value> tag with a number in it, but I guess there might be other things. To be safe we want to remove these tags, the next similar closing tag and anything between.


I tried to do this with sed, but as sed works line-by-line it struggled to remove the things between the opening and closing tags. Instead, I wrote a 3-line PHP, which was Huw’s suggestion in the first place 🙂

$file = file_get_contents("first.tcx");
$output = preg_replace("#<.*HeartRateBpm>(\n*) *[<>A-Za-z0-9/]*(\n*) *#", "", $file);
file_put_contents("output.tcx", $output);

The regex could probably do with a bit of fine tuning, but I was getting tired and bored with it and this works, so I thought I’d post it in case it ever helps anyone else!


From → Programming, Running

  1. Phil permalink

    I found another way to do it in Windows using Notepad++ and a slightly different regular expression. Maybe this might help someone who doesn’t have linux:

    • Yep, good shout Phil. Notepad++ is lovely, if slightly more manual. You can also run a PHP script on the Mac Terminal, and even Windows if you have PHP installed 🙂

      If there appeared to be much demand for this amongst non-technicals, I could whip up a small webpage that could do this for them. Would anyone be interested? Reply here if you would!

      • Phil permalink

        Good point, Linux isn’t necessary. I think people would be interested if your page could upload a big file and strip the heart rate info and then either render to the page or provide a downloadable link. Well I’d be interested anyway 🙂 Take note that the RegEx I provided only works for files in the Garmin History folder as they have the text xsi:type=”HeartRateInBeatsPerMinute_t” in the HeartRate tags, but if you download a tcx from Garmin Connect that xsi declaration is missing, so the regex wont match it.

        I’m also looking for an automatic way of removing or smoothing spurious data “spikes”. For example when it thinks I’ve traveled 500 metres in 5 seconds when it received a bad GPS signal (thus giving a false maximum speed reading); or heart rate way above my maximum. But there’s probably more detailed tools for that job.

  2. Thanks for these infos! I would like to point out, that you are able to do it with sed as well 😉 Multiline Pattern Matching is described in this post:

  3. Here’s an online tool I wrote that allows editing spikes or removal of HR data from a TCX file.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: