Skip to content

Commit

Permalink
Simplified FWHM (#62)
Browse files Browse the repository at this point in the history
* Simplifying FWHM algorithm

* Improved computation

* Simplified data model

* Refactoring

* Trying improved FWHM

* Changing Debug mode
  • Loading branch information
Ilia-Kosenkov authored Jun 25, 2021
1 parent 0756b4a commit b760f55
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 89 deletions.
3 changes: 2 additions & 1 deletion src/DIPOL-UF/DipolUfApp.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@


#define HOST_SERVER
// #define IN_PROCESS
#define IN_PROCESS

using System;
using System.Diagnostics;
Expand Down Expand Up @@ -31,6 +31,7 @@ private static int Main()
var host = connStr is null ? null : new DipolHost(new Uri(connStr));
host?.Open();
#else

var connStr = UiSettingsProvider.Settings.GetArray<string>("RemoteLocations")?.FirstOrDefault();

var pInfo = new ProcessStartInfo()
Expand Down
58 changes: 56 additions & 2 deletions src/DIPOL-UF/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace DIPOL_UF
{
public static class Helper
{

private static readonly double DEpsilon = Math.Pow(2, -53);
public static Dispatcher UiDispatcher =>
Application.Current?.Dispatcher
?? throw new InvalidOperationException("Dispatcher is unavailable.");
Expand Down Expand Up @@ -410,7 +410,61 @@ DescriptionAttribute attr
: key);




internal static double Interpolate(double x1, double x2, double y1, double y2, double x0)
{
if (Math.Abs(x2 - x1) < 1e-15)
{
return 0.0;
}
return y1 + (y2 - y1) / (x2 - x1) * (x0 - x1);
}

internal static double Percentile(ReadOnlySpan<double> data, double p)
{
if (data.IsEmpty || p is < 0 or > 1)
{
return double.NaN;
}

if (Math.Abs(p) < DEpsilon)
{
return data[0];
}

if (Math.Abs(1 - p) < DEpsilon)
{
return data[data.Length - 1];
}


var buffer = ArrayPool<double>.Shared.Rent(data.Length);
try
{
data.CopyTo(buffer);
Array.Sort(buffer, 0, data.Length);

// Strictly non-negative value between `0` and `n - 1`
var x = (data.Length - 1) * p;

// Integral part of `x`
var i = (int)Math.Floor(x);
// Non-integral part of `x`
var g = x - i;

if (i == data.Length - 1)
{
return buffer[i];
}

return (1 - g) * buffer[i] + g * buffer[i + 1];
}
finally
{
ArrayPool<double>.Shared.Return(buffer);
}
}

}

internal class CameraTupleOrderComparer : IComparer<(string Id, IDevice Camera)>
Expand Down
Loading

0 comments on commit b760f55

Please sign in to comment.