Logarithmic sampling

白昼怎懂夜的黑 提交于 2019-12-06 17:14:39

You can start with by generating numbers from 0 to 1 with constant step (for example 0.1). Then power them with some exponent - the bigger exponent, the sharper curve. Then shift and multiply to get into your desired min-max range.

Pseudocode:

min = 1.0
max = 100.0
exponent = 2.0 // Sharpness
result = []

for(i = 0.0; i <= 1.0; i += 0.1) {
    result.push(pow(i, exponent) * (max - min) + min)
}

I had the same problem. I wanted well spaced points, but with much more point near the minimal value. I used a logarithmic transformation. Firstly the code:

function SampleData (min, max, points) {

    min = min || 1; // Minimum value
    max = max || 1600; // Maximum value
    points = points || 20; // data points between Min&Max

    var step = (Math.log(max)-Math.log(min))/(points-1);

    var data = [];

    var D= 100; // max asy
    var A= 0; // min asy
    var C= 50;  // inflectio
    var B= 1; // Hills slope

    for (i = Math.log(min); i <= Math.log(max); i=i+step) { 
        data.push ([Math.exp(i), math.eval (D+'+('+A+'-'+D+')/(1+('+math.exp(i)+'/'+C+')^'+B+')')]);
    }
}

The trick I used is to compress the data range (here 1 to 1600) with the logarithmic function; thereby, I was able to use a linear constant step width. Before feeding the x value into the math function, you have to back transform (math.exp) the values.

The function in math.eval is a rather complicated 4 paramater logistic fit, you might of course use something else.

In the image you see a plot of above mentioned function once with linear step width (orange) and once with my logarithmic step width (red). Visualisation of linear and logarithmic step width in data sampling.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!