Added a entropy-calculating filter.
All checks were successful
🚀 Pack skyscraper8 / make-zip (push) Successful in 3m54s
All checks were successful
🚀 Pack skyscraper8 / make-zip (push) Successful in 3m54s
This commit is contained in:
parent
d1ba3976cb
commit
8aa9ac82d4
@ -54,18 +54,21 @@ namespace skyscraper8.Skyscraper.Math
|
||||
double p_i = (double)freq[i] / (double)internalPosition;
|
||||
if (p_i > 0)
|
||||
newEntropy -= p_i * System.Math.Log2(p_i);
|
||||
}
|
||||
|
||||
//Raise events if we feel like it
|
||||
if (newEntropy != Entropy)
|
||||
{
|
||||
double oldEntropy = Entropy;
|
||||
double oldPercentage = Percentage;
|
||||
double newPercentage = (newEntropy / 8.0) * 100.0;
|
||||
if (newEntropy > Entropy)
|
||||
EntropyRising?.Invoke(oldEntropy, newEntropy, oldPercentage, newPercentage);
|
||||
else if (newEntropy < Entropy)
|
||||
EntropyFalling?.Invoke(oldEntropy, newEntropy, oldPercentage, newPercentage);
|
||||
}
|
||||
|
||||
//Raise events if we feel like it
|
||||
if (EntropyRising != null || EntropyFalling != null)
|
||||
{
|
||||
if (newEntropy != Entropy)
|
||||
{
|
||||
double oldEntropy = Entropy;
|
||||
double oldPercentage = Percentage;
|
||||
double newPercentage = (newEntropy / 8.0) * 100.0;
|
||||
if (newEntropy > Entropy)
|
||||
EntropyRising?.Invoke(oldEntropy, newEntropy, oldPercentage, newPercentage);
|
||||
else if (newEntropy < Entropy)
|
||||
EntropyFalling?.Invoke(oldEntropy, newEntropy, oldPercentage, newPercentage);
|
||||
}
|
||||
}
|
||||
|
||||
_entropy = newEntropy;
|
||||
|
||||
45
skyscraper8/Skyscraper/Math/PidEntryCalculatorFilter.cs
Normal file
45
skyscraper8/Skyscraper/Math/PidEntryCalculatorFilter.cs
Normal file
@ -0,0 +1,45 @@
|
||||
using skyscraper5.Mpeg2;
|
||||
using skyscraper5.src.Mpeg2.PacketFilter;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace skyscraper8.Skyscraper.Math
|
||||
{
|
||||
public class PidEntryCalculatorFilter : IPacketFilter
|
||||
{
|
||||
public bool PassPacket(TsPacket packet)
|
||||
{
|
||||
if (entropyCalculatorStreams == null)
|
||||
entropyCalculatorStreams = new EntropyCalculatorStream[0x1fff];
|
||||
|
||||
if (packet.PayloadFlagSet)
|
||||
{
|
||||
uint pid = packet.PID;
|
||||
if (entropyCalculatorStreams[pid] == null)
|
||||
entropyCalculatorStreams[pid] = new EntropyCalculatorStream();
|
||||
entropyCalculatorStreams[pid].Write(packet.Payload);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private EntropyCalculatorStream[] entropyCalculatorStreams;
|
||||
|
||||
public double GetEntropy(int pid)
|
||||
{
|
||||
if (pid < 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(pid));
|
||||
|
||||
if (pid > entropyCalculatorStreams.Length)
|
||||
throw new ArgumentOutOfRangeException(nameof(pid));
|
||||
|
||||
EntropyCalculatorStream stream = entropyCalculatorStreams[pid];
|
||||
if (stream == null)
|
||||
return double.NaN;
|
||||
|
||||
return stream.Entropy;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user