I have been using a threshold-based step count code in RStudio sourced from Ducharme et al. 2021 “A transparent method for step detection using an acceleration threshold” to count steps in my accelerometer data. The code works to count steps as intended, but occasionally it will double count a step peak in the data. In the image below, you can see the third and tenth step have been double counted, causing the code to count 12 steps instead of the correct 10.
Frequency of step count graph
I’m using the z-axis data to smooth and count steps, here is a a minute’s worth of the data represented in the photo:
structure(list(z_only = c(-0.238, -0.234, -0.227, -0.219, -0.207,
-0.203, -0.207, -0.203, -0.207, -0.203, -0.203, -0.211, -0.211,
-0.211, -0.211, -0.215, -0.219, -0.215, -0.203, -0.211, -0.207,
-0.211, -0.219, -0.211, -0.215, -0.215, -0.211, -0.215, -0.207,
-0.199, -0.195, -0.191, -0.188, -0.195, -0.207, -0.211, -0.215,
-0.211, -0.211, -0.203, -0.199, -0.199, -0.195, -0.195, -0.191,
-0.191, -0.195, -0.191, -0.191, -0.195, -0.195, -0.195, -0.191,
-0.191, -0.191, -0.191, -0.188, -0.191, -0.188, -0.184, -0.184,
-0.184, -0.184, -0.184, -0.188, -0.195, -0.195, -0.195, -0.188,
-0.172, -0.172, -0.164, -0.156, -0.148, -0.148, -0.148, -0.145,
-0.148, -0.152, -0.156, -0.16, -0.156, -0.16, -0.16, -0.16, -0.164,
-0.16, -0.156, -0.145, -0.148, -0.148, -0.152, -0.16, -0.168,
-0.176, -0.18, -0.176, -0.172, -0.172, -0.164, -0.16, -0.16,
-0.148, -0.145, -0.145, -0.141, -0.145, -0.152, -0.148, -0.152,
-0.148, -0.145, -0.145, -0.145, -0.133, -0.141, -0.137, -0.137,
-0.145, -0.145, -0.141, -0.137, -0.137, -0.129, -0.129, -0.133,
-0.137, -0.133, -0.129, -0.117, -0.109, -0.102, -0.098, -0.086,
-0.082, -0.078, -0.078, -0.078, -0.078, -0.074, -0.078, -0.078,
-0.082, -0.09, -0.09, -0.078, -0.082, -0.074, -0.07, -0.07, -0.066,
-0.066, -0.066, -0.07, -0.074, -0.07, -0.074, -0.082, -0.078,
-0.078, -0.074, -0.066, -0.063, -0.063, -0.063, -0.059, -0.055,
-0.059, -0.059, -0.055, -0.059, -0.059, -0.055, -0.063, -0.063,
-0.059, -0.055, -0.063, -0.059, -0.055, -0.059, -0.059, -0.055,
-0.059, -0.059, -0.063, -0.063, -0.066, -0.063, -0.055, -0.047,
-0.043, -0.035, -0.027, -0.023, -0.012, -0.008, -0.02, -0.02,
-0.02, -0.023, -0.023, -0.027, -0.023, -0.027, -0.027, -0.027,
-0.027, -0.023, -0.02, -0.023, -0.02, -0.02, -0.023, -0.023,
-0.027, -0.027, -0.027, -0.027, -0.023, -0.027, -0.027, -0.012,
-0.004, 0, 0.008, 0.012, 0.008, 0.004, 0, -0.004, 0.004, 0, -0.004,
-0.004, -0.008, -0.004, -0.004, -0.004, -0.008, -0.016, -0.016,
-0.02, -0.02, -0.016, -0.004, -0.004, 0, 0.004, 0.008, 0.008,
0.016, 0.02, 0.023, 0.02, 0.02, 0.02, 0.016, 0.016, 0.016, 0.012,
0.008, 0.008, 0.008, 0.008, 0.008, 0.012, 0.008, 0.012, 0.016,
0.02, 0.023, 0.016, 0.02, 0.02, 0.02, 0.023, 0.023, 0.02, 0.027,
0.027, 0.035, 0.031, 0.043, 0.043, 0.051, 0.043, 0.059, 0.055,
0.055, 0.051, 0.047, 0.047, 0.039, 0.039, 0.039, 0.035, 0.031,
0.031, 0.035, 0.035, 0.035, 0.043, 0.043, 0.043, 0.043, 0.043,
0.039, 0.043, 0.039, 0.035, 0.043, 0.051, 0.043, 0.055, 0.051,
0.066, 0.066, 0.07, 0.07, 0.066, 0.063, 0.059, 0.063, 0.063,
0.07, 0.07, 0.074, 0.074, 0.07, 0.063, 0.063, 0.063, 0.063, 0.066,
0.066, 0.051, 0.051, 0.059, 0.051, 0.051, 0.047, 0.051, 0.043,
0.043, 0.039, 0.039, 0.039, 0.051, 0.043, 0.047, 0.043, 0.043,
0.039, 0.031, 0.027, 0.031, 0.035, 0.035, 0.035, 0.039, 0.031,
0.031, 0.035, 0.027, 0.031, 0.027, 0.031, 0.035, 0.035, 0.039,
0.047, 0.043, 0.039, 0.039, 0.039, 0.027, 0.027, 0.031, 0.035,
0.035, 0.023, 0.027, 0.027, 0.023, 0.02, 0.02, 0.023, 0.02, 0.016,
0.016, 0.02, 0.023, 0.035, 0.027, 0.027, 0.023, 0.02, 0.023,
0.012, 0.004, 0, -0.004, -0.008, 0, 0.004, 0.004, 0, -0.004,
-0.012, -0.016, -0.02, -0.02, -0.012, 0, 0.004, 0.012, 0.012,
0.008, 0.008, 0.004, 0.004, 0.008, 0.004, -0.004, -0.016, -0.023,
-0.023, -0.023, -0.027, -0.016, -0.02, -0.02, -0.023, -0.023,
-0.023, -0.012, -0.016, -0.016, -0.016, -0.02, -0.023, -0.023,
-0.016, -0.027, -0.023, -0.023, -0.02, -0.023, -0.023, -0.027,
-0.023, -0.023, -0.02, -0.012, -0.016, -0.02, -0.023, -0.027,
-0.016, -0.023, -0.031, -0.004, -0.012, 0.008, 0.004, 0.004,
0.008, 0, 0.012, 0, 0, -0.004, -0.008, -0.008, -0.008, -0.008,
-0.008, -0.008, -0.008, -0.012, -0.008, 0, 0.008, 0.008, 0.004,
0.004, 0, -0.016, -0.02, -0.02, -0.016, -0.02, -0.004, 0, -0.004,
0, 0, -0.004, -0.008, -0.012, -0.012, -0.004, -0.004, -0.012,
0.008, 0.008, 0.004, 0.004, 0, -0.008, 0, 0, 0, -0.008, 0.004,
-0.008, 0, 0.004, 0, 0, -0.008, -0.012, -0.012, -0.016, -0.02,
-0.016, -0.02, -0.023, 0.004, 0.004, 0.008, 0.012, 0.008, 0.004,
-0.008, -0.016, -0.008, -0.008, 0, -0.004, -0.008, 0, -0.004,
0, -0.004, 0.012, 0.016, 0.004, 0.012, 0.012, 0.008, 0.004, 0,
-0.008, -0.016, -0.02, -0.023, -0.027, -0.02, -0.008, 0, 0, 0.008,
0.02, 0.027, 0.016, 0.012, 0.02, 0.02, 0.016, 0.012, 0.008, 0.008,
0.008, 0.02, 0.008, 0.023, 0.016, 0.016, -0.008, -0.008, 0.004,
0.008, 0.031, 0.051, 0.09, 0.109, 0.121, 0.121, 0.133, 0.152,
0.152, 0.16, 0.156, 0.164, 0.156, 0.156, 0.133, 0.078, 0.039,
-0.027, -0.059, -0.09, 0, 0.363, 1.277, 0.988, 1.074, 1.059,
0.906, 0.695, 0.727, 0.469, 0.152, 0.113, 0.27, 0.676, 1.086,
1.531, 1.77, 2.055, 0.875, -1.512, 0.406, 0.652, -0.516, 0.168,
0, 0.02, -0.012, -0.008, -0.023, -0.031, -0.125, -0.223, -0.262,
-0.262, -0.262, -0.266, -0.266, -0.242, -0.23, -0.223, -0.215,
-0.215, -0.219, -0.207, -0.242, -0.25, -0.258, -0.262, -0.254,
-0.242, -0.277, -0.273, -0.27, -0.266, -0.266, -0.262, -0.25,
-0.246, -0.246, -0.238, -0.262, -0.273, -0.27, -0.266, -0.262,
-0.258, -0.254, -0.246, -0.238, -0.234, -0.234, -0.23, -0.234,
-0.234, -0.238, -0.238, -0.242, -0.234, -0.23, -0.23, -0.23,
-0.23, -0.215, -0.234, -0.223, -0.23, -0.23, -0.227, -0.223,
-0.219, -0.219, -0.223, -0.223, -0.23, -0.227, -0.227, -0.219,
-0.207, -0.203, -0.195, -0.191, -0.191, -0.191, -0.191, -0.188,
-0.195, -0.199, -0.199, -0.203, -0.195, -0.195, -0.199, -0.195,
-0.195, -0.191, -0.191, -0.184, -0.184, -0.176, -0.18, -0.172,
-0.172, -0.176, -0.184, -0.18, -0.176, -0.168, -0.16, -0.16,
-0.156, -0.152, -0.141, -0.137, -0.133, -0.129, -0.133, -0.129,
-0.129, -0.129, -0.133, -0.137, -0.137, -0.137, -0.141, -0.141,
-0.145, -0.141, -0.137, -0.137, -0.133, -0.129, -0.129, -0.129,
-0.129, -0.133, -0.129, -0.125, -0.125, -0.117, -0.117, -0.117,
-0.117, -0.113, -0.109, -0.113, -0.113, -0.109, -0.109, -0.113,
-0.113, -0.109, -0.109, -0.105, -0.102, -0.102, -0.098, -0.098,
-0.098, -0.094, -0.094, -0.098, -0.109, -0.121, -0.129, -0.133,
-0.133, -0.133, -0.129, -0.129, -0.117, -0.113, -0.094, -0.094,
-0.09, -0.086, -0.086, -0.094, -0.102, -0.105, -0.113, -0.121,
-0.125, -0.109, -0.109, -0.102, -0.102, -0.105, -0.098, -0.098,
-0.098, -0.094, -0.086, -0.09, -0.082, -0.082, -0.086, -0.094,
-0.094, -0.098, -0.098, -0.098, -0.094, -0.094, -0.09, -0.094,
-0.094, -0.09, -0.074, -0.078, -0.07, -0.07, -0.063, -0.063,
-0.059, -0.059, -0.059, -0.059, -0.055, -0.063, -0.063, -0.055,
-0.059, -0.059, -0.063, -0.066, -0.063, -0.07, -0.066, -0.063,
-0.063, -0.066, -0.07, -0.078, -0.082, -0.082, -0.086, -0.074,
-0.07, -0.059, -0.063, -0.066, -0.051, -0.043, -0.039, -0.031,
-0.023, -0.023, 0.012, -0.016, 0.012, -0.012, 0.012, 0, -0.004,
-0.004, -0.004, -0.004, -0.008, 0.004, 0, 0.02, 0.012, 0.027,
0.02, 0.023, 0.004, 0.008, 0.02, 0.004, 0.023, 0.023, 0.051,
0.063, 0.07, 0.078, 0.082, 0.086, 0.098, 0.098, 0.094, 0.098,
0.094, 0.105, 0.098, 0.098, 0.094, 0.078, 0.094, 0.117, 0.113,
0.121, 0.137, 0.148, 0.148, 0.164, 0.172, 0.164, 0.148, 0.137,
0.133, 0.129, 0.121, 0.105, 0.129, 0.16, 0.18, 0.18, 0.219, 0.234,
0.25, 0.258, 0.258, 0.262, 0.254, 0.25, 0.238, 0.246, 0.242,
0.246, 0.258, 0.254, 0.277, 0.297, 0.313, 0.313, 0.313, 0.305,
0.301, 0.289, 0.277, 0.277, 0.273, 0.27, 0.273, 0.281, 0.297,
0.309, 0.332, 0.332, 0.344, 0.352, 0.363, 0.371, 0.352, 0.348,
0.348, 0.344, 0.344, 0.344, 0.34, 0.34, 0.332, 0.324, 0.297,
0.297, 0.305, 0.293, 0.258, 0.211, 0.219, 0.441, 0.516, 0.797,
0.883, 0.98, 0.949, 1.129, 0.91, 0.902, 0.809, 0.703, 0.754,
0.793, 0.422, 0.43, 1.035, 1.371, 1.344, 1.535, 2.086, 1.816,
-0.418, -0.445, 0.766, 0.035, -0.266, 0.117, 0.02, -0.063, -0.074,
-0.105, -0.109, -0.094, -0.09, -0.07, -0.07, -0.063, -0.07, -0.063,
-0.066, -0.082, -0.133, -0.172, -0.23, -0.266, -0.273, -0.266,
-0.266, -0.266, -0.254, -0.234, -0.223, -0.238, -0.242, -0.25,
-0.238, -0.262, -0.258, -0.262, -0.262, -0.254, -0.242, -0.23,
-0.223, -0.215, -0.207, -0.223, -0.238, -0.242, -0.238, -0.234,
-0.246, -0.254, -0.242, -0.234, -0.234, -0.246, -0.242, -0.238,
-0.242, -0.246, -0.246, -0.25, -0.246, -0.246, -0.234, -0.234,
-0.23, -0.23, -0.234, -0.227, -0.23, -0.23, -0.234, -0.238, -0.234,
-0.238, -0.246, -0.246, -0.246, -0.242, -0.238, -0.246, -0.211,
-0.25, -0.266, -0.246, -0.246, -0.242, -0.238, -0.246, -0.254,
-0.25, -0.254, -0.238, -0.262, -0.254, -0.25, -0.246, -0.238,
-0.238, -0.234, -0.23, -0.23, -0.23, -0.234, -0.234, -0.23, -0.227,
-0.219, -0.227, -0.23, -0.223, -0.219, -0.215, -0.211, -0.211,
-0.215, -0.211, -0.211, -0.203, -0.195, -0.199, -0.188, -0.195,
-0.195, -0.191, -0.191, -0.195, -0.191, -0.18, -0.18, -0.176,
-0.172, -0.172, -0.168, -0.172, -0.176, -0.172, -0.168, -0.164,
-0.176, -0.168, -0.168, -0.164, -0.164, -0.156, -0.152, -0.152,
-0.145, -0.148, -0.148, -0.145, -0.145, -0.148, -0.145, -0.141,
-0.145, -0.145, -0.141, -0.141, -0.137, -0.141, -0.141, -0.141,
-0.133, -0.137, -0.137, -0.133, -0.137, -0.137, -0.133, -0.137,
-0.133, -0.133, -0.133, -0.141, -0.133, -0.133, -0.133, -0.129,
-0.129, -0.129, -0.129, -0.125, -0.125, -0.129, -0.129, -0.129,
-0.125, -0.125, -0.117, -0.121, -0.121, -0.117, -0.113, -0.109,
-0.113, -0.105, -0.105, -0.109, -0.102, -0.109, -0.105, -0.102,
-0.105, -0.102, -0.098, -0.102, -0.098, -0.098, -0.098, -0.102,
-0.109, -0.109, -0.105, -0.109, -0.109, -0.109, -0.105, -0.105,
-0.102, -0.105, -0.102, -0.105, -0.109, -0.121, -0.113, -0.121,
-0.117, -0.113, -0.109, -0.109, -0.102, -0.098, -0.098, -0.094,
-0.094, -0.094, -0.098, -0.098, -0.102, -0.102, -0.109, -0.105,
-0.105, -0.105, -0.102, -0.105, -0.102, -0.094, -0.09, -0.09,
-0.086, -0.09, -0.094, -0.098, -0.098, -0.102, -0.094, -0.09,
-0.078, -0.07, -0.07, -0.066, -0.07, -0.07, -0.066, -0.066, -0.066,
-0.066, -0.063, -0.066, -0.066, -0.063, -0.063, -0.059, -0.055,
-0.047, -0.051, -0.059, -0.066, -0.078, -0.086, -0.094, -0.094,
-0.082, -0.066, -0.043, -0.031, -0.02, -0.012, -0.012, -0.02,
-0.02, -0.027, -0.039, -0.051, -0.059, -0.063, -0.07, -0.066,
-0.07, -0.066, -0.059, -0.055, -0.043, -0.027, -0.02, -0.023,
-0.016, -0.027, -0.035, -0.039, -0.039, -0.039, -0.039, -0.023,
-0.035, -0.027, -0.027, -0.012, -0.023, 0, -0.016, 0.016, 0,
0.016, 0, 0.02, 0.008, 0.023, 0.027, 0.035, 0.023, 0.047, 0.043,
0.043, 0.039, 0.047, 0.051, 0.043, 0.043, 0.055, 0.043, 0.039,
0.039, 0.035, 0.055, 0.063, 0.07, 0.082, 0.078, 0.102, 0.105,
0.117, 0.121, 0.129, 0.137, 0.145, 0.168, 0.16, 0.18, 0.168,
0.18, 0.172, 0.18, 0.18, 0.184, 0.18, 0.199, 0.215, 0.23, 0.242,
0.262, 0.254, 0.273, 0.266, 0.262, 0.254, 0.25, 0.254, 0.242,
0.242, 0.246, 0.234, 0.246, 0.238, 0.258, 0.262, 0.27, 0.277,
0.355, 0.328, 0.359, 0.387, 0.367, 0.336, 0.301, 0.336, 0.754,
0.539, 0.926, 1.121, 1.637, 1.355, 1.254, 1.211, 1.004, 1.172,
1.031, 1.02, 1.035, 0.832, 0.574, 0.703, 1.164, 1.523, 1.359,
1.035, 0.711, 0.992, 1.375, 1.402, 1.645, 0.91, -0.57, -0.453,
1.035, 0.465, -0.227, 0.25, 0.184, -0.043, -0.02, 0.008, -0.027,
0.012, 0.02, 0.023, 0.016, -0.012, -0.277, -0.605, -0.355, -0.25,
-0.266, -0.254, -0.234, -0.215, -0.203, -0.195, -0.199, -0.207,
-0.207, -0.199, -0.207, -0.23, -0.215, -0.215, -0.207, -0.188,
-0.176, -0.164, -0.156, -0.148, -0.129, -0.129, -0.121, -0.105,
-0.043, -0.102, -0.156, -0.156, -0.176, -0.176, -0.172, -0.148,
-0.125, -0.082, -0.043, -0.027, -0.031, -0.027, -0.047, -0.059,
-0.055, -0.059, -0.043, -0.02, -0.004, 0.02, 0.027, 0.039, 0.031,
0.055, 0.055, 0.098, 0.145, 0.164, 0.199, 0.191, 0.238, 0.191,
0.246, 0.207, 0.129, 0.438, 0.637, 0.762, 0.949, 1.406, 1.113,
1.121, 1.266, 0.996, 1.09, 1.191, 1.191, 1.285, 1.215, 0.969,
0.805, 0.945, 1.477, 1.48, 1.379, 1.34, 1.652, 1.637, 1.363,
-0.172, -0.234, 0.684, -0.195, -0.363, -0.234, -0.32, -0.223,
-0.18, -0.203, -0.309, -0.34, -0.191, -0.172, -0.285, -0.258,
-0.23, -0.23, -0.215, -0.207, -0.203, -0.184, -0.129, -0.16,
-0.215, -0.211, -0.203, -0.195, -0.195, -0.172, -0.137, -0.102,
-0.051, -0.008, 0.008, 0.031, 0.035, 0.086, 0.109, 0.121, 0.113,
0.113, 0.156, 0.18, 0.168, 0.199, 0.246, 0.242, 0.246, 0.387,
0.441, 0, -0.297, -0.277, 2.023, 0.07, 2.684, 0.941, 1.176, 1.215,
0.781, 1.242, 1.523, 1.754, 1.656, 1.34, 0.902, 0.879, 0.758,
1.18, 1.395, 1.633, 1.863, 1.84, 2.078, -0.594, 0.07, 0.301,
-0.363, -0.887, -0.434, -0.246, -0.094, -0.277, -0.34, -0.313,
-0.305, -0.313, -0.309, -0.309, -0.301, -0.27, -0.223, -0.211,
-0.227, -0.215, -0.203, -0.195, -0.172, -0.164, -0.105, -0.078,
0.039, 0.004, 0.023, 0.063, 0.145, 0.137, 0.156, 0.156, 0.184,
0.199, 0.199, 0.188, 0.203, 0.211, 0.199, 0.211, 0.336, 0.496,
0.191, -0.594, -0.477, 2.488, 0.848, 2.809, 0.535, 1.199, 0.941,
0.875, 1.32, 1.344, 1.605, 1.602, 1.371, 1.074, 1.047, 1.191,
1.191, 1.363, 1.828, 2.012, 2.598, 1.703, -0.102, 0.984, -0.582,
-0.613, -0.414, -0.141, -0.25, -0.445, -0.371, -0.344, -0.344,
-0.332, -0.293, -0.27, -0.301, -0.281, -0.273, -0.246, -0.215,
-0.195, -0.191, -0.129, -0.102, -0.074, -0.031, -0.016, 0.086,
0.102, 0.086, 0.133, 0.168, 0.172, 0.16, 0.199, 0.184, 0.207,
0.176, 0.164, 0.223, 0.449, 0.141, -0.734, -0.488, 2.656, 1.316,
2.238, 0.43, 1.449, 0.727, 1.039, 1.348, 1.387, 1.711, 1.609,
1.34, 1.09, 1.004, 1.098, 1.016, 1.23, 1.609, 1.59, 1.953, 2.207,
-0.598, 0.801, -0.547, -0.441, -0.254, -0.188, -0.336, -0.398,
-0.414, -0.375, -0.344, -0.297, -0.258, -0.305, -0.293, -0.27,
-0.254, -0.234, -0.207, -0.172, -0.133, -0.074, -0.063, 0, 0.012,
0.094, 0.105, 0.098, 0.145, 0.188, 0.199, 0.223, 0.246, 0.238,
0.195, 0.148, 0.215, 0.285, 0.512, -0.637, -1.141, 1.031, 0.367,
3.512, 0.664, 1.293, 1.031, 0.984, 1.445, 1.371, 1.68, 1.586,
1.301, 1.043, 1.18, 1.266, 1.434, 1.797, 1.813, 1.906, 2.57,
0.828, -0.227, 0.688, -0.594, -0.273, -0.238, -0.27, -0.324,
-0.375, -0.336, -0.309, -0.313, -0.309, -0.289, -0.254, -0.234,
-0.258, -0.25, -0.238, -0.23, -0.215, -0.211, -0.168, -0.156,
-0.133, -0.117, -0.102, -0.039, 0.047, 0.051, 0.031, 0.082, 0.105,
0.117, 0.133, 0.141, 0.117, 0.168, 0.133, 0.152, 0.172, 0.23,
0.414, 0.461, 0.199, -0.242, -0.66, 0.652, 2.141, 1.516, 1.98,
0.785, 1.25, 0.918, 1.152, 1.352, 1.469, 1.605, 1.484, 1.23,
1.02, 1.016, 1.078, 1.059, 1.43, 2.41, 1.957, 2.461, 1.465, -0.91,
-0.02, 0.258, -0.863, -0.285, -0.164, -0.094, -0.27, -0.301,
-0.266, -0.227, -0.266, -0.273, -0.273, -0.27, -0.246, -0.195,
-0.18, -0.219, -0.195, -0.203, -0.195, -0.191, -0.188, -0.148,
-0.125, -0.105, -0.078, -0.023, 0.039, 0.105, 0.086, 0.086, 0.102,
0.113, 0.09, 0.094, 0.129, 0.148, 0.184, 0.215, 0.18, 0.211,
0.215, 0.25, 0.219, -1.195, 0.301, 2.527, 2.172, 1.582, 0.809,
1.367, 0.809, 1.215, 1.484, 1.625, 1.668, 1.426, 1.219, 0.906,
0.824, 0.789, 1.027, 1.367, 1.566, 1.801, 2.324, 1.516, -0.691,
-0.094, -0.238, -0.395, -0.141, -0.84, -0.266, -0.145, -0.082,
0.027, -0.031, -0.18, -0.23, -0.184, -0.191, -0.254, -0.262,
-0.266, -0.277, -0.262, -0.25, -0.262, -0.266, -0.266, -0.238,
-0.219, -0.203, -0.207, -0.211, -0.211, -0.211, -0.227, -0.227,
-0.219, -0.215, -0.211, -0.199, -0.195, -0.195, -0.188, -0.188,
-0.18, -0.18, -0.184, -0.184, -0.184, -0.188, -0.188, -0.172,
-0.172, -0.164, -0.16, -0.164, -0.16, -0.16, -0.16, -0.16, -0.152,
-0.145, -0.145, -0.137, -0.137, -0.129, -0.137, -0.129, -0.129,
-0.133, -0.129, -0.129, -0.129, -0.113, -0.121, -0.117, -0.113,
-0.117, -0.113, -0.117, -0.117, -0.117, -0.113, -0.113, -0.117,
-0.109, -0.105, -0.109, -0.133, -0.145, -0.09, -0.066, -0.098,
-0.102, -0.102, -0.098, -0.105, -0.098, -0.102, -0.109, -0.098,
-0.098, -0.098, -0.105, -0.102, -0.102, -0.102, -0.102, -0.098,
-0.098, -0.098, -0.098, -0.102, -0.102, -0.098, -0.098, -0.098,
-0.098, -0.098, -0.098, -0.098, -0.098, -0.098, -0.094, -0.098,
-0.094, -0.094, -0.094, -0.094, -0.094, -0.098, -0.098, -0.098,
-0.098, -0.098, -0.098, -0.094, -0.094, -0.094, -0.09, -0.094,
-0.098, -0.09, -0.09, -0.086, -0.094, -0.09, -0.09, -0.094, -0.094,
-0.094, -0.09, -0.086, -0.094, -0.098, -0.066, -0.074, -0.09,
-0.082, -0.078, -0.086, -0.082, -0.078, -0.086, -0.078, -0.078,
-0.078, -0.074, -0.074, -0.074, -0.078, -0.078, -0.078, -0.078,
-0.078, -0.078, -0.082, -0.078, -0.074, -0.074, -0.07, -0.07,
-0.082, -0.078, -0.078, -0.082, -0.078, -0.078, -0.07, -0.074,
-0.074, -0.07, -0.074, -0.07, -0.074, -0.07, -0.066, -0.063,
-0.063, -0.063, -0.066, -0.07, -0.07, -0.066, -0.066, -0.07,
-0.066, -0.066, -0.063, -0.063, -0.063, -0.059, -0.059, -0.059,
-0.059, -0.059, -0.055, -0.055, -0.055, -0.055, -0.055, -0.055,
-0.055, -0.047, -0.051, -0.043, -0.047, -0.047, -0.043, -0.047,
-0.039, -0.039, -0.039, -0.039, -0.043, -0.043, -0.039, -0.035,
-0.035, -0.039, -0.035, -0.035, -0.039, -0.035, -0.035, -0.031,
-0.031, -0.031, -0.031, -0.031, -0.035, -0.031, -0.07, -0.051,
0.027, -0.004, -0.039, -0.016, -0.016, -0.031, -0.02, -0.027,
-0.035, -0.027, -0.027, -0.027, -0.027, -0.027, -0.023, -0.027,
-0.023, -0.023, -0.023, -0.023, -0.023, -0.027, -0.027, -0.027,
-0.023, -0.02, -0.02, -0.027, -0.027, -0.027, -0.023, -0.023,
-0.02, -0.02, -0.02, -0.02, -0.02, -0.016, -0.012, -0.016, -0.008,
-0.016, -0.016, -0.02, -0.02, -0.02, -0.012, -0.02, -0.016, -0.016,
-0.016, -0.012, -0.016, -0.023, -0.023, -0.016, -0.02, -0.016,
-0.023, -0.023, -0.02, -0.02, -0.023, -0.02, -0.02, -0.023, -0.023,
-0.027, -0.023, -0.023, -0.023, -0.027, -0.02, -0.023, -0.023,
-0.023, -0.027, -0.027, -0.031, -0.027, -0.027, -0.027, -0.023,
-0.023, -0.023, -0.027, -0.031, -0.027, -0.027, -0.027, -0.027,
-0.027, -0.027, -0.031, -0.027, -0.031, -0.031, -0.031, -0.027,
-0.027, -0.023, -0.027, -0.027, -0.031, -0.039, -0.031, -0.031,
-0.035, -0.035, -0.031, -0.035, -0.035, -0.031, -0.027, -0.031,
-0.027, -0.035, -0.027, -0.035, -0.027, -0.027, -0.031, -0.031,
-0.031, -0.031, -0.027, -0.031, -0.027, -0.031, -0.031, -0.031,
-0.031, -0.027, -0.027, -0.027, -0.035, -0.027, -0.039, -0.039,
-0.035, -0.035, -0.039, -0.043, -0.031, -0.031, -0.031, -0.027,
-0.031, -0.031, -0.027, -0.031, -0.027, -0.027, -0.031, -0.027,
-0.031, -0.031, -0.027, -0.031, -0.031, -0.027, -0.031, -0.027,
-0.031, -0.031, -0.027, -0.031, -0.027, -0.027, -0.023, -0.027,
-0.031, -0.027, -0.031, -0.031, -0.027, -0.027, -0.031, -0.027,
-0.027, -0.027, -0.023, -0.031, -0.035, -0.035, -0.039, -0.035,
-0.031, -0.035, -0.031, -0.031, -0.027, -0.027, -0.031, -0.027,
-0.027, -0.031, -0.031, -0.035, -0.027, -0.027, -0.027, -0.039,
-0.031, -0.031, -0.031, -0.035, -0.031, -0.027, -0.027, -0.031,
-0.035, -0.027, -0.031, -0.031, -0.027, -0.031, -0.031, -0.027,
-0.027, -0.027, -0.027, -0.031, -0.027, -0.031, -0.027, -0.031,
-0.027, -0.035, -0.031, -0.031, -0.035, -0.027, -0.031, -0.027,
-0.031, -0.031, -0.031, -0.027, -0.027, -0.027, -0.027, -0.027,
-0.027, -0.027, -0.031, -0.031)), row.names = c(NA, -2440L), spec = structure(list(
cols = list(z_axis = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), delim = ","), class = "col_spec"), problems = <pointer: 0x000001e8d4a0fd60>, class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"))
And here is the code I am using to count steps:
# Subtract mean
zsmooth <- zonly - mean(zonly)
# Filter data
# Define sample frequency
fs <- 40
# Define filter order (4th order)
order <- 4
# Define high-pass and low-pass thresholds
High <- 0.25
Low <- 2.5
# Create coefficients for a 4th order bandpass Butterworth filter
b <- butter(n = order,
W = c(High, Low)/(fs/2),
type = 'pass',
plane = c('z'))
# Dual pass filter to avoid signal shift
zfiltered <- filtfilt(b, zsmooth)
# Create time label
time <- seq(0, 1, length.out =2440)
# Get peaks
pks <- rep(NA, length(zfiltered))
for (i in 2:length(zfiltered)-1){
ifelse (zfiltered[i] > zfiltered[i-1] && zfiltered[i] > zfiltered[i+1], pks[i] <- 1, pks[i] <- NA)
}
pks.locs <- which(!is.na(pks))
threshold <- 0.6
for (p in 1:length(pks.locs)){
ifelse(zfiltered[pks.locs][p] > threshold, next, pks[pks.locs][p] <- NA)
}
steps <- as.numeric(length(which(!is.na(pks))))
idx <- (1:length(pks))[!is.na(pks)]
pks.acc <- zfiltered[idx]
pks.time <- time[idx]
plot(time, zfiltered, type = 'l', col = 'blue')
for (p in 1:length(pks.time)){
points(x = pks.time[p], y = pks.acc[p], col = 'red')
}
I’ve searched for other examples of step count using this method and tried to find one that will address my problem, but have been unsuccessful. Is there a parameter I can add or an edit I can make to the “pks” code that will allow me to remove the double peak counting? This is my first foray into attempting step count, so any and all advice is appreciated. Thank you!
2