Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize the CircularAverageMagnitudeDifference method #620

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/base/URecord.pas
Original file line number Diff line number Diff line change
Expand Up @@ -489,17 +489,22 @@ function TCaptureBuffer.CircularAverageMagnitudeDifference(): TCorrelationArray;
ToneIndex: integer;
Correlation: TCorrelationArray;
SampleIndex: integer; // index of sample to analyze
const
// the value is fairly arbitrarily chosen, but this makes it analyze only 4096/SampleIndexStep samples
SampleIndexStep = 16;
begin
// accumulate the magnitude differences for samples in AnalysisBuffer
for ToneIndex := 0 to NumHalftones-1 do
begin
Correlation[ToneIndex] := 0;
for SampleIndex := 0 to (AnalysisBufferSize-1) do
SampleIndex := 0;
while SampleIndex < AnalysisBufferSize - 1 do
begin
// Suggestion for calculation efficiency improvement from deuteragenie:
// Replacing 'i mod buffersize' by 'i & (buffersize-1)' when i is positive and buffersize is a power of two should speed the modulo compuation by 5x-10x
//Correlation[ToneIndex] += Abs(AnalysisBuffer[(SampleIndex+Delays[ToneIndex]) mod AnalysisBufferSize] - AnalysisBuffer[SampleIndex]);
Correlation[ToneIndex] := Correlation[ToneIndex] + Abs(AnalysisBuffer[(SampleIndex+Delays[ToneIndex]) and (AnalysisBufferSize-1)] - AnalysisBuffer[SampleIndex]);
SampleIndex := SampleIndex + SampleIndexStep;
end;
Correlation[ToneIndex] := Correlation[ToneIndex] / AnalysisBufferSize;
end;
Expand Down