Pages: [1]
Author Topic: Enhanced Interpolation and Extrapolation Tool, written in Python  (Read 1135 times)
dgpb
Full Member
***

Karma: +3/-3
Offline Offline

Posts: 181



Hello guys,

I'm right now in the middle of a master's degree in Data Science and I'm starting to work on little projects to practise my Python skills.
(If you can code properly, maybe you think my code is shit-code... I won't blame you for that).

The one I'm about to present might be interesting:

The thing is that everytime we need to modify an axis lot's of people here take for granted that linear-interpolating the map values for them is the best approach. But it's not.

I mean, myself and some of you maybe fine tune individual values by eye to try to overcome the phenomenon I'm explaining below.

You see, our ECU Maps are indeed a type of function called "Piecewise linear funcion". VAG Engineers had to make the compromise to recreate real life beheavour by a set of points.
They had 2 options here:



Left) They sample X points out of the real funcion and create the map with them as a piecewise linear function.

Right) They average the function between an endpoint tangent piecewise and a midpoint tangent one. I hope they did this.

If you do the left approach you're introducing error (quantization error), sign of it depending on function's convexity/concavity in that point, as you can clearly see below:



Now, things get worse if you re-define the axis and linear interpolate map values.
Let's imagine original axis to have setpoints on int values and you deceide to offset them by 0.5... Look at this simple example, y = x^2.



Blue is the real function.
Orange, the original piecewise from our ECUs.
The thicker segments, the new resultant interpolation. (Big positive error for the orange)
And what about the green one? Pretty good results, ah?

It's a recreation of the original function defined by its original setpoints.
I tried several approaches and this one threw the best results, "PCHIP Splines" (Piecewise Cubic Hermite Interpolating Polynomial).
A special kind of splines much less likely to overshoot.
If we place our new axis setpoints and extract their value from this PCHIP curve, we'll get a much better recreation of the original function and will avoid the vast majority of the error.
If we used the straight forward method of just linear interpolation, we'd be making error once (if engineers took the procedure on the right image above) or twice (if they took the left one).

So I came up with the idea of making an "enhanced" map inter/extrapolator... And yep, extrapolator, cause you can see PCHIP could perform better than linear for extrapolation too:




(Anyways, you'll can always choose which option, linear vs pchip, you prefer for each interpolation and extrapolation, cause sometimes pchip show strange beheavour for extrapolation. You'll can evaluate it on al final graph).



It'll show you, graphically and numerically, how important each original setpoint is, in case you want to delete one to put it outside the current defined zone instead of making a whole new one:

For the moment it only works for 2D, but I'm planning to make a 3D version.
Take this just as a demo.
Here's my GitHub repository just in case any of you is bored enough to give it a try:
https://github.com/Diegogp92/ECU-Map-Inter-Extrapolator/

My initial intention was to generate a GUI integrated in an exe with Tkinter + Pyinstaller. However, I was forced to include all libraries I used and they weight +300MB, which I consider unacceptable for a program like this. I know I could just import the parts of the libraries I use rather than whole ones but I don't know, I felt it wasn't the right approach... Any ideas?

So I deceided to use a notebook + Google Colab instead (You can download it from the github above, the .ipynb. It comes with instructions and I tried to make it as much user friendly as possible and I suggest to colapse all "Code" markdowns, you don't need them.)

In case you never used colab, I attach a PDF with instructions for this too.

And please, feel free to post here what you think of this tool.
« Last Edit: March 14, 2025, 01:42:29 PM by dgpb » Logged
dgpb
Full Member
***

Karma: +3/-3
Offline Offline

Posts: 181


« Reply #1 on: March 14, 2025, 01:24:57 PM »

There are several files in that repository, the one you wanna download is this one:
https://github.com/Diegogp92/ECU-Map-Inter-Extrapolator/blob/main/2D_Map_Inter_Extrapolator_NoteBook.ipynb

Attached, How to use it in Google Colab  Wink
« Last Edit: March 14, 2025, 01:49:06 PM by dgpb » Logged
jcsbanks
Full Member
***

Karma: +20/-3
Offline Offline

Posts: 155


« Reply #2 on: March 14, 2025, 01:33:04 PM »

Which real world functions do you think would have significant quantization error with linear interpolation and the breakpoints used in an OEM calibration? By significant I mean enough to cause an uncorrected control error of significance to control quality or emissions.
Logged
dgpb
Full Member
***

Karma: +3/-3
Offline Offline

Posts: 181


« Reply #3 on: March 14, 2025, 01:39:35 PM »

Which real world functions do you think would have significant quantization error with linear interpolation and the breakpoints used in an OEM calibration? By significant I mean enough to cause an uncorrected control error of significance to control quality or emissions.

From factory, none of them significant. For sure engineers placed their setpoints wisely enough. Either by left or right approach.

However, when redefining an axis, things can complicate, as shown in the original OP.
Logged
jcsbanks
Full Member
***

Karma: +20/-3
Offline Offline

Posts: 155


« Reply #4 on: March 15, 2025, 05:32:59 AM »

The main things I've ever changed that have these sorts of inflections are PID gains, but it isn't unusual to halve or double their values to alter response depending on what has changed, so they don't seem sensitive to quantisation error due to misrepresented curves. I wondered if this is a solution looking for a real world problem, although an interesting thought.
Logged
dgpb
Full Member
***

Karma: +3/-3
Offline Offline

Posts: 181


« Reply #5 on: March 15, 2025, 11:01:08 AM »

The main things I've ever changed that have these sorts of inflections are PID gains, but it isn't unusual to halve or double their values to alter response depending on what has changed, so they don't seem sensitive to quantisation error due to misrepresented curves. I wondered if this is a solution looking for a real world problem, although an interesting thought.

If you wanted to change kfmiop axis, that will affect, in the first place, kfmiop itself (and we all know how delicate it is) but it'll also affect kfzwop, cause their axis is shared and it's not very linear. (If you zeroed dlambts won't affect you thouhg)

It's the first example that comes to my mind. And yes, that's 3d. But the op is just a demo as I said. By doing it for each values of the other axis should work ok btw.
« Last Edit: March 15, 2025, 11:05:59 AM by dgpb » Logged
jcsbanks
Full Member
***

Karma: +20/-3
Offline Offline

Posts: 155


« Reply #6 on: March 16, 2025, 06:59:30 AM »

KFMIOP's load axis and contents are close to linear on the MED17 and MG1 I work with and the axis isn't shared. Maybe the example is more relevant to ME7?
Logged
dgpb
Full Member
***

Karma: +3/-3
Offline Offline

Posts: 181


« Reply #7 on: March 16, 2025, 09:00:07 AM »

KFMIOP's load axis and contents are close to linear on the MED17 and MG1 I work with and the axis isn't shared. Maybe the example is more relevant to ME7?

Me7 for sure and I'd say med9 too
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Page created in 2.103 seconds with 15 queries. (Pretty URLs adds 0s, 0q)