Add dual mic coherence suppression
This commit is contained in:
parent
c43dfd4285
commit
324de581d4
7 changed files with 282 additions and 19 deletions
|
|
@ -80,6 +80,11 @@ if (args.Any(arg => string.Equals(arg, "--calibrated-audio-composite-smoke", Str
|
|||
return RunCalibratedAudioCompositeSmoke();
|
||||
}
|
||||
|
||||
if (args.Any(arg => string.Equals(arg, "--dual-mic-coherence-cancellation-smoke", StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
return RunDualMicCoherenceCancellationSmoke();
|
||||
}
|
||||
|
||||
if (args.Any(arg => string.Equals(arg, "--targeted-bioacoustic-probe-smoke", StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
return RunTargetedBioacousticProbeSmoke();
|
||||
|
|
@ -3411,6 +3416,63 @@ static int RunCalibratedAudioCompositeSmoke()
|
|||
: 1;
|
||||
}
|
||||
|
||||
static int RunDualMicCoherenceCancellationSmoke()
|
||||
{
|
||||
const int sampleRate = 48_000;
|
||||
const int frameCount = sampleRate;
|
||||
const double poisonHz = 3_520.0;
|
||||
var targetBands = new[] { 440.0, 880.0, 1_760.0 };
|
||||
var measuredBands = new[] { 440.0, 880.0, 1_760.0, poisonHz };
|
||||
var reference = RenderCalibrationToneStack(frameCount, sampleRate, targetBands, [1.0, 0.85, 0.70], noiseScale: 0.0, seed: 31);
|
||||
var micA = reference.ToArray();
|
||||
var micB = reference.ToArray();
|
||||
AddSine(micA, sampleRate, poisonHz, amplitude: 0.080, phaseRadians: 0.30);
|
||||
AddSine(micB, sampleRate, 250.0, amplitude: 0.018, phaseRadians: 1.70);
|
||||
micA = ApplyFractionalDelay(micA, 9.25);
|
||||
micB = ApplyFractionalDelay(micB, -6.50);
|
||||
|
||||
var uncalibrated = AverageSignals([micA, micB]);
|
||||
var result = new MimirCalibratedAudioCompositeBuilder(new MimirCalibratedAudioCompositeOptions(
|
||||
MinimumConfidence: 0.05,
|
||||
TargetBandEnergy: 1.0,
|
||||
MinimumBandEnergy: 0.001,
|
||||
MinimumBandGain: 0.90,
|
||||
MaximumBandGain: 1.10,
|
||||
NoiseFloorRms: 1.0e-5,
|
||||
MinimumCoherence: 0.55,
|
||||
IncoherentBandSuppression: 0.95)).Build(
|
||||
[
|
||||
NewCompositeSource("shotgun", micA, 9.25, 0.96, measuredBands, [1.0, 1.0, 1.0, 1.0], 0.020),
|
||||
NewCompositeSource("cardioid", micB, -6.50, 0.96, measuredBands, [1.0, 1.0, 1.0, 1.0], 0.014),
|
||||
]);
|
||||
var uncalibratedSnr = EstimateSnrDb(reference, uncalibrated);
|
||||
var cancelledSnr = EstimateSnrDb(reference, result.Samples);
|
||||
var uncalibratedPoison = ToneEnergy(uncalibrated, sampleRate, poisonHz);
|
||||
var cancelledPoison = ToneEnergy(result.Samples, sampleRate, poisonHz);
|
||||
var poisonBand = result.BandReports
|
||||
.OrderBy(report => Math.Abs(report.CenterHz - poisonHz))
|
||||
.FirstOrDefault();
|
||||
var sharedCoherence = result.BandReports
|
||||
.Where(report => targetBands.Any(targetHz => Math.Abs(report.CenterHz - targetHz) < 1.0))
|
||||
.Select(report => report.Coherence)
|
||||
.DefaultIfEmpty(0.0)
|
||||
.Average();
|
||||
Console.WriteLine(
|
||||
$"dual-mic-coherence-cancellation-smoke sources={result.SourceReports.Count} bands={result.BandReports.Count} " +
|
||||
$"uncalibratedSnrDb={uncalibratedSnr:0.000} cancelledSnrDb={cancelledSnr:0.000} " +
|
||||
$"poisonBefore={uncalibratedPoison:0.000000} poisonAfter={cancelledPoison:0.000000} " +
|
||||
$"poisonCoherence={poisonBand?.Coherence:0.000} poisonSuppression={poisonBand?.Suppression:0.000} sharedCoherence={sharedCoherence:0.000}");
|
||||
|
||||
return result.SourceReports.Count == 2 &&
|
||||
result.BandReports.Count >= measuredBands.Length &&
|
||||
poisonBand is { Coherence: < 0.45, Suppression: > 0.15 } &&
|
||||
sharedCoherence > 0.80 &&
|
||||
cancelledPoison < uncalibratedPoison * 0.70 &&
|
||||
cancelledSnr > uncalibratedSnr + 3.0
|
||||
? 0
|
||||
: 1;
|
||||
}
|
||||
|
||||
static int RunTargetedBioacousticProbeSmoke()
|
||||
{
|
||||
var residuals = new[]
|
||||
|
|
@ -4398,6 +4460,15 @@ static float[] RenderCalibrationToneStack(
|
|||
return samples;
|
||||
}
|
||||
|
||||
static void AddSine(float[] samples, int sampleRate, double centerHz, double amplitude, double phaseRadians)
|
||||
{
|
||||
var omega = 2.0 * Math.PI * centerHz / sampleRate;
|
||||
for (var index = 0; index < samples.Length; index++)
|
||||
{
|
||||
samples[index] += (float)(Math.Sin(omega * index + phaseRadians) * amplitude);
|
||||
}
|
||||
}
|
||||
|
||||
static float[] AverageSignals(IReadOnlyList<float[]> signals)
|
||||
{
|
||||
var frameCount = signals.Min(signal => signal.Length);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,9 @@ public sealed record MimirCalibratedAudioCompositeOptions(
|
|||
double MinimumBandEnergy = 0.01,
|
||||
double MinimumBandGain = 0.20,
|
||||
double MaximumBandGain = 4.00,
|
||||
double NoiseFloorRms = 1.0e-5);
|
||||
double NoiseFloorRms = 1.0e-5,
|
||||
double MinimumCoherence = 0.35,
|
||||
double IncoherentBandSuppression = 0.72);
|
||||
|
||||
public sealed record MimirCalibratedAudioCompositeSource(
|
||||
string SourceId,
|
||||
|
|
@ -25,12 +27,20 @@ public sealed record MimirCalibratedAudioCompositeSourceReport(
|
|||
double ResponseFlatnessBefore,
|
||||
double ResponseFlatnessAfter,
|
||||
double Confidence,
|
||||
double NoiseRms);
|
||||
double NoiseRms,
|
||||
double AverageCoherence);
|
||||
|
||||
public sealed record MimirCalibratedAudioCompositeBandReport(
|
||||
double CenterHz,
|
||||
double Coherence,
|
||||
double Suppression,
|
||||
int SourceCount);
|
||||
|
||||
public sealed record MimirCalibratedAudioCompositeResult(
|
||||
int SampleRate,
|
||||
float[] Samples,
|
||||
IReadOnlyList<MimirCalibratedAudioCompositeSourceReport> SourceReports,
|
||||
IReadOnlyList<MimirCalibratedAudioCompositeBandReport> BandReports,
|
||||
double CompositeRms,
|
||||
double SourceMeanRms,
|
||||
double ResponseFlatnessBefore,
|
||||
|
|
@ -47,7 +57,7 @@ public sealed class MimirCalibratedAudioCompositeBuilder(MimirCalibratedAudioCom
|
|||
.ToArray();
|
||||
if (accepted.Length == 0)
|
||||
{
|
||||
return new MimirCalibratedAudioCompositeResult(0, [], [], 0.0, 0.0, 0.0, 0.0);
|
||||
return new MimirCalibratedAudioCompositeResult(0, [], [], [], 0.0, 0.0, 0.0, 0.0);
|
||||
}
|
||||
|
||||
var sampleRate = accepted[0].SampleRate;
|
||||
|
|
@ -59,6 +69,7 @@ public sealed class MimirCalibratedAudioCompositeBuilder(MimirCalibratedAudioCom
|
|||
var frameCount = accepted.Min(source => source.Samples.Length);
|
||||
var output = new float[frameCount];
|
||||
var reports = new List<MimirCalibratedAudioCompositeSourceReport>(accepted.Length);
|
||||
var prepared = new List<PreparedCompositeSource>(accepted.Length);
|
||||
var totalWeight = 0.0;
|
||||
var sourceRmsTotal = 0.0;
|
||||
var beforeFlatnessTotal = 0.0;
|
||||
|
|
@ -90,25 +101,43 @@ public sealed class MimirCalibratedAudioCompositeBuilder(MimirCalibratedAudioCom
|
|||
var noise = Math.Max(options.NoiseFloorRms, source.NoiseRms);
|
||||
var responseWeight = Math.Clamp(afterFlatness <= 0.0 ? 1.0 : afterFlatness, 0.10, 1.0);
|
||||
var weight = confidence * responseWeight / (noise * noise);
|
||||
totalWeight += weight;
|
||||
sourceRmsTotal += RootMeanSquare(source.Samples.AsSpan(0, frameCount));
|
||||
beforeFlatnessTotal += beforeFlatness;
|
||||
afterFlatnessTotal += afterFlatness;
|
||||
for (var index = 0; index < frameCount; index++)
|
||||
{
|
||||
output[index] += (float)(corrected[index] * weight);
|
||||
}
|
||||
|
||||
reports.Add(new MimirCalibratedAudioCompositeSourceReport(
|
||||
prepared.Add(new PreparedCompositeSource(
|
||||
source.SourceId,
|
||||
corrected,
|
||||
weight,
|
||||
delaySamples,
|
||||
appliedGain,
|
||||
weight,
|
||||
correctedBands,
|
||||
beforeFlatness,
|
||||
afterFlatness,
|
||||
confidence,
|
||||
source.NoiseRms));
|
||||
source.NoiseRms,
|
||||
source.BandResponses));
|
||||
}
|
||||
|
||||
var bandReports = ApplyCoherenceSuppression(prepared, sampleRate);
|
||||
foreach (var source in prepared)
|
||||
{
|
||||
totalWeight += source.Weight;
|
||||
for (var index = 0; index < frameCount; index++)
|
||||
{
|
||||
output[index] += (float)(source.Samples[index] * source.Weight);
|
||||
}
|
||||
|
||||
reports.Add(new MimirCalibratedAudioCompositeSourceReport(
|
||||
source.SourceId,
|
||||
source.AppliedDelaySamples,
|
||||
source.AppliedGain,
|
||||
source.Weight,
|
||||
source.CorrectedBandCount,
|
||||
source.ResponseFlatnessBefore,
|
||||
source.ResponseFlatnessAfter,
|
||||
source.Confidence,
|
||||
source.NoiseRms,
|
||||
AverageSourceCoherence(source, bandReports)));
|
||||
}
|
||||
|
||||
if (totalWeight > 0.0)
|
||||
|
|
@ -124,12 +153,73 @@ public sealed class MimirCalibratedAudioCompositeBuilder(MimirCalibratedAudioCom
|
|||
sampleRate,
|
||||
output,
|
||||
reports,
|
||||
bandReports,
|
||||
RootMeanSquare(output),
|
||||
sourceRmsTotal / reportCount,
|
||||
beforeFlatnessTotal / reportCount,
|
||||
afterFlatnessTotal / reportCount);
|
||||
}
|
||||
|
||||
private IReadOnlyList<MimirCalibratedAudioCompositeBandReport> ApplyCoherenceSuppression(
|
||||
IReadOnlyList<PreparedCompositeSource> sources,
|
||||
int sampleRate)
|
||||
{
|
||||
if (sources.Count < 2)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
var bandCenters = sources
|
||||
.SelectMany(source => source.BandResponses.Select(response => response.CenterHz))
|
||||
.Where(centerHz => centerHz > 0.0 && centerHz < sampleRate * 0.48)
|
||||
.DistinctBy(centerHz => Math.Round(12.0 * Math.Log2(centerHz / 110.0)))
|
||||
.Order()
|
||||
.ToArray();
|
||||
var reports = new List<MimirCalibratedAudioCompositeBandReport>(bandCenters.Length);
|
||||
foreach (var centerHz in bandCenters)
|
||||
{
|
||||
var measurements = sources
|
||||
.Select(source => new BandMeasurement(source, ToneCoefficient(source.Samples, sampleRate, centerHz)))
|
||||
.Where(measurement => measurement.Coefficient.Magnitude > 1.0e-9)
|
||||
.ToArray();
|
||||
if (measurements.Length < 2)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var coherence = PairwiseCoherence(measurements.Select(static measurement => measurement.Coefficient).ToArray());
|
||||
var suppression = Math.Clamp((options.MinimumCoherence - coherence) / Math.Max(1.0e-6, options.MinimumCoherence), 0.0, 1.0) *
|
||||
Math.Clamp(options.IncoherentBandSuppression, 0.0, 1.0);
|
||||
if (suppression > 0.0)
|
||||
{
|
||||
foreach (var measurement in measurements)
|
||||
{
|
||||
AddBandGainDelta(measurement.Source.Samples, measurement.Source.Samples, sampleRate, centerHz, -suppression);
|
||||
}
|
||||
}
|
||||
|
||||
reports.Add(new MimirCalibratedAudioCompositeBandReport(
|
||||
centerHz,
|
||||
coherence,
|
||||
suppression,
|
||||
measurements.Length));
|
||||
}
|
||||
|
||||
return reports;
|
||||
}
|
||||
|
||||
private static double AverageSourceCoherence(
|
||||
PreparedCompositeSource source,
|
||||
IReadOnlyList<MimirCalibratedAudioCompositeBandReport> reports)
|
||||
{
|
||||
var centers = source.BandResponses.Select(response => response.CenterHz).ToArray();
|
||||
var matched = reports
|
||||
.Where(report => centers.Any(center => BandsTooClose(center, report.CenterHz)))
|
||||
.Select(report => report.Coherence)
|
||||
.ToArray();
|
||||
return matched.Length == 0 ? 1.0 : matched.Average();
|
||||
}
|
||||
|
||||
private float[] ApplySparseBandEqualization(
|
||||
ReadOnlySpan<float> samples,
|
||||
int sampleRate,
|
||||
|
|
@ -186,6 +276,60 @@ public sealed class MimirCalibratedAudioCompositeBuilder(MimirCalibratedAudioCom
|
|||
}
|
||||
}
|
||||
|
||||
private static ComplexToneCoefficient ToneCoefficient(ReadOnlySpan<float> samples, int sampleRate, double centerHz)
|
||||
{
|
||||
var sine = 0.0;
|
||||
var cosine = 0.0;
|
||||
var omega = 2.0 * Math.PI * centerHz / sampleRate;
|
||||
for (var index = 0; index < samples.Length; index++)
|
||||
{
|
||||
var phase = omega * index;
|
||||
sine += samples[index] * Math.Sin(phase);
|
||||
cosine += samples[index] * Math.Cos(phase);
|
||||
}
|
||||
|
||||
var scale = 2.0 / Math.Max(1, samples.Length);
|
||||
sine *= scale;
|
||||
cosine *= scale;
|
||||
return new ComplexToneCoefficient(cosine, sine);
|
||||
}
|
||||
|
||||
private static double PairwiseCoherence(IReadOnlyList<ComplexToneCoefficient> coefficients)
|
||||
{
|
||||
var total = 0.0;
|
||||
var count = 0;
|
||||
for (var left = 0; left < coefficients.Count; left++)
|
||||
{
|
||||
for (var right = left + 1; right < coefficients.Count; right++)
|
||||
{
|
||||
var leftCoefficient = coefficients[left];
|
||||
var rightCoefficient = coefficients[right];
|
||||
var magnitude = leftCoefficient.Magnitude * rightCoefficient.Magnitude;
|
||||
if (magnitude <= 1.0e-12)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var phaseAgreement = (leftCoefficient.Real * rightCoefficient.Real + leftCoefficient.Imaginary * rightCoefficient.Imaginary) / magnitude;
|
||||
var energyBalance = Math.Min(leftCoefficient.Magnitude, rightCoefficient.Magnitude) / Math.Max(leftCoefficient.Magnitude, rightCoefficient.Magnitude);
|
||||
total += Math.Clamp((phaseAgreement + 1.0) * 0.5, 0.0, 1.0) * Math.Sqrt(Math.Clamp(energyBalance, 0.0, 1.0));
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
return count == 0 ? 0.0 : total / count;
|
||||
}
|
||||
|
||||
private static bool BandsTooClose(double leftHz, double rightHz)
|
||||
{
|
||||
if (leftHz <= 0.0 || rightHz <= 0.0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return Math.Abs(Math.Log(leftHz / rightHz)) < 0.06;
|
||||
}
|
||||
|
||||
private double OverallGain(IReadOnlyList<MimirChirpletBandResponse> responses)
|
||||
{
|
||||
var usable = responses
|
||||
|
|
@ -272,4 +416,24 @@ public sealed class MimirCalibratedAudioCompositeBuilder(MimirCalibratedAudioCom
|
|||
var variance = energies.Sum(value => (value - mean) * (value - mean)) / energies.Length;
|
||||
return 1.0 / (1.0 + Math.Sqrt(variance));
|
||||
}
|
||||
|
||||
private sealed record PreparedCompositeSource(
|
||||
string SourceId,
|
||||
float[] Samples,
|
||||
double Weight,
|
||||
double AppliedDelaySamples,
|
||||
double AppliedGain,
|
||||
int CorrectedBandCount,
|
||||
double ResponseFlatnessBefore,
|
||||
double ResponseFlatnessAfter,
|
||||
double Confidence,
|
||||
double NoiseRms,
|
||||
IReadOnlyList<MimirChirpletBandResponse> BandResponses);
|
||||
|
||||
private sealed record BandMeasurement(PreparedCompositeSource Source, ComplexToneCoefficient Coefficient);
|
||||
|
||||
private readonly record struct ComplexToneCoefficient(double Real, double Imaginary)
|
||||
{
|
||||
public double Magnitude => Math.Sqrt(Real * Real + Imaginary * Imaginary);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue