Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
sheirerd
committed
Apr 30, 2023
1 parent
ff4b7ef
commit 4147f75
Showing
4 changed files
with
146 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
src/main/java/io/github/dsheirer/dsp/gain/ObjectiveGainControl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package io.github.dsheirer.dsp.gain; | ||
|
||
import org.apache.commons.math3.util.FastMath; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class ObjectiveGainControl | ||
{ | ||
private static final Logger mLog = LoggerFactory.getLogger(ObjectiveGainControl.class); | ||
private float mMinGain; | ||
private float mMaxGain; | ||
private float mCurrentGain = 1.0f; | ||
private float mObjectiveGain = mCurrentGain; | ||
private float mObjectiveAmplitude; | ||
private float mMaxObservedAmplitude; | ||
|
||
public ObjectiveGainControl(float minGain, float maxGain, float objectiveAmplitude) | ||
{ | ||
mMinGain = minGain; | ||
mMaxGain = maxGain; | ||
mObjectiveAmplitude = objectiveAmplitude; | ||
} | ||
|
||
public void reset() | ||
{ | ||
mMaxObservedAmplitude = 0.0f; | ||
} | ||
|
||
public float[] process(float[] samples) | ||
{ | ||
float currentAmplitude; | ||
|
||
for(float sample: samples) | ||
{ | ||
currentAmplitude = FastMath.abs(sample); | ||
|
||
if(currentAmplitude > mMaxObservedAmplitude) | ||
{ | ||
mMaxObservedAmplitude = currentAmplitude; | ||
} | ||
} | ||
|
||
mObjectiveGain = mObjectiveAmplitude / mMaxObservedAmplitude; | ||
|
||
if(mObjectiveGain > mMaxGain) | ||
{ | ||
mObjectiveGain = mMaxGain; | ||
} | ||
else if(mObjectiveGain < mMinGain) | ||
{ | ||
mObjectiveGain = mMinGain; | ||
} | ||
|
||
if(mCurrentGain != mObjectiveGain) | ||
{ | ||
mLog.info("Current: " + mCurrentGain + " Objective: " + mObjectiveGain); | ||
float incrementalGainChange = mObjectiveGain / samples.length / 4; //Aim to achieve objective over 4x sample buffers | ||
float gain = mCurrentGain; | ||
|
||
float[] processed = new float[samples.length]; | ||
for(int x = 0; x < samples.length; x++) | ||
{ | ||
gain += incrementalGainChange; | ||
|
||
if(gain > mMaxGain) | ||
{ | ||
gain = mMaxGain; | ||
} | ||
if(gain < mMinGain) | ||
{ | ||
gain = mMinGain; | ||
} | ||
|
||
processed[x] = samples[x] * gain; | ||
} | ||
|
||
if(Math.abs(mObjectiveGain - mCurrentGain) < incrementalGainChange) | ||
{ | ||
mLog.info("Objective reached!"); | ||
mCurrentGain = mObjectiveGain; | ||
} | ||
else | ||
{ | ||
mCurrentGain = gain; | ||
} | ||
|
||
return processed; | ||
} | ||
else | ||
{ | ||
return samples; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters