Search code examples
mathsignal-processing

How to increase a value in a logarithmic range by 100th proportionally?


I'm working on a logarithmic slider/knob and I am not able to increase its value by a proportional step. Its easy in linear terms, given a range [a,b] to increase a value by a 100th step:

increment = (b - a) / 100
value += increment

My difficulty is with the logarithmic range knob, I want the knob to turn as if linearly, small jumps at the begging and larger steps and the end of the scale, I got this to work so far:

increment = (b - a) / 100
value += log(increment)

Using this formula the knob turns how its supposed at the beginning of the range, however its obviously incomplete and at the end of the range turns very slow instead.

Thanks


Solution

  • You need to multiply by an increment, not add. It's a geometric progression, not an arithmetic progression.

    Think about it like the keys on a piano. Each note is the frequency of the previous note times the 12th root of 2 -- that way, the frequency doubles evey 12th note.

    So, this code goes from 1 to 100 in 100 steps:

    import math
    z = math.pow(100,.01)
    
    k = 1
    for i in range(100):
        print(i,k)
        k *= z
    

    Here are the results. It takes 12 steps to move from 1 to 2, but that's what you have to do if you're going to have reasonable-sized steps at the higher end.

    0 1
    1 1.0471285480508996
    2 1.096478196143185
    3 1.1481536214968828
    4 1.2022644346174132
    5 1.2589254117941675
    6 1.3182567385564075
    7 1.3803842646028852
    8 1.445439770745928
    9 1.5135612484362089
    10 1.5848931924611143
    11 1.6595869074375615
    12 1.7378008287493765
    13 1.8197008586099845
    14 1.9054607179632486
    15 1.9952623149688813
    16 2.0892961308540414
    17 2.1877616239495548
    18 2.2908676527677754
    19 2.398832919019493
    20 2.511886431509583
    21 2.630267991895385
    22 2.75422870333817
    23 2.8840315031266095
    24 3.01995172040202
    25 3.1622776601683835
    26 3.3113112148259156
    27 3.4673685045253215
    28 3.630780547701019
    29 3.801893963205618
    30 3.9810717055349794
    31 4.168693834703362
    32 4.365158322401668
    33 4.57088189614876
    34 4.786300923226394
    35 5.011872336272734
    36 5.248074602497739
    37 5.495408738576259
    38 5.754399373371584
    39 6.025595860743593
    40 6.309573444801949
    41 6.606934480075978
    42 6.918309709189384
    43 7.244359600749922
    44 7.585775750291861
    45 7.94328234724284
    46 8.317637711026737
    47 8.709635899560835
    48 9.120108393559129
    49 9.549925860214392
    50 10.000000000000036
    51 10.471285480509033
    52 10.96478196143189
    53 11.481536214968871
    54 12.022644346174175
    55 12.58925411794172
    56 13.182567385564123
    57 13.803842646028905
    58 14.454397707459336
    59 15.135612484362147
    60 15.848931924611204
    61 16.59586907437568
    62 17.378008287493834
    63 18.19700858609992
    64 19.054607179632562
    65 19.952623149688893
    66 20.8929613085405
    67 21.877616239495637
    68 22.908676527677848
    69 23.988329190195028
    70 25.118864315095934
    71 26.30267991895396
    72 27.542287033381815
    73 28.84031503126622
    74 30.19951720402033
    75 31.622776601683974
    76 33.1131121482593
    77 34.673685045253364
    78 36.30780547701035
    79 38.018939632056345
    80 39.81071705534996
    81 41.68693834703379
    82 43.65158322401686
    83 45.70881896148778
    84 47.863009232264126
    85 50.118723362727536
    86 52.480746024977584
    87 54.9540873857628
    88 57.54399373371606
    89 60.25595860743616
    90 63.09573444801973
    91 66.06934480076004
    92 69.18309709189411
    93 72.4435960074995
    94 75.85775750291889
    95 79.4328234724287
    96 83.17637711026768
    97 87.09635899560868
    98 91.20108393559163
    99 95.49925860214428