-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAnalysis.Spectrum.Quantization.pas
More file actions
54 lines (41 loc) · 1.21 KB
/
Analysis.Spectrum.Quantization.pas
File metadata and controls
54 lines (41 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
unit Analysis.Spectrum.Quantization;
interface
uses
System.SysUtils, Common.ComplexNum;
procedure Quantization(
AFFTData : TArray<TComplexNum>; // данные БПФ
AFFTLength : Word; // размер выборки для FFT
APalSize : Integer; // размер палитры
AOnComplete : TProc<TArray<Integer>>);
implementation
uses
System.Math;
procedure Quantization(
AFFTData : TArray<TComplexNum>; // данные БПФ
AFFTLength : Word; // размер выборки для FFT
APalSize : Integer; // размер палитры
AOnComplete : TProc<TArray<Integer>>);
const
dB_range = 120;
var
idx, i: Integer;
c: TComplexNum;
dBfs, magnitude: Single;
qData: TArray<Integer>;
begin
SetLength(qData, Length(AFFTData));
i := 0;
for c in AFFTData do
begin
magnitude := Sqrt(Sqr(c.Real) + Sqr(c.Imm));
if magnitude <> 0 then
dBfs := 20 * Log10(magnitude / AFFTLength + 0.000001)
else
dBfs := -dB_range; // тишина
idx := Trunc(Abs(APalSize + dBfs * APalSize / dB_range));
qData[i] := Min(Max(0, idx), APalSize - 1);
Inc(i);
end;
AOnComplete(qData);
end;
end.