Skip to content

Commit

Permalink
Use max not average in bucket for spectral density amplitude
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Thaler <[email protected]>
  • Loading branch information
dthaler committed Dec 23, 2024
1 parent 3a65d7a commit fb48afc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
6 changes: 3 additions & 3 deletions OrcanodeMonitor/Pages/SpectralDensity.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
}

<div class="text-center">
<h1 class="display-4">Spectral Density</h1>
<h1 class="display-4">Spectral Density of Audio From @Model.NodeName</h1>

<!-- Include Chart.js from CDN -->
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
Expand All @@ -19,8 +19,8 @@
data: {
labels: @Html.Raw(Json.Serialize(Model.Labels)),
datasets: [{
label: 'Last Sample Spectral Density',
data: @Html.Raw(Json.Serialize(Model.Data)),
label: 'Last Sample',
data: @Html.Raw(Json.Serialize(Model.MaxBucketAmplitude)),
backgroundColor: 'rgba(75, 192, 192, 0.2)',
borderColor: 'rgba(75, 192, 192, 1)',
borderWidth: 1
Expand Down
32 changes: 18 additions & 14 deletions OrcanodeMonitor/Pages/SpectralDensity.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ public class SpectralDensityModel : PageModel
private readonly ILogger<SpectralDensityModel> _logger;
private string _nodeId;
public string Id => _nodeId;
public string NodeName { get; private set; }
private List<string> _labels;
private List<int> _data;
private List<double> _maxBucketAmplitude;
public List<string> Labels => _labels;
public List<int> Data => _data;
public List<double> MaxBucketAmplitude => _maxBucketAmplitude;
public int MaxAmplitude { get; private set; }
public int MaxNonHumAmplitude { get; private set; }
public int SignalRatio { get; private set; }
Expand All @@ -33,18 +34,20 @@ public SpectralDensityModel(OrcanodeMonitorContext context, ILogger<SpectralDens
_databaseContext = context;
_logger = logger;
_nodeId = string.Empty;
NodeName = "Unknown";
}

private async Task UpdateFrequencyDataAsync()
{
_labels = new List<string> { };
_data = new List<int> { };
_maxBucketAmplitude = new List<double> { };
Orcanode? node = _databaseContext.Orcanodes.Where(n => n.ID == _nodeId).FirstOrDefault();
if (node == null)
{
_logger.LogWarning("Node not found with ID: {NodeId}", _nodeId);
return;
}
NodeName = node.DisplayName;
TimestampResult? result = await GetLatestS3TimestampAsync(node, false, _logger);
if (result != null)
{
Expand All @@ -59,33 +62,34 @@ private async Task UpdateFrequencyDataAsync()
double logb = Math.Log(b);

double maxAmplitude = frequencyInfo.MaxAmplitude;
var sums = new double[PointCount];
var count = new int[PointCount];
var maxBucketAmplitude = new double[PointCount];
var maxBucketFrequency = new int[PointCount];

foreach (var pair in frequencyInfo.FrequencyAmplitudes)
{
double frequency = pair.Key;
double amplitude = pair.Value;
int bucket = (frequency < 1) ? 0 : (int)(Math.Log(frequency) / logb);
count[bucket]++;
sums[bucket] += amplitude;
if (maxBucketAmplitude[bucket] < amplitude)
{
maxBucketAmplitude[bucket] = amplitude;
maxBucketFrequency[bucket] = (int)Math.Round(frequency);
}
}

// Fill in graph points.
for (int i = 0; i < PointCount; i++)
{
if (count[i] > 0)
if (maxBucketAmplitude[i] > 0)
{
int frequency = (int)Math.Pow(b, i);
int amplitude = (int)(sums[i] / count[i]);
_labels.Add(frequency.ToString());
_data.Add(amplitude);
_labels.Add(maxBucketFrequency[i].ToString());
_maxBucketAmplitude.Add(maxBucketAmplitude[i]);
}
}

double maxNonHumAmplitude = frequencyInfo.GetMaxNonHumAmplitude();
MaxAmplitude = (int)maxAmplitude;
MaxNonHumAmplitude = (int)maxNonHumAmplitude;
MaxAmplitude = (int)Math.Round(maxAmplitude);
MaxNonHumAmplitude = (int)Math.Round(maxNonHumAmplitude);
SignalRatio = (int)Math.Round(100 * maxNonHumAmplitude / maxAmplitude);
Status = Orcanode.GetStatusString(frequencyInfo.Status);
}
Expand Down

0 comments on commit fb48afc

Please sign in to comment.