Ejemplo n.º 1
0
int _asegment_fixed(char *seg_info, dsp_sample_t *signal, int n_samples, dsp_sample_t ***signal_segment, int **length) {
	int i, frame_length, frame_shift;
	int n_segments =0;
	int *_length;
	dsp_sample_t **_segments;
	
	if (sscanf(seg_info,"%d:%d",&frame_length,&frame_shift) != 2) {
		rs_error("Frame length and/or frame shift information in wrong format!");
		return -1;
	}
	n_segments= (n_samples - frame_length) / frame_shift + 1;

	_segments = (dsp_sample_t **) rs_malloc(n_segments * sizeof(dsp_sample_t *),"Signal segments");
	_length = (int *) rs_malloc(n_segments*sizeof(int),"Segment lengths");

	for (i=0;i<n_segments;i++) {
		_segments[i] = (dsp_sample_t *) rs_malloc(frame_length * sizeof(dsp_sample_t),"signal segments"); 
		_length[i]=frame_length;
	
		if (!memcpy(_segments[i],signal+i*frame_shift,frame_length*sizeof(dsp_sample_t))) {
			rs_warning("Signal was shorter than expected!");
			return -1;
		}
	}

	*signal_segment=_segments;
	*length=_length;
	return n_segments;
}
Ejemplo n.º 2
0
fextract_series_t *series_create(int max){
  fextract_series_t *data_series= (fextract_series_t *) rs_malloc(sizeof(fextract_series_t),"data series");
  data_series->maxSeries=max;
  data_series->nSeries=0;
  data_series->series= (mx_real_t *) rs_malloc(max*sizeof(mx_real_t),"data of series");
  return data_series;
}
Ejemplo n.º 3
0
fextract_t *fextract_pitch_energy_create(int frame_len, char *m_e_params) {
	fextract_t *fex;
	dsp_fextract_t *mfcc;

	fex = (fextract_t *) rs_malloc(sizeof(fextract_t),"emotion feature extraction data");
  
	fex->n_features = V2_N_FEATURES;

  
	/* Abtastrate etc. ... */
	fex->samplerate = SAMPLERATE;
	fex->frame_len = frame_len; /* global frame length (in frames) */
  
	fex->pitch = pitch_create(AC_GAUSS);
    
	fex->frame_shift = fex->frame_len - ((fex->pitch->method == AC_GAUSS? 2 : 1 ) * fex->pitch->periodsPerWindow / fex->pitch->minimumPitch - fex->pitch->dt) * fex->samplerate ; /* global shift (in frames) */
  

	fex->hnr = NULL;
  
	fex->vq = NULL;
  
	/* MFCCs ... */
	mfcc = (dsp_fextract_t *) rs_malloc(sizeof(dsp_fextract_t), "feature extraction data");
	mfcc->type = dsp_fextype_MFCC;
	mfcc->version = DSP_MK_VERSION(1, 4);
	if (!dsp_mfcc_create(mfcc, m_e_params)) {
		rs_free(mfcc);
		mfcc = NULL;
		rs_warning("Could not initialize MFCC configuration!");
	}
	fex->mfcc=mfcc;
  
	return(fex);
}
Ejemplo n.º 4
0
mx_real_t svm_class_prob(svm_classifier_t *svm, mx_real_t *instance, int class_ind) {
    int i, svm_type=svm_get_svm_type(svm->model);
    double *prob_estimates=NULL;
    struct svm_node *x;
	mx_real_t class_prob;

    if (svm_type!=C_SVC && svm_type != NU_SVC)
	rs_error("Cannot give class probability for 1-class or regression SVM!");

    x = (struct svm_node *) rs_malloc((svm->feature_dim+1)*sizeof(struct svm_node),"Feature vector representation for svm");
    for (i=0;i<svm->feature_dim;i++) {
		x[i].index=i+1;
		x[i].value=instance[i];
    }
    x[i].index=-1;

    _scale_instance(&x,svm->feature_dim,svm->max,svm->min);
    
    prob_estimates = (double *) rs_malloc(svm->n_classes*sizeof(double),"SVM probability estimates");
    
    svm_predict_probability(svm->model,x,prob_estimates);
    
	class_prob = prob_estimates[class_ind];
	rs_free (x);
	rs_free (prob_estimates);
	return class_prob;
}
Ejemplo n.º 5
0
		SignatureEventData(const void *_data,int32_t _len,unsigned int _signlen, std::string _reason)
		{
			// We need a new memory chnk because there's no guarranty _sign nor _signlen are not in the stack

			sign = (unsigned char *)rs_malloc(_signlen) ;
            
            		if(!sign)
		    {
			    signlen = NULL ;
			    signature_result = SELF_SIGNATURE_RESULT_FAILED ;
			    return ;
		    }
                    
			signlen = new unsigned int ;
			*signlen = _signlen ;
            signature_result = SELF_SIGNATURE_RESULT_PENDING ;
			data = rs_malloc(_len) ;
            
            		if(!data)
                    {
                        len = 0 ;
                        return ;
                    }
			len = _len ;
			memcpy(data,_data,len) ;
			reason = _reason ;
		}
Ejemplo n.º 6
0
/**	Create a new dsp_csp_data_t struct. 
 * 
 *	Use this function to create and initialize a new dsp_csp_data_t 
 *	structure. NULL will be returned when the parameters are not valid. 
 *	Valid parameters are: 
 *	- n_channels > 0 
 *	- n_elements > 0 
 * 
 *	@param	n_channels	number of channel for the buffer. 
 *	@param	n_elements	number of max elements for each channel. 
 * 
 *	@return	pointer to the new created structure or NULL. 
 */ 
dsp_csp_data_t* dsp_csp_data_create (int n_channels, int n_elements) 
{ 
	dsp_csp_data_t* data = NULL; 
	int channel; 
 
	/* first check parameters ... */ 
	if ((n_channels < 1) || (n_elements < 1)) 
		return (NULL); 
 
	/* ... allocate memory for csp_data element ... */ 
	data = rs_malloc (sizeof(dsp_csp_data_t), "csp-data element"); 
 
	/* ... and initialize it */ 
	(data->n_channels) = n_channels; 
	(data->max_len)    = n_elements; 
	(data->n_len)      = rs_malloc ((n_channels * sizeof(int)), "csp-data element"); 
	(data->data)       = rs_malloc ((n_channels * sizeof(mx_real_t*)), "csp-data element"); 
 
	for (channel = 0; channel < n_channels; channel++) { 
		(data->n_len)[channel] = 0; 
		(data->data) [channel] = rs_malloc ((n_elements * sizeof(mx_real_t)), "csp-data element"); 
	}	 
	 
	return (data); 
} 
Ejemplo n.º 7
0
lsFloat_t * lsFloat_ConsNil(float i)
{
  lsFloat_t * il = rs_malloc(sizeof(lsFloat_t), "list");

  il->list = rs_malloc((il->max_list=LIST_BUF) * sizeof(float), "list item");
  il->list[0] = i;
  il->n_list = 1;
  return (il);
}
Ejemplo n.º 8
0
list_t * ls_ConsNil(LIST_TYPE i)
{
  list_t * il = rs_malloc(sizeof(list_t), "list");

  il->list = rs_malloc((il->max_list=LIST_BUF) * sizeof(LIST_TYPE), "list item");
  il->list[0] = i;
  il->n_list = 1;
  return (il);
}
Ejemplo n.º 9
0
lsDouble_t * lsDouble_ConsNil(double i)
{
  lsDouble_t * il = rs_malloc(sizeof(lsDouble_t), "list");

  il->list = rs_malloc((il->max_list=LIST_BUF) * sizeof(double), "list item");
  il->list[0] = i;
  il->n_list = 1;
  return (il);
}
Ejemplo n.º 10
0
rs_IO *rs_fopen(char *name, char *mode)
	{
	FILE *fp;
	rs_IO *io;
	int mode_read, mode_mapped;
	void *buf;
	int buf_size;

	/* Lese-/Schreibmodus bestimmen ... */
	if (strchr(mode, 'w') || strchr(mode, 'a'))
		mode_read = 0;
	else if (strchr(mode, 'r'))
		mode_read = 1;
	else	rs_error("illegal mode '%s' for 'rs_fopen()'!", mode);

	/* ... ggf. ge-"mapped" (nur lesend) ... */
	if (strchr(mode, 'm')) {
		if (mode_read)
			mode_mapped = 1;
		else	{
			mode_mapped = 0;
			rs_warning("mapped file IO not supported for writing!");
			}
		}
	else	mode_mapped = 0;

	/* ... Spezialnamen '-' umsetzen ... */
	if (strcmp(name, "-") == 0) {
		if (mode_mapped)
			rs_error("mapped file IO not supported for stdin/out!");

		fp = (mode_read) ? stdin : stdout;
		}
	else	fp = fopen(name, (mode_read) ? "r" : "w");
	if (!fp)
		return(NULL);
	
	if (!mode_mapped) {
		io = rs_malloc(sizeof(rs_IO), "IO data");
		io->type = rs_iotype_file;
		io->base_size = RS_BINIO_BASE_SIZE_DEFAULT;
		io->fp = fp;
		}
	else	{
		fseek(fp, 0, SEEK_END);
		buf_size = ftell(fp);
		rewind(fp);

		buf = rs_malloc(buf_size, "mapped file buffer");
		fread(buf, buf_size, 1, fp);

		io = rs_mopen(buf, buf_size, "r");
		}

	return(io);
	}
