svm_classifier_t *svm_update_classifier(svm_classifier_t *svm, int class_ind, mx_real_t *features) { int i, j, cur; struct svm_problem *prob = &(svm->problem); if (svm->finished) rs_warning("SVM classifier is being updated after training!"); cur=prob->l++; if (prob->l > svm->max_instances) { svm->max_instances += MAX_INSTANCES; prob->x = (struct svm_node **) rs_realloc(prob->x,sizeof(struct svm_node *)*svm->max_instances,"SVM list of instances"); prob->y = (double *) rs_realloc(prob->y,sizeof(double)*svm->max_instances,"SVM number of instances"); } prob->y[cur] = (mx_real_t) class_ind; prob->x[cur]= (struct svm_node *) rs_malloc(sizeof(struct svm_node)*(svm->feature_dim+1),"SVM instance"); for (i=0,j=0;i<svm->feature_dim;i++) { if (features[i]) { prob->x[cur][j].index=i+1; prob->x[cur][j++].value=features[i]; } } prob->x[cur][j].index=-1; return svm; }
/** * _dsp_fft_init(n) * Interne Datenstrukturen fuer eine FFT der Laenge 'n' initialisieren, * naemlich: * - 'w[]' die Liste der n-ten Einheitswurzeln exp(-i2pi/n) **/ static void _dsp_dft_init(int n) { static int last_n = -1; /* Laenge der letzten Initialisierung */ int i; mx_real_t phi; if (last_n == n) return; /* Daten koennen weiterverwendet werden */ /* Liste der Einheitswurzeln (wieder) allozieren ... */ w = rs_realloc(w, (n*n) * sizeof(mx_complex_t), "unit roots for DFT!"); /* Ergebnisvektor */ _z = rs_realloc(_z, n * sizeof(mx_complex_t), "result vector for DFT!"); /* ... und Werte berechnen */ mx_re(w[0]) = 1; /* w^0 == (1,0) */ mx_im(w[0]) = 0; phi = -2.0 * M_PI / n; mx_re(w[1]) = cos(phi); /* w^1 == exp(-i*2*M_PI/n) */ mx_im(w[1]) = sin(phi); for (i = 2; i < n*n; i++) mx_cmul(w[i], w[i-1], w[1]); last_n = n; }
void series_add(fextract_series_t *data_series, mx_real_t element) { if (data_series->maxSeries <= data_series->nSeries) { data_series->maxSeries+=50; data_series->series = (mx_real_t *) rs_realloc(data_series->series,data_series->maxSeries*sizeof(mx_real_t),"data of series"); } data_series->series[data_series->nSeries++] = element; }
/* * Truncate or extend program. * * If length is less than the program's current length, the program is * truncated. If length is greater than the current size of the * program, additional space is added. The extra space is padded with * 0xFF, with the exception of bytes that fall at the start of a page. */ int rs_program_set_length(RSProgram* prgm, /* program */ unsigned long length) /* new length of program */ { unsigned long length_a, i; unsigned char* dptr; if (length <= prgm->length) { prgm->length = length; return RS_SUCCESS; } else { if (length > prgm->length_a) { length_a = length + 16384; dptr = rs_realloc(prgm->data, length_a); if (!dptr) return RS_ERR_OUT_OF_MEMORY; prgm->data = dptr; prgm->length_a = length_a; } memset(prgm->data + prgm->length, 0xff, length - prgm->length); for (i = ((prgm->length + 0x3fff) & ~0x3fff); i < length; i += 0x4000) prgm->data[i] = 0x42; prgm->length = length; return RS_SUCCESS; } }
float lsFloat_setIndex(lsFloat_t *il, int index, float i, float i0) { float ret; int j; if (!il) rs_error("lsFloat_setIndex: got NULL-pointer list"); if (index < 0) rs_error("lsFloat_setIndex: illegal index %d",index); if (index >= il->max_list) { int d = (index - il->max_list) / LIST_BUF + 1; il->list = rs_realloc(il->list, (il->max_list+= d*LIST_BUF) * sizeof(float), "list items"); } for (j=il->n_list; j <= index; j++) il->list[j] = i0; ret = il->list[index]; il->list[index] = i; if (il->n_list <= index) il->n_list = index+1; return (ret); }
char *rs_line_read(FILE *fp, char comment_char) { static char *line = NULL; char *cp; int len, i; /* evtl. Zeilenpuffer erzeugen ... */ if (line == NULL) line = rs_malloc(_line_len * sizeof(char), "line buffer"); /* ... und solange noch Zeilen gelesen werden koennen ... */ while (fgets(line, _line_len, fp) != NULL) { /* ... ggf. '\n' am Zeilenende loeschen ... */ while (line[(len = strlen(line)) - 1] != '\n') { /* ... bei Pufferueberlauf ggf. erweitern ... */ rs_warning("line longer than %d characters read - expanding!", _line_len); /* ... falls ueberlange TEXT-Zeile ... */ for (i = 0; i < len; i++) if (!(isprint(line[i]) || isspace(line[i]))) rs_error("non-printable character '0x%02x' encountered in excess text line!", line[i]); _line_len += RS_LINE_LEN_DEFAULT; line = rs_realloc(line, _line_len * sizeof(char), "extended line buffer"); if (fgets(line + len, _line_len - len, fp) == NULL) { len++; break; } } line[len - 1] = '\0'; /* ... ggf. Kommentare ueberspringen ... */ if (comment_char && line[0] == comment_char) continue; /* ... und am Zeilenende loeschen falls nicht "escaped" ... */ if (comment_char) { cp = line + 1; while (cp = strrchr(cp, comment_char)) { if (cp[-1] != '\\') { *cp = '\0'; break; } } } /* ... und Zeilen mit Inhalt zurueckgeben */ for (cp = line; *cp; cp++) if (!isspace(*cp)) return(line); } /* ... sonst Dateiende signalisieren */ return(NULL); }
LIST_TYPE *ls_getNewItem(list_t *il) { LIST_TYPE *item; if (!il) return (NULL); if (il->n_list>=il->max_list) il->list = rs_realloc(il->list,(il->max_list+=LIST_BUF) * sizeof(LIST_TYPE), "list item"); item = &il->list[il->n_list++]; return (item); }
double *lsDouble_getNewItem(lsDouble_t *il) { double *item; if (!il) return (NULL); if (il->n_list>=il->max_list) il->list = rs_realloc(il->list,(il->max_list+=LIST_BUF) * sizeof(double), "list item"); item = &il->list[il->n_list++]; return (item); }
float *lsFloat_getNewItem(lsFloat_t *il) { float *item; if (!il) return (NULL); if (il->n_list>=il->max_list) il->list = rs_realloc(il->list,(il->max_list+=LIST_BUF) * sizeof(float), "list item"); item = &il->list[il->n_list++]; return (item); }
lsFloat_t * lsFloat_realloc(lsFloat_t *il, int n) { if (!il) il = lsFloat_nil(NULL); if (n > il->max_list) { il->list = rs_realloc(il->list, (il->max_list = n) * sizeof(float), "list items"); } return (il); }
list_t * ls_realloc(list_t *il, int n) { if (!il) il = ls_nil(NULL); if (n > il->max_list) { il->list = rs_realloc(il->list, (il->max_list = n) * sizeof(LIST_TYPE), "list items"); } return (il); }
lsDouble_t * lsDouble_realloc(lsDouble_t *il, int n) { if (!il) il = lsDouble_nil(NULL); if (n > il->max_list) { il->list = rs_realloc(il->list, (il->max_list = n) * sizeof(double), "list items"); } return (il); }
lsDouble_t * lsDouble_add(lsDouble_t * il, double i) { if (!il) il = lsDouble_Nil(); if (il->n_list>=il->max_list) il->list = rs_realloc(il->list,(il->max_list+=LIST_BUF) * sizeof(double), "list item"); il->list[il->n_list] = i; il->n_list++; return (il); }
lsFloat_t * lsFloat_add(lsFloat_t * il, float i) { if (!il) il = lsFloat_Nil(); if (il->n_list>=il->max_list) il->list = rs_realloc(il->list,(il->max_list+=LIST_BUF) * sizeof(float), "list item"); il->list[il->n_list] = i; il->n_list++; return (il); }
list_t * ls_add(list_t * il, LIST_TYPE i) { if (!il) il = ls_Nil(); if (il->n_list>=il->max_list) il->list = rs_realloc(il->list,(il->max_list+=LIST_BUF) * sizeof(LIST_TYPE), "list item"); il->list[il->n_list] = i; il->n_list++; return (il); }
list_t * ls_cons(list_t *il, LIST_TYPE i) { if (!il) il = ls_Nil(); if (il->n_list>=il->max_list) il->list = rs_realloc(il->list,(il->max_list+=LIST_BUF) * sizeof(LIST_TYPE), "list item"); memmove(il->list+1, il->list, il->n_list * sizeof(LIST_TYPE)); il->list[0] = i; il->n_list++; return (il); }
lsDouble_t * lsDouble_cons(lsDouble_t *il, double i) { if (!il) il = lsDouble_Nil(); if (il->n_list>=il->max_list) il->list = rs_realloc(il->list,(il->max_list+=LIST_BUF) * sizeof(double), "list item"); memmove(il->list+1, il->list, il->n_list * sizeof(double)); il->list[0] = i; il->n_list++; return (il); }
lsFloat_t * lsFloat_cons(lsFloat_t *il, float i) { if (!il) il = lsFloat_Nil(); if (il->n_list>=il->max_list) il->list = rs_realloc(il->list,(il->max_list+=LIST_BUF) * sizeof(float), "list item"); memmove(il->list+1, il->list, il->n_list * sizeof(float)); il->list[0] = i; il->n_list++; return (il); }
rs_block_sig_t *rs_signature_add_block(rs_signature_t *sig, rs_weak_sum_t weak_sum, rs_strong_sum_t *strong_sum) { rs_signature_check(sig); /* If block_sigs is full, allocate more space. */ if (sig->count == sig->size) { sig->size = sig->size ? sig->size * 2 : 16; sig->block_sigs = rs_realloc(sig->block_sigs, sig->size * rs_block_sig_size(sig), "signature->block_sigs"); } rs_block_sig_t *b = rs_block_sig_ptr(sig, sig->count++); rs_block_sig_init(b, weak_sum, strong_sum, sig->strong_sum_len); return b; }
int emo_afile_segment(char *file, asegmentation_method_t *method, asegmentation_type_t type, dsp_sample_t ***signal_segment, int **segment_length) { int size=SEGMENT_LENGTH, n_samples=0, n_segments, samples_read; FILE *fp; dsp_sample_t *signal=NULL; dsp_vad_t *Vad; signal = (dsp_sample_t *) rs_malloc(sizeof(dsp_sample_t) * size, "Signal data"); if (!method) method = (asegmentation_method_t *) rs_malloc(sizeof(asegmentation_method_t),"Audio segmentation method"); if (strcmp(file,"-")==0) fp = stdin; else fp = fopen(file,"r"); if (!fp) { rs_warning("Cannot open file %s!",file); return -1; } while ((samples_read =fread(signal+n_samples,sizeof(dsp_sample_t),BLOCKSIZE,fp)) && samples_read >0) { n_samples+=samples_read; if (size <= n_samples) { size +=SEGMENT_LENGTH; signal = (dsp_sample_t *) rs_realloc(signal,sizeof(dsp_sample_t) * size, "Signal data"); } if (samples_read != BLOCKSIZE) break; } fclose(fp); if (type == vad && !method->vad) { Vad = dsp_vad_create(DSP_MK_VERSION(1,0),VAD_FRAME_LEN); method->vad = Vad; } n_segments = emo_asegment(method,type,signal,n_samples,signal_segment,segment_length); if (n_segments == -1) rs_error("Aborting during procession of file %s!",file); rs_free(signal); return n_segments; }
/* * Add data to the end of the program. */ int rs_program_append_data(RSProgram* prgm, /* program */ const unsigned char* data, /* data */ unsigned long length) /* size of data */ { unsigned long nlength, length_a; unsigned char* dptr; nlength = prgm->length + length; if (nlength > prgm->length_a) { length_a = nlength + 16384; dptr = rs_realloc(prgm->data, length_a); if (!dptr) return RS_ERR_OUT_OF_MEMORY; prgm->data = dptr; prgm->length_a = length_a; } memcpy(prgm->data + prgm->length, data, length); prgm->length = nlength; return RS_SUCCESS; }
int mx_scoreset_resize(mx_scoreset_t *scoreset, int max_lists) { int i; /* first check parameters ... */ if (!scoreset || max_lists <= 0) return(-1); /* can't resize below number of lists used ... */ if (max_lists < scoreset->n_lists) return(-1); /* if resizing to length 0 ... */ if (max_lists == 0) { /* ... clear existing list set ... */ scoreset->max_lists = 0; scoreset->n_lists = 0; /* should have been 0 anyway! */ rs_free(scoreset->list); scoreset->list = NULL; } else { /* ... resize set of available lists ... */ scoreset->max_lists = max_lists; scoreset->list = rs_realloc(scoreset->list, scoreset->max_lists * sizeof(mx_sscore_t), "set of score lists"); /* ... and initialize unused entries to NULL */ for (i = scoreset->n_lists; i < scoreset->max_lists; i++) scoreset->list[i] = NULL; } return(scoreset->max_lists); }
int mx_scorelist_resize(mx_scorelist_t *scorelist, int max_scores) { int i; /* first check parameters ... */ if (!scorelist || max_scores <= 0) return(-1); /* can't resize below number of scores used ... */ if (max_scores < scorelist->n_scores) return(-1); /* if resizing to length 0 ... */ if (max_scores == 0) { /* ... clear existing score list ... */ scorelist->max_scores = 0; scorelist->n_scores = 0; /* should have been 0 anyway! */ rs_free(scorelist->score); scorelist->score = NULL; } else { /* ... resize list of available scores ... */ scorelist->max_scores = max_scores; scorelist->score = rs_realloc(scorelist->score, scorelist->max_scores * sizeof(mx_sscore_t), "list of scores"); /* ... and initialize unused entries to {-1, ?} */ for (i = scorelist->n_scores; i < scorelist->max_scores; i++) scorelist->score[i].id = -1; } return(scorelist->max_scores); }
int nB_classify_file(naive_bayes_classifier_t *nB, char *filename, fx_select_t *sel, int **evaluation) { char x; int i, class_ind, n_instances=0, eval_size=1000, start,end; int *_eval; mx_real_t *instance; FILE *fp, *feature_fp; char *fname = (char *) rs_malloc(STRING_LENGTH*sizeof(char),"String"); char *class_info = (char *) rs_malloc(STRING_LENGTH*sizeof(char),"String"); char *class_name = (char *) rs_malloc(STRING_LENGTH*sizeof(char),"String"); int full_n_features = sel? sel->n_features : nB->feature_dim; _eval = (int *) rs_malloc(eval_size*sizeof(int),"evaluation data"); if (!nB) rs_error("Cannot classify without classifier - use -b or -m options to get one"); if (sel) if (sel->n_selected != nB->feature_dim) rs_error("Wrong dimension of classifier!"); instance = (mx_real_t *) rs_malloc(full_n_features*sizeof(mx_real_t),"instance to classify"); fp = fopen(filename,"r"); if (!fp) rs_error("cannot open file %s with instances to classify!",filename); while (fscanf(fp,"%s%c",fname,&x)==2) { feature_fp = fopen(fname,"r"); if (!feature_fp) rs_error("Cannot open feature file %s!",fname); if (x != '\n' && x != EOF ) { while (fscanf(fp,"%s",class_info) ==1) { if (strcmp(class_info,";")==0) break; if (sscanf(class_info,"%[^[]s",class_name)!=1) rs_error("Class information for file %s is incorrect!",fname); if(sscanf(class_info+strlen(class_name),"[%d..%d]",&start,&end) !=2) rs_error("Time information for file %s is incorrect!",fname); for (i=start;i<=end;i++) { int k; fseek(feature_fp,i*sizeof(mx_real_t)*full_n_features,SEEK_SET); fread(instance,sizeof(mx_real_t)*full_n_features,1,feature_fp); if (sel) if (fx_select_apply(&instance,sel,instance) != sel->n_features) rs_error("Feature selection did not succeed!"); class_ind = nB_classify(nB,instance); if ((n_instances+1)*2 > eval_size) { eval_size+=1000; _eval = (int *) rs_realloc(_eval,eval_size*sizeof(int),"evaluation data"); } k=cl_name2number(&(nB->mapping),class_name,nB->n_classes); _eval[n_instances*2]=cl_name2number(&(nB->mapping),class_name,nB->n_classes); _eval[n_instances*2+1]=class_ind; n_instances++; } } } else { printf("%s\t",fname); i = fread(instance, sizeof(mx_real_t), full_n_features, feature_fp); while (i == full_n_features) { if (sel) if (fx_select_apply(&instance,sel,instance) != sel->n_features) rs_error("Feature selection did not succeed!"); class_ind=nB_classify(nB,instance); n_instances++; printf("%s ",nB->mapping[class_ind]); i=fread(instance,sizeof(mx_real_t),full_n_features,feature_fp); } if (i!=0 && i != full_n_features) rs_perror("fread stopped after %d elements -- %d", i, errno); printf(";\n"); } fclose(feature_fp); } fclose(fp); rs_free(fname); rs_free(class_info); rs_free(class_name); rs_free(instance); *evaluation=_eval; return n_instances; }
int _fextract_calc(fextract_t *fex, mx_real_t *features, dsp_sample_t *signal, char *pfile) { mx_real_t *pitch=NULL, *hnr=NULL; mx_real_t **_mfcc_series, **_energy_series; mx_real_t overall_max=0, overall_max_ind=0; int nframes=0, i, j, n_voiced_frames, max; int last_max=0, last_min=0, ser=0, max_sum=0, en_mean=0, fcount=0; fextract_series_t * serie=NULL; if (fex->frame_len==0) { rs_warning("Cannot compute features for segment of size 0!"); return -1; } fex->n_features=V1_N_FEATURES; _mfcc_series= (mx_real_t **) rs_malloc(MFCC_FEATURES * sizeof(mx_real_t *), "mfcc feature series"); for (i=0;i<MFCC_FEATURES;i++) _mfcc_series[i]= (mx_real_t *) rs_malloc(MAX_FRAMES * sizeof(mx_real_t), "mfcc features"); _energy_series= (mx_real_t **) rs_malloc(ENERGY_FEATURES * sizeof(mx_real_t *), "energy feature series"); for (i=0;i<3;i++) _energy_series[i]= (mx_real_t *) rs_malloc(MAX_FRAMES * sizeof(mx_real_t), "energy features"); if (pfile) { FILE *pfile_fp= fopen(pfile,"r"); if (!pfile_fp) rs_warning("pitch file %s not found!",pfile); /* read external pitch values */ pitch = (mx_real_t *) rs_malloc(MAX_FRAMES * sizeof(mx_real_t),"external pitch values"); j=MAX_FRAMES; i=0; while (fscanf(pfile_fp,"%g",&pitch[i])==1) { if (j == i+1) { j+=MAX_FRAMES; pitch = (mx_real_t *) rs_realloc(pitch,j*sizeof(mx_real_t),"external pitch values"); } i++; } nframes=i; fclose(pfile_fp); } else { pitch= pitch_calc (fex->pitch,signal,fex->frame_len); nframes=fex->pitch->nframes; } if (!pitch) { for (;fcount<STATS*9+7;fcount++) features[fcount]=-1.0; n_voiced_frames=-1.0; } else { const int series_size=9; // @begin_change_johannes //fextract_series_t *serien[series_size]; fextract_series_t *serien[9]; // @end_change_johannes // printRawXML(pitch,nframes,"pitch"); pitch_frame_candidates_destroy(fex->pitch); for (i=0;i<series_size;i++) serien[i]=series_create(MAX_SERIES); if (pitch[0]!=0) series_add(serien[0],pitch[0]); for (i=1;i<nframes-1;i++) { if (pitch[i]!=0) series_add(serien[0],pitch[i]); if (pitch[i-1]<pitch[i] && pitch[i]>pitch[i+1]) { series_add(serien[1],pitch[i]); if (pitch[i] > overall_max) { overall_max=pitch[i]; overall_max_ind=i; } series_add(serien[2],i-last_min); series_add(serien[3],pitch[i]-pitch[last_min]); series_add(serien[4],(pitch[i]-pitch[last_min])/(i-last_min)); last_max=i; } else if (pitch[i-1]>pitch[i] && pitch[i]<pitch[i+1]) { series_add(serien[5],pitch[i]); series_add(serien[6],i-last_max); series_add(serien[7],pitch[i]-pitch[last_max]); series_add(serien[8],(pitch[i]-pitch[last_max])/(i-last_max)); last_min=i; } } overall_max_ind = 1.0 * (overall_max_ind+1) / nframes; if (nframes >1 && pitch[nframes-1]!=0) series_add(serien[0],pitch[nframes-1]); for (i=0; i<series_size; i++) { getStatistics(features+fcount,serien[i]->series,serien[i]->nSeries); //pitch statistics features fcount+=STATS; } // normierter Mittelwert, normierter Median, normiertes 1. Quartil, normiertes 3. Quartil if (features[1]-features[2]) { features[fcount++]=(features[0]-features[2])/(features[1]-features[2]); features[fcount++]=(features[5]-features[2])/(features[1]-features[2]); features[fcount++]=(features[6]-features[2])/(features[1]-features[2]); features[fcount++]=(features[7]-features[2])/(features[1]-features[2]); } else { features[fcount++]=0; features[fcount++]=0; features[fcount++]=0; features[fcount++]=0; } features[fcount++]=1.0 * serien[1]->nSeries/nframes; // Anzahl Maxima pro Frames pro Segment features[fcount++]=1.0 * serien[5]->nSeries/nframes; // Anzahl Minima pro Frames pro Segment features[fcount++]=overall_max_ind; // Position des Maximums n_voiced_frames=serien[0]->nSeries; for (i=0;i<series_size;i++) series_destroy(serien[i]); } getEnergy_and_MFCC(fex->mfcc, signal, fex->frame_len, &_energy_series, &_mfcc_series, &ser); // ser=0; if (ser) { const int series_size = 8; // @begin_change_johannes //fextract_series_t *serien[series_size]; fextract_series_t *serien[8]; // @end_change_johannes for (i=0;i<series_size;i++) serien[i]=series_create(MAX_SERIES); fcount+=getMFCCFeatures(features+fcount, _mfcc_series,ser); /* Energie-Merkmale: */ last_max=last_min=0; for (i=1;i<ser-1;i++) { if (_energy_series[0][i-1]<_energy_series[0][i] && _energy_series[0][i]>_energy_series[0][i+1]) { series_add(serien[0], _energy_series[0][i]); //Maxima max_sum += _energy_series[0][i]; series_add(serien[1], i-last_min); // Laenge der Maxima series_add(serien[2], _energy_series[0][i]-_energy_series[0][last_min]); // Groesse der Maxima series_add(serien[3], (_energy_series[0][i]-_energy_series[0][last_min])/(i-last_min)); // Steigung der Maxima last_max=i; } else if (_energy_series[0][i-1]>_energy_series[0][i] && _energy_series[0][i]<_energy_series[0][i+1]) { series_add(serien[4], _energy_series[0][i]); //Maxima series_add(serien[5], i-last_max); // Laenge der Maxima series_add(serien[6], _energy_series[0][i]-_energy_series[0][last_max]); // Groesse der Maxima series_add(serien[7], (_energy_series[0][i]-_energy_series[0][last_min])/(i-last_max)); // Steigung der Maxima last_max=i; } } getStatistics(features+fcount,_energy_series[0],ser); // energy statistics en_mean=features[fcount+1]; fcount+=STATS; for (i=0;i<series_size;i++) { getStatistics(features+fcount,serien[i]->series,serien[i]->nSeries); fcount+=STATS; } max=serien[0]->nSeries; for (j=1;j<3;j++) { series_reset(serien[0]); series_reset(serien[1]); for (i=1;i<ser-1;i++) { if (_energy_series[j][i-1]<_energy_series[j][i] && _energy_series[j][i]>_energy_series[j][i+1]) { series_add(serien[0], _energy_series[j][i]); } else if (_energy_series[j][i-1]>_energy_series[j][i] && _energy_series[j][i]<_energy_series[j][i+1]) { series_add(serien[1], _energy_series[j][i]); } } getStatistics(features+fcount,_energy_series[j],ser); // energy delta statistics fcount+=STATS; getStatistics(features+fcount,serien[0]->series,serien[0]->nSeries); fcount+=STATS; getStatistics(features+fcount,serien[1]->series,serien[1]->nSeries); fcount+=STATS; } features[fcount++]= max? (1.0 * max_sum/max) - en_mean:0; // Mittelwert der Maxima minus globaler Mittelwert features[fcount++]= (1.0 * max)/ser; // Anzahl Maxima pro Frames pro Segment for (i=0;i<series_size;i++) series_destroy(serien[i]); } else { for (;fcount < 141*STATS+9;fcount++) features[fcount]=-1; } /* Voiced/Unvoiced-Merkmal: */ features[fcount++]=nframes ? (1.0 * n_voiced_frames)/nframes : -1; // Anzahl stimmloser Frames pro Frames pro Segment /* Spectral Center of gravity-Merkmale: */ serie = getOnlyCoG(signal,fex->frame_len); if (serie) { getStatistics(features+fcount,serie->series,serie->nSeries); series_destroy(serie); fcount+=STATS; } else for (;fcount< 142*STATS+10;fcount++) features[fcount]=-1; /* Dauer-Merkmal: */ features[fcount++]=fex->frame_len; /* Harmonics-to-Noise-Ratio-Merkmale: */ hnr=hnr_calc(fex->hnr,signal,fex->frame_len); nframes=fex->hnr->nframes; if (!hnr) for (;fcount<145*STATS+11;fcount++) features[fcount]=-1.0; else { const int series_size=3; // @begin_change_johannes //fextract_series_t *serien[series_size]; fextract_series_t *serien[3]; // @end_change_johannes pitch_frame_candidates_destroy(fex->hnr); for (i=0;i<series_size;i++) serien[i]=series_create(MAX_SERIES); if (hnr[0]!=0) series_add(serien[0],hnr[0]); for (i=1;i<nframes-1;i++) { if (hnr[i]!=-200) { series_add(serien[0],hnr[i]); if ((hnr[i-1]!=-200) && (hnr[i+1]!=200)) { // stimmlose Bereiche nicht beruecksichtigen if (hnr[i-1]<hnr[i] && hnr[i]>hnr[i+1]) series_add(serien[1],hnr[i]); else if (hnr[i-1]>hnr[i] && hnr[i]<hnr[i+1]) series_add(serien[2],hnr[i]); } } } if (nframes >1 && hnr[nframes-1]!=0) series_add(serien[0],hnr[nframes-1]); for (i=0; i<series_size; i++) { getStatistics(features+fcount,serien[i]->series,serien[i]->nSeries); //hnr statistics features fcount+=STATS; } } /* Aufraeumen... */ for (i=0;i<MFCC_FEATURES;i++) if (_mfcc_series[i]) rs_free(_mfcc_series[i]); if (_mfcc_series) rs_free(_mfcc_series); for (i=0;i<ENERGY_FEATURES;i++) if (_energy_series[i]) rs_free(_energy_series[i]); if (_energy_series) rs_free(_energy_series); if (pitch) rs_free(pitch); if (hnr) rs_free(hnr); /* for (;fcount<V1_N_FEATURES;fcount++) */ /* features[fcount]=0.0; */ if (fcount != V1_N_FEATURES) rs_error("number of features is not correct (%d expected, %d calculated)!",V1_N_FEATURES,fcount); return V1_N_FEATURES; }
int Criterion(double *value, int* indices, int dim, int n_classes, fx_select_t *sel, char **file_names, int n_splits, cType classifier, char *cparam) { int i, n_feat=-1, j, n_instances=0,n; int *evaluation=NULL, *_eval=NULL; fx_select_t *sel_subset = fx_select_create(sel->n_features); classifier_t *cl = NULL; double val=0; if (ev_verbose) { fprintf(stderr,"Now evaluating subset "); printFset(indices,dim); } j=0; for (i=0; i<sel_subset->n_features && j<dim;i++) { if (sel->selected[i]==1) { n_feat++; if (indices[j]==n_feat) { sel_subset->selected[i]=1; j++; } } } if (j!=dim) { rs_warning("Problem with feature subset selection!"); return -1; } sel_subset->n_selected=dim; for (i=0;i<n_splits*2;i+=2) { char *tmpparam=NULL; if (cparam) { tmpparam=(char *) rs_malloc((strlen(cparam)+1)*sizeof(char),"classifier param"); strcpy(tmpparam,cparam); } cl=cl_new_classifier(classifier,n_classes,sel_subset->n_selected,tmpparam); cl=cl_fscan(cl,classifier,file_names[i],sel_subset); n=cl_classify_file(cl,classifier,file_names[i+1],sel_subset,&_eval); n_instances+=n; if (n_instances-n==0) evaluation=rs_malloc(n_instances*2*sizeof(int),"evaluation data"); else evaluation=rs_realloc(evaluation,n_instances*2*sizeof(int),"evaluation data"); for (j=0;j<n*2;j++) evaluation[j+(n_instances-n)*2]=_eval[j]; rs_free(_eval); if (tmpparam) rs_free(tmpparam); cl_destroy(cl,classifier); } if (evaluation) val=cl_eval(evaluation,n_classes,n_instances); rs_free(evaluation); fx_select_destroy(sel_subset); if (ev_verbose) fprintf(stderr,"... has evaluation %g\n",val); *value=val; return 0; }
void rs_compress(RSCompressionType enc, uint8_t* rawdata, size_t rawdatalen, uint8_t** gzdata, size_t* gzdatalen) { z_stream strm; int ret; /* keep track of our flush state */ int flush; /* initialize our output */ *gzdata = NULL; *gzdatalen = 0; /* return error if not gzip or zlib */ if (enc != RS_GZIP && enc != RS_ZLIB) return; /* the buffer we read from -- data is copied in to it */ uint8_t input_buffer[RS_Z_BUFFER_SIZE]; /* wheere to copy from next in rawdata */ size_t input_read_head = 0; /* temporary storage for gzip'd output */ uint8_t output_buffer[RS_Z_SMALL_BUFFER_SIZE]; /* initialize zlib state */ strm.zalloc = Z_NULL; strm.zfree = Z_NULL; strm.opaque = Z_NULL; /* start deflating, with magick gzip arguments (or not) */ /* RLE -- significantly faster, but very slightly less space efficient */ /* compression level 1 gives OK results faster -- bandwidth is cheap, latency is not */ /* FIXME configurable settings! */ ret = deflateInit2(&strm, 1, Z_DEFLATED, (enc == RS_GZIP) ? (16 + MAX_WBITS) : MAX_WBITS, 8, Z_RLE); if (ret != Z_OK) { /* level data could not be deflated */ return; } /* shovel in data until we run out */ do { size_t copylen = MIN(RS_Z_BUFFER_SIZE, rawdatalen - input_read_head); memcpy(input_buffer, &rawdata[input_read_head], copylen); input_read_head += copylen; strm.avail_in = copylen; /* set up the input pointer */ strm.next_in = input_buffer; /* set flush if this is the end */ flush = (input_read_head == rawdatalen) ? Z_FINISH : Z_NO_FLUSH; /* now, deflate until there is no more output */ do { /* set up output buffer */ strm.next_out = output_buffer; strm.avail_out = RS_Z_SMALL_BUFFER_SIZE; /* deflate */ ret = deflate(&strm, flush); /* make sure we're not corrupted */ rs_return_if_fail(ret != Z_STREAM_ERROR); /* FIXME error checking? */ /* available output */ size_t avail = RS_Z_SMALL_BUFFER_SIZE - strm.avail_out; /* copy output buffer to the end of gzdata */ *gzdata = rs_realloc(*gzdata, *gzdatalen + avail); memcpy(*gzdata + *gzdatalen, output_buffer, avail); *gzdatalen += avail; } while (strm.avail_out == 0); /* be sure we used up all input */ rs_return_if_fail(strm.avail_in == 0); } while (flush != Z_FINISH); /* we've finished the stream */ rs_return_if_fail(ret == Z_STREAM_END); /* clean up zlib */ deflateEnd(&strm); }
void rs_decompress(RSCompressionType enc, uint8_t* gzdata, size_t gzdatalen, uint8_t** outdata, size_t* outdatalen) { z_stream strm; int ret; /* initialize output to NULL, so we can check later also, this means error */ *outdata = NULL; *outdatalen = 0; /* guess compression type */ if (enc == RS_AUTO_COMPRESSION) enc = rs_get_compression_type(gzdata, gzdatalen); /* if it's not GZIP or ZLIB, return error */ if (enc != RS_GZIP && enc != RS_ZLIB) return; /* our output buffer */ uint8_t output_buffer[RS_Z_BUFFER_SIZE]; size_t output_write_head = 0; /* initialize zlib state */ strm.zalloc = Z_NULL; strm.zfree = Z_NULL; strm.opaque = Z_NULL; strm.avail_in = gzdatalen; strm.next_in = gzdata; /* magick deflate init to handle gzip headers (or not!) */ ret = inflateInit2(&strm, (enc == RS_GZIP) ? (16 + MAX_WBITS) : MAX_WBITS); if (ret != Z_OK) { /* data could not be inflated */ return; } /* loop continues until the output buffer is not full */ do { /* set up the output buffer */ strm.avail_out = RS_Z_BUFFER_SIZE; strm.next_out = output_buffer; /* inflate as much as possible */ ret = inflate(&strm, Z_NO_FLUSH); /* make sure we're not clobbered */ if (ret == Z_STREAM_ERROR) rs_error("error decompressing stream"); /* FIXME */ /* handle errors */ switch (ret) { case Z_NEED_DICT: case Z_DATA_ERROR: /* level data is not valid gzip data */ inflateEnd(&strm); if (*outdata) { rs_free(*outdata); *outdata = NULL; *outdatalen = 0; } return; case Z_MEM_ERROR: /* ran out of memory */ inflateEnd(&strm); if (*outdata) { rs_free(*outdata); *outdata = NULL; *outdatalen = 0; } return; }; size_t avail = RS_Z_BUFFER_SIZE - strm.avail_out; /* do something with output_buffer */ if (*outdata == NULL) { *outdatalen = avail; *outdata = rs_malloc(avail); output_write_head = 0; } else { *outdatalen += avail; *outdata = rs_realloc(*outdata, *outdatalen); } /* write the data after it */ memcpy(*outdata + output_write_head, output_buffer, avail); output_write_head += avail; } while (strm.avail_out == 0); /* free zlib info */ inflateEnd(&strm); /* we ran out of input, so we better be at the end of the stream */ if (ret != Z_STREAM_END) { /* level data is not valid gzip data */ if (*outdata) { rs_free(*outdata); *outdata = NULL; *outdatalen = 0; } return; } /* our data is correct and properly placed, so... */ }
int _asegment_info(char *seg_info, dsp_sample_t *signal, int n_samples, dsp_sample_t ***signal_segment, int **length) { char delim; int stat=0, frame_length, start, end, i; int last=0, n_segments=0; int max_segments=MAX_SEGMENTS; int *_length; dsp_sample_t **_segments; _segments = (dsp_sample_t **) rs_malloc(max_segments * sizeof(dsp_sample_t *),"Signal segments"); _length = (int *) rs_malloc(max_segments * sizeof(int),"Segment lengths"); if ((seg_info = strchr(seg_info,'['))) seg_info++; while (seg_info && (stat = sscanf(seg_info, "%d..%d%c", &start,&end,&delim)) == 3) { if (end +1 > n_samples) { rs_warning("Wrong segmentation info: Trying to read %d samples from an audio signal that is only %d samples long!",end,n_samples); return -1; } if (n_segments >= max_segments) { max_segments+=MAX_SEGMENTS; _segments = (dsp_sample_t**) rs_realloc(_segments,max_segments*sizeof(dsp_sample_t *),"Signal segments"); _length = (int *) rs_realloc(_length,max_segments*sizeof(int),"Segment lengths"); } frame_length = end-start+1; if (last) { _segments[n_segments] = (dsp_sample_t *) rs_realloc(_segments[n_segments],(last+frame_length)*sizeof(dsp_sample_t),"signal segments"); _length[n_segments]+=frame_length; } else { _segments[n_segments] = (dsp_sample_t *) rs_malloc(frame_length *sizeof(dsp_sample_t),"signal segments"); _length[n_segments]=frame_length; } memcpy(_segments[n_segments]+last,signal+start,frame_length*sizeof(dsp_sample_t)); switch (delim) { case '-': last+=frame_length; if ((seg_info = strchr(seg_info,'-'))) seg_info++; break; case ']': last=0; n_segments++; if ((seg_info = strchr(seg_info,'['))) seg_info++; break; default: rs_warning("Problem with segmentation information: wrong delimiter '%c'!",delim); rs_free(_length); for (i=0;i<n_segments;i++) rs_free(_segments[i]); rs_free(_segments); return -1; } } if (stat !=3) { rs_warning("Problem with segmentation information: %s!",seg_info); return -1; } *signal_segment=_segments; *length=_length; return n_segments; }
int _asegment_vad(dsp_vad_t *vad, dsp_sample_t *signal, int n_samples, dsp_sample_t ***signal_segment, int **length) { int va=0, i, out_sample; int n_segments =0, last_va=0, samples=0; int max_segments=MAX_SEGMENTS, segment_length = SEGMENT_LENGTH; int delay_len=vad->sigbuf->length; int *_length; dsp_sample_t *voice; dsp_sample_t **_segments; int temp=0; dsp_sample_t *_signal; _signal = (dsp_sample_t *) rs_malloc(n_samples*sizeof(dsp_sample_t),"signal copy"); for (i=0;i<n_samples;i++) _signal[i]=signal[i]; if (!vad) { rs_warning("No voice activity detection info available!"); return -1; } voice = (dsp_sample_t *) rs_malloc(vad->frame_len * sizeof(dsp_sample_t),"Voice frame"); _segments = (dsp_sample_t **) rs_malloc(max_segments * sizeof(dsp_sample_t *),"Signal segments"); _length = (int *) rs_malloc(max_segments * sizeof(int),"Segment lengths"); for (i=0;i<=n_samples-VAD_FRAME_SHIFT || va >=0;i+=VAD_FRAME_SHIFT) { if (i > n_samples-VAD_FRAME_SHIFT) { va = dsp_vad_calc(voice,vad,NULL); } else { if (i>n_samples-vad->frame_len) { int j, new_len=i+vad->frame_len; _signal= (dsp_sample_t *) rs_realloc(_signal,new_len*sizeof(dsp_sample_t),"Signal buffer"); for (j=n_samples;j<new_len;j++) _signal[j]=0; } va = dsp_vad_calc(voice, vad, _signal+i); } if (va >=0 && ((va && !last_va) || (!va && last_va)) && samples >0) { out_sample=i-(delay_len-vad->sigbuf->need_elems)*160; if (out_sample>=n_samples) out_sample=n_samples-1; if (va) { if (n_segments >= max_segments) { max_segments += MAX_SEGMENTS; _segments = (dsp_sample_t **) rs_realloc(_segments,max_segments * sizeof(dsp_sample_t *),"Signal segments"); _length = (int *) rs_realloc(_length,max_segments * sizeof(int),"Segment lengths"); } _segments[n_segments] = (dsp_sample_t *) rs_malloc(segment_length * sizeof(dsp_sample_t),"Signal segment"); if (output) fprintf(stderr,"[%d..",out_sample); } else { if (output) fprintf(stderr,"%d] ",out_sample-1); _length[n_segments]=last_va*VAD_FRAME_SHIFT; n_segments++; segment_length=SEGMENT_LENGTH; } } if (!va) last_va=0; if (va==1 && samples>0) { if ((last_va+1)*VAD_FRAME_SHIFT > segment_length) { segment_length += SEGMENT_LENGTH; _segments[n_segments] = (dsp_sample_t *) rs_realloc(_segments[n_segments],segment_length * sizeof(dsp_sample_t),"Signal segment"); } samples+=VAD_FRAME_SHIFT; memcpy(_segments[n_segments]+last_va*VAD_FRAME_SHIFT,voice,VAD_FRAME_SHIFT*sizeof(dsp_sample_t)); last_va++; } temp=0; while (va==0) { temp++; samples+=VAD_FRAME_SHIFT; va = dsp_vad_calc(voice, vad, NULL); } } if (last_va && samples >0) { out_sample=i-(delay_len-vad->sigbuf->need_elems)*160; if (out_sample>=n_samples) out_sample=n_samples; if (output) fprintf(stderr,"%d] ",out_sample-1); _length[n_segments]=last_va*VAD_FRAME_SHIFT; n_segments++; } if (output) fprintf(stderr,";\n"); rs_free(voice); rs_free(_signal); *signal_segment=_segments; *length=_length; return n_segments; }