Problem with imaginary numbers

I posted a topic on Stack overflow and have received no answers, so I'm trying here.
The post is:

It relates to a function that I'd like to use in a project for a microscope illuminator. The function will serve nicely as a transfer function between input and output where the sensitivity can be controlled by the slope. I've already developed function for LINear (easy), LOG, and EXP. The function I'm interested in porting to C++ is originally from here and behaves essentially as a inverse sigmoid (Logit) function:

The beauty of the above example is that it anchors at (0,0), (0.5,0.5) and (1,1) making it ideal for a first-stage transfer function with easy remapping to PWM values to operate the LED illuminator.

The problem, described in the Stack overflow question, is that the denominator soon becomes the root of a negative number, yielding imaginary number solutions.

My first step is to try it in Excel (actually LibreOffice Calc) before porting to C++ for the ESP32, but I'm stuck with the -ve root.
Can anyone help?
Thanks,
Ric

Why would you allow inputs that result in taking negative square roots? That can't correspond to a physical quantity.

Perhaps if you explain the purpose a bit more clearly, forum members will have suggestions.

1 Like

Is the area where the denominator is imaginary of any interest to you? It seems like that would only be for values of x larger than sqrt(k) + 0.5. If you're only looking at the range from 0-1 then you could simply test to exclude those values.

Yes. The range is in real numbers for the domain [0.0, 1.0] all valeus of the parameter k.

Nice graphing website!

a7

Hi,
That's exactly the point. I don't want imaginary numbers.
Assume a display shows "brightness" as 0-100%. This maps to 0-255 for 8 bit resolution or 0-511for 9 bit resolution (etc) PWM. With a linear scaling factor of 2.55 or 5.12, integer arithmetic can drive the PWM values presented to the ESP32.

LED illumination is non-linear to the eye. In addition the LED illuminator is used to provide light for photographic exposure.

A LOG function flattening towards (1,1) will show greatest sensitivity at higher luminous values, an EXP function will show greatest sensitivity at low values of illumination where the gradient is least. An inverse sigmoid function flattens to the middle, allowing greatest sensitivity at mid-range values.

The other functions are all tested and working fine but the LOGIT function is hardest to achieve. The very clever algorithm in the link simulates the logit curve altering the gradient around the mid-point but not the anchors at (0,0) and (1,1).

The shape of the curve is ideal. But I need it to deal with real (0-100%) input values and real 0 --> whatever PWM values. And that's where the denominator is tricky for me to work with. I can't find an easy solution.

At least until I read a later post in this same thread. The interval is 0-1 for the input and not 0-100. I need to scale after the function using the y-values and not before by exceeding the input range 0-1.

you got it :slight_smile:

Yes. The input range I'm supplying is 0-100% and the function is valid only for 0<=x<=1. Doh!

I've been really thick in not spotting this before

A thousand thanks.
Ric

When I want to perform an interpolation between point A and point B using an initial vector Va and an ending vector Vb, I often use a cubic Bézier curve.

The formula is defined by four control points (commonly found in vector drawing software): P0 (start point, A), P1, P2, and P3 (end point, B).

f(t) = (1 - t)³ * P0 + 3*(1 - t)² * t * P1 + 3*(1 - t)P2 + t³*P3

where t represents the progress along the curve, it is a parameter ranging from 0 to 1. the formula only uses addition and multiplication

In the case of 2 points and 2 vectors (initial velocity, ending velocity), P0 corresponds to A, P3 corresponds to B, and the vectors Va and Vb are used to define the tangents at points A and B.

P0 = A(Xa, Ya)
P1 = A + Va
P2 = B - Vb
P3 = B(Xb, Yb)

by changing the initial and ending vectors you can define the shape of your curve

1 Like

This is a very elegant "generic solution" that will simulate pretty much all of the desired functions:


With A and B anchored at (0,0) and (1,1) I'd only have to specify four parameters, namely the size and direction of each of the two vectors. This greatly simplifies the maths and would reduce the program size.

It's straightforward to 'condition' the inputs for 0<=x<=1 (eq'n of the form y=mx+c) and to further condition the output to scale for the max PWM output (2^resolution - 1) and adjust for a threshold value (the PWM output below which the LED is not illuminated); the latter depending, of course, on the LED and series resistor. Again the output conditioning is little more than a variation of y=mx+c with the threshold determining the intercept.

A further variation, the sigmoid curve, has no direct use for me but a friend is developing a 6-axis robot arm where the sigmoid function would provide a nice "soft start" and "soft landing" at the limits of travel:
SIGMOID
I like the universality of your approach so thanks very much for the suggestion.
Ric

1 Like

You got it !

You can see t varying between 0 and 1 as the time stepping through the curve

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.