Ejemplo n.º 11
0
fextract_series_t *getOnlyCoG(dsp_sample_t *signal, int nframes) {
	int n_samples, offset=0, frames=0, i;
	dsp_sample_t s[MAX_SAMPLES], last_s[MAX_SAMPLES];
	dsp_sample_t *frame, *last_frame, *__tmp;
	mx_real_t *window;
	mx_complex_t *z;
	fextract_series_t *serie=NULL;
	
   	serie=series_create(MAX_SERIES);

	frame = s; 
	last_frame = last_s;

	window = (mx_real_t *) rs_malloc(sizeof(mx_real_t) * SP_FRAME_LEN, "w");
	z = (mx_complex_t *) rs_malloc(sizeof(mx_complex_t) * SP_FRAME_LEN, "z");

	n_samples = next_mfcc_frame(frame, NULL, SP_FRAME_LEN, SP_FRAME_RATE, signal,nframes);
	offset += SP_FRAME_LEN;

	while (n_samples > 0) {
    	if (n_samples < SP_FRAME_LEN)
    		break;

    	frames++;
	
		// FFT: ev. mit prae-emphase und zero-padding
		/* ... Hamming-Fenstern ... */
		dsp_window_hamming(window, frame, SP_FRAME_LEN);
		
 		for (i = 0; i < SP_FRAME_LEN; i++) {
			mx_re(z[i]) = window[i];
			mx_im(z[i]) = 0.0;
		}
				
		/* FFT */
		dsp_xfft(z, SP_FRAME_LEN, 0);
    
    	series_add(serie,get_CoG(z,SP_FRAME_LEN));
    
    
	    /* ... und naechsten Frame einlesen */
    	__tmp = last_frame;
    	last_frame = frame;
    	frame = __tmp;
    	n_samples = next_mfcc_frame(frame, last_frame, SP_FRAME_LEN, SP_FRAME_RATE, signal+offset,nframes-offset);
    	offset += SP_FRAME_RATE;
	}

	rs_free(window);
	rs_free(z);

	return serie;
}
Ejemplo n.º 12
0
bool JPEGVideo::encodeData(const QImage& image,uint32_t /* size_hint */,RsVOIPDataChunk& voip_chunk)
{
    // check if we make a diff image, or if we use the full frame.

    QImage encoded_frame ;
    bool differential_frame ;

    if (_encoded_ref_frame_count++ < _encoded_ref_frame_max_distance
        && image.size() == _encoded_reference_frame.size()
        && image.byteCount() == _encoded_reference_frame.byteCount())
	{
	    // compute difference with reference frame.
	    encoded_frame = image ;

	    for(int i=0;i<image.byteCount();++i)
	    {
		    // We cannot use basic modulo 256 arithmetic, because the decompressed JPeg frames do not follow the same rules (values are clamped)
		    // and cause color blotches when perturbated by a differential frame.

		    int diff = ( (int)image.bits()[i] - (int)_encoded_reference_frame.bits()[i]) + 128;
		    encoded_frame.bits()[i] = (unsigned char)std::max(0,std::min(255,diff)) ;
	    }

	    differential_frame = true ;
    }
    else
    {
	    _encoded_ref_frame_count = 0 ;
	    _encoded_reference_frame = image.copy() ;
	    encoded_frame = image ;

	    differential_frame = false ;
    }

    QByteArray qb ;

    QBuffer buffer(&qb) ;
    buffer.open(QIODevice::WriteOnly) ;
    encoded_frame.save(&buffer,"JPEG") ;

    voip_chunk.data = rs_malloc(HEADER_SIZE + qb.size());
    
    if(!voip_chunk.data)
        return false ;

    // build header
    uint32_t flags = differential_frame ? JPEG_VIDEO_FLAGS_DIFFERENTIAL_FRAME : 0x0 ;

    ((unsigned char *)voip_chunk.data)[0] =  VideoProcessor::VIDEO_PROCESSOR_CODEC_ID_JPEG_VIDEO       & 0xff ;
    ((unsigned char *)voip_chunk.data)[1] = (VideoProcessor::VIDEO_PROCESSOR_CODEC_ID_JPEG_VIDEO >> 8) & 0xff ;
    ((unsigned char *)voip_chunk.data)[2] = flags & 0xff ;
    ((unsigned char *)voip_chunk.data)[3] = (flags >> 8) & 0xff ;

    memcpy(&((unsigned char*)voip_chunk.data)[HEADER_SIZE],qb.data(),qb.size()) ;

    voip_chunk.size = HEADER_SIZE + qb.size() ;
    voip_chunk.type = RsVOIPDataChunk::RS_VOIP_DATA_TYPE_VIDEO ;

    return true ;
}
Ejemplo n.º 13
0
lsBuffer_t *lsBuffer_init(lsBuffer_t *b, 
			  lsBuffer_Copy_t *_copy, lsBuffer_Free_t *_free)
{
  int err;

  if (!b)
    b = rs_malloc(sizeof(lsBuffer_t),"lsBuffer");

  lsPt_nil(&b->new);
  lsPt_nil(&b->orig);
  lsPt_nil(&b->copy);
  lsInt_nil(&b->status);
  b->update = 1;

  if ((err = pthread_mutex_init(&b->mutexNew, NULL)) != 0) {
    rs_error("lsBuffer_init: error initializing mutex: %d.", err);
  }
  if ((err = pthread_mutex_init(&b->mutexOrig, NULL)) != 0) {
    rs_error("lsBuffer_init: error initializing mutex: %d.", err);
  }
  if ((err = pthread_mutex_init(&b->mutexCopy, NULL)) != 0) {
    rs_error("lsBuffer_init: error initializing mutex: %d.", err);
  }
  if (!_copy || !_free)
    rs_error("lsBuffer_init: no copy or free function specified.");

  b->_copy = _copy;
  b->_free = _free;
  
  return (b);
}
Ejemplo n.º 14
0
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;
}
Ejemplo n.º 15
0
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);
	}
