对数-线性值转换

roejwanj  于 2022-10-16  发布在  其他
关注(0)|答案(1)|浏览(293)

下面的代码使用DrawGrid()绘制对数网格。看起来垂直线还行。当我使用函数SetPositionHzValue()时,结果位置似乎可以(它使用与DrawGrid()相同的逻辑,并且似乎与网格匹配)。但是如何将这个使用线性显示宽度的0-1.0归一化值转换为实际的Hz值呢?为什么GetPositionsHzValue()函数是错误的?
更复杂的是,显示器具有起始频率(在本例中为20赫兹)和结束频率(在本例中为44100赫兹)。

  1. procedure TAudioBezierCurves.DrawGrid(Bitmap32: TBitmap32);
  2. var
  3. GridPosition: Integer;
  4. GridPositionF: Double;
  5. i: Integer;
  6. Base: Double;
  7. LogOffsetValue: Double;
  8. LogMaxValue: Double;
  9. begin
  10. GridPosition := 0;
  11. Base := 1;
  12. if GridFrequencyMin = 0 then begin
  13. LogOffsetValue := 0;
  14. end else begin
  15. LogOffsetValue := Log10(GridFrequencyMin);
  16. end;
  17. LogMaxValue := Log10(GridFrequencyMax) - LogOffsetValue;
  18. repeat
  19. for i := 2 to 10 do begin
  20. if Base * i < GridFrequencyMin then begin
  21. Continue;
  22. end;
  23. //* This gives the % value relative to the total scale
  24. GridPositionF := (Log10(Base * i) - LogOffsetValue) / LogMaxValue;
  25. GridPositionF := GridPositionF * Bitmap32.Width;
  26. GridPosition := Trunc(GridPositionF);
  27. Bitmap32.VertLineS(GridPosition, 0, Bitmap32.Height - 1, GridColor);
  28. end;
  29. Base := Base * 10;
  30. until GridPosition > Bitmap32.Width;
  31. end;
  32. procedure TAudioBezierCurve.SetPositionHzValue(AValue: Double);
  33. var
  34. LogOffsetValue: Double;
  35. LogMaxValue: Double;
  36. begin
  37. if AValue = 0 then begin
  38. Self.Position := 0;
  39. end else begin
  40. if Parent.GridFrequencyMin = 0 then begin
  41. LogOffsetValue := 0;
  42. end else begin
  43. LogOffsetValue := Log10(Parent.GridFrequencyMin);
  44. end;
  45. LogMaxValue := Log10(Parent.GridFrequencyMax) - LogOffsetValue;
  46. //* This gives the % value relative to the total scale
  47. AValue := (Log10(AValue) - LogOffsetValue) / LogMaxValue;
  48. Self.Position := AValue;
  49. end;
  50. end;
  51. function TAudioBezierCurve.GetPositionsHzValue: Double;
  52. var
  53. AValue: Double;
  54. begin
  55. AValue := Power(AValue, 2);
  56. Result := AValue * (Parent.GridFrequencyMax);
  57. Result := Result - (AValue * Parent.GridFrequencyMin) + Parent.GridFrequencyMin;
  58. end;
guykilcj

guykilcj1#

在函数GetPositionsHzValue中,行“AValue:=Power(AValue,2);”“AValue”的值来自哪里?
也许您应该像您在“SetPositionHzValue(AValue:Double);”中所做的那样。AValue应为参数,而不是局部变量。

相关问题