kerneltest/e32utils/nistsecurerng/src/discreteFourierTransform.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          D I S C R E T E  F O U R I E R  T R A N S F O R M  T E S T 
       
    34  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
       
    35 void  __ogg_fdrffti(int n, double* wsave, int* ifac);
       
    36 void  __ogg_fdrfftf(int n, double* X, double* wsave, int* ifac);
       
    37 
       
    38 void
       
    39 DiscreteFourierTransform(int n)
       
    40 {
       
    41 	double     p_value, upperBound, percentile, N_l, N_o, d;
       
    42 	double*    m = NULL;
       
    43 	double*    X = NULL;
       
    44 	double*    wsave = NULL;
       
    45 	int*       ifac = NULL;
       
    46 	int		i, count;
       
    47 	
       
    48 	if ( ((X = (double*) calloc(n,sizeof(double))) == NULL) ||
       
    49 		 ((wsave = (double *)calloc(2*n+15,sizeof(double))) == NULL) ||
       
    50 		 ((ifac = (int *)calloc(15,sizeof(int))) == NULL) ||
       
    51 		 ((m = (double*)calloc(n/2+1, sizeof(double))) == NULL) ) {
       
    52 			fprintf(stats[7],"\t\tUnable to allocate working arrays for the DFT.\n");
       
    53 			if( X != NULL )
       
    54 				free(X);
       
    55 			if( wsave != NULL )
       
    56 				free(wsave);
       
    57 			if( ifac != NULL )
       
    58 				free(ifac);
       
    59 			if( m != NULL )
       
    60 				free(m);
       
    61 			return;
       
    62 	}
       
    63 	for ( i=0; i<n; i++ )
       
    64 		X[i] = 2*(int)epsilon[i] - 1;
       
    65 	
       
    66 	__ogg_fdrffti(n, wsave, ifac);		/* INITIALIZE WORK ARRAYS */
       
    67 	__ogg_fdrfftf(n, X, wsave, ifac);	/* APPLY FORWARD FFT */
       
    68 	
       
    69 	m[0] = sqrt(X[0]*X[0]);	    /* COMPUTE MAGNITUDE */
       
    70 	
       
    71 	for ( i=0; i<n/2; i++ ) {	   	    /* DISPLAY FOURIER POINTS */
       
    72 		m[i+1] = sqrt(pow(X[2*i+1],2)+pow(X[2*i+2],2)); 
       
    73 	}
       
    74 	count = 0;				       /* CONFIDENCE INTERVAL */
       
    75 	upperBound = sqrt(2.995732274*n);
       
    76 	for ( i=0; i<n/2; i++ )
       
    77 		if ( m[i] < upperBound )
       
    78 			count++;
       
    79 	percentile = (double)count/(n/2)*100;
       
    80 	N_l = (double) count;       /* number of peaks less than h = sqrt(3*n) */
       
    81 	N_o = (double) 0.95*n/2.0;
       
    82 	d = (N_l - N_o)/sqrt(n/2.0*0.95*0.05);
       
    83 	p_value = erfc(fabs(d)/sqrt(2.0));
       
    84 
       
    85 	fprintf(stats[TEST_FFT], "\t\t\t\tFFT TEST\n");
       
    86 	fprintf(stats[TEST_FFT], "\t\t-------------------------------------------\n");
       
    87 	fprintf(stats[TEST_FFT], "\t\tCOMPUTATIONAL INFORMATION:\n");
       
    88 	fprintf(stats[TEST_FFT], "\t\t-------------------------------------------\n");
       
    89 	fprintf(stats[TEST_FFT], "\t\t(a) Percentile = %f\n", percentile);
       
    90 	fprintf(stats[TEST_FFT], "\t\t(b) N_l        = %f\n", N_l);
       
    91 	fprintf(stats[TEST_FFT], "\t\t(c) N_o        = %f\n", N_o);
       
    92 	fprintf(stats[TEST_FFT], "\t\t(d) d          = %f\n", d);
       
    93 	fprintf(stats[TEST_FFT], "\t\t-------------------------------------------\n");
       
    94 
       
    95 	fprintf(stats[TEST_FFT], "%s\t\tp_value = %f\n\n", p_value < ALPHA ? "FAILURE" : "SUCCESS", p_value);
       
    96 	fprintf(results[TEST_FFT], "%f\n", p_value);
       
    97 
       
    98 	free(X);
       
    99 	free(wsave);
       
   100 	free(ifac);
       
   101 	free(m);
       
   102 }