File: scc.cpp

package info (click to toggle)
entropybroker 2.9-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,600 kB
  • sloc: cpp: 14,386; sh: 934; makefile: 188; java: 148; perl: 12
file content (113 lines) | stat: -rw-r--r-- 1,778 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <stdio.h>

#include "error.h"
#include "math.h"
#include "log.h"
#include "scc.h"

scc::scc()
{
	index = bytes_in = 0;

	memset(buffer, 0x00, sizeof buffer);

	threshold = 0.2;

	user = NULL;
}

scc::~scc()
{
	free(user);
}

void scc::set_user(const char *puser)
{
	free(user);
        user = strdup(puser);
        if (!user)
                error_exit("memory allocation error");

        dolog(LOG_DEBUG, "registered scc-user %s", user);
}

void scc::set_threshold(double t)
{
	threshold = t;
}

void scc::add(unsigned char byte)
{
	buffer[index++] = byte;

	if (bytes_in < SCC_BUFFER_SIZE)
		bytes_in++;

	if (index >= SCC_BUFFER_SIZE)
		index = 0;
}

// 0: ok, -1: not ok
double scc::get_cur_scc()
{
	double scc_val;
	double prev_val = 0.0, u0 = 0.0;
	double t[3];
	int loop;

	if (bytes_in < 2)
		return 0;	// ok

	t[0] = t[1] = t[2] = 0.0;

	for(loop=0; loop<bytes_in; loop++)
	{
		double cur_val = double(buffer[loop]);

		if (loop == 0)
		{
			prev_val = 0;
			u0 = cur_val;
		}
		else
			t[0] += prev_val * cur_val;

		t[1] = t[1] + cur_val;
		t[2] = t[2] + (cur_val * cur_val);
		prev_val = cur_val;
	}

	t[0] = t[0] + prev_val * u0;
	t[1] = t[1] * t[1];
	scc_val = double(bytes_in) * t[2] - t[1];
	if (scc_val == 0.0)
		scc_val = -100000.0;
	else
		scc_val = (double(bytes_in) * t[0] - t[1]) / scc_val;

	return scc_val;
}

bool scc::is_ok()
{
	double cur_scc = fabs(get_cur_scc());
	bool rc = cur_scc < threshold;

	if (rc == false)
		dolog(LOG_WARNING, "SCC %f above threshold %f", cur_scc, threshold);

	return rc;
}

std::string scc::stats()
{
	char stats_buffer[128];

	snprintf(stats_buffer, sizeof stats_buffer, "%f (%d)", get_cur_scc(), bytes_in);

	return stats_buffer;
}