#include <math.h> #include <stdio.h> /* speesy_Entropy_f - ( ) speesy_GetBasicTone - speesy_GetAllCoincidence - speesy_GetBasicToneEntropy - speesy_SetFreqMeander - speesy_Meander - */ #define SPEESY_SAMPLE_FREQ8 8000 #define SPEESY_SAMPLE_FREQ16 16000 #define SPEESY_SAMPLE_FREQ SPEESY_SAMPLE_FREQ16 #define SPEESY_MEANDER_MAX 100 #define FOR_FLOAT_EPSILON 1.19209e-007 static float speesy_all_coincidence = 0; // static float speesy_fliter600_Entropy = 1.0; static float speesy_meander_period = 0.01;// float speesy_Entropy_f(const float* source, unsigned long int start, unsigned long int finish, unsigned char binsCount, float minRaw, float maxRaw) { float entropy = 0; float binSize = fabs(maxRaw - minRaw) / (float)binsCount; //FOR_FLOAT_EPSILON == numeric_limits<float>::epsilon() if (fabs(binSize) < FOR_FLOAT_EPSILON) { return 0; } //float* p = new float[binsCount]; float p[256]; for (unsigned char i = 0; i < binsCount; i++) { p[i] = 0.0; } // Calculate probabilities unsigned char index; float value; for (unsigned long int i = start; i <= finish; i++) { value = source[i]; //for 8-bit data index = floor((value - minRaw) / binSize); if (index >= binsCount) { index = binsCount - 1; } p[index] += 1.0; } unsigned char Normalize_size = finish - start + 1; for (unsigned char i = 0; i < binsCount; i++) { p[i] /= Normalize_size; } for (unsigned char i = 0; i < binsCount; i++) { if (p[i] > FOR_FLOAT_EPSILON) { entropy += p[i] * log2(p[i]); } } entropy = -entropy; return entropy; } float speesy_GetBasicTone(float source) { static float matrix[6][6] ={0,0,0,0,0,0, 0,0,0,0,0,0, 0,0,0,0,0,0, 0,0,0,0,0,0, 0,0,0,0,0,0, 0,0,0,0,0,0}; const float max_detector_p = 0.0255; const float min_detector_p = 0.0016; static float detector_p[6] = {min_detector_p}; static float detector_old_p[6] = {min_detector_p}; static float detector_t[6] = {0}; static float detector_tay[6] = {0.016}; static float detector_t_end = 0; //static float detector_beta[6] = {0}; static float detector_value[6] = {0}; float f_data = 0; //char detector_p_t0_f = 0; static float sig_old = 0; static char sig_p = 0; static char sig_m = 0; static unsigned short tim160 = 0; float detector_m1; float detector_m2; float detector_m3; float detector_m4; float detector_m5; float detector_m6; int detector_data = 0; static float detector_old_m1; static float detector_old_m4 = 0; static char speesy_tone_i = 0; static char speesy_tone_x = 0; static char speesy_tone_y = 0; static char speesy_tone_inter = 0; //char n_coincidence[4] ={0}; char n_coincidence_matrix[6][4] ={0}; static float out_t; #if SPEESY_SAMPLE_FREQ == SPEESY_SAMPLE_FREQ8 static float source_data[16] = {0}; const int max_source_data = 16; const float p_conts = 0.000125; #endif // SPEESY_SAMPLE_FREQ #if SPEESY_SAMPLE_FREQ == SPEESY_SAMPLE_FREQ16 static float source_data[32] = {0}; const int max_source_data = 32; const float p_conts = 0.0000625; #endif // SPEESY_SAMPLE_FREQ /*************Filter 600 Hz**********************/ #if SPEESY_SAMPLE_FREQ == SPEESY_SAMPLE_FREQ8 const float filter600_ACoef1[5] = { 0.00161978575856732190, 0.00647914303426928760, 0.00971871455140393280, 0.00647914303426928760, 0.00161978575856732190 }; const float filter600_BCoef1[4] = { -2.63228606617055720000, 2.68865140959361830000, -1.25580694576241330000, 0.22536111137571077000 }; #endif // SPEESY_SAMPLE_FREQ #if SPEESY_SAMPLE_FREQ == SPEESY_SAMPLE_FREQ16 const float filter600_ACoef1[5] = { 0.00013538805748957640, 0.00054155222995830559, 0.00081232834493745844, 0.00054155222995830559, 0.00013538805748957640 }; const float filter600_BCoef1[4] = { -3.29078386336302660000, 4.09122986596582550000, -2.27618508727807440000, 0.47792443748067198000 }; #endif // SPEESY_SAMPLE_FREQ static float filter600_y[5] = {0}; //output samples static float filter600_x[5] = {0}; //input samples static float out_filter600[240] = {0}; short out_i = 0; filter600_x[4] = filter600_x[3]; filter600_y[4] = filter600_y[3]; filter600_x[3] = filter600_x[2]; filter600_y[3] = filter600_y[2]; filter600_x[2] = filter600_x[1]; filter600_y[2] = filter600_y[1]; filter600_x[1] = filter600_x[0]; filter600_y[1] = filter600_y[0]; filter600_x[0] = source; filter600_y[0] = filter600_ACoef1[0] * filter600_x[0]; filter600_y[0] += filter600_ACoef1[1] * filter600_x[1] - filter600_BCoef1[0] * filter600_y[1]; filter600_y[0] += filter600_ACoef1[2] * filter600_x[2] - filter600_BCoef1[1] * filter600_y[2]; filter600_y[0] += filter600_ACoef1[3] * filter600_x[3] - filter600_BCoef1[2] * filter600_y[3]; filter600_y[0] += filter600_ACoef1[4] * filter600_x[4] - filter600_BCoef1[3] * filter600_y[4]; /***************End Filter 600 Hz*********************/ for (out_i = 239;out_i>0; out_i--) { out_filter600[out_i] = out_filter600[out_i - 1]; } out_filter600[0] = filter600_y[0]; if (tim160 < 160) {tim160 = tim160 + 1;} else { tim160 = 0; speesy_fliter600_Entropy = speesy_Entropy_f(out_filter600,0,159,255,-32768,32768); speesy_fliter600_Entropy = speesy_Entropy_f(out_filter600,160,239,255,-32768,32768); } detector_m1 = 0; detector_m2 = 0; detector_m3 = 0; detector_m4 = 0; detector_m5 = 0; detector_m6 = 0; //printf("\nSpeesy max_source_data = %d\n",max_source_data); if (filter600_y[0] >= 0) { if (filter600_y[0] > sig_old) {sig_p = 1;} else { if (sig_p == 1) { sig_p = 0; detector_m1 = filter600_y[0]; if (detector_m1 > detector_old_m1) { detector_m3 = detector_m1 - detector_old_m1; } else detector_m3 = 0; detector_m2 = detector_m1 + detector_old_m4; detector_old_m1 = detector_m1; } } sig_old = filter600_y[0]; } else { if ((-filter600_y[0]) > sig_old) {sig_m = 1;} else { if (sig_m == 1) { sig_m = 0; detector_m4 = -filter600_y[0]; if (detector_m4 > detector_old_m4) { detector_m6 = detector_m4 - detector_old_m4; } else detector_m6 = 0; detector_m5 = detector_m4 + detector_old_m1; detector_old_m4 = detector_m4; } } sig_old = -filter600_y[0]; } /*****************************************************/ //6 if (detector_t[5] > detector_tay[5]) { // tay f_data = detector_value[5]*(exp(-(detector_t[5] - detector_tay[5])/(detector_p[5]/0.695))); // if (detector_m6 > f_data) { // detector_value[5] = detector_m6; // detector_p[5] = (detector_t[5] + detector_p[5])/2; // if (detector_p[5] > max_detector_p) detector_p[5] = max_detector_p; if (detector_p[5] < min_detector_p) detector_p[5] = min_detector_p; detector_tay[5] = 0.4*detector_p[5]; // detector_t[5] = 0; //detector_p_t0_f = 1; matrix[5][2] = matrix[5][1]; matrix[5][1] = matrix[5][0]; matrix[5][0] = detector_p[5]; } else { detector_t[5] = detector_t[5] + p_conts; // } } else { detector_t[5] = detector_t[5] + p_conts; } //5 if (detector_t[4] > detector_tay[4]) { // tay f_data = detector_value[4]*(exp(-(detector_t[4] - detector_tay[4])/(detector_p[4]/0.695))); // if (detector_m5 > f_data) { // detector_value[4] = detector_m5; // detector_p[4] = (detector_t[4] + detector_p[4])/2; // if (detector_p[4] > max_detector_p) detector_p[4] = max_detector_p; if (detector_p[4] < min_detector_p) detector_p[4] = min_detector_p; detector_tay[4] = 0.4*detector_p[4]; // detector_t[4] = 0; //detector_p_t0_f = 1; matrix[4][2] = matrix[4][1]; matrix[4][1] = matrix[4][0]; matrix[4][0] = detector_p[4]; } else { detector_t[4] = detector_t[4] + p_conts; // } } else { detector_t[4] = detector_t[4] + p_conts; } //4 if (detector_t[3] > detector_tay[3]) { // tay f_data = detector_value[3]*(exp(-(detector_t[3] - detector_tay[3])/(detector_p[3]/0.695))); // if (detector_m4 > f_data) { // detector_value[3] = detector_m4; // detector_p[3] = (detector_t[3] + detector_p[3])/2; // if (detector_p[3] > max_detector_p) detector_p[3] = max_detector_p; if (detector_p[3] < min_detector_p) detector_p[3] = min_detector_p; detector_tay[3] = 0.4*detector_p[3]; // detector_t[3] = 0; //detector_p_t0_f = 1; matrix[3][2] = matrix[3][1]; matrix[3][1] = matrix[3][0]; matrix[3][0] = detector_p[3]; } else { detector_t[3] = detector_t[3] + p_conts; // } } else { detector_t[3] = detector_t[3] + p_conts; } //3 if (detector_t[2] > detector_tay[2]) { // tay f_data = detector_value[2]*(exp(-(detector_t[2] - detector_tay[2])/(detector_p[2]/0.695))); // if (detector_m3 > f_data) { // detector_value[2] = detector_m3; // detector_p[2] = (detector_t[2] + detector_p[2])/2; // if (detector_p[2] > max_detector_p) detector_p[2] = max_detector_p; if (detector_p[2] < min_detector_p) detector_p[2] = min_detector_p; detector_tay[2] = 0.4*detector_p[2]; // detector_t[2] = 0; //detector_p_t0_f = 1; matrix[2][2] = matrix[2][1]; matrix[2][1] = matrix[2][0]; matrix[2][0] = detector_p[2]; } else { detector_t[2] = detector_t[2] + p_conts; // } } else { detector_t[2] = detector_t[2] + p_conts; } //2 if (detector_t[1] > detector_tay[1]) { // tay f_data = detector_value[1]*(exp(-(detector_t[1] - detector_tay[1])/(detector_p[1]/0.695))); // if (detector_m2 > f_data) { // detector_value[1] = detector_m2; // detector_p[1] = (detector_t[1] + detector_p[1])/2; // if (detector_p[1] > max_detector_p) detector_p[1] = max_detector_p; if (detector_p[1] < min_detector_p) detector_p[1] = min_detector_p; detector_tay[1] = 0.4*detector_p[1]; // detector_t[1] = 0; //detector_p_t0_f = 1; matrix[1][2] = matrix[1][1]; matrix[1][1] = matrix[1][0]; matrix[1][0] = detector_p[1]; } else { detector_t[1] = detector_t[1] + p_conts; // } } else { detector_t[1] = detector_t[1] + p_conts; } //1 if (detector_t[0] > detector_tay[0]) { // tay f_data = detector_value[0]*(exp(-(detector_t[0] - detector_tay[0])/(detector_p[0]/0.695))); // if (detector_m1 > f_data) { // detector_value[0] = detector_m1; // detector_p[0] = (detector_t[0] + detector_p[0])/2; // if (detector_p[0] > max_detector_p) detector_p[0] = max_detector_p; if (detector_p[0] < min_detector_p) detector_p[0] = min_detector_p; detector_tay[0] = 0.4*detector_p[0]; // detector_t[0] = 0; //detector_p_t0_f = 1; matrix[0][2] = matrix[0][1]; matrix[0][1] = matrix[0][0]; matrix[0][0] = detector_p[0]; } else { detector_t[0] = detector_t[0] + p_conts; // } } else { detector_t[0] = detector_t[0] + p_conts; } /************************************************************************/ if (detector_t_end == 0) { for (speesy_tone_x = 0;speesy_tone_x<6;speesy_tone_x++) { matrix[speesy_tone_x][3] = matrix[speesy_tone_x][0] + matrix[speesy_tone_x][1]; matrix[speesy_tone_x][4] = matrix[speesy_tone_x][1] + matrix[speesy_tone_x][2]; matrix[speesy_tone_x][5] = matrix[speesy_tone_x][1] + matrix[speesy_tone_x][2] + matrix[speesy_tone_x][0]; } for (speesy_tone_inter = 0; speesy_tone_inter<4;speesy_tone_inter++) { n_coincidence_matrix[0][speesy_tone_inter] = 0; n_coincidence_matrix[1][speesy_tone_inter] = 0; n_coincidence_matrix[2][speesy_tone_inter] = 0; n_coincidence_matrix[3][speesy_tone_inter] = 0; n_coincidence_matrix[4][speesy_tone_inter] = 0; n_coincidence_matrix[5][speesy_tone_inter] = 0; for (speesy_tone_x = 0;speesy_tone_x<6;speesy_tone_x++) { for (speesy_tone_y = 0;speesy_tone_y<6;speesy_tone_y++) { //printf("\nValue_matrix %f",matrix[speesy_tone_x][speesy_tone_y]); //printf("\nmatrix %f",(float)matrix[speesy_tone_x][speesy_tone_y]); for (speesy_tone_i = 0;speesy_tone_i<6;speesy_tone_i++) { //printf("\nmatrix %f",(float)matrix[speesy_tone_x][speesy_tone_y]); //printf("\nspeesy_tone_i %d",speesy_tone_i); //printf("\nsr matrix %f",(float)matrix[speesy_tone_i][0]); if (((speesy_tone_y != 0)&(speesy_tone_x!=speesy_tone_i))|(speesy_tone_y > 0)) if ((matrix[speesy_tone_i][0] >= 0.0016)&(matrix[speesy_tone_i][0] <= 0.0031)) { f_data = 0.0001*((float)speesy_tone_inter + 1.0); if ((((float)matrix[speesy_tone_i][0] + (float)f_data) >= (float)matrix[speesy_tone_x][speesy_tone_y])& (((float)matrix[speesy_tone_i][0] - (float)f_data) <= (float)matrix[speesy_tone_x][speesy_tone_y])) { n_coincidence_matrix[speesy_tone_i][speesy_tone_inter] = n_coincidence_matrix[speesy_tone_i][speesy_tone_inter] + 1; //printf("\ncoincidence 0.0016 - 0.0031"); } } else if ((matrix[speesy_tone_i][0] > 0.0031)&(matrix[speesy_tone_i][0] <= 0.0063)) { f_data = 0.0002*((float)speesy_tone_inter + 1.0); if ((((float)matrix[speesy_tone_i][0] + (float)f_data) >= (float)matrix[speesy_tone_x][speesy_tone_y])& (((float)matrix[speesy_tone_i][0] - (float)f_data) <= (float)matrix[speesy_tone_x][speesy_tone_y])) { n_coincidence_matrix[speesy_tone_i][speesy_tone_inter] = n_coincidence_matrix[speesy_tone_i][speesy_tone_inter] + 1; //printf("\ncoincidence 0.0031 - 0.0063"); } } else if ((matrix[speesy_tone_i][0] > 0.0063)&(matrix[speesy_tone_i][0] <= 0.0127)) { f_data = 0.0004*((float)speesy_tone_inter + 1.0); if ((((float)matrix[speesy_tone_i][0] + (float)f_data) >= (float)matrix[speesy_tone_x][speesy_tone_y])& (((float)matrix[speesy_tone_i][0] - (float)f_data) <= (float)matrix[speesy_tone_x][speesy_tone_y])) { n_coincidence_matrix[speesy_tone_i][speesy_tone_inter] = n_coincidence_matrix[speesy_tone_i][speesy_tone_inter] + 1; //printf("\ncoincidence 0.0063 - 0.0127"); } } else if ((matrix[speesy_tone_i][0] > 0.0127)&(matrix[speesy_tone_i][0] <= 0.0255)) { f_data = 0.0008*((float)speesy_tone_inter + 1.0); if (((matrix[speesy_tone_i][0] + f_data) >= matrix[speesy_tone_x][speesy_tone_y])& ((matrix[speesy_tone_i][0] - f_data) <= matrix[speesy_tone_x][speesy_tone_y])) { n_coincidence_matrix[speesy_tone_i][speesy_tone_inter] = n_coincidence_matrix[speesy_tone_i][speesy_tone_inter] + 1; //printf("\ncoincidence 0.0127 - 0.0255"); } } else { //printf("\nNO coincidence"); } //printf("\ncoincidence %d",n_coincidence_matrix[speesy_tone_i][speesy_tone_inter]); } //end for } //end for for (speesy_tone_inter = 0; speesy_tone_inter<4;speesy_tone_inter++) { for (speesy_tone_i = 0;speesy_tone_i<6;speesy_tone_i++) { //printf("\nDo mat_ton %d",(int)n_coincidence_matrix[speesy_tone_i][speesy_tone_inter]); } } if (speesy_tone_inter == 0) { for (speesy_tone_i = 0;speesy_tone_i<6;speesy_tone_i++) { if (n_coincidence_matrix[speesy_tone_i][speesy_tone_inter] >= 1) { n_coincidence_matrix[speesy_tone_i][speesy_tone_inter] -= 1; } else { n_coincidence_matrix[speesy_tone_i][speesy_tone_inter] = 0; } } } else if (speesy_tone_inter == 1) { for (speesy_tone_i = 0;speesy_tone_i<6;speesy_tone_i++) { if (n_coincidence_matrix[speesy_tone_i][speesy_tone_inter] >= 2) { n_coincidence_matrix[speesy_tone_i][speesy_tone_inter] -= 2; } else { n_coincidence_matrix[speesy_tone_i][speesy_tone_inter] = 0; } } } else if (speesy_tone_inter == 2) { for (speesy_tone_i = 0;speesy_tone_i<6;speesy_tone_i++) { if (n_coincidence_matrix[speesy_tone_i][speesy_tone_inter] >= 5) { n_coincidence_matrix[speesy_tone_i][speesy_tone_inter] -= 5; } else { n_coincidence_matrix[speesy_tone_i][speesy_tone_inter] = 0; } } } else if (speesy_tone_inter == 3) { for (speesy_tone_i = 0;speesy_tone_i<6;speesy_tone_i++) { if (n_coincidence_matrix[speesy_tone_i][speesy_tone_inter] >= 7) { n_coincidence_matrix[speesy_tone_i][speesy_tone_inter] -= 7; } else { n_coincidence_matrix[speesy_tone_i][speesy_tone_inter] = 0; } } } } //end for } //end for () for inter out_t = 0; speesy_tone_x = 0; for (speesy_tone_inter = 0; speesy_tone_inter<4;speesy_tone_inter++) { for (speesy_tone_i = 0;speesy_tone_i<6;speesy_tone_i++) { //printf("\n mat_ton %d",(int)n_coincidence_matrix[speesy_tone_i][speesy_tone_inter]); if (speesy_tone_x < n_coincidence_matrix[speesy_tone_i][speesy_tone_inter]) { speesy_tone_x = n_coincidence_matrix[speesy_tone_i][speesy_tone_inter]; out_t = matrix[speesy_tone_i][0]; } } } speesy_all_coincidence = speesy_tone_x; } // end if detector_t_end = detector_t_end + p_conts; if (detector_t_end > 0.01) detector_t_end = 0; return out_t; } float speesy_GetAllCoincidence(void) { return speesy_all_coincidence; } float speesy_GetBasicToneEntropy(void) { return speesy_fliter600_Entropy; } void speesy_SetFreqMeander(float freq) { if (freq > 0) { speesy_meander_period = (1.0/freq)/2; } } signed char speesy_Meander(void) { static float tim = 0; static signed char out = SPEESY_MEANDER_MAX; if (tim < speesy_meander_period) { tim = tim + 0.0000625; } else { tim = 0; out = -out; } return out; }