Ejemplo n.º 16
0
naive_bayes_classifier_t *nB_fscan(naive_bayes_classifier_t *nB, char *filename, fx_select_t *sel) { 
	int class_ind, start, end, i;
    FILE *fp = NULL, *feature_fp = NULL;
    mx_real_t *features=NULL;
    char *line=NULL, *token;
    char *fname =NULL;
    char *class_name = (char *) rs_malloc(STRING_LENGTH*sizeof(char),"String");
    int full_feature_dim=sel?sel->n_features:nB->feature_dim;

    if (!nB)
		rs_error("classifier could not be initialised!");
     
	fp = fopen(filename,"r");
    if (fp == NULL) {
		rs_error("Cannot open file %s!",filename);
    }
    else {
		features = (mx_real_t *) rs_calloc(full_feature_dim, sizeof(mx_real_t), "feature vector");
	 
	 	while ((line= (char *) rs_line_read(fp,';'))) {
	    	fname = (char *) strtok(line,"\t ");
	     	feature_fp = fopen(fname, "r");
	     	if (feature_fp == NULL) {
		 		rs_warning("can't open '%s '!", fname);
		 		while (getchar() != '\n');
		 			continue;
	     	}
	     	line+= strlen(fname)+1;

		    if (feature_fp) {
				for (token = (char *) strtok(line," "); token != NULL; token = (char *) strtok(NULL, " ")) {
		     		if (sscanf(token,"%[^[]s",class_name) !=1 ) 
						rs_error("wrong length information for file %s!",fname);
		     		token += strlen(class_name);
		     		if (sscanf(token,"[%d..%d]",&start,&end) !=2)
			 			rs_error("wrong length information for file %s!",fname);
		     		for (i=start;i<=end;i++) {
			 			if (fread(features,sizeof(mx_real_t),full_feature_dim,feature_fp)!=full_feature_dim)
			     			rs_perror("fread stopped after %d elements in file %s -- %d", i, fname, errno);
			 			else {
							if (sel) 
							    if ( fx_select_apply(&features,sel,features)!= sel->n_features)
									rs_error("Feature selection did not succeed!");
							class_ind = cl_name2number(&(nB->mapping),class_name,nB->n_classes);
							nB=nB_update_classifier(nB,class_ind,features);
			 			}
		     		}
				}
				fclose(feature_fp);
	     	}
		}
		nB_finish_classifier(nB);
	}
    fclose(fp);
    rs_free(class_name);
    rs_free(features);
    
    return nB;
}
Ejemplo n.º 17
0
int
pf_anchor_setup(struct pf_rule *r, const struct pf_ruleset *s,
    const char *name)
{
	char			*p, *path;
	struct pf_ruleset	*ruleset;

	r->anchor = NULL;
	r->anchor_relative = 0;
	r->anchor_wildcard = 0;
	if (!name[0])
		return (0);
	path = (char *)rs_malloc(MAXPATHLEN);
	if (path == NULL)
		return (1);
	if (name[0] == '/')
		strlcpy(path, name + 1, MAXPATHLEN);
	else {
		/* relative path */
		r->anchor_relative = 1;
		if (s->anchor == NULL || !s->anchor->path[0])
			path[0] = 0;
		else
			strlcpy(path, s->anchor->path, MAXPATHLEN);
		while (name[0] == '.' && name[1] == '.' && name[2] == '/') {
			if (!path[0]) {
				DPFPRINTF(LOG_NOTICE,
				    "pf_anchor_setup: .. beyond root");
				rs_free(path);
				return (1);
			}
			if ((p = strrchr(path, '/')) != NULL)
				*p = 0;
			else
				path[0] = 0;
			r->anchor_relative++;
			name += 3;
		}
		if (path[0])
			strlcat(path, "/", MAXPATHLEN);
		strlcat(path, name, MAXPATHLEN);
	}
	if ((p = strrchr(path, '/')) != NULL && !strcmp(p, "/*")) {
		r->anchor_wildcard = 1;
		*p = 0;
	}
	ruleset = pf_find_or_create_ruleset(path);
	rs_free(path);
	if (ruleset == NULL || ruleset->anchor == NULL) {
		DPFPRINTF(LOG_NOTICE,
		    "pf_anchor_setup: ruleset");
		return (1);
	}
	r->anchor = ruleset->anchor;
	r->anchor->refcnt++;
	return (0);
}
Ejemplo n.º 18
0
lsFloat_t *  lsFloat_nil(lsFloat_t *il)
{
  if (!il) il = rs_malloc(sizeof(lsFloat_t), "list");
  il->list = NULL;
  il->n_list = 0;
  il->max_list = 0;

  return (il);
}
Ejemplo n.º 19
0
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;
}
Ejemplo n.º 20
0
svm_classifier_t *svm_finish_classifier(svm_classifier_t *svm) {
    int i, j, nr_fold=5, total_correct, best_correct=0, best_c=svm->param.C, best_g=svm->param.gamma;
    const char *error_msg;
    double *result = (double *) rs_malloc(sizeof(double)*svm->problem.l,"cross validation result");

    rs_msg("Building SVM classifier...");

    if (svm->finished)
	rs_warning("SVM classifier is already trained!");

    error_msg = svm_check_parameter(&(svm->problem),&(svm->param));
    
    if(error_msg) 
	rs_error("%s",error_msg);
    
    /* Skalierung */
    _create_scaling(svm->problem,svm->feature_dim,&(svm->max),&(svm->min));
    
    for (i=0;i<svm->problem.l;i++) 
	_scale_instance(&(svm->problem.x[i]),svm->feature_dim,svm->max,svm->min);
    
    /* Cross-Validation, um C und G zu bestimmen bei RBF-Kernel */
    if (svm->param.kernel_type == RBF) {
	svm->param.probability = 0;
	for (i=0;i<C_G_ITER;i++) {
	    total_correct=0;
	    svm->param.C=pow(2,C[i]);
	    svm->param.gamma=pow(2,G[i]);
	    svm_cross_validation(&(svm->problem),&(svm->param),nr_fold,result);
	    for(j=0;j<svm->problem.l;j++) {
		if(result[j] == svm->problem.y[j])
		    ++total_correct;
	    }
	    if (total_correct > best_correct) {
		best_correct=total_correct;
		best_c=C[i];
		best_g=G[i];
	    }
	    rs_msg("C-G-Selektion-Iteration # %d: tried c=%g and g=%g => CV rate is %g; current best c=%g and g=%g with CV rate %g",i+1,pow(2,C[i]),pow(2,G[i]),total_correct*100.0/svm->problem.l,pow(2,best_c),pow(2,best_g),best_correct*100.0/svm->problem.l);
	}
	
	/* Training */
	svm->param.C=pow(2,best_c);
	svm->param.gamma=pow(2,best_g);
	svm->param.probability = 1;
    }
    
    svm->model=svm_train(&(svm->problem),&(svm->param));
    svm->finished=1;

	// @begin_add_johannes
	rs_free (result);
	// @end_add_johannes

    return svm;
}
Ejemplo n.º 21
0
dsp_vad_t *dsp_vad_create(int version, int frame_len)
	{
	dsp_vad_t *vad;

	/* Parameter pruefen ... */
	if (frame_len <= 0)
		return(NULL);

	/* ... speziellen Datenbereich erzeugen ... */
	vad = rs_malloc(sizeof(dsp_vad_t), "VAD configuration data");

	/* ... und gemaess Version fuellen ... */
	vad->version = version;
	vad->frame_len = frame_len;
	vad->state = dsp_vad_no_decision;
	vad->n_no_va_frames = 0;

	vad->signal = rs_malloc(sizeof(dsp_sample_t) * vad->frame_len,
				"VAD frame buffer");
		/* optionale Eintraege - noch nicht verfuegbar! */
	vad->sigbuf = NULL;
	vad->ehist = NULL;

	switch(vad->version) {
		case DSP_MK_VERSION(1, 0):
			/* ... Signalpuffer hinzufuegen ... */
			vad->sigbuf = dsp_delay_create(V1_0_BUFFER_LEN,
					sizeof(dsp_sample_t), vad->frame_len);

			/* ... und begrenztes Energiehistogramm erzeugen */
			vad->ehist = mx_histogram_create(
				V1_0_EHIST_MIN, V1_0_EHIST_MAX, V1_0_EHIST_RES);
			mx_histogram_limit_set(vad->ehist, V1_0_EHIST_LEN);
			vad->last_idx = -1;

			break;
		default:
			dsp_vad_destroy(vad);
			return(NULL);
		}

	return(vad);
	}
