一个数字频谱转换成另一种 [英] Convert one number spectrum into another
问题描述
我试图想出一个公式推它把一个数字谱成另一种。 例如:
I'm trying to come up with a formular which converts one number spectrum into another. For example:
0 - 800
到 -1 - 1
其中, 0 = -1
, 200 = -0.5
, 400 = 0
, 600 = 0.5
, 800 = 1
,等等。
Where 0 = -1
, 200 = -0.5
, 400 = 0
, 600 = 0.5
, 800 = 1
, and so on.
困难的部分对我来说似乎是负的范围内。
The difficult part for me seems to be the negative range.
推荐答案
如果你的范围是 A0,A1
和 B0,B1
,那么想要 X
去
If your ranges are a0, a1
and b0, b1
, then you want x
to go to
((x-a0)/(a1-a0)) * (b1-b0) + b0
基本上,(X-A0)
是如何远远超过了你是从第一个范围的下侧,而(X-A0) /(A1-A0)
除以范围的宽度,这样的数量是现在归 [0,1]
。在那之后,我们乘以(B1-B0)
扩大范围到新的规模,并添加 B0
来转移过来。
Basically, (x-a0)
is how far over you are from the lower side of the first range, and (x-a0)/(a1-a0)
divides by the width of the range so the number is now normalized to [0, 1]
. After that, we multiply by (b1-b0)
to expand the range to the new scale, and add b0
to shift it over.
例如:
>>> a0, a1 = 0.0, 800.0
>>> b0, b1 = -1.0, 1.0
>>>
>>> x = 400 # should go to 0
>>> x-a0
400.0
>>> (x-a0)/(a1-a0)
0.5
>>> (x-a0)/(a1-a0) * (b1-b0)
1.0
>>> (x-a0)/(a1-a0) * (b1-b0) + b0
0.0
>>> x = 0 # should go to -1
>>> (x-a0)/(a1-a0) * (b1-b0) + b0
-1.0
>>> x = 800 # should go to 1
>>> (x-a0)/(a1-a0) * (b1-b0) + b0
1.0
这篇关于一个数字频谱转换成另一种的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!