00001 #include <math.h> 00002 #include "c_lpc10.h" 00003 00004 /**********************************************************/ 00005 /* Onset detection. 00006 Detection of onsets in (or slightly preceding) the futuremost frame 00007 of speech. 00008 00009 Inputs 00010 . pebuf[] - Preemphasized speech (SBUFH+1 samples, last LFRAME used) 00011 In/Outputs 00012 . osbuf[OSLEN] - Buffer which holds sorted indexes of onsets 00013 . osptr - Free pointer into OSBUF 00014 Constants: 00015 . L2LAG - Lag due to both filters which compute filtered slope of FPC 00016 . L2WID - Width of the filter which computes the slope of FPC 00017 . OSHYST - The number of samples which of slope (FPC) which must be below 00018 . the threshold before a new onset may be declared. 00019 . L2 - Threshold for filtered slope of FPC (function of L2WID!) 00020 Variables 00021 . n, d - Numerator and denominator of prediction filters 00022 . fpc - Current prediction coefs 00023 . l2buf, l2sum1, l2sum2 - State of slope filter 00024 Globals 00025 . FLOAT n, d, fpc 00026 . FLOAT l2buf[L2WID], l2sum1 00027 . INDEX l2ptr1, l2ptr2, lasti 00028 . BOOL hyst */ 00029 00030 VOID onset( FLOAT pebuf[], INDEX osbuf[OSLEN], INDEX* osptr ) 00031 { 00032 #define L2LAG (L2WID/2+1) /* 16/2+1=9 */ 00033 #define OSHYST 10 00034 #define L2 ((FLOAT)1.7) 00035 INDEX i; 00036 FLOAT l2sum2; 00037 00038 if (g_hyst) 00039 g_lasti -= LFRAME; 00040 00041 for (i = (SBUFH-LFRAME+1); i <= SBUFH; i++) { 00042 00043 /* Compute FPC; Use old FPC on divide by zero; 00044 Clamp FPC to +/-1; 0.015625=1/64 */ 00045 g_n = (pebuf[i]*pebuf[i-1] + (FLOAT)63.0*g_n) * (FLOAT)0.015625; 00046 g_d = (pebuf[i-1]*pebuf[i-1] + (FLOAT)63.0*g_d) * (FLOAT)0.015625; 00047 if (g_d) { 00048 if (g_n > g_d) 00049 g_fpc = (FLOAT)1.0; 00050 else if (g_n<-g_d) 00051 g_fpc = (FLOAT)-1.0; 00052 else 00053 g_fpc = g_n / g_d; 00054 } 00055 00056 /* Filter FPC */ 00057 l2sum2 = g_l2buf[g_l2ptr1]; 00058 g_l2sum1 += g_fpc - g_l2buf[g_l2ptr2]; 00059 g_l2buf[g_l2ptr2] = g_l2sum1; 00060 g_l2buf[g_l2ptr1] = g_fpc; 00061 g_l2ptr1++; 00062 if (g_l2ptr1==L2WID) 00063 g_l2ptr1=0; 00064 g_l2ptr2++; 00065 if (g_l2ptr2==L2WID) 00066 g_l2ptr2=0; 00067 00068 if ((FLOAT)fabs((g_l2sum1 - l2sum2)) > L2) { 00069 if (!g_hyst) { 00070 if (*osptr < OSLEN) { /* Ignore if buffer full */ 00071 osbuf[*osptr] = i - L2LAG; 00072 (*osptr)++; 00073 } 00074 g_hyst = TRUE; 00075 } 00076 g_lasti = i; 00077 } 00078 else if (g_hyst && (i - g_lasti) >= OSHYST) 00079 g_hyst = FALSE; 00080 } 00081 } 00082 00083 /**********************************************************/