Ejemplo n.º 22
0
btree_t *btree_nil(btree_t *t)
{
    if (!t) {
	t = (btree_t *) rs_malloc(sizeof(btree_t));
    }
    ls_nil(&t->list);
    lsPairInt_nil(&t->children);
    lsInt_nil(&t->parent);
    return (t);
}
Ejemplo n.º 23
0
/* for numerically stable scalar sum ... */
mx_ssum_t *mx_ssum_create(void)
	{
	mx_ssum_t *ssum;

	/* create emtpy container ... */
	ssum = rs_malloc(sizeof(mx_ssum_t), "numerically stable vector sum");

	mx_ssum_zero(ssum);

	return(ssum);
	}
Ejemplo n.º 24
0
/* compute mean, maximum, minimum, difference between min & max, variance, median, 
   1st quartile, 3rd quartile, interquartile range of a list of real values */
int getStatistics(mx_real_t* features, mx_real_t* list,int count) {
  int i;
  mx_real_t *_list;
  mx_real_t mean=0, max=-MX_REAL_MAX, min=MX_REAL_MAX, variance=0;
  mx_real_t median, quartile_1, quartile_3;

  if (count==0) {
    for (i=0;i<STATS;i++)
      features[i]=0.0;
    return STATS;
  }


  if (count==1) {
    features[0]=features[1]=features[2]=features[5]=features[6]=features[7]=list[0];
    features[3]=features[4]=features[8]=0.0;
    return STATS;
  }

  _list= (mx_real_t *) rs_malloc(count*sizeof(mx_real_t),"statistics list");

  for (i=0;i<count;i++) {
    _list[i]=list[i];
    mean+=_list[i];
    variance+=mx_sqr(_list[i]);
    if (_list[i] <min)
      min=_list[i];
    if (_list[i] >max)
      max=_list[i];
  }
  
  mean /= count;
  variance = variance/count - mx_sqr(mean);


  qsort(_list,count,sizeof(mx_real_t),_cmp_mx_real);

  median = _list[count/2];
  quartile_1 = _list[count/4];
  quartile_3 = _list[3*count/4];

  features[0] = mean;
  features[1] = max;
  features[2] = min;
  features[3] = max - min;
  features[4] = variance;
  features[5] = median;
  features[6] = quartile_1;
  features[7] = quartile_3;
  features[8] = fabs(quartile_3 - quartile_1);

  rs_free(_list);
  return STATS;
}
Ejemplo n.º 25
0
lsDouble_t * lsDouble_setConsNil(lsDouble_t * il, double i)
{
  if (!il)
    return lsDouble_ConsNil(i);

  if (!il->list)
    il->list = rs_malloc((il->max_list=LIST_BUF) * sizeof(double), 
			 "list item");
  il->list[0] = i;
  il->n_list = 1;
  return (il);
}
Ejemplo n.º 26
0
lsFloat_t * lsFloat_setConsNil(lsFloat_t * il, float i)
{
  if (!il)
    return lsFloat_ConsNil(i);

  if (!il->list)
    il->list = rs_malloc((il->max_list=LIST_BUF) * sizeof(float), 
			 "list item");
  il->list[0] = i;
  il->n_list = 1;
  return (il);
}
Ejemplo n.º 27
0
/* for numerically stable vector sums ... */
mx_vsum_t *mx_vsum_create(int dim)
	{
	mx_vsum_t *vsum;

	/* create emtpy container ... */
	vsum = rs_malloc(sizeof(mx_vsum_t), "numerically stable vector sum");
	vsum->dim = dim;
	vsum->sum = mx_vector_create(vsum->dim);
	vsum->err = mx_vector_create(vsum->dim);

	return(vsum);
	}
