• API Main Page
  • Documentation
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

src/hmm.c

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2008/2009 by Matthias Ihrke   *
00003  *   mihrke@uni-goettingen.de   *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 #include "hmm.h"
00022 
00023 
00036 double cphmm_get_transition_prob( CPHiddenMarkovModel *m, int k, int i, int j ){
00037   double p_phi=0, p_tau=0;
00038 
00039 
00040   if( j-i >= 0 && j-i < m->J ){
00041      p_tau = m->d[j-i];
00042   }
00043   
00044   return p_phi*p_tau;
00045 }
00046 
00047 
00055 CPHiddenMarkovModel* cphmm_alloc( int K, int n, int M, int Q, int J ){
00056   CPHiddenMarkovModel *m;
00057   int i;
00058 
00059   m = (CPHiddenMarkovModel*)malloc( sizeof(CPHiddenMarkovModel) );
00060   m->K = K;
00061   m->n = n;
00062   m->M = M;
00063   m->Q = Q;
00064   m->J = J; 
00065   
00066   m->q = (double*) malloc( Q*sizeof(double) );
00067   m->u = (double*) malloc( K*sizeof(double) );
00068   m->d = (double*) malloc( J*sizeof(double) );
00069   m->z = (double*) malloc( M*sizeof(double) );
00070 
00071   m->tau = (double**) malloc( K*sizeof(double*) );
00072   m->phi = (int   **) malloc( K*sizeof(int   *) );
00073   for( i=0; i<K; i++ ){
00074      m->tau[i] = (double*) malloc( n*sizeof(double) );
00075      m->phi[i] = (int   *) malloc( n*sizeof(int   ) );
00076   }
00077 
00078   gsl_rng_env_setup();
00079   m->random_number_type = (gsl_rng_type *)gsl_rng_default;
00080   m->rng = gsl_rng_alloc (m->random_number_type);
00081 
00082   return m;
00083 }
00084 
00097 void cphmm_init( CPHiddenMarkovModel *m, double **X ){
00098   int i;
00099   double minx, maxx;
00100   int start;
00101 
00102   minx = dblp_min( X[0], m->n, NULL );
00103   maxx = dblp_max( X[0], m->n, NULL );
00104   m->sigma = 0.15*(maxx-minx);
00105 
00106   start = (m->M-(2*m->n))/2.0;
00107   if( start<0 ){
00108      warnprintf("M<2*n: %i<%i -> skipping z-initialization\n", m->M, 2*m->n);
00109   }
00110 
00111   for( i=0; i<start; i++ ){
00112      m->z[i] = minx;
00113   }
00114   for( i=start; i<2*m->n; i++ ){
00115      m->z[i] = X[0][i-start]+gsl_ran_gaussian( m->rng, m->sigma );
00116   }
00117 
00118   for( i=0; i<m->K; i++ ){
00119      m->u[i] = 1;
00120   }
00121   for( i=0; i<m->J; i++ ){
00122      m->d[i] = i/(double)m->J;
00123   }
00124   m->s[0] = 0.5;
00125   m->s[1] = 0.25;
00126 
00127   for( i=0; i<m->Q; i++ ){
00128      m->q[i] = i*(2.0/(double)m->Q);
00129   }
00130 
00131 }
00132 
00140 CPHiddenMarkovModel* eeg_cphmm_init( EEG *eeg, int channel, double **X ){
00141 #ifdef FIXEEG
00142   CPHiddenMarkovModel *m;
00143   int M;
00144   double epsilon = 0.2;
00145   int Q;
00146   int J;
00147   int i;
00148 
00149   M = (2+epsilon)*eeg->n;
00150   Q = 7;
00151   J = 3;
00152 
00153   m = cphmm_alloc( eeg->ntrials, eeg->n, M, Q, J );
00154   for( i=0; i<m->K; i++ ){
00155      X[i] = eeg->data[channel][i];
00156   }
00157   cphmm_init( m, X );
00158 
00159   return m;
00160 #endif
00161 }
00162 
00165 void cphmm_free( CPHiddenMarkovModel *m ){
00166   int i;
00167   if(m->q) free(m->q);
00168   if(m->u) free(m->u);
00169   if(m->d) free(m->d);
00170   if(m->z) free(m->z);
00171   for( i=0; i<m->K; i++ ){
00172      if( m->tau[i] )
00173         free( m->tau[i] );
00174      if( m->phi[i] )
00175         free( m->phi[i] );
00176   }
00177   if( m->tau ) 
00178      free( m->tau );
00179   if( m->phi )
00180      free( m->phi );
00181 
00182   gsl_rng_free (m->rng);
00183 }

Generated on Fri Jun 25 2010 14:10:18 for libeegtools by  doxygen 1.7.0