指数迭代到特定范围

时间:2020-03-26 20:40:35

标签: c++ math

我需要使用线性和指数步长迭代到特定范围。线性步长不是问题,但是以指数形式我不知道提名人需要什么。

//Linear:
start = 20;
end = 200;
sampleRate = 1000;
duration = 1;
while(1)
{
   delta = (end - start) / (sampleRate * duration);
// 0.18  = (200 - 20)    / (1000       * 1);
   f += delta
   if(f >= end)
      f = start;
}
//Exponential
while(1)
{
   delta = /*???*/ / (sampleRate * duration);
   f += delta
   if(f >= end)
      f = start;
}

感谢!

2 个答案:

答案 0 :(得分:0)

对于指数变化,您需要在每次迭代中将分子乘以某个因子。

//Exponential
range = end - start;
expFactor = 1.5; //Choose this according to your requirement
while(1)
{
   delta = range / (sampleRate * duration);
   f += delta
   if(f >= end)
      f = start;
   range *= expFactor;
}

答案 1 :(得分:0)

好的,我找到了答案。

第一次要设置两个范围。首先是线性刻度的原始范围。第二-目标对数刻度范围。 对于我的问题:

[开始====================结束]

[firstSample ============= sampleRate * Duration]

然后您要计算与公式的对话。我写在Desmos上:https://www.desmos.com/calculator/utb5nu1pvq

我的代码:

SinSweep()
    {

    start=20;
    end=200; 
    sampleRate=1000;
    range=end-start; 
    sampleCount=sampleRate*duration;
    logMax=log10f(end/start);
    currentSample=0;
    f=start;

    While(1){
        switch (type) {
        case Linear:
            f = (((currentSample-1)/sampleCount)*range)+start;
            break;
        case Logarithmic:
            f = start*(powf(10, logMax*(currentSample/sampleCount)));
            break;
            }
    currentSample++;

    if(f >= end){
        f = start;
        currentSample = 0;
        }
    }
}