kerneltest/e32utils/nistsecurerng/src/approximateEntropy.cpp
changeset 152 657f875b013e
equal deleted inserted replaced
139:95f71bcdcdb7 152:657f875b013e
       
     1 /*
       
     2 * Portions Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 * The original NIST Statistical Test Suite code is placed in public domain.
       
    16 * (http://csrc.nist.gov/groups/ST/toolkit/rng/documentation_software.html) 
       
    17 * 
       
    18 * This software was developed at the National Institute of Standards and Technology by 
       
    19 * employees of the Federal Government in the course of their official duties. Pursuant
       
    20 * to title 17 Section 105 of the United States Code this software is not subject to 
       
    21 * copyright protection and is in the public domain. The NIST Statistical Test Suite is
       
    22 * an experimental system. NIST assumes no responsibility whatsoever for its use by other 
       
    23 * parties, and makes no guarantees, expressed or implied, about its quality, reliability, 
       
    24 * or any other characteristic. We would appreciate acknowledgment if the software is used.
       
    25 */
       
    26 
       
    27 #include "openc.h"
       
    28 #include "../include/externs.h"
       
    29 #include "../include/utilities.h"
       
    30 #include "../include/cephes.h"  
       
    31 
       
    32 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
       
    33                 A P P R O X I M A T E  E N T R O P Y   T E S T
       
    34  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
       
    35 
       
    36 void
       
    37 ApproximateEntropy(int m, int n)
       
    38 {
       
    39 	int				i, j, k, r, blockSize, seqLength, powLen, index;
       
    40 	double			sum, numOfBlocks, ApEn[2], apen, chi_squared, p_value;
       
    41 	unsigned int	*P;
       
    42 	
       
    43 	fprintf(stats[TEST_APEN], "\t\t\tAPPROXIMATE ENTROPY TEST\n");
       
    44 	fprintf(stats[TEST_APEN], "\t\t--------------------------------------------\n");
       
    45 	fprintf(stats[TEST_APEN], "\t\tCOMPUTATIONAL INFORMATION:\n");
       
    46 	fprintf(stats[TEST_APEN], "\t\t--------------------------------------------\n");
       
    47 	fprintf(stats[TEST_APEN], "\t\t(a) m (block length)    = %d\n", m);
       
    48 
       
    49 	seqLength = n;
       
    50 	r = 0;
       
    51 	
       
    52 	for ( blockSize=m; blockSize<=m+1; blockSize++ ) {
       
    53 		if ( blockSize == 0 ) {
       
    54 			ApEn[0] = 0.00;
       
    55 			r++;
       
    56 		}
       
    57 		else {
       
    58 			numOfBlocks = (double)seqLength;
       
    59 			powLen = (int)pow(2, blockSize+1)-1;
       
    60 			if ( (P = (unsigned int*)calloc(powLen,sizeof(unsigned int)))== NULL ) {
       
    61 				fprintf(stats[TEST_APEN], "ApEn:  Insufficient memory available.\n");
       
    62 				return;
       
    63 			}
       
    64 			for ( i=1; i<powLen-1; i++ )
       
    65 				P[i] = 0;
       
    66 			for ( i=0; i<numOfBlocks; i++ ) { /* COMPUTE FREQUENCY */
       
    67 				k = 1;
       
    68 				for ( j=0; j<blockSize; j++ ) {
       
    69 					k <<= 1;
       
    70 					if ( (int)epsilon[(i+j) % seqLength] == 1 )
       
    71 						k++;
       
    72 				}
       
    73 				P[k-1]++;
       
    74 			}
       
    75 			/* DISPLAY FREQUENCY */
       
    76 			sum = 0.0;
       
    77 			index = (int)pow(2, blockSize)-1;
       
    78 			for ( i=0; i<(int)pow(2, blockSize); i++ ) {
       
    79 				if ( P[index] > 0 )
       
    80 					sum += P[index]*log(P[index]/numOfBlocks);
       
    81 				index++;
       
    82 			}
       
    83 			sum /= numOfBlocks;
       
    84 			ApEn[r] = sum;
       
    85 			r++;
       
    86 			free(P);
       
    87 		}
       
    88 	}
       
    89 	apen = ApEn[0] - ApEn[1];
       
    90 	
       
    91 	chi_squared = 2.0*seqLength*(log(2) - apen);
       
    92 	p_value = cephes_igamc(pow(2, m-1), chi_squared/2.0);
       
    93 	
       
    94 	fprintf(stats[TEST_APEN], "\t\t(b) n (sequence length) = %d\n", seqLength);
       
    95 	fprintf(stats[TEST_APEN], "\t\t(c) Chi^2               = %f\n", chi_squared);
       
    96 	fprintf(stats[TEST_APEN], "\t\t(d) Phi(m)	       = %f\n", ApEn[0]);
       
    97 	fprintf(stats[TEST_APEN], "\t\t(e) Phi(m+1)	       = %f\n", ApEn[1]);
       
    98 	fprintf(stats[TEST_APEN], "\t\t(f) ApEn                = %f\n", apen);
       
    99 	fprintf(stats[TEST_APEN], "\t\t(g) Log(2)              = %f\n", log(2.0));
       
   100 	fprintf(stats[TEST_APEN], "\t\t--------------------------------------------\n");
       
   101 
       
   102 	if ( m > (int)(log(seqLength)/log(2)-5) ) {
       
   103 		fprintf(stats[TEST_APEN], "\t\tNote: The blockSize = %d exceeds recommended value of %d\n", m,
       
   104 			MAX(1, (int)(log(seqLength)/log(2)-5)));
       
   105 		fprintf(stats[TEST_APEN], "\t\tResults are inaccurate!\n");
       
   106 		fprintf(stats[TEST_APEN], "\t\t--------------------------------------------\n");
       
   107 	}
       
   108 	
       
   109 	fprintf(stats[TEST_APEN], "%s\t\tp_value = %f\n\n", p_value < ALPHA ? "FAILURE" : "SUCCESS", p_value);
       
   110 	fprintf(results[TEST_APEN], "%f\n", p_value);
       
   111 }