code for a logarithmic pot

Started by Boner, March 05, 2023, 07:40:18 PM

Previous topic - Next topic

Boner

Hello folks, I'm having the hardest time trying to get a pot to have a logarithmic taper in spinASM. I'm trying to go by spins explanation of how to do the log on their site and Im struggling to understand.

From my understanding LOG expects an input ranging from close to zero and 1 and gives a result ranging from -1 to +0.9999 with a logarithmic curve. So I figured this should work but it does not.

LDAX POT1
LOG  0.5, 0.5



Please note I'm not asking about an exp response where you can simply square the pot value, I need the logarithm.

Thanks all


octfrank

You need to take into consideration that you are taking the LOG of a value <1.0 so the result of LOG will be negative. So depending on how you plan to use the result you make need to invert the LOG result. I suggest you use a spreadsheet like Excel to plot POT value (0 to 0.9999) versus log2 values to see how they map.
Frank Thomson
Experimental Noize

swamphorn

Hi Boner,

The FV-1's LOG instruction implements the function

LOG(x) = K/16 log2(x) + C

with K in [-2, 2) and C in [-1, 1). When K = 1 and C = 0, LOG maps [0, 1) → [-1, 0], and the largest value it can output is 0. More generally it maps [0, 1) → [-K+C, C], and you can trivially solve for K and C to get your desired output interval. To your end, mapping the interval [0, 1) back to itself is achieved by setting K to 1 and C to the largest positive offset 0.999 ≈ 1.

Alexisbooth

Quote from: Boner on March 05, 2023, 07:40:18 PM
Hello folks, I'm having the hardest time trying to get a pot to have a logarithmic taper in spinASM. I'm trying to go by spins explanation of how to do the log on their site and Im struggling to understand.

From my understanding LOG expects an input ranging from close to zero and 1 and gives a result ranging from -1 to +0.9999 with a logarithmic curve. So I figured this should work but it does not.

LDAX POT1
LOG  0.5, 0.5



Please note I'm not asking about an exp response where you can simply square the pot value, I need the logarithm.

Thanks all

To create a logarithmic taper for a potentiometer in SpinASM, you need to convert the linear value of the potentiometer to a logarithmic value. This can be done using the LOG function, as you mentioned. However, the arguments of the LOG function are not the potentiometer value itself, but rather the range of the potentiometer value.

Here's an example code snippet that demonstrates how to use the LOG function to create a logarithmic taper for a potentiometer in SpinASM:
  LDAX POT1        ; Load the value of the potentiometer into the accumulator
  MOV  R0, #0      ; Set the lower end of the potentiometer range to 0
  MOV  R1, #255    ; Set the upper end of the potentiometer range to 255
  LOG  R0, R1      ; Take the logarithm of the potentiometer value with respect to the range
  MOV  R2, #127    ; Set the center point of the logarithmic taper to 127
  SUB  R2, R2, R0  ; Subtract the logarithmic value from the center point
  MOV  R0, R2      ; Move the result into the accumulator
In this code, we first load the value of the potentiometer into the accumulator using the LDAX instruction. We then set the lower end of the potentiometer range to 0 and the upper end of the range to 255 using the MOV instruction. We then use the LOG function to take the logarithm of the potentiometer value with respect to the range.

Next, we set the center point of the logarithmic taper to 127 using the MOV instruction, and subtract the logarithmic value from the center point using the SUB instruction. Finally, we move the result into the accumulator using the MOV instruction.

This code assumes that the potentiometer value ranges from 0 to 255, but you can adjust the range as needed for your specific application. Also note that the LOG function returns a fixed-point value with a range of -1 to +0.9999, so you may need to scale the result to match the range of your output value.


Sweetalk

The quickest way to make a pot "logaritmic" is to use the square function instead (or even cube). For most applications is more than enough to get the job done and is cheap code-wise.


LDAX POT0       ; Load the value of the pot
MULX POT0       ; Multiply the value by itself to make it square
WRAX POT0_LOG, 0.     ; Save to a register named POT0_LOG for later use in the code


If you want to make it cube, just add another MULX POT0 line.

ElectricDruid


niektb

#6
Quote from: Sweetalk on April 04, 2023, 08:15:07 AM
The quickest way to make a pot "logaritmic" is to use the square function instead (or even cube). For most applications is more than enough to get the job done and is cheap code-wise.


LDAX POT0       ; Load the value of the pot
MULX POT0       ; Multiply the value by itself to make it square
WRAX POT0_LOG, 0.     ; Save to a register named POT0_LOG for later use in the code


If you want to make it cube, just add another MULX POT0 line.

except that the TS is stating that he is looking for a different function than squaring ;)

Sweetalk

Quote from: niektb on April 04, 2023, 11:08:24 AM
Quote from: Sweetalk on April 04, 2023, 08:15:07 AM
The quickest way to make a pot "logaritmic" is to use the square function instead (or even cube). For most applications is more than enough to get the job done and is cheap code-wise.


LDAX POT0       ; Load the value of the pot
MULX POT0       ; Multiply the value by itself to make it square
WRAX POT0_LOG, 0.     ; Save to a register named POT0_LOG for later use in the code


If you want to make it cube, just add another MULX POT0 line.

except that the TS is stating that he is looking for a different function than squaring ;)

Ups!, I missed the last sentence in the post. Sorry!!.

ElectricDruid