Skip to content

Commit

Permalink
Spectrogram: fix multiple runs
Browse files Browse the repository at this point in the history
  • Loading branch information
meee1 committed Aug 5, 2024
1 parent f99e1ce commit 1ac579c
Showing 1 changed file with 17 additions and 88 deletions.
105 changes: 17 additions & 88 deletions ExtLibs/Utilities/Spectrogram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,19 @@ public static Image<Rgba32> GenerateImage(DFLogBuffer cb, out double[] freqtout,
Console.WriteLine("GenerateImage seen ISBH");
int sensorno = (type.Contains("1") ? 0 : (type.Contains("2") ? 1 : 2));
int sensor = type.Contains("ACC") ? 0 : 1;
int Ns = -1;
int type1 = -1;
int instance = -1;

(double, (double, double)[]) cachedata;

double sample_rate = -1;
double multiplier = -1;

(double time, double d)[] data = new (double time, double d)[0];
if (!cache.TryGetValue(cb.GetHashCode() + sensorno + "" + sensor, out data))
if (!cache.TryGetValue(cb.GetHashCode() + sensorno + "" + sensor, out cachedata))
{
int Ns = -1;
int type1 = -1;
int instance = -1;
double multiplier = -1;

data = cb.GetEnumeratorType(new string[] { "ISBH", "ISBD" })
.SelectMany(
item =>
Expand Down Expand Up @@ -96,12 +100,17 @@ public static Image<Rgba32> GenerateImage(DFLogBuffer cb, out double[] freqtout,

return new (double time, double d)[0];
}).ToArray();
cache.Set(cb.GetHashCode() + sensorno + "" + sensor, data,
cache.Set(cb.GetHashCode() + sensorno + "" + sensor, (sample_rate, data),
new MemoryCacheEntryOptions()
{
AbsoluteExpirationRelativeToNow = System.TimeSpan.FromMinutes(10)
});
}
else
{
sample_rate = cachedata.Item1;
data = cachedata.Item2;
}


var bins = 10;
Expand All @@ -117,10 +126,8 @@ public static Image<Rgba32> GenerateImage(DFLogBuffer cb, out double[] freqtout,
int totalsamples = data.Count();
int count = totalsamples / N;
int done = 0;
// 50% overlap
int divisor = 4;
if (count > 2048)
divisor = 1;
// batch sampling is non continuous
int divisor = 1;
count *= divisor;
var img = new Image<Rgba32>(count, freqt.Length);
log.Debug("done and count ");
Expand Down Expand Up @@ -160,84 +167,6 @@ public static Image<Rgba32> GenerateImage(DFLogBuffer cb, out double[] freqtout,

return img;
}

{
var bins = 10;
int N = 1 << bins;

var fft = new FFT2();

DFLog.DFItem[] acc1data = null;
if (!cache.TryGetValue(cb.GetHashCode() + type, out acc1data))
{
acc1data = cb.GetEnumeratorType(type).ToArray();
cache.Set(cb.GetHashCode() + type, acc1data,
new MemoryCacheEntryOptions()
{
AbsoluteExpirationRelativeToNow = System.TimeSpan.FromMinutes(10)
});
}
log.Debug(type);
if (cb.dflog.FindMessageOffset(acc1data[0].msgtype, "TimeUS") == -1)
timeus = "SampleUS";

int totalsamples = acc1data.Count();
int count = totalsamples / N;
int done = 0;
double timedelta = 0;
// 50% overlap
int divisor = 4;
if (count > 2048)
divisor = 1;
count *= divisor;
Console.WriteLine("Image Size " + count + " " + N / 2);
var img = new Image<Rgba32>(count, N / 2);
log.Debug("done and count ");



foreach (var fftdata in acc1data.Windowed(N, divisor))
{
if (fftdata.Count() < N)
{
break;
}

var timeusvalue = double.MaxValue;
var timeusvalueend = double.MinValue;
var data = new double[N];
int c = 0;
foreach (var a in fftdata)
{
var time = Convert.ToDouble(a.GetRaw(timeus));
timeusvalue = Math.Min(timeusvalue, time);
timeusvalueend = Math.Max(timeusvalueend, time);
data[c++] = Convert.ToDouble(a.GetRaw(field));
}

timedelta = timedelta * 0.99 + ((timeusvalueend - timeusvalue)) * 0.01;

var fftanswerz = fft.rin(data, (uint)bins);

allfftdata.Add((timeusvalue, fftanswerz));

var i = 0;
foreach (var y in Enumerable.Range(0, N/2))
{
img[done, ((N / 2) - 1) - i] = GetColor(fftanswerz[i], min, max);
i++;
}

count--;
done++;
}

// 1s / (1/ (N/delta))
var sample_rate = (int)(1000000 / (1.0 / (N / (timedelta))));
freqtout = fft.FreqTable(N, sample_rate);

return img;
}
}

static double SCALE = 20 / Math.Log(10);
Expand Down

0 comments on commit 1ac579c

Please sign in to comment.