Shape canary packets with intra-call anchors
This commit is contained in:
parent
4222d6533f
commit
880a7b5abf
7 changed files with 23482 additions and 8 deletions
|
|
@ -1762,7 +1762,7 @@ static int CalibrateContestantAsioFloat32(
|
|||
foreach (var channel in channelCalibrations)
|
||||
{
|
||||
Console.WriteLine(
|
||||
$"contestant-calibration-channel {channel.SourceId}: events={channel.DetectedEvents}/{channel.ExpectedEvents} payload={channel.PayloadAccuracy:0.000} confidence={channel.Confidence:0.000} polarity={channel.Polarity:+0;-0;0} scheduleOffsetSamples={channel.ScheduleOffsetSamples:0.000} offsetSamples={channel.SourceOffsetSamples:0.000000} offsetUs={channel.SourceOffsetSamples * 1_000_000.0 / sampleRate:0.000} maeSamples={channel.MeanAbsoluteErrorSamples:0.000000} maeUs={channel.MeanAbsoluteErrorSamples * 1_000_000.0 / sampleRate:0.000} rms={channel.Rms:0.000000} peak={channel.Peak:0.000000} usableBands={channel.Bands.Count(band => band.Usable)}/{channel.Bands.Length}");
|
||||
$"contestant-calibration-channel {channel.SourceId}: events={channel.DetectedEvents}/{channel.ExpectedEvents} payload={channel.PayloadAccuracy:0.000} confidence={channel.Confidence:0.000} polarity={channel.Polarity:+0;-0;0} scheduleOffsetSamples={channel.ScheduleOffsetSamples:0.000} offsetSamples={channel.SourceOffsetSamples:0.000000} offsetUs={channel.SourceOffsetSamples * 1_000_000.0 / sampleRate:0.000} maeSamples={channel.MeanAbsoluteErrorSamples:0.000000} maeUs={channel.MeanAbsoluteErrorSamples * 1_000_000.0 / sampleRate:0.000} anchors={channel.AnchorCount} anchorMeanResidualSamples={channel.MeanAnchorResidualSamples:0.000000} rms={channel.Rms:0.000000} peak={channel.Peak:0.000000} usableBands={channel.Bands.Count(band => band.Usable)}/{channel.Bands.Length}");
|
||||
}
|
||||
|
||||
foreach (var path in paths)
|
||||
|
|
@ -2617,6 +2617,14 @@ static BioacousticPhysicalChannelCalibration CalibrateContestantChannel(
|
|||
}
|
||||
|
||||
var residual = ComputeContestantResidual(samples, template, (int)Math.Round(refinedOffset), motifSamples, signedScore);
|
||||
var anchors = MeasureContestantAnchors(
|
||||
samples,
|
||||
template,
|
||||
renderer,
|
||||
eventIndex,
|
||||
sampleRate,
|
||||
refinedOffset,
|
||||
motifSamples);
|
||||
var delaySamples = refinedOffset - renderer.EventStartSeconds(eventIndex) * sampleRate;
|
||||
observations.Add(new BioacousticPhysicalEventCalibration(
|
||||
eventIndex,
|
||||
|
|
@ -2627,10 +2635,11 @@ static BioacousticPhysicalChannelCalibration CalibrateContestantChannel(
|
|||
Math.Clamp(Math.Abs(signedScore), 0.0, 1.0),
|
||||
polarity,
|
||||
residual,
|
||||
EstimateContestantEventBands(samples, template, renderer, eventIndex, sampleRate, (int)Math.Round(refinedOffset), motifSamples)));
|
||||
EstimateContestantEventBands(samples, template, renderer, eventIndex, sampleRate, (int)Math.Round(refinedOffset), motifSamples),
|
||||
anchors));
|
||||
}
|
||||
|
||||
var clock = FitContestantClockHypothesis(
|
||||
var wordClock = FitContestantClockHypothesis(
|
||||
observations
|
||||
.Select(observation => new CepstralWordObservation(
|
||||
observation.EventIndex,
|
||||
|
|
@ -2642,6 +2651,12 @@ static BioacousticPhysicalChannelCalibration CalibrateContestantChannel(
|
|||
renderer,
|
||||
sampleRate,
|
||||
expectedEvents.Count);
|
||||
var anchorClock = FitContestantClockFromAnchors(observations, renderer, sampleRate, expectedEvents.Count);
|
||||
var clock = anchorClock != null &&
|
||||
wordClock != null &&
|
||||
anchorClock.MeanAbsoluteErrorSamples <= wordClock.MeanAbsoluteErrorSamples * 0.82
|
||||
? anchorClock
|
||||
: wordClock ?? anchorClock;
|
||||
var sourceOffsetSamples = clock?.SourceOffsetSamples ?? 0.0;
|
||||
var meanAbsoluteErrorSamples = clock?.MeanAbsoluteErrorSamples ?? 0.0;
|
||||
var confidence = observations.Count == 0
|
||||
|
|
@ -2660,6 +2675,8 @@ static BioacousticPhysicalChannelCalibration CalibrateContestantChannel(
|
|||
sourceOffsetSamples * 1_000_000.0 / sampleRate,
|
||||
meanAbsoluteErrorSamples,
|
||||
meanAbsoluteErrorSamples * 1_000_000.0 / sampleRate,
|
||||
observations.Sum(observation => observation.Anchors.Length),
|
||||
observations.SelectMany(observation => observation.Anchors).DefaultIfEmpty().Average(anchor => anchor?.TimingResidualSamples ?? 0.0),
|
||||
samples.Length,
|
||||
RootMeanSquare(samples),
|
||||
samples.Length == 0 ? 0.0 : samples.Max(sample => Math.Abs(sample)),
|
||||
|
|
@ -2790,6 +2807,171 @@ static double RefineContestantExpectedPayloadOffset(float[] samples, float[] tem
|
|||
: bestOffset + Math.Clamp(0.5 * (left - right) / denominator, -0.5, 0.5);
|
||||
}
|
||||
|
||||
static BioacousticPhysicalAnchorCalibration[] MeasureContestantAnchors(
|
||||
float[] samples,
|
||||
float[] template,
|
||||
MimirBioacousticContestantRenderer renderer,
|
||||
ulong eventIndex,
|
||||
int sampleRate,
|
||||
double eventSampleOffset,
|
||||
int motifSamples)
|
||||
{
|
||||
var anchors = renderer.AnchorPlan(eventIndex);
|
||||
var output = new List<BioacousticPhysicalAnchorCalibration>(anchors.Count);
|
||||
foreach (var anchor in anchors
|
||||
.OrderByDescending(anchor => anchor.Weight)
|
||||
.Take(8)
|
||||
.OrderBy(anchor => anchor.StartSeconds))
|
||||
{
|
||||
var anchorSamples = Math.Clamp(
|
||||
(int)Math.Round(anchor.DurationSeconds * sampleRate),
|
||||
Math.Max(12, sampleRate / 24_000),
|
||||
Math.Max(16, motifSamples / 5));
|
||||
var expectedLocalOffset = (int)Math.Round(anchor.StartSeconds * sampleRate);
|
||||
var expectedObservedOffset = (int)Math.Round(eventSampleOffset) + expectedLocalOffset;
|
||||
var templateStart = Math.Clamp(expectedLocalOffset, 0, Math.Max(0, template.Length - anchorSamples));
|
||||
var observedStart = Math.Clamp(expectedObservedOffset, 0, Math.Max(0, samples.Length - anchorSamples));
|
||||
if (templateStart + anchorSamples > template.Length ||
|
||||
observedStart + anchorSamples > samples.Length)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var observedWindow = samples.AsSpan(observedStart, anchorSamples);
|
||||
var expectedWindow = template.AsSpan(templateStart, anchorSamples);
|
||||
var bestScore = ScoreAnchorWindow(observedWindow, expectedWindow);
|
||||
var refinedOffset = observedStart +
|
||||
EnergyCentroid(observedWindow) -
|
||||
EnergyCentroid(expectedWindow);
|
||||
var residual = refinedOffset - (eventSampleOffset + expectedLocalOffset);
|
||||
var observedEnergy = WindowMeanSquare(observedWindow);
|
||||
var expectedEnergy = WindowMeanSquare(expectedWindow);
|
||||
output.Add(new BioacousticPhysicalAnchorCalibration(
|
||||
anchor.Kind.ToString(),
|
||||
anchor.SyllableIndex,
|
||||
anchor.StartSeconds,
|
||||
anchor.DurationSeconds,
|
||||
anchor.CenterHz,
|
||||
refinedOffset,
|
||||
residual,
|
||||
Math.Clamp(Math.Abs(bestScore) * anchor.Weight, 0.0, 1.0),
|
||||
expectedEnergy <= 1.0e-12 ? 0.0 : Math.Sqrt(Math.Max(0.0, observedEnergy) / expectedEnergy)));
|
||||
}
|
||||
|
||||
return output.ToArray();
|
||||
}
|
||||
|
||||
static double ScoreAnchorWindow(ReadOnlySpan<float> observed, ReadOnlySpan<float> expected)
|
||||
{
|
||||
var count = Math.Min(observed.Length, expected.Length);
|
||||
if (count <= 0)
|
||||
{
|
||||
return double.NegativeInfinity;
|
||||
}
|
||||
|
||||
var dot = 0.0;
|
||||
var observedEnergy = 0.0;
|
||||
var expectedEnergy = 0.0;
|
||||
for (var index = 0; index < count; index++)
|
||||
{
|
||||
dot += observed[index] * expected[index];
|
||||
observedEnergy += observed[index] * observed[index];
|
||||
expectedEnergy += expected[index] * expected[index];
|
||||
}
|
||||
|
||||
return observedEnergy <= 1.0e-12 || expectedEnergy <= 1.0e-12
|
||||
? double.NegativeInfinity
|
||||
: dot / Math.Sqrt(observedEnergy * expectedEnergy);
|
||||
}
|
||||
|
||||
static double EnergyCentroid(ReadOnlySpan<float> samples)
|
||||
{
|
||||
var weighted = 0.0;
|
||||
var energy = 0.0;
|
||||
for (var index = 0; index < samples.Length; index++)
|
||||
{
|
||||
var value = samples[index] * samples[index];
|
||||
weighted += index * value;
|
||||
energy += value;
|
||||
}
|
||||
|
||||
return energy <= 1.0e-12
|
||||
? samples.Length * 0.5
|
||||
: weighted / energy;
|
||||
}
|
||||
|
||||
static MimirBioacousticClockHypothesis? FitContestantClockFromAnchors(
|
||||
IReadOnlyList<BioacousticPhysicalEventCalibration> observations,
|
||||
MimirBioacousticContestantRenderer renderer,
|
||||
int sampleRate,
|
||||
int expectedEventCount)
|
||||
{
|
||||
var wordAnchors = observations
|
||||
.Select(observation => new MimirBioacousticClockAnchor(
|
||||
observation.EventIndex,
|
||||
renderer.EventStartSeconds(observation.EventIndex),
|
||||
observation.SampleOffset,
|
||||
Math.Clamp(observation.Confidence, 0.001, 1.0)));
|
||||
var contourAnchors = observations.SelectMany(observation =>
|
||||
observation.Anchors
|
||||
.Where(anchor => anchor.Confidence >= 0.12)
|
||||
.Select(anchor => new MimirBioacousticClockAnchor(
|
||||
observation.EventIndex,
|
||||
renderer.EventStartSeconds(observation.EventIndex) + anchor.ExpectedStartSeconds,
|
||||
anchor.SampleOffset,
|
||||
Math.Clamp(anchor.Confidence, 0.001, 1.0))));
|
||||
var anchors = wordAnchors
|
||||
.Concat(contourAnchors)
|
||||
.OrderByDescending(anchor => anchor.Confidence)
|
||||
.Take(160)
|
||||
.ToArray();
|
||||
if (anchors.Length < 3)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var totalWeight = anchors.Sum(anchor => Math.Max(1.0e-6, anchor.Confidence));
|
||||
var meanTimeline = anchors.Sum(anchor => anchor.TimelineSeconds * Math.Max(1.0e-6, anchor.Confidence)) / totalWeight;
|
||||
var meanSample = anchors.Sum(anchor => anchor.SampleOffset * Math.Max(1.0e-6, anchor.Confidence)) / totalWeight;
|
||||
var covariance = 0.0;
|
||||
var variance = 0.0;
|
||||
foreach (var anchor in anchors)
|
||||
{
|
||||
var weight = Math.Max(1.0e-6, anchor.Confidence);
|
||||
var dt = anchor.TimelineSeconds - meanTimeline;
|
||||
covariance += weight * dt * (anchor.SampleOffset - meanSample);
|
||||
variance += weight * dt * dt;
|
||||
}
|
||||
|
||||
var effectiveRate = variance > 1.0e-12 ? covariance / variance : sampleRate;
|
||||
if (!double.IsFinite(effectiveRate) || effectiveRate < sampleRate * 0.98 || effectiveRate > sampleRate * 1.02)
|
||||
{
|
||||
effectiveRate = sampleRate;
|
||||
}
|
||||
|
||||
var sourceOffset = meanSample - effectiveRate * meanTimeline;
|
||||
var residual = anchors.Sum(anchor =>
|
||||
{
|
||||
var predicted = sourceOffset + anchor.TimelineSeconds * effectiveRate;
|
||||
return Math.Abs(anchor.SampleOffset - predicted) * Math.Max(1.0e-6, anchor.Confidence);
|
||||
}) / totalWeight;
|
||||
var residualConfidence = 1.0 / (1.0 + residual / Math.Max(1.0, sampleRate * 0.00025));
|
||||
var countConfidence = Math.Clamp(anchors.Length / 80.0, 0.0, 1.0);
|
||||
var anchorConfidence = Math.Clamp(anchors.Average(anchor => anchor.Confidence), 0.0, 1.0);
|
||||
var confidence = residualConfidence * 0.45 + countConfidence * 0.30 + anchorConfidence * 0.25;
|
||||
var coverage = expectedEventCount <= 0 ? 0.0 : observations.Count / (double)expectedEventCount;
|
||||
var score = confidence + Math.Min(anchors.Length, 80) * 0.015 - residual / Math.Max(1.0, sampleRate * 0.004);
|
||||
return new MimirBioacousticClockHypothesis(
|
||||
sourceOffset,
|
||||
effectiveRate,
|
||||
anchors.Length,
|
||||
coverage,
|
||||
residual,
|
||||
confidence,
|
||||
score,
|
||||
anchors);
|
||||
}
|
||||
|
||||
static int ClassifyContestantPayloadAt(
|
||||
float[] samples,
|
||||
int sampleRate,
|
||||
|
|
@ -4144,6 +4326,8 @@ public sealed record BioacousticPhysicalChannelCalibration(
|
|||
double SourceOffsetMicroseconds,
|
||||
double MeanAbsoluteErrorSamples,
|
||||
double MeanAbsoluteErrorMicroseconds,
|
||||
int AnchorCount,
|
||||
double MeanAnchorResidualSamples,
|
||||
int SampleCount,
|
||||
double Rms,
|
||||
double Peak,
|
||||
|
|
@ -4171,7 +4355,19 @@ public sealed record BioacousticPhysicalEventCalibration(
|
|||
double Confidence,
|
||||
int Polarity,
|
||||
double NormalizedResidual,
|
||||
BioacousticPhysicalBandCalibration[] Bands);
|
||||
BioacousticPhysicalBandCalibration[] Bands,
|
||||
BioacousticPhysicalAnchorCalibration[] Anchors);
|
||||
|
||||
public sealed record BioacousticPhysicalAnchorCalibration(
|
||||
string Kind,
|
||||
int SyllableIndex,
|
||||
double ExpectedStartSeconds,
|
||||
double DurationSeconds,
|
||||
double CenterHz,
|
||||
double SampleOffset,
|
||||
double TimingResidualSamples,
|
||||
double Confidence,
|
||||
double Gain);
|
||||
|
||||
public sealed record BioacousticPhysicalBandCalibration(
|
||||
double CenterHz,
|
||||
|
|
|
|||
|
|
@ -26,6 +26,22 @@ public sealed record MimirBioacousticContestantProfile(
|
|||
int PayloadBitsPerEvent,
|
||||
string BeautyNotes);
|
||||
|
||||
public enum MimirBioacousticAnchorKind
|
||||
{
|
||||
TimingChip,
|
||||
FormantPivot,
|
||||
HarmonicNotch,
|
||||
PayloadOrnament
|
||||
}
|
||||
|
||||
public sealed record MimirBioacousticContestantAnchor(
|
||||
MimirBioacousticAnchorKind Kind,
|
||||
int SyllableIndex,
|
||||
double StartSeconds,
|
||||
double DurationSeconds,
|
||||
double CenterHz,
|
||||
double Weight);
|
||||
|
||||
public static class MimirBioacousticContestants
|
||||
{
|
||||
public const double FirstEventSeconds = 0.08;
|
||||
|
|
@ -166,6 +182,8 @@ public static class MimirBioacousticContestants
|
|||
|
||||
public sealed class MimirBioacousticContestantRenderer(MimirBioacousticContestantProfile profile)
|
||||
{
|
||||
private readonly Dictionary<(ulong EventIndex, int SampleRate, int PayloadSymbol), float[]> eventCache = [];
|
||||
|
||||
public MimirBioacousticContestantProfile Profile { get; } = profile;
|
||||
|
||||
public double EventStartSeconds(ulong eventIndex) =>
|
||||
|
|
@ -178,11 +196,63 @@ public sealed class MimirBioacousticContestantRenderer(MimirBioacousticContestan
|
|||
|
||||
public float[] RenderEventMonoFloat(ulong eventIndex, int sampleRate, int payloadSymbol)
|
||||
{
|
||||
if (eventCache.TryGetValue((eventIndex, sampleRate, payloadSymbol), out var cached))
|
||||
{
|
||||
return cached;
|
||||
}
|
||||
|
||||
var samples = new float[Math.Max(1, (int)Math.Round(Profile.MotifDurationSeconds * sampleRate))];
|
||||
AddEvent(samples, sampleRate, eventIndex, payloadSymbol, EventStartSeconds(eventIndex), EventStartSeconds(eventIndex));
|
||||
eventCache[(eventIndex, sampleRate, payloadSymbol)] = samples;
|
||||
return samples;
|
||||
}
|
||||
|
||||
public IReadOnlyList<MimirBioacousticContestantAnchor> AnchorPlan(ulong eventIndex)
|
||||
{
|
||||
var payload = PayloadSymbolForEvent(eventIndex);
|
||||
var symbolId = SymbolForEvent(eventIndex);
|
||||
var root = RootForSymbol(symbolId);
|
||||
var rng = Mix(symbolId ^ (payload << 9), (uint)Profile.Kind);
|
||||
var anchors = new List<MimirBioacousticContestantAnchor>(Profile.SyllableCount * 3);
|
||||
for (var syllable = 0; syllable < Profile.SyllableCount; syllable++)
|
||||
{
|
||||
var contour = SyllableContour(symbolId, payload, syllable, root, rng);
|
||||
var centerHz = Math.Sqrt(contour.StartHz * contour.EndHz);
|
||||
anchors.Add(new MimirBioacousticContestantAnchor(
|
||||
syllable < 2 ? MimirBioacousticAnchorKind.TimingChip : MimirBioacousticAnchorKind.FormantPivot,
|
||||
syllable,
|
||||
contour.StartSeconds,
|
||||
Math.Min(contour.DurationSeconds, Profile.MotifDurationSeconds * 0.028),
|
||||
centerHz,
|
||||
syllable < 2 ? 1.25 : contour.Weight));
|
||||
if (Profile.Kind == MimirBioacousticContestantKind.CanaryPacketTrill)
|
||||
{
|
||||
anchors.Add(new MimirBioacousticContestantAnchor(
|
||||
MimirBioacousticAnchorKind.HarmonicNotch,
|
||||
syllable,
|
||||
contour.StartSeconds + contour.DurationSeconds * 0.46,
|
||||
Math.Min(contour.DurationSeconds * 0.24, Profile.MotifDurationSeconds * 0.018),
|
||||
centerHz * (syllable % 2 == 0 ? 1.50 : 2.00),
|
||||
0.72));
|
||||
if (syllable >= 2)
|
||||
{
|
||||
anchors.Add(new MimirBioacousticContestantAnchor(
|
||||
MimirBioacousticAnchorKind.PayloadOrnament,
|
||||
syllable,
|
||||
contour.StartSeconds + contour.DurationSeconds * 0.72,
|
||||
Math.Min(contour.DurationSeconds * 0.18, Profile.MotifDurationSeconds * 0.014),
|
||||
centerHz * (1.20 + 0.08 * (payload & 3)),
|
||||
0.58));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return anchors
|
||||
.Where(anchor => anchor.StartSeconds >= 0.0 && anchor.StartSeconds < Profile.MotifDurationSeconds)
|
||||
.OrderBy(anchor => anchor.StartSeconds)
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
public float[] RenderSequenceMonoFloat(double seconds, int sampleRate)
|
||||
{
|
||||
var samples = new float[Math.Max(1, (int)Math.Round(seconds * sampleRate))];
|
||||
|
|
@ -320,7 +390,7 @@ public sealed class MimirBioacousticContestantRenderer(MimirBioacousticContestan
|
|||
return value;
|
||||
}
|
||||
|
||||
private static void AddSyllable(
|
||||
private void AddSyllable(
|
||||
float[] output,
|
||||
int sampleRate,
|
||||
double startSeconds,
|
||||
|
|
@ -346,8 +416,31 @@ public sealed class MimirBioacousticContestantRenderer(MimirBioacousticContestan
|
|||
phase += Math.Tau * (previousFrequency + frequency) * 0.5 / sampleRate;
|
||||
previousFrequency = frequency;
|
||||
var envelope = Math.Sin(Math.PI * Math.Clamp(t, 0.0, 1.0));
|
||||
var harmonic = 0.28 * Math.Sin(phase * 2.0 + 0.25) + 0.12 * Math.Sin(phase * 3.0 + 0.7);
|
||||
output[sample] += (float)(gain * envelope * (Math.Sin(phase) + harmonic));
|
||||
var harmonicEnvelope = ProfileAwareHarmonicEnvelope(t, startHz, endHz);
|
||||
var harmonic = harmonicEnvelope.Second * Math.Sin(phase * 2.0 + 0.25) +
|
||||
harmonicEnvelope.Third * Math.Sin(phase * 3.0 + 0.7) +
|
||||
harmonicEnvelope.Notch * Math.Sin(phase * 1.5 + 1.35);
|
||||
var anchorClick = 0.18 * Math.Exp(-Math.Pow((t - 0.08) / 0.035, 2.0)) -
|
||||
0.12 * Math.Exp(-Math.Pow((t - 0.52) / 0.045, 2.0)) +
|
||||
0.09 * Math.Exp(-Math.Pow((t - 0.76) / 0.035, 2.0));
|
||||
output[sample] += (float)(gain * envelope * (Math.Sin(phase) + harmonic + anchorClick * Math.Sin(phase * 2.5 + 0.4)));
|
||||
}
|
||||
}
|
||||
|
||||
private (double Second, double Third, double Notch) ProfileAwareHarmonicEnvelope(double t, double startHz, double endHz)
|
||||
{
|
||||
if (Profile.Kind != MimirBioacousticContestantKind.CanaryPacketTrill)
|
||||
{
|
||||
return (0.28, 0.12, 0.0);
|
||||
}
|
||||
|
||||
var sweep = Math.Abs(endHz - startHz) / Math.Max(1.0, startHz);
|
||||
var attack = Math.Exp(-Math.Pow((t - 0.13) / 0.075, 2.0));
|
||||
var pivot = Math.Exp(-Math.Pow((t - 0.48) / 0.060, 2.0));
|
||||
var tail = Math.Exp(-Math.Pow((t - 0.78) / 0.070, 2.0));
|
||||
return (
|
||||
Second: 0.18 + 0.26 * attack + 0.10 * sweep,
|
||||
Third: 0.08 + 0.22 * tail,
|
||||
Notch: 0.16 * pivot);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue