1
$\begingroup$

Suppose I have a formula

$$f(x) = x,$$

where $0 \leq x \leq 255$

Now I want to have a formula

$$f(x) = y,$$

where $0 \leq x \leq 255$, where $f(0) = 0$, $f(255) = 255$ and e.g., $f(128) = 150$ (the value of $150$ might vary).

All other values should be interpolated.

So actually I want a function that is nonlinear (starts with $0$ and goes up to $255$, starting increasing fast and finishes increasing slow); opposite as a parabolic function.

What (kind of) function should I use?

$\endgroup$
  • 3
    $\begingroup$ So you want a function $f(x)$ such that $f(0) = 0$ , $f(128)=150$ and $f(255) = 255$? $\endgroup$ – NoChance May 15 at 21:17
  • $\begingroup$ Welcome to Mathematics Stack Exchange! A quick tour will enhance your experience. Here are helpful tips to write a good question and write a good answer. For equations, please refer to this MathJax tutorial. $\endgroup$ – Pantelis Sopasakis May 15 at 21:17
  • 2
    $\begingroup$ You may use a logistic function. $\endgroup$ – minori minus May 15 at 21:18
  • $\begingroup$ You can interpolate and get a parabolic function, I don't get what you mean by "opposite as a parabolic function". $\endgroup$ – Crostul May 15 at 21:18
  • 1
    $\begingroup$ See wolframalpha.com/input/… . Wolfram Alpha describes an arc of a parabola. $\endgroup$ – Crostul May 15 at 21:26
6
$\begingroup$

WA gives $$\frac{10933x-11x^2}{8128}$$

https://www.wolframalpha.com/input/?i=interpolate+((0,0),(128,150)(255,255))

$\endgroup$
6
$\begingroup$

I think one classical example of nonlinearity could be the gamma for color correction.

enter image description here

Instead of using the interval $X=0\ ..\ 255$ it is easier to work in $[0,1]$ by scaling $x=\frac X{255}$.

Now, remark that any $x^\gamma$ stays in $[0,1]$ since $0^\gamma=0$ and $1^\gamma=1$.

Remark: this is not exactly color correction, which is $x^{1/\gamma}$, but this is just a convention.


So you have linearity for $\gamma=1$ and non-linearity for any other value of the exponent.

Note that if you really want to work with bytes the result can be scaled back using $255\times\left(\frac X{255}\right)^\gamma$

Depending of your choice of $\gamma<1$ or $\gamma>1$ you will get either a fast increase at the start of the interval or at the end of the interval.

Try it here (move the g cursor): https://www.desmos.com/calculator/cnc2diykyx

$\endgroup$
  • $\begingroup$ Actually, this is indeed exact the reason I use it for ... but I kind of promised the acceptance of the answer to Crostul (since he exactly answered my question), but your answer is perfect for the reason I need it for. Hope you understand. $\endgroup$ – Michel Keijzers May 15 at 22:14
  • 2
    $\begingroup$ @MichelKeijzers In that case, I'd wager there already exists a library function for precisely that purpose $\endgroup$ – Hagen von Eitzen May 16 at 5:53
  • $\begingroup$ Desmos seems like an excellent tool. Thanks for the tip! $\endgroup$ – Eric Duminil May 16 at 6:34
  • $\begingroup$ @HagenvonEitzen Probably yes, but for this so simple function I can program the one line formula. $\endgroup$ – Michel Keijzers May 16 at 11:09
2
$\begingroup$

To satisfy the requirements: $$f(128)=150, f(255)=255$$

However, $f(0)=0$ can't be satisfied by this method. I assumed that f(1)=1, however you can change the numbers but not use zero, otherwise, there would be no inverse.

enter image description here

An example of the curve looks like this Curve

I can provide more info about the derivation if you want.

$\endgroup$
  • $\begingroup$ Thanks ... Crostul gave me already a satisfactory formula, but good to know it can be derived too... I'm not fully understand the derivation, but for me it's not that needed right know... I always can search on internet how to solve matrix calculations (I had them at school long time ago, but forgotten about them meanwhile). $\endgroup$ – Michel Keijzers May 15 at 21:40
  • 1
    $\begingroup$ Thank you for your feedback. You can construct an equation of the form $f(x)=Ax^2+Bx+C$ and substitute 3 non-zero values you know x and y for. You will get a system of equations having 3 unknowns (A, B, C). You solve for theses unknowns and plug the values back into $f(x)=Ax^2+Bx+C$. Matrix calculations are optional. $\endgroup$ – NoChance May 15 at 21:44
  • $\begingroup$ Thanks ... that makes a lot of sense indeed, thanks for that info (very useful if in future I encounter similar problems, solving such equations even I should still be able to do that :-) ). $\endgroup$ – Michel Keijzers May 15 at 21:47
  • 1
    $\begingroup$ Thank you, I will be very happy to help. $\endgroup$ – NoChance May 15 at 21:48
  • $\begingroup$ Thanks (although most of my questions are in the electronics/Arduino stack exchange). $\endgroup$ – Michel Keijzers May 15 at 21:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.