double get_chisqr_p(unsigned * first, unsigned * second) { double CriticalValue = 0.0; double XSqr; unsigned int NumberOfEffectiveDatapoints = 0; for (unsigned index = 0; index < 100; index++) { if (first[index] + second[index] > 0.0) NumberOfEffectiveDatapoints++; XSqr = first[index] - second[index]; if (first[index] > 0.0) CriticalValue += (XSqr * XSqr) / first[index]; } /* for (unsigned index = 0; index < 100; index++) { std::cout << first[index] << " "; } std::cout << std::endl; for (unsigned index = 0; index < 100; index++) { std::cout << second[index] << " "; } std::cout << std::endl; */ if (NumberOfEffectiveDatapoints < 2) NumberOfEffectiveDatapoints = 2; double returnP = chisqr(NumberOfEffectiveDatapoints, CriticalValue); std::cout << CriticalValue << "\t" << NumberOfEffectiveDatapoints << "\t" << returnP << std::endl; return returnP; }
float estimate_offpulse_redchi2(double *inprofs, foldstats *stats, int numparts, int numsubbands, int proflen, int numtrials, double dofeff) // Randomly offset each pulse profile in a .pfd data square or cube // and combine them to estimate a "true" off-pulse level. Do this // numtrials times in order to improve the statistics. Return the // inverse of the average of the off-pulse reduced-chi^2 (i.e. the // correction factor). dofeff is the effective number of DOF as // returned by DOF_corr(). { int ii, jj, kk, offset, trialnum, phsindex, statindex; float *chis; double chi_avg, chi_var, redchi; double prof_avg, prof_var, *prof_ptr, *sumprof; sumprof = gen_dvect(proflen); chis = gen_fvect(numtrials); for (trialnum = 0; trialnum < numtrials; trialnum++) { // Initialize the summed profile for (ii = 0; ii < proflen; ii++) sumprof[ii] = 0.0; prof_avg = 0.0; prof_var = 0.0; prof_ptr = inprofs; for (ii = 0; ii < numparts; ii++) { // parts for (jj = 0; jj < numsubbands; jj++) { // subbands statindex = ii * numsubbands + jj; offset = random() % proflen; phsindex = 0; for (kk = offset; kk < proflen; kk++, phsindex++) // phases sumprof[phsindex] += prof_ptr[kk]; for (kk = 0; kk < offset; kk++, phsindex++) // phases sumprof[phsindex] += prof_ptr[kk]; prof_ptr += proflen; prof_avg += stats[statindex].prof_avg; prof_var += stats[statindex].prof_var; } } /* Calculate the current chi-squared */ redchi = chisqr(sumprof, proflen, prof_avg, prof_var) / dofeff; chis[trialnum] = (float) redchi; } avg_var(chis, numtrials, &chi_avg, &chi_var); vect_free(chis); vect_free(sumprof); return 1.0/chi_avg; }
double X2BetweenTwo(unsigned int * FirstOriginal, unsigned int * SecondOriginal, unsigned int dispots) { // default dispots = 100 // delare double *ExpFirst; double *ExpSecond; double *SumBoth; ExpFirst = new double [dispots + 1]; ExpSecond = new double [dispots + 1]; SumBoth = new double [dispots + 1]; double SumFirst = 0.0; double SumSecond = 0.0; double SumTotal = 0.0; // calculation for (unsigned i = 0; i < dispots; i++) { //Firstcount[i] = 0; //Secondcount[i] = 0; SumBoth[i] = FirstOriginal[i] + SecondOriginal[i]; SumFirst += FirstOriginal[i]; SumSecond += SecondOriginal[i]; } SumTotal = SumFirst + SumSecond; for (unsigned i = 0; i < dispots; i++) { ExpFirst[i] = SumBoth[i] * SumFirst / SumTotal; ExpSecond[i] = SumBoth[i] * SumSecond / SumTotal; } double result = 0.0; unsigned Degree = 0; for (unsigned i = 0; i < dispots; i++) { if (FirstOriginal[i] + SecondOriginal[i] > 0.0) Degree++; if (ExpFirst[i]) { result += (FirstOriginal[i] - ExpFirst[i]) * (FirstOriginal[i] - ExpFirst[i]) / ExpFirst[i]; } if (ExpSecond[i]) { result += (SecondOriginal[i] - ExpSecond[i]) * (SecondOriginal[i] - ExpSecond[i]) / ExpSecond[i]; } } // release mem double PValue; if (Degree == 1) PValue = 1.0; else { Degree--; PValue = chisqr(Degree, result); } if (PValue < 0) PValue = PValue * (-1); /* if (PValue < 0.01) { for (unsigned index = 0; index < 30; index++) { std::cout << FirstOriginal[index] << " "; } std::cout << std::endl; for (unsigned index = 0; index < 30; index++) { std::cout << SecondOriginal[index] << " "; } std::cout << std::endl; //std::cout << result << "\t" << Degree << "\t" << PValue << std::endl; for (unsigned index = 0; index < 30; index++) { std::cout << ExpFirst[index] << " "; } std::cout << std::endl; for (unsigned index = 0; index < 30; index++) { std::cout << ExpSecond[index] << " "; } std::cout << std::endl; std::cout << result << "\t" << Degree << "\t" << PValue << std::endl; } */ delete [] ExpFirst; delete [] ExpSecond; delete [] SumBoth; return PValue; }
int main() { double Y_freq = 0.0; double Y_serial_two = 0.0; double Y_serial_three = 0.0; // p-value parameters double expected_freq = n_input/(2*4096); //50% chance of occurring for each sample double expected_serial_two = n_input/(2*4*2048); //25% chance of occurrence every 2 samples double expected_serial_three = n_input/(3*8*2048); //12.5% chance of occurrence every 3 samples init_platform(); //Setup the freq serial hw platform int status = XFreq_serial_init(&FreqSerial); if (status != XST_SUCCESS) { print("Freq Serial peripheral setup failed\n\r"); exit(-1); } //----------------// //---START TEST---// //----------------// //set the input parameters of the HLS block XFreq_serial_Set_seed_V(&FreqSerial, seed_input); XFreq_serial_Set_n_V(&FreqSerial, n_input); XFreq_serial_Set_offset_V(&FreqSerial, offset_input); //check that suite is ready if (XFreq_serial_IsReady(&FreqSerial)){ printf("--------------------------------,--------------------------------\n"); printf("Sample Size, %llu \n\r", XFreq_serial_Get_n_V(&FreqSerial)); printf("Seed, %lu \n\r", XFreq_serial_Get_seed_V(&FreqSerial)); printf("Offset, %lu \n\r", XFreq_serial_Get_offset_V(&FreqSerial)); printf("--------------------------------,--------------------------------\n"); printf("Results ETA, %.2f, seconds\n\r",(double)(n_input/(COUNTS_PER_SECOND*0.3))); }else{ print("!!!WARNING: Freq Serial peripheral is not ready! Exiting...\n\r"); exit(-1); } // Simple non-interrupt driven test XFreq_serial_Start(&FreqSerial); //start time measurement XTime_GetTime(&tStart); do{ //get freq test hardware specification information freq_base_address_hw = XFreq_serial_Get_freqStream_V_BaseAddress(&FreqSerial); freq_high_address_hw = XFreq_serial_Get_freqStream_V_HighAddress(&FreqSerial); freq_total_bytes_hw = XFreq_serial_Get_freqStream_V_TotalBytes(&FreqSerial); freq_bit_width_hw = XFreq_serial_Get_freqStream_V_BitWidth(&FreqSerial); freq_depth_hw = XFreq_serial_Get_freqStream_V_Depth(&FreqSerial); //get serial two-tuple test hardware specification information serial_2_base_address_hw = XFreq_serial_Get_serialTwoStream_V_BaseAddress(&FreqSerial); serial_2_high_address_hw = XFreq_serial_Get_serialTwoStream_V_HighAddress(&FreqSerial); serial_2_total_bytes_hw = XFreq_serial_Get_serialTwoStream_V_TotalBytes(&FreqSerial); serial_2_bit_width_hw = XFreq_serial_Get_serialTwoStream_V_BitWidth(&FreqSerial); serial_2_depth_hw = XFreq_serial_Get_serialTwoStream_V_Depth(&FreqSerial); //get serial three-tuple test hardware specification information serial_3_base_address_hw = XFreq_serial_Get_serialThreeStream_V_BaseAddress(&FreqSerial); serial_3_high_address_hw = XFreq_serial_Get_serialThreeStream_V_HighAddress(&FreqSerial); serial_3_total_bytes_hw = XFreq_serial_Get_serialThreeStream_V_TotalBytes(&FreqSerial); serial_3_bit_width_hw = XFreq_serial_Get_serialThreeStream_V_BitWidth(&FreqSerial); serial_3_depth_hw = XFreq_serial_Get_serialThreeStream_V_Depth(&FreqSerial); //read freq test values freqchardebug_value = XFreq_serial_Read_freqStream_V_Bytes(&FreqSerial, 0, charDataAddress, 128); freqintdebug_value = XFreq_serial_Read_freqStream_V_Words(&FreqSerial, 0, intDataAddress, 32); //read serial two-tuple test values serialtwochardebug_value = XFreq_serial_Read_serialTwoStream_V_Bytes(&FreqSerial, 0, charDataAddress+128, 512); serialtwointdebug_value = XFreq_serial_Read_serialTwoStream_V_Words(&FreqSerial, 0, intDataAddress+32, 128); //read serial three-tuple test values serialthreechardebug_value = XFreq_serial_Read_serialThreeStream_V_Bytes(&FreqSerial, 0, charDataAddress+640, 512); serialthreeintdebug_value = XFreq_serial_Read_serialThreeStream_V_Words(&FreqSerial, 0, intDataAddress+160, 128); }while(!XFreq_serial_IsReady(&FreqSerial)); //get time measurement XTime_GetTime(&tEnd); //output time measurements printf("Test completed in ,%.2f, us (,%.2f, seconds).\n", 1.0 * (tEnd - tStart) / (COUNTS_PER_SECOND/1000000), 1.0 * (tEnd - tStart) / (COUNTS_PER_SECOND)); //Xilinx time functions measure time in PS clock cycles, so conversion to PL clock rate is required printf("PL clock cycles per sample:, %.2f \n", (1.0)*((2*(tEnd - tStart))/((n_input*6.66666687)))); if(debugValid){ //number of chi-squared categories printf("Freq Char Debug Value: %d\n\r", freqchardebug_value); printf("Serial 2 Char Debug Value: %d\n\r", serialtwochardebug_value); printf("Serial 3 Char Debug Value: %d\n\r", serialthreechardebug_value); printf("Freq Int Debug Value: %d\n\r", freqintdebug_value); printf("Serial 2 Int Debug Value: %d\n\r", serialtwointdebug_value); printf("Serial 3 Int Debug Value: %d\n\r", serialthreeintdebug_value); //hardware specification information printf("Freq Base Address: %d\n\r", freq_base_address_hw); printf("Freq High Address: %d\n\r", freq_high_address_hw); printf("Freq Total Bytes: %d\n\r", freq_total_bytes_hw); printf("Freq Bit Width: %d\n\r", freq_bit_width_hw); printf("Freq Depth: %d\n\r", freq_depth_hw); printf("Serial 2 Base Address: %d\n\r", serial_2_base_address_hw); printf("Serial 2 High Address: %d\n\r", serial_2_high_address_hw); printf("Serial 2 Total Bytes: %d\n\r", serial_2_total_bytes_hw); printf("Serial 2 Bit Width: %d\n\r", serial_2_bit_width_hw); printf("Serial 2 Depth: %d\n\r", serial_2_depth_hw); printf("Serial 3 Base Address: %d\n\r", serial_3_base_address_hw); printf("Serial 3 High Address: %d\n\r", serial_3_high_address_hw); printf("Serial 3 Total Bytes: %d\n\r", serial_3_total_bytes_hw); printf("Serial 3 Bit Width: %d\n\r", serial_3_bit_width_hw); printf("Serial 3 Depth: %d\n\r", serial_3_depth_hw); } cleanup_platform(); //read previous test values //read freq test values freqchardebug_value = XFreq_serial_Read_freqStream_V_Bytes(&FreqSerial, 0, charDataAddress, 128); freqintdebug_value = XFreq_serial_Read_freqStream_V_Words(&FreqSerial, 0, intDataAddress, 32); //read serial two-tuple test values serialtwochardebug_value = XFreq_serial_Read_serialTwoStream_V_Bytes(&FreqSerial, 0, charDataAddress+128, 512); serialtwointdebug_value = XFreq_serial_Read_serialTwoStream_V_Words(&FreqSerial, 0, intDataAddress+32, 128); //read serial three-tuple test values serialthreechardebug_value = XFreq_serial_Read_serialThreeStream_V_Bytes(&FreqSerial, 0, charDataAddress+640, 512); serialthreeintdebug_value = XFreq_serial_Read_serialThreeStream_V_Words(&FreqSerial, 0, intDataAddress+160, 128); //output previous test parameters and results //Histogram Decompression //serial two-tuple test decompression for (int i = 0; i<96; i++){ serial_two_decompressed[i+i/3] = (unsigned long int) *(intDataAddress + 32 + i); } for (int i = 0; i<32; i++){ serial_two_decompressed[i * 4 + 3] = (expected_serial_two * 4) - (serial_two_decompressed[i * 4]) - (serial_two_decompressed[i * 4 + 1]) - (serial_two_decompressed[i * 4 + 2]); } //serial three-tuple test decompression for (int i = 0; i < 112; i++) { serial_three_decompressed[i + (i / 7)] = (unsigned long int) *(intDataAddress + 160 + i); } for (int i = 0; i < 16; i++) { serial_three_decompressed[i * 8 + 7] = (expected_serial_three * 8) - (serial_three_decompressed[i * 8]) - (serial_three_decompressed[i * 8 + 1]) - (serial_three_decompressed[i * 8 + 2]) - (serial_three_decompressed[i * 8 + 3]) - (serial_three_decompressed[i * 8 + 4]) - (serial_three_decompressed[i * 8 + 5]) - (serial_three_decompressed[i * 8 + 6]); } //report previous test histograms printf("--------------------------------,--------------------------------\n"); printf("Frequency Histogram,"); for (int i = 0; i<32; i++){ printf("%lu,", (unsigned long int) *(intDataAddress + i)); } printf("\n"); printf("Serial 2-Tuple Histogram,"); for (int i = 0; i<128; i++){ printf("%lu,", serial_two_decompressed[i]); } printf("\n"); printf("Serial 3-Tuple Histogram,"); for (int i = 0; i<128; i++){ printf("%lu,", serial_three_decompressed[i]); } printf("\n"); printf("--------------------------------,--------------------------------\n"); printf("Test, Y\n"); printf("--------------------------------,--------------------------------\n"); // Y-value computations for(int i=0; i<32; i++){ Y_freq += (((unsigned long int) *(intDataAddress + i)-expected_freq)*((unsigned long int) *(intDataAddress + i)-expected_freq))/expected_freq; } for(int i=0; i<128; i++){ Y_serial_two += ((serial_two_decompressed[i]-expected_serial_two)*(serial_two_decompressed[i]-expected_serial_two))/expected_serial_two; } for(int i=0; i<128; i++){ Y_serial_three += ((serial_three_decompressed[i]-expected_serial_three)*(serial_three_decompressed[i]-expected_serial_three))/expected_serial_three; } // p-value computations p_val_freq = chisqr(32, Y_freq); //degrees of freedom for frequency: 32-1 p_val_serial_two = chisqr(96, Y_serial_two); //degrees of freedom for serial: 128-1 p_val_serial_three = chisqr(112, Y_serial_three); //degrees of freedom for serial: 128-1 printf("--------------------------------,--------------------------------\n"); printf("Test, p-value\n"); printf("--------------------------------,--------------------------------\n"); printf("Frequency Test, %g\n", p_val_freq); printf("Serial 2-Tuple Test, %g\n", p_val_serial_two); printf("Serial 3-Tuple Test, %g\n", p_val_serial_three); printf("--------------------------------,--------------------------------\n"); return 0; }