Ejemplo n.º 28
0
struct pf_anchor *
pf_find_anchor(const char *path)
{
	struct pf_anchor	*key, *found;

	key = (struct pf_anchor *)rs_malloc(sizeof(*key));
	memset(key, 0, sizeof(*key));
	strlcpy(key->path, path, sizeof(key->path));
	found = RB_FIND(pf_anchor_global, &pf_anchors, key);
	rs_free(key);
	return (found);
}
Ejemplo n.º 29
0
list_t * ls_setConsNil(list_t * il, LIST_TYPE i)
{
  if (!il)
    return ls_ConsNil(i);

  if (!il->list)
    il->list = rs_malloc((il->max_list=LIST_BUF) * sizeof(LIST_TYPE), 
			 "list item");
  il->list[0] = i;
  il->n_list = 1;
  return (il);
}
Ejemplo n.º 30
0
svm_classifier_t* svm_new_classifier(int classes, int feature_dim, char *param) {
  
  int i;
  svm_classifier_t *svm_cl = (svm_classifier_t*) rs_malloc(sizeof(svm_classifier_t),"Naive Bayes classifier");
  
  svm_cl->type = svm;
  svm_cl->n_classes = classes;
  svm_cl->feature_dim = feature_dim;
  svm_cl->finished = 0;
  svm_cl->mapping = (char**) rs_malloc(classes*sizeof(char*),"class name to number mapping");
  for (i=0;i<classes;i++) 
      svm_cl->mapping[i] = NULL;

  svm_cl->max_instances=MAX_INSTANCES;
  svm_cl->max= (double *) rs_malloc(sizeof(double)*feature_dim,"SVM array of maxima");
  svm_cl->min= (double *) rs_malloc(sizeof(double)*feature_dim,"SVM array of minima");
  svm_cl->problem.y = (double *) rs_malloc(sizeof(double)*svm_cl->max_instances,"SVM number of instances");
  svm_cl->problem.x = (struct svm_node **) rs_malloc(sizeof(struct svm_node*)*svm_cl->max_instances,"SVM list of instances");
  svm_cl->problem.l=0;

  _set_parameters(&(svm_cl->param),param);
  if (!svm_cl->param.gamma)
      svm_cl->param.gamma=1.0/svm_cl->feature_dim;

  return svm_cl;
}