【问题标题】:Calculating the average of an array of logarithmic values in JavaScript在 JavaScript 中计算对数数组的平均值
【发布时间】:2019-05-02 13:12:54
【问题描述】:

我正在计算 JavaScript 中一组日志值(分贝,如果重要的话)的平均值。我突然想到,如果数字是对数(其中 55 是 10 乘以 54),则平均分贝水平不会是对数的平均值。解决这个问题的最佳方法是什么?

下面是一些代码,我将如何通常在 JS 中计算平均值。在 JavaScript 中计算日志值数组平均值的优雅方法是什么?

// These values are in decibels
const samples = [40, 50, 55, 80, 45];
document.getElementById('samples').innerText = JSON.stringify(samples);

let total = 0;

samples.forEach(value => {
  total = total + value;
});

const average = total / samples.length;

document.getElementById('avg').innerText = JSON.stringify(average);
<div>
  Decibel samples:
  <span id="samples"></span>
</div>

<div>
  Average:
  <span id="avg"></span>
</div>

【问题讨论】:

  • 你可以使用reduce()samples.reduce((ac,a) =&gt; ac + a,0)/samples.length
  • 分贝是对数值:en.wikipedia.org/wiki/Decibel
  • 也许他需要对数平均值?
  • 物理交流上有一个类似的问题physics.stackexchange.com/questions/46228/averaging-decibels
  • @schnauss 也许你可以从这个链接中得到一些有用的东西:cirrusresearch.co.uk/blog/2013/01/…。不确定你需要什么样的平均值,但对我来说,噪声数据平均值听起来非常接近你需要的平均值。无论如何,这听起来不像是一个编程问题,而是一个合乎逻辑的问题。这里涉及到太多超出编程的理论,我建议您返回一个公式以便我们可以使用它。

标签: javascript math


【解决方案1】:

基于分贝 (@schnauss)Decibel on Wikipedia 的定义。 有 2 个定义:功率和幅度分贝。我看看power decibel。

摆脱对数,求平均值并重新应用对数。

var samples = [40, 50, 55, 80, 45],
    x = 0.0;

samples.forEach(value => {
    x += Math.pow(10, value/10.0);
});

var average = x / samples.length,
    logAverage = 10 * Math.log10(average);
    
console.log(logAverage);

结果约为 73 dB

【讨论】:

    【解决方案2】:

    一种简单的方法可能是反转值中的对数,照常取平均值,然后将对数应用于结果。这可能是执行此操作的一种更有效的方法,但如果性能不重要,您可以尝试一下。

    我无法尝试此代码,所以如果出现任何问题,请不要说什么!欢迎提出任何建议。

    编辑:忘记在最后重新做日志,只是添加了它

    // These values are in decibels
    const samples = [40, 50, 55, 80, 45];
    
    //Undo the log. Be careful if base 10 is not the one you are using!
    samples.forEach(function(element, index, array){
        array[index] = Math.pow(10, element);
    });
    
    document.getElementById('samples').innerText = JSON.stringify(samples);
    
    let total = 0;
     
     samples.forEach(value => {
       total = total + value;
     });
    
    const average = Math.log10(total / samples.length);
    
    document.getElementById('avg').innerText = JSON.stringify(average);
    <div>
      Decibel samples:
      <span id="samples"></span>
    </div>
    
    <div>
      Average:
      <span id="avg"></span>
    </div>

    【讨论】:

      【解决方案3】:

      我认为这是合适的公式。 (分贝可能会让人很困惑。)

      const mean = (ns) => ns.reduce((a, b) => a + b, 0) / (ns.length || 1)
      
      const decibelAverage = (decibels) =>
          10 * Math.log10(mean(decibels.map(n => 10 ** (n / 10))))
      
      console.log(decibelAverage([40, 50, 55, 80, 45])) //~> 73.03013877157564

      重点是分贝代表功率级别的以 10 为底的对数的 10 倍。 (还有另一种分贝类型。如果您需要的话,可以在某处浮动平方根。)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-14
        • 1970-01-01
        • 2013-10-25
        • 1970-01-01
        相关资源
        最近更新 更多