remisalmon / gpx-interpolate

Python function to interpolate GPX data using piecewise cubic Hermite splines
MIT License
49 stars 12 forks source link

Linear interpolation doesn't preserve gradients #3

Closed jonathancolledge closed 3 years ago

jonathancolledge commented 4 years ago

Demo Slope.txt

Hi,

This is an amazing bit of work, thank you for releasing it here. Can you please help with this problem I found when interpolating using it?

I have attached a file (please rename .txt. to .gpx). This is a smooth gradient of 7.2%

When I interpolate using option 1 (linear), it does not linearly interpolate the elevation - so the elevation becomes sawtooth with slopes of 7.2, 7.2, 7.1, 7.2, 7.2, 7.1, 7.2, 7.2, 7.1 and so on.

Is there a way to retain the slope (in this case 7.2%) throughout please?

This is an artificial scenario, but the real scenario is a hill with multiple points with uneven distance between points where I want to interpolate between points without messing up the gradients, because riding the route on a turbo trainer needs smooth gradients without changing (the particular software I use, requires close to evenly spaced points and doesn't allow smoothing in a manner that would help, so it has to be perfect to begin with).

remisalmon commented 4 years ago

Thanks for the bug report, it looks like a rounding error. The actual interpolation is handled by scipy so it might a numpy/scipy issue...

Your GPX file only has 2 points, does the issue occur with 3 or more points?

remisalmon commented 4 years ago

In GPX_interpolate(), try replacing: ele_interp = np.round(ele_interp*1e1)/1e1 with: ele_interp = np.round(ele_interp*1e6)/1e6

That should fix your problem

jonathancolledge commented 4 years ago

Thank you, but sadly, that is effectively the same result. Yes, it happens with any number of points, I chose two to illustrate the effect.

jonathancolledge commented 4 years ago

[Uploading Test Hill 8 points-fixed.txt…]() I've attached a better example file. 8 points with 7 linear gradients.

Oops, I'll have to attach it when on my computer rather than phone

remisalmon commented 4 years ago

The code in my last answer does fix your problem. Here is what it looks like on your first example: https://i.imgur.com/23SfrAV.png

jonathancolledge commented 4 years ago

GPX_interpolate.txt

2020-02-08 (7) 2020-02-08 (5)

How Strange! What am I doing wrong? The images are of the original and interpolated with ele_interp = np.round(ele_interp*1e6)/1e6

Edit: ignore attached images, best way seems to be plotting in Excel

jonathancolledge commented 4 years ago

Ah for me:

Commenting out all these solves it:

remove insignificant digits

    # lat_interp = np.round(lat_interp*1e6)/1e6
    # lon_interp = np.round(lon_interp*1e6)/1e6
    # ele_interp = np.round(ele_interp*1e1)/1e1

OR

remove insignificant digits

    # lat_interp = np.round(lat_interp*1e6)/1e6
    # lon_interp = np.round(lon_interp*1e6)/1e6
    ele_interp = np.round(ele_interp*1e6)/1e6

Thanks!

jonathancolledge commented 4 years ago

This problem is back with the new release! This was originally 2 points interpolated -d 1 -r 1. If linear interpolation, it should be a straight line between the two.

Should be a straight line
remisalmon commented 4 years ago

Can you share you GPX file? I can't reproduce the issue with the last version of the code and your Demo_Slope.gpx file.

jonathancolledge commented 3 years ago

I'm very sorry, I think it was due to rounding in the application I used afterwards. I'm so sorry for the trouble.

Jonathan

Sent from Mail https://go.microsoft.com/fwlink/?LinkId=550986 for Windows 10

From: Remi Salmon notifications@github.com Sent: 04 July 2020 16:44 To: remisalmon/gpx_interpolate gpx_interpolate@noreply.github.com Cc: jonathancolledge jonathancolledge@googlemail.com; Author author@noreply.github.com Subject: Re: [remisalmon/gpx_interpolate] Linear interpolation doesn't preserve gradients (#3)

Can you share you GPX file? I can't reproduce the issue with the last version of the code and your Demo_Slope.gpx file.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/remisalmon/gpx_interpolate/issues/3#issuecomment-653781110, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABBEFAEQMLYVUL73Z7Y6WCTRZ5E7VANCNFSM4KNLYOOQ .

remisalmon commented 3 years ago

No problem, thanks for following up.