Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to downsample x y data?

I have a trajectory that consists of 42 x,y coordinates;

I need to downsample these coordinates into 11 x,y points;

Just wonder if there is a function in R that can do that for me.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

the data is :

x-coordinate

x= c(1.07,0.98,0.90,0.81,0.73,0.65,0.57,0.49,0.41,0.33,0.25,0.17,0.1,0.01,-0.06,
     -0.14,-0.21,-0.28,-0.36,-0.43,-0.49,-0.54,-0.59,-0.63,-0.66,-0.69,-0.72,-0.73,
      -0.72,-0.70,-0.67,-0.64,-0.61,-0.63-0.64,-0.65,-0.65,-0.65,-0.65,-0.66,-0.66,
      -0.64)

y-coordinate

y=c(-0.11,-0.09,-0.10,-0.12,-0.13,-0.14,-0.19,-0.21,-0.22,-0.24,-0.26,-0.28,-0.30,
     -0.32,-0.34,-0.37,-0.41,-0.45,-0.50,-0.55,-0.61,-0.68,-0.76,-0.84,-0.92,-1.00,
     -1.08,-1.18,-1.28,-1.38,-1.47,-1.56,-1.61,-1.67,-1.73,-1.79,-1.85,-1.91,-1.96,
     -2.02,-2.07)

>Solution :

The function approx allows you to interpolate values, so assuming your points are measured at regular time intervals, you can downsample the path into 11 regularly measured points along the path as follows:

x2 <- approx(seq(0, 1, length = length(x)), x, xout = seq(0, 1, length = 11))$y
y2 <- approx(seq(0, 1, length = length(y)), y, xout = seq(0, 1, length = 11))$y

Now if you plot the original path:

plot(x, y, type = "l")

enter image description here

Then you can see the 11 downsampled points along the path:

points(x2, y2, col = 'red')

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading