Need help with log for 1/X block
Posted: Sun Mar 24, 2024 10:07 am
I've always been really bad at the log/exp functions.
Here's what I'm trying to do.
I want a control that operates over a given range of the pot, but does not go to zero.
For example. if the ratio is 2, then pot 0->1 is output 0.5->1.0.
For a ratio of ten, Pot 0->1 maps to 0.1->1.0.
Now I also want another output that implements y = 1/(x * ratio).
So for example, as the pot is going from 0 to 1,
Output A goes linearly from 1/ratio to 1.0.
Output B follows y = 1/((output A)*ratio)
When pot = 0,
Output A = 1/ratio
Output B = 1
When pot = 1,
Output A = 1.0
Output B = 1/ratio
Here's my code fragment so far.
This is SpinCAD Builder code showing how scale and offfset are calculated from the given ratio invRatio.
The variables such as scale, number1, and offsetVal have to be given initial values in order to maintain syntax. If they show up in a subsequent assignment, they'll be immediately overwritten.
ratioToLogOffset is an xtend function I just wrote that is like this:
This generates intermediate Java code that helps make the calculation.
We calculate the "threshold" as 1/ratio.
Then the code substitution winds up saying in essence,
BUT that said, I am not dealing with the EXP instruction correctly. Under some circumstances, I do see the two outputs switching places, but the one using the 1/X function is not going the full width. I also tried:
but that was even worse.
Maybe one of you nice people who knows these instructions better than I seem to can help? Don't worry about the SpinCAD Builder part of it. Even if I had two examples showing the right values for the log and exp coefficients, say for ratio = 2 and ratio = 10.
equ ratio 4
equ offsetVal 1/ratio
equ scale 1 - offsetVal
rdax input,1 ;read input signal
sof scale, offsetVal
wrax fullRange,1 ;output to fullRange pin
log AAA, BBB
exp CCC, DDD
wrax ratioOut,0 ;output to ratioOut pin
Thanks,
DL
Here's what I'm trying to do.
I want a control that operates over a given range of the pot, but does not go to zero.
For example. if the ratio is 2, then pot 0->1 is output 0.5->1.0.
For a ratio of ten, Pot 0->1 maps to 0.1->1.0.
Now I also want another output that implements y = 1/(x * ratio).
So for example, as the pot is going from 0 to 1,
Output A goes linearly from 1/ratio to 1.0.
Output B follows y = 1/((output A)*ratio)
When pot = 0,
Output A = 1/ratio
Output B = 1
When pot = 1,
Output A = 1.0
Output B = 1/ratio
Here's my code fragment so far.
Code: Select all
rdax input,1 ;read input signal
sof scale, offsetVal
wrax fullRange,1 ;output to fullRange pin
log 1.0, logOffset
exp 1.99, -0.1875 [b]<<<====== ??????????[/b]
wrax ratioOut,0 ;output to ratioOut pin
Code: Select all
equ scale 0.2
// number1 is used in calculations
// get scale and offset for fullRange Output
equ number1 1.0
@divideDouble offsetVal number1 invRatio // offsetVal = 1/invRatio
equ offsetVal 0.9
@minusDouble scale number1 offsetVal // scale = 1 - offsetVal
Code: Select all
equ logOffset 0.4
@ratioToLogOffset logOffset invRatio
Code: Select all
def genRatioToLogOffset(RatioToLogOffset mp) {
'''
double threshold = 1.0/«mp.ratio»;
double «mp.varName» = Math.log(threshold) / (16 * Math.log(2.0));
'''
}
We calculate the "threshold" as 1/ratio.
Then the code substitution winds up saying in essence,
Which I THINK is how we get the FV-1 corrected base 2 log.logOffset = Math.log(threshold)/(16 * Math.log(2.0));
BUT that said, I am not dealing with the EXP instruction correctly. Under some circumstances, I do see the two outputs switching places, but the one using the 1/X function is not going the full width. I also tried:
Code: Select all
EXP 1.0, 0
Maybe one of you nice people who knows these instructions better than I seem to can help? Don't worry about the SpinCAD Builder part of it. Even if I had two examples showing the right values for the log and exp coefficients, say for ratio = 2 and ratio = 10.
equ ratio 4
equ offsetVal 1/ratio
equ scale 1 - offsetVal
rdax input,1 ;read input signal
sof scale, offsetVal
wrax fullRange,1 ;output to fullRange pin
log AAA, BBB
exp CCC, DDD
wrax ratioOut,0 ;output to ratioOut pin
Thanks,
DL