This repository has been archived by the owner on May 11, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
blockFrequency.c
43 lines (37 loc) · 1.71 KB
/
blockFrequency.c
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
#include <stdio.h>
#include <math.h>
#include <string.h>
#include "../include/externs.h"
#include "../include/cephes.h"
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
B L O C K F R E Q U E N C Y T E S T
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void
BlockFrequency(int M, int n)
{
int i, j, N, blockSum;
double p_value, sum, pi, v, chi_squared;
N = n/M; /* # OF SUBSTRING BLOCKS */
sum = 0.0;
for ( i=0; i<N; i++ ) {
blockSum = 0;
for ( j=0; j<M; j++ )
blockSum += epsilon[j+i*M];
pi = (double)blockSum/(double)M;
v = pi - 0.5;
sum += v*v;
}
chi_squared = 4.0 * M * sum;
p_value = cephes_igamc(N/2.0, chi_squared/2.0);
fprintf(stats[TEST_BLOCK_FREQUENCY], "\t\t\tBLOCK FREQUENCY TEST\n");
fprintf(stats[TEST_BLOCK_FREQUENCY], "\t\t---------------------------------------------\n");
fprintf(stats[TEST_BLOCK_FREQUENCY], "\t\tCOMPUTATIONAL INFORMATION:\n");
fprintf(stats[TEST_BLOCK_FREQUENCY], "\t\t---------------------------------------------\n");
fprintf(stats[TEST_BLOCK_FREQUENCY], "\t\t(a) Chi^2 = %f\n", chi_squared);
fprintf(stats[TEST_BLOCK_FREQUENCY], "\t\t(b) # of substrings = %d\n", N);
fprintf(stats[TEST_BLOCK_FREQUENCY], "\t\t(c) block length = %d\n", M);
fprintf(stats[TEST_BLOCK_FREQUENCY], "\t\t(d) Note: %d bits were discarded.\n", n % M);
fprintf(stats[TEST_BLOCK_FREQUENCY], "\t\t---------------------------------------------\n");
fprintf(stats[TEST_BLOCK_FREQUENCY], "%s\t\tp_value = %f\n\n", p_value < ALPHA ? "FAILURE" : "SUCCESS", p_value); fflush(stats[TEST_BLOCK_FREQUENCY]);
fprintf(results[TEST_BLOCK_FREQUENCY], "%f\n", p_value); fflush(results[TEST_BLOCK_FREQUENCY]);
}