00001 00024 /* 00025 * Copyright (c) 1991-2006 Kawahara Lab., Kyoto University 00026 * Copyright (c) 2000-2005 Shikano Lab., Nara Institute of Science and Technology 00027 * Copyright (c) 2005-2006 Julius project team, Nagoya Institute of Technology 00028 * All rights reserved 00029 */ 00030 00031 /* Sat Feb 19 13:48:00 JST 1994 */ 00032 /* Kawahara 1986 */ 00033 /* Munetsugu 1991 */ 00034 /* shinohara 1993 */ 00035 /* mikik 1993 */ 00036 /* ri 1997 for cycle buffer */ 00037 00038 #include <sent/stddefs.h> 00039 00040 #define UNDEF 2 00041 #define POSITIVE 1 00042 #define NEGATIVE -1 00043 00044 static int trigger; 00045 static int length; 00046 static int offset; 00047 static int *is_zc; 00048 static SP16 *data; 00049 static int zero_cross; 00050 static int sign; 00051 static int is_trig; 00052 static int top; 00053 static int valid_len; 00054 00062 void 00063 init_count_zc_e(int c_trigger, int c_length, int c_offset) 00064 { 00065 int i; 00066 00067 trigger = c_trigger; 00068 length = c_length; 00069 offset = c_offset; 00070 00071 zero_cross = 0; 00072 is_trig = FALSE; 00073 sign = POSITIVE; 00074 top = 0; 00075 valid_len = 0; 00076 00077 /* data spool for header-margin */ 00078 data = (SP16 *)mymalloc(length * sizeof(SP16)); 00079 /* zero-cross location */ 00080 is_zc = (int *)mymalloc(length * sizeof(int)); 00081 for (i=0; i<length; i++){ 00082 is_zc[i] = UNDEF; 00083 } 00084 } 00085 00090 void 00091 end_count_zc_e() 00092 { 00093 free(is_zc); 00094 free(data); 00095 } 00096 00106 int 00107 count_zc_e(SP16 *buf,int step) 00108 { 00109 int i; 00110 SP16 tmp; 00111 00112 for (i=0; i<step; i++) { 00113 if (is_zc[top]==TRUE) { 00114 --zero_cross; 00115 } 00116 is_zc[top] = FALSE; 00117 /* exchange old data and buf */ 00118 tmp = buf[i] + offset; 00119 if (is_trig) { 00120 if (sign==POSITIVE && tmp<0) { 00121 ++zero_cross; 00122 is_zc[top] = TRUE; 00123 is_trig = FALSE; 00124 sign = NEGATIVE; 00125 } else if (sign==NEGATIVE && tmp>0) { 00126 ++zero_cross; 00127 is_zc[top] = TRUE; 00128 is_trig = FALSE; 00129 sign = POSITIVE; 00130 } 00131 } 00132 if (abs(tmp)>trigger) { 00133 is_trig = TRUE; 00134 } 00135 data[top] = buf[i]; 00136 top++; 00137 if (valid_len < top) valid_len = top; 00138 if (top >= length) { 00139 top = 0; 00140 } 00141 } 00142 return (zero_cross); 00143 } 00144 00156 int 00157 count_zc_e_level(SP16 *buf,int step,int *levelp) 00158 { 00159 int i; 00160 SP16 tmp, level; 00161 00162 level = 0; 00163 for (i=0; i<step; i++) { 00164 if (is_zc[top]==TRUE) { 00165 --zero_cross; 00166 } 00167 is_zc[top] = FALSE; 00168 /* exchange old data and buf */ 00169 tmp = buf[i] + offset; 00170 if (is_trig) { 00171 if (sign==POSITIVE && tmp<0) { 00172 ++zero_cross; 00173 is_zc[top] = TRUE; 00174 is_trig = FALSE; 00175 sign = NEGATIVE; 00176 } else if (sign==NEGATIVE && tmp>0) { 00177 ++zero_cross; 00178 is_zc[top] = TRUE; 00179 is_trig = FALSE; 00180 sign = POSITIVE; 00181 } 00182 } 00183 if (abs(tmp)>trigger) { 00184 is_trig = TRUE; 00185 } 00186 if (abs(tmp)>level) level = abs(tmp); 00187 data[top] = buf[i]; 00188 top++; 00189 if (valid_len < top) valid_len = top; 00190 if (top >= length) { 00191 top = 0; 00192 } 00193 } 00194 *levelp = (int)level; 00195 return (zero_cross); 00196 } 00197 00204 void 00205 zc_copy_buffer(SP16 *newbuf, int *len) 00206 { 00207 int i, t; 00208 if (valid_len < length) { 00209 t = 0; 00210 } else { 00211 t = top; 00212 } 00213 for(i=0;i<valid_len;i++) { 00214 newbuf[i] = data[t]; 00215 if (++t == length) t = 0; 00216 } 00217 *len = valid_len; 00218 }