如何计算VU表刻度的对数标签? [英] How do I calculate logrithmic labels for a VU meter scale?

查看:272
本文介绍了如何计算VU表刻度的对数标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用画布编写一个仪表小部件,需要计算比例的标签值。没问题,除非我正在尝试重建VU表的刻度。我理解它是对数的,但是这种类型的仪表上的值不是10的幂。

I writing a meter widget using canvas and need to calculate the label values for the scale. No problem except when I'm trying to re-create the scale for a VU meter. I understand it's logrithmic, but the values aren't powers of 10 on that type of meter.

参见: https://en.wikipedia.org/wiki/VU_meter

我的功能是给出比例的最小值和最大值。对于正常尺度,它也被赋予值之间的步长。例如,给定-20和30,步长为10,它将产生标签:

The function I have is given the min and max values for the scale. For normal scales, it also is given the step between values. For example, given -20 and 30 with a step of 10, it would produce labels:

-20 -10 0 10 20 30

-20 -10 0 10 20 30

对于-20和6的VU表,我需要生产标签:

For a VU meter given -20 and 6, I need to produce labels:

-20 -10 -5 -3 0 3 6

-20 -10 -5 -3 0 3 6

而且,这些值之间的间距不一样。

And, the spacing isn't the same on the scale between those values.

我不是要求实际的代码示例,而是关于如何最好地实现这个的想法。

I am not asking for actual code examples, but instead for ideas on how to best approach implementing this.

我应该编写函数,因此其中一个参数是标签列表,然后绘制它们在对数尺度?这似乎不能正常工作,因为数字不会在正确的VU表的上图中显示在正确的位置。

Should I write the function so one of the parameters is a list of labels, then plot them on a logarithmic scale? That doesn't appear to work properly because the numbers do not end up in the right places as seen in the image above of a proper VU meter.

是否有一些特殊的公式仅适用于不是简单日志函数的dB级别?

Is there some special formula just for dB levels that isn't a simple log function?

同样,我不是要求代码示例,只是为了帮助理解最佳使用方法。

Again, I'm not asking for code examples, just for some help understanding the best approach to use.

谢谢!

推荐答案

我想你有一个价值 - 像素功能。你需要写的是函数。

I suppose you have a value-to pixel function. What you need to write is the inverse function of that.

当你有逆函数时,你只需将屏幕区域除以N等于部分(图中你有6个区域)。一个区域的宽度为X像素。现在你可以使用你的反函数来获得该像素位置的值。

When you have the inverse function, you just divide the screen area to N equal parts (in the picture you have 6 regions). One region will be X pixels in width. Now you can use your inverse function to get the value for that pixel-position.

这不是一个整数,它将是3.434或11.34 - 你需要一个更漂亮的函数,它将生成最接近的漂亮数字(假设只是在小数点后切掉部分)。

This will not be an integer, it will be something like 3.434 or 11.34 - you need a prettyfier function which will generate the closest "pretty" number (let's say just chop off the parts after the decimal).

现在你取漂亮的值并计算像素它的原始功能位置。

Now you take the pretty value and calculate the pixel position for it with your original function.

一些代码:

function value2px(value, valueMin, valueMax, pxMin, pxMax) {
    var valueWidth = sigLog(valueMax) - sigLog(valueMin);
    var pixelWidth = pxMax - pxMin;
    var ratio = pixelWidth / valueWidth;

    return ratio * (sigLog(value) - sigLog(valueMin)) + pxMin;
}

function px2value(px, valueMin, valueMax, pxMin, pxMax) {
    var valueWidth = sigLog(valueMax) - sigLog(valueMin);
    var pixelWidth = pxMax - pxMin;
    var ratio = pixelWidth / valueWidth;

    return sigExp((px - pxMin) / ratio + sigLog(valueMin));
}

和演示:
http://jsfiddle.net/ambcwoLg/1/

这篇关于如何计算VU表刻度的对数标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