int swr_rematrix(SwrContext *s, AudioData *out, AudioData *in, int len, int mustcopy){ int out_i, in_i, i, j; av_assert0(out->ch_count == av_get_channel_layout_nb_channels(s->out_ch_layout)); av_assert0(in ->ch_count == av_get_channel_layout_nb_channels(s-> in_ch_layout)); for(out_i=0; out_i<out->ch_count; out_i++){ switch(s->matrix_ch[out_i][0]){ case 1: in_i= s->matrix_ch[out_i][1]; if(mustcopy || s->matrix[out_i][in_i]!=1.0){ if(s->int_sample_fmt == AV_SAMPLE_FMT_FLT){ copy_float(out->ch[out_i], in->ch[in_i], s->matrix[out_i][in_i], len); }else copy_s16 (out->ch[out_i], in->ch[in_i], s->matrix16[out_i][in_i], len); }else{ out->ch[out_i]= in->ch[in_i]; } break; case 2: if(s->int_sample_fmt == AV_SAMPLE_FMT_FLT){ sum2_float(out->ch[out_i], in->ch[ s->matrix_ch[out_i][1] ], in->ch[ s->matrix_ch[out_i][2] ], s->matrix[out_i][ s->matrix_ch[out_i][1] ], s->matrix[out_i][ s->matrix_ch[out_i][2] ], len); }else{ sum2_s16 (out->ch[out_i], in->ch[ s->matrix_ch[out_i][1] ], in->ch[ s->matrix_ch[out_i][2] ], s->matrix16[out_i][ s->matrix_ch[out_i][1] ], s->matrix16[out_i][ s->matrix_ch[out_i][2] ], len); } break; default: if(s->int_sample_fmt == AV_SAMPLE_FMT_FLT){ for(i=0; i<len; i++){ float v=0; for(j=0; j<s->matrix_ch[out_i][0]; j++){ in_i= s->matrix_ch[out_i][1+j]; v+= ((float*)in->ch[in_i])[i] * s->matrix[out_i][in_i]; } ((float*)out->ch[out_i])[i]= v; } }else{ for(i=0; i<len; i++){ int v=0; for(j=0; j<s->matrix_ch[out_i][0]; j++){ in_i= s->matrix_ch[out_i][1+j]; v+= ((int16_t*)in->ch[in_i])[i] * s->matrix16[out_i][in_i]; } ((int16_t*)out->ch[out_i])[i]= (v + 16384)>>15; } } } } return 0; }
void overlp(float *input, int npts, float *output, float *c, int nc, int nfft, float *buffer, float *cbuff) { int i, iptr, nbad, ngood, nload1, nload2, nrem; float ci, cr, scale, scale1, scale2, xi, xr; float *const Buffer = &buffer[0] - 1; float *const C = &c[0] - 1; float *const Cbuff = &cbuff[0] - 1; float *const Input = &input[0] - 1; float *const Output = &output[0] - 1; nrem = npts; nbad = nc - 1; ngood = nfft - nbad; iptr = 1; /* DFT of filter sequence * */ zero( &Cbuff[1], 2*nfft ); /* copy( (int*)&C[1], (int*)&Cbuff[1], nc ); */ copy_float(&(C[1]), &(Cbuff[1]), nc ); fft( &Cbuff[1], &Cbuff[nfft + 1], nfft, -1 ); /* Initial conditions in buffer * */ zero( &Buffer[ngood + 1], nbad ); L_6: ; if( nrem <= 0 ) goto L_7; /* Load data into buffer * */ nload2 = 0; nload1 = min( ngood, nrem ); /* copy( (int*)&Input[iptr], (int*)&Buffer[1], nload1 ); */ copy_float(&(Input[iptr]), &(Buffer[1]), nload1 ); nrem = nrem - nload1; /* Load second buffer if the available data is not exhausted * */ if( nrem > 0 ){ nload2 = min( ngood, nrem ); /* Data */ /* copy( (int*)&Input[iptr + nload1], (int*)&Buffer[nfft + 1], nload2 ); */ copy_float( &(Input[iptr + nload1]), &(Buffer[nfft + 1]), nload2 ); /* Initial condition */ /* copy( (int*)&Input[iptr + ngood - nbad], (int*)&Buffer[nfft + 1 + ngood], nbad ); */ copy_float( &(Input[iptr + ngood - nbad]), &(Buffer[nfft + 1 + ngood]), nbad ); nrem = nrem - nload2; } else{ zero( &Buffer[nfft + 1], nfft ); } /* Scale buffers when both contain data * */ if( !(nload2 == 0) ){ scale1 = 0.; scale2 = 0.; for( i = 1; i <= nfft; i++ ){ scale1 = scale1 + fabs( Buffer[i] ); scale2 = scale2 + fabs( Buffer[nfft + i] ); } if( scale1 == 0. ){ scale1 = 1.; } scale = scale2/scale1; if( scale == 0. ){ scale = 1.; } for( i = 1; i <= nfft; i++ ){ Buffer[i] = Buffer[i]*scale; } } /* Transform data * */ fft( &Buffer[1], &Buffer[nfft + 1], nfft, -1 ); /* Product of data transform with filter transform * */ for( i = 1; i <= nfft; i++ ){ xr = Buffer[i]; xi = Buffer[nfft + i]; cr = Cbuff[i]; ci = Cbuff[nfft + i]; Buffer[i] = xr*cr - xi*ci; Buffer[nfft + i] = xr*ci + xi*cr; } /* Inverse transform * */ fft( &Buffer[1], &Buffer[nfft + 1], nfft, 1 ); /* Load initial conditions from input sequence * */ if( !(nrem <= 0) ){ /* copy( (int*)&Input[iptr + 2*ngood - nbad], (int*)&Buffer[ngood + 1], nbad ); */ copy_float( &(Input[iptr + 2*ngood - nbad]), &(Buffer[ngood + 1]), nbad ); } /* Save filtered data to output sequence * */ if( !(nload2 == 0) ){ for( i = 1; i <= nload1; i++ ){ Buffer[i] = Buffer[i]/scale; } } /* copy( (int*)&Buffer[1], (int*)&Output[iptr], nload1 ); */ copy_float( &(Buffer[1]), &(Output[iptr]), nload1 ); iptr = iptr + nload1; if( !(nload2 == 0) ){ /* copy( (int*)&Buffer[nfft + 1], (int*)&Output[iptr], nload2 ); */ copy_float( &(Buffer[nfft + 1]), &(Output[iptr]), nload2 ); iptr = iptr + nload2; } goto L_6; L_7: ; return; }
void /* FUNCTION */ fdWriteFiles ( int * memptr , char * kprefix , float * userData , int newnpts , int * nerr ) { /* index sacmem for amplitude, phase, group delay, and the impulse response. */ int fileDescriptor = 0 , hdrindex , xbegin = 0 , idx, jdx, nlcmem, nlcdsk, nptwr, unused1 , unused2 , unused3 ; char kname[ MCPFN ] , ksuffix[ 3 ][ 6 ] ; float *bufout = NULL , *ptr , amph[ 2 ][ 2 * NDATPTS - 2 ] ; void zwabs() ; *nerr = 0; /* handle strings */ if ( strlen ( kprefix ) > MCPFN - 4 ) kprefix[ MCPFN - 4 ] = '\0' ; strcpy ( ksuffix[ 0 ] , ".spec" ) ; strcpy ( ksuffix[ 1 ] , ".gd" ) ; strcpy ( ksuffix[ 2 ] , ".imp" ) ; /* Determine the begin of the impulse */ for ( ptr = cmmem.sacmem[memptr[ 9 ]] ; *ptr == 0.0 ; ptr++ ) xbegin++ ; /* fill the amplitude and phase array */ for ( idx = 0 ; idx < NDATPTS ; idx++ ) { amph[ 0 ][ idx ] = cmmem.sacmem[ memptr[ 6 ] ][ idx ] ; amph[ 1 ][ idx ] = cmmem.sacmem[ memptr[ 7 ] ][ idx ] ; } for ( ; idx < 2 * NDATPTS - 2 ; idx++ ) { amph[ 0 ][ idx ] = cmmem.sacmem[ memptr[ 6 ] ][ 2*NDATPTS-idx-2 ] ; amph[ 1 ][ idx ] = -cmmem.sacmem[ memptr[ 7 ] ][ 2*NDATPTS-idx-2 ] ; } /* Allocate block for headers. */ allamb ( &cmmem, SAC_HEADER_WORDS, &hdrindex , nerr ) ; if ( *nerr != 0 ) goto L_ERROR ; /* null the header */ for ( idx = 0 ; idx < SAC_HEADER_FLOATS ; idx++ ) cmhdr.fhdr[ idx ] = SAC_FLOAT_UNDEFINED ; for ( idx = 0 ; idx < SAC_HEADER_INTEGERS ; idx++ ) cmhdr.nhdr[ idx ] = SAC_INT_UNDEFINED ; for ( idx = 0 ; idx < SAC_HEADER_ENUMS ; idx++ ) cmhdr.ihdr[ idx ] = SAC_INT_UNDEFINED ; for ( idx = 0 ; idx < SAC_HEADER_STRINGS ; idx++ ) strcpy ( kmhdr.khdr[ idx ] , SAC_CHAR_UNDEFINED ) ; /* fill some fields. */ for ( idx = 0 ; idx < 9 ; idx++ ) /* user fields */ *( user0 + idx ) = userData[ idx ] ; switch ( (int) (*user0 + 0.5) ) { case 1: strcpy ( kuser0 , "lowpass " ) ; break ; case 2: strcpy ( kuser0 , "highpass" ) ; break ; case 3: strcpy ( kuser0 , "bandpass" ) ; break ; case 4: strcpy ( kuser0 , "bandrej " ) ; break ; default: strcpy ( kuser0 , "-12345 " ) ; break ; } switch ( (int) (*user1 + 0.5) ) { case 1: strcpy ( kuser1 , "Butter " ) ; break ; case 2: strcpy ( kuser1 , "Bessel " ) ; break ; case 3: strcpy ( kuser1 , "C1 " ) ; break ; case 4: strcpy ( kuser1 , "C2 " ) ; break ; default: strcpy ( kuser1 , "-12345 " ) ; break ; } fillNZ () ; /* time fields */ *begin = 0.0 ; /* other fields */ *sb = 0.0 ; *nvhdr = 6 ; *idep = IUNKN ; *iztype = IB ; *leven = TRUE ; *lpspol = TRUE ; *lovrok = TRUE ; *lcalda = FALSE ; for ( jdx = 0 ; jdx < 3 ; jdx++ ) { /* loop between output files. */ /* fill other header fields specific to the data */ switch ( jdx ) { case 0: aphdr( newnpts ) ; nlcmem = memptr[ 6 ] ; break ; case 1: gdhdr( newnpts ) ; nlcmem = memptr[ 8 ] ; break ; case 2: irhdr( newnpts ) ; nlcmem = memptr[ 9 ] ; break ; default: goto L_ERROR ; } /* Get file name */ sprintf ( kname , "%s%s" , kprefix , ksuffix[ jdx ] ) ; /* Open file */ znfile( &fileDescriptor , kname , MCPFN , "DATA" , 5 , nerr ); if ( *nerr ) goto L_ERROR ; /* Get ready to write header to disk */ nlcdsk = 0; nptwr = SAC_HEADER_WORDS_FILE; if ( ( bufout = (float *) malloc ( SAC_HEADER_SIZEOF_FILE) ) == NULL ) { *nerr = 301; goto L_ERROR ; } /* move header into working memory */ /* copy ( (int*) cmhdr.fhdr , (int*) cmmem.sacmem[ hdrindex ] , SAC_HEADER_NUMBERS ); */ copy_float( cmhdr.fhdr, cmmem.sacmem[ hdrindex ], SAC_HEADER_NUMBERS ); zputc ( kmhdr.khdr[ 0 ] , 9 , (int *)(cmmem.sacmem[ hdrindex ] + SAC_HEADER_NUMBERS), ( MCPW + 1 ) * SAC_HEADER_STRINGS) ; /* move header into output buffer */ map_hdr_out ( cmmem.sacmem[ hdrindex ] , bufout , FALSE) ; /* write the headers */ zwabs( (int *)&fileDescriptor, (char *)(bufout), nptwr, (int *)&nlcdsk, (int *)nerr ); free(bufout); bufout = NULL ; nlcdsk += nptwr; nptwr = NDATPTS ; /* Write data to disk */ switch ( jdx ) { case 0: nptwr = 2 * NDATPTS - 2 ; zwabs ( (int *)&fileDescriptor, (char *)(amph[ 0 ]) , nptwr, (int *)&nlcdsk, (int *)nerr ) ; /* nlcmem = memptr[ 7 ] ; */ nlcdsk += nptwr; zwabs ( (int *)&fileDescriptor, (char *)(amph[ 1 ]) , nptwr, (int *)&nlcdsk, (int *)nerr ) ; break ; case 1: zwabs( (int *)&fileDescriptor, (char *)(cmmem.sacmem[nlcmem]), nptwr, (int *)&nlcdsk, (int *)nerr ); break ; case 2: zwabs( (int *)&fileDescriptor, (char *)(cmmem.sacmem[nlcmem] + xbegin), nptwr, (int *)&nlcdsk, (int *)nerr ); break ; } /* Close file */ zclose ( &fileDescriptor , nerr ) ; fileDescriptor = 0 ; } /* end for */ L_ERROR: if ( *nerr ) { setmsg ( "ERROR" , *nerr ) ; outmsg () ; clrmsg () ; } if ( cmdfm.ndfl > 0 ) getfil ( 1 , TRUE , &unused1 , &unused2 , &unused3 , nerr ) ; if ( bufout ) free ( bufout ) ; if ( fileDescriptor ) zclose ( &fileDescriptor , nerr ) ; relamb ( cmmem.sacmem , hdrindex , nerr ); }
void kmeans_ad (int DIMENSIONS, int NUMBER_OF_RECORDS, float *records, float *orig_center, float *new_center, int *split, float AD_CV, int CSS_NUM_SPUS, int block_records) { int CENTERS = 2; int i, j; int *assigned_centers; if (NUMBER_OF_RECORDS < 2) { #ifdef KMEANSAD_DEBUG printf("\n\n######### no split... NUM_REC < 2 #########\n\n"); #endif *split = 0; return; } float *centers; // Alocacao de 2 centros a mais para nao gerar erro na rotina de leitura do KMeans. centers = memalign(128, 4 * DIMENSIONS * sizeof(float)); clear_float(¢ers[2*DIMENSIONS], 2*DIMENSIONS); for (i = 0; i < 2; i++) { copy_float(¢ers[i * DIMENSIONS], &records[i * DIMENSIONS], DIMENSIONS); } assigned_centers = memalign(128, NUMBER_OF_RECORDS * sizeof(int)); /** * * Phase 3, two-centers kmeans * */ twocenters_kmeans(DIMENSIONS, CENTERS, NUMBER_OF_RECORDS, records, centers, assigned_centers, CSS_NUM_SPUS, block_records); /** * * Phase 4, vector projection * */ // Calculation of vector vec float vec[DIMENSIONS]; float powvec = 0; for (j = 0; j < DIMENSIONS; j++) { vec[j] = centers[0*DIMENSIONS + j] - centers[1*DIMENSIONS + j]; powvec += vec[j]*vec[j]; } // projection in the vector c1 - c2 float *p_records; p_records = memalign(128, NUMBER_OF_RECORDS * sizeof(float)); float sum = 0; float sum2 = 0; #ifdef PROJAB block_records = BLOCK_SIZE; for (i = 0; i < NUMBER_OF_RECORDS; i += block_records) { int number_of_records = (i + block_records > NUMBER_OF_RECORDS ? NUMBER_OF_RECORDS - i : block_records); projab_cpu_task (DIMENSIONS, number_of_records, &records[i * DIMENSIONS], vec, powvec, &p_records[i], &sum, &sum2); } #pragma css barrier #else for (j = 0; j < NUMBER_OF_RECORDS; j++) { p_records[j] = projab(&records[j*DIMENSIONS], vec, powvec, DIMENSIONS); sum += p_records[j]; sum2 += p_records[j]*p_records[j]; } #endif /** * * Phases 5 and 6 inside the andersondarling() * */ float ad; andersondarling(p_records, NUMBER_OF_RECORDS, &ad, sum, sum2); if (ad >= AD_CV) { #ifdef KMEANSAD_DEBUG printf("\n\n######### SPLIT! AD = %f, CV = %f #########\n\n", ad, AD_CV); #endif // copy_float(orig_center, ¢ers[0*DIMENSIONS], DIMENSIONS); // copy_float(new_center, ¢ers[1*DIMENSIONS], DIMENSIONS); memcpy(orig_center, ¢ers[0*DIMENSIONS], DIMENSIONS * sizeof(float)); memcpy(new_center, ¢ers[1*DIMENSIONS], DIMENSIONS * sizeof(float)); *split = 1; } else { #ifdef KMEANSAD_DEBUG printf("\n\n######### no split... AD = %f, CV = %f #########\n\n", ad, AD_CV); #endif *split = 0; } free(centers); free(assigned_centers); free(p_records); }
/** * Compute the Cross-Correlation Function * * @param data1 * Array containing the first data sequence * @param data2 * Array containing the second data sequence * @param nsamps * Number of samples in data sequence * @param nwin * Requested Number of windows * @param wlen * Requested number of samples in each window. The * subroutine will calculate the window overlap. * Maximum value is 2048 * @param type * Type of data analysis window to use. Valid values * are: * - <HAM>MING * - <HAN>NING * - <C>OSINE * - <R>ECTAN * - <T>RIANG * @param c * Output Array containing resulting 2*\p wlen -1 length * correlation coefficients. The correlation sequence is * circularly rotated in the array so that the zeroth lag * is at the beginning. Array dimensions 0:4095 * @param nfft * Number of samples in the correlation sequence. * May be padded with zeros. * @param err * Error Message * @param err_s * Length of string \p err * * @return Nothing * * \author Dave Harris * L-205 * Lawrence Livermore National Laboratory * Livermore, Ca 94550 * * \date 800130 Created * \date 840621 Last Modified * * */ void crscor(float *data1, float *data2, int nsamps, int nwin, int wlen, char *type, float *c, int *nfft, char *err, int err_s) { char temp[131]; int half, i, j, k, lsamp, nlags, nverlp, point; float scale, scale1, scale2, xi, xr, yi, yr; float *const Data1 = &data1[0] - 1; float *const Data2 = &data2[0] - 1; /* Initializations * */ fstrncpy( err, err_s-1, " ", 1 ); /* Check for legal window length and compute overlap * */ nlags = 2*wlen - 1; if( nwin < 1 ){ fstrncpy( err, err_s-1, " CRSCOR - too few windows ", 26 ); return; } else if( wlen < 1 || wlen > nsamps ){ fstrncpy( err, err_s-1," CRSCOR - illegal window length " , 32 ); return; } else{ /* Everything OK */ if( nwin*wlen <= nsamps ){ nverlp = 0; } else{ nverlp = (nwin*wlen - nsamps)/(nwin - 1); if( nwin*wlen - nverlp*(nwin - 1) > nsamps ){ nverlp = nverlp + 1; } } lsamp = wlen - 1; } /* Find first power of two >= #LAGS * */ *nfft = 8; L_2: ; if( *nfft >= nlags ) goto L_3; *nfft = *nfft*2; goto L_2; L_3: ; half = *nfft/2; if ((big.w = (float *)malloc(wlen*sizeof(float))) == NULL) { printf("memory allocation failed in crscor\n"); goto L_8892; } if ((big.caux = (float *)malloc(*nfft*sizeof(float))) == NULL) { printf("memory allocation failed in crscor\n"); goto L_8891; } if ((big.workr = (float *)malloc(*nfft*sizeof(float))) == NULL) { printf("memory allocation failed in crscor\n"); goto L_8890; } if ((big.worki = (float *)malloc(*nfft*sizeof(float))) == NULL) { printf("memory allocation failed in crscor\n"); goto L_8889; } /* Generate window * */ for( i = 0; i <= lsamp; i++ ){ big.w[i] = 1.; /* I */ } window( &big.w[0], wlen, type, 1, wlen, &big.w[0], err,err_s ); /* Check validity of window calculation * */ if( memcmp(err," ",8) != 0 ){ fstrncpy(temp, 130, err, strlen(err)); fstrncpy(temp+strlen(err),130-strlen(err), " (from CROSS)", 13); fstrncpy(err,err_s-1,temp,strlen(temp)); goto L_8888; } /* Compute cross-correlation function * * * Initialize window pointer * */ point = 1; /* Initialize correlation arrays * */ zero( &c[0], *nfft ); zero( &big.caux[0],*nfft ); /* Compute cross-spectrum for each window, then average * */ for( i = 1; i <= nwin; i++ ){ /* Zero work arrays * */ zero( &big.workr[0], *nfft ); zero( &big.worki[0], *nfft ); /* Load data into arrays * */ /* copy( (int*)&Data1[point], (int*)&big.workr[0], wlen ); */ /* copy( (int*)&Data2[point], (int*)&big.worki[0], wlen ); */ copy_float( &(Data1[point]), big.workr, wlen ); copy_float( &(Data2[point]), big.worki, wlen ); /* Compute scale factors * */ scale1 = rms( &big.workr[0], wlen ); scale2 = rms( &big.worki[0], wlen ); scale = scale1*scale2; /* Window and scale data * */ for( j = 0; j <= lsamp; j++ ){ big.workr[j] = big.workr[j]*big.w[j]/scale1; big.worki[j] = big.worki[j]*big.w[j]/scale2; /* J */ } /* Compute and average cross spectra * */ fft( &big.workr[0], &big.worki[0], *nfft, -1 ); /* Special case for point at 0 * */ c[0] = c[0] + big.workr[0]*big.worki[0]*scale; /* All other points * */ for( j = 1; j <= half; j++ ){ k = *nfft - j; xr = (big.workr[j] + big.workr[k])*.5; xi = (big.worki[j] - big.worki[k])*.5; yr = (big.worki[j] + big.worki[k])*.5; yi = (big.workr[k] - big.workr[j])*.5; c[j] = c[j] + (xr*yr + xi*yi)*scale; big.caux[j] = big.caux[j] + (xr*yi - xi*yr)*scale; c[k] = c[j]; big.caux[k] = -big.caux[j]; } /* Update window pointer * */ point = point + wlen - nverlp; } /* Inverse fft for correlation computation * */ fft( &c[0], &big.caux[0], *nfft, 1 ); /* Bye * */ L_8888: free(big.worki); L_8889: free(big.workr); L_8890: free(big.caux); L_8891: free(big.w); L_8892: return; }