plugins/qm-dsp/dsp/segmentation/cluster_segmenter.c
/* segment using HMM and then histogram clustering */
segment using HMM and then histogram clustering
segment using HMM and then histogram clustering
void cluster_segment(int* q, double** features, int frames_read, int feature_length, int nHMM_states, 
                     int histogram_length, int nclusters, int neighbour_limit)
{
    int i, j;
    if (0) {
        nHMM_states = feature_length + 1;       
        double chroma_thresh = 0.05;
        double maxval;
        int maxbin;
        for (i = 0; i < frames_read; i++) {
            maxval = 0;
            for (j = 0; j < feature_length; j++) {
                if (features[i][j] > maxval) {
                    maxval = features[i][j];
                    maxbin = j;
                }                               
            }
            if (maxval > chroma_thresh) {
                q[i] = maxbin;
            } else {
                q[i] = feature_length;
            }
        }
    }
    if (1) {
        double scale = 10;
        for (i = 0; i < frames_read; i++)
            for (j = 0; j < feature_length; j++)
                features[i][j] *= scale;
        model_t* model = hmm_init(features, frames_read, feature_length, nHMM_states);
        hmm_train(features, frames_read, model);
        viterbi_decode(features, frames_read, model, q);  
        hmm_close(model);
    }
    double* h = (double*) malloc(frames_read*nHMM_states*sizeof(double));   
    create_histograms(q, frames_read, nHMM_states, histogram_length, h);
    int nbsched = 20;       
    double* bsched = (double*) malloc(nbsched*sizeof(double));      
    double b0 = 100;
    double alpha = 0.7;
    bsched[0] = b0;
    for (i = 1; i < nbsched; i++) {
        bsched[i] = alpha * bsched[i-1];
    }
    cluster_melt(h, nHMM_states, frames_read, bsched, nbsched, nclusters, neighbour_limit, q);
    free(h);  
    free(bsched);
}
void cluster_segment ( int * q , double * * features , int frames_read , int feature_length , int nHMM_states , int histogram_length , int nclusters , int neighbour_limit ) { int i , j ; if ( 0 ) { nHMM_states = feature_length + 1 ; double chroma_thresh = 0.05 ; double maxval ; int maxbin ; for ( i = 0 ; i < frames_read ; i ++ ) { maxval = 0 ; for ( j = 0 ; j < feature_length ; j ++ ) { if ( features [ i ] [ j ] > maxval ) { maxval = features [ i ] [ j ] ; maxbin = j ; } } if ( maxval > chroma_thresh ) { q [ i ] = maxbin ; } else { q [ i ] = feature_length ; } } } if ( 1 ) { double scale = 10 ; for ( i = 0 ; i < frames_read ; i ++ ) for ( j = 0 ; j < feature_length ; j ++ ) features [ i ] [ j ] *= scale ; model_t * model = hmm_init ( features , frames_read , feature_length , nHMM_states ) ; hmm_train ( features , frames_read , model ) ; viterbi_decode ( features , frames_read , model , q ) ; hmm_close ( model ) ; } double * h = ( double * ) malloc ( frames_read * nHMM_states * sizeof ( double ) ) ; create_histograms ( q , frames_read , nHMM_states , histogram_length , h ) ; int nbsched = 20 ; double * bsched = ( double * ) malloc ( nbsched * sizeof ( double ) ) ; double b0 = 100 ; double alpha = 0.7 ; bsched [ 0 ] = b0 ; for ( i = 1 ; i < nbsched ; i ++ ) { bsched [ i ] = alpha * bsched [ i - 1 ] ; } cluster_melt ( h , nHMM_states , frames_read , bsched , nbsched , nclusters , neighbour_limit , q ) ; free ( h ) ; free ( bsched ) ; }
segment using HMM and then histogram clustering
segment using HMM and then histogram clustering
/*****************************/ /* try just using the predominant bin number as a 'decoded state' */ /* allow a 'zero' state */ /*****************************/ /* scale all the features to 'balance covariances' during HMM training */ /* train an HMM on the features */ /* create a model */ /* train the model */ /*      
        printf("\n\nafter training:\n");
        hmm_print(model);
*/ /* decode the hidden state sequence */ /*****************************/ /*****************************/ /*
  fprintf(stderr, "HMM state sequence:\n");
  for (i = 0; i < frames_read; i++)
  fprintf(stderr, "%d ", q[i]);
  fprintf(stderr, "\n\n");
*/ /* create histograms of states */ /* vector in row major order */ /* cluster the histograms */ /* length of inverse temperature schedule */ /* inverse temperature schedule */ /* now q holds a sequence of cluster assignments */