gpt4 book ai didi

javascript - 麦克风输入电平的对数函数的反函数

转载 作者:行者123 更新时间:2023-12-02 21:36:35 25 4
gpt4 key购买 nike

我正在尝试编写一个具有对数缩放功能的 slider 来控制麦克风 RMS 阈值。

我写的下面的函数可能很糟糕(数学不是我的强项),但似乎确实可以实现我想要的对数缩放。但是,我现在需要在使用刻度的条形图中显示 RMS 级别,因此我需要反转此函数。

const rmsLogarithmicScale = (value, max) => {
const log = ((100 - (Math.log((1 - value) * 100) / 4.605170185988092) * 100) / 100) * max;
return log === Infinity ? max : log;
};

console.log(rmsLogarithmicScale(0.5, 0.05)); // Result: 0.007525749891599539
console.log(rmsLogarithmicScale(0.5, 300)); // Result: 45.15449934959723
const rmsLogarithmicScaleInverse = (rms, max) => {
// I have literally no idea where to start...
}

rmsLogarithmicScaleInverse(0.007525749891599539, 0.05); // Result: 0.5

有没有一位好心的数学家可以帮助我解决这个问题?

最佳答案

在 @Bergi 建议我尝试简化原始函数后,我以 @Andy 的风格重写了该函数,并成功地反转了它。

You should start by simplifying to const log = -Math.log(1 - value) / Math.log(100) * max; — Bergi

const logScale = (value, max) => {
const x1 = 1 - value;
const x2 = -Math.log(x1);
const x3 = x2 / Math.log(100);
const x4 = x3 * max;
return x4;
};

const altInverseRmsLogScale = (lg, max) => {
const x4 = lg / max;
const x3 = x4 * Math.log(100);
const x2 = Math.exp(-x3);
const x1 = 1 - x2;
return x1;
};

console.log(logScale(0.7, 300)); // 4.727557716909737
console.log(inverseLogScale(logScale(0.7, 300), 300)); // 0.07000000000000006

简洁版本

const logScale = (value, max) => (-Math.log(1 - value) / Math.log(100)) * max;

const inverseLogScale = (lg, max) => 1 - Math.exp(-(lg / max * Math.log(100)));

简洁版本

const logScale = (value, max) => (-Math.log(1 - value) / Math.log(100)) * max;
const inverseLogScale = (lg, max) => 1 - Math.pow(100, -lg / max);

console.log(logScale(0.6, 300));
console.log(inverseLogScale(logScaleTwo(0.6, 300), 300));

https://repl.it/@sarcoma/log-scale

最终函数

我还需要处理 value === max 时返回的 Infinity

const logScale = (value, max) => {
const log = (-Math.log(1 - value) / Math.log(100)) * max;
return log === Infinity ? max : log;
};
const inverseLogScale = (lg, max) => 1 - Math.pow(100, -lg / max);

关于javascript - 麦克风输入电平的对数函数的反函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60477788/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com