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

src/filter.c

Go to the documentation of this file.
00001 /* **************************************************************************
00002  *   Copyright (C) 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 "filter.h"
00022 #include "eeg.h"
00023 #include "distances.h"
00024 #include "fidlib/fidlib.h"
00025 
00026 /* ---------------------------------------------------------------------------- 
00027    -- Denoising routines                                                     -- 
00028    ---------------------------------------------------------------------------- */
00036 EEG* eeg_filter_running_median(EEG *eeg, int win, bool alloc){
00037 #ifdef FIXEEG
00038   EEG *s;
00039   int c,i;
00040   if( alloc ){
00041      s = eeg_clone( eeg, EEG_CLONE_ALL  );
00042   } else {
00043      s = eeg;
00044   }
00045   for(c=0; c<s->nbchan; c++){
00046      for(i=0; i<s->ntrials; i++){
00047         running_median(s->data[c][i], s->n, win);
00048      }
00049   }
00050   return s;
00051 #endif
00052 }
00053 
00060 double* running_median(double *d, int n, int win){
00061   double *tmp, *org;
00062   int i, awin, ewin;
00063   double med;
00064   dprintf("called with %p, of length %i, win=%i\n", d,n,win);
00065   org = (double*)malloc(n*sizeof(double));
00066   org = (double*)memcpy(org, d, n*sizeof(double));
00067   tmp = (double*)malloc((2*win+1)*sizeof(double));
00068 
00069 
00070   for(i=0; i<n; i++){
00071      awin = i-win;
00072      ewin = i+win;
00073      if(awin<0) awin=0;
00074      if(ewin>n-1) ewin=n;
00075     
00076     tmp = (double*)memcpy(tmp, &(org[awin]), sizeof(double)*(ewin-awin));
00077     gsl_sort(tmp, 1, ewin-awin);
00078      if( (ewin-awin) % 2==0 ){
00079         med = (tmp[(ewin-awin)/2]+tmp[(ewin-awin)/2+1])/2.0;
00080      } else {
00081         med = tmp[(ewin-awin)/2+1];
00082      }
00083     d[i] = med;
00084   }
00085   free(tmp);
00086   free(org);
00087   return d;
00088 }
00089 
00090 
00094 double* moving_average(double *s, int n, int win){
00095   int i, awin, ewin;
00096   double m;
00097   double *org;
00098 
00099   org = (double*)malloc(n*sizeof(double));
00100   org = (double*)memcpy(org, s, n*sizeof(double));
00101 
00102   for(i=0; i<n; i++){
00103      awin = i-win;
00104      ewin = i+win;
00105      if(awin<0) awin=0;
00106      if(ewin>n-1) ewin=n;
00107      m = dblp_mean( &(org[awin]), ewin-awin);
00108      s[i] = m;
00109   }
00110   free(org);
00111   return s;
00112 }
00113 
00121 EEG* eeg_filter_weighted_running_median( EEG *eeg, int win, bool alloc){
00122 #ifdef FIXEEG
00123   EEG *s;
00124   int c,i;
00125   if( alloc ){
00126      s = eeg_clone( eeg, EEG_CLONE_ALL );
00127   } else {
00128      s = eeg;
00129   }
00130   for(c=0; c<s->nbchan; c++){
00131      for(i=0; i<s->ntrials; i++){
00132         weighted_running_median(s->data[c][i], s->n, win );
00133      }
00134   }
00135   return s;
00136 #endif
00137 }
00138 
00139 
00143 double* weighted_running_median(double *d, int n, int win ){
00144   double *pptr, *wptr;
00145   int i, j;
00146   int awin, ewin;
00147   double med;
00148   
00149   pptr = (double*)malloc((2*win+1)*sizeof(double));
00150   wptr = (double*)malloc((2*win+1)*sizeof(double));
00151 
00152   for(i=0; i<n; i++){
00153      awin = i-win;
00154      ewin = i+win;
00155      if(awin<0) awin=0;
00156      if(ewin>n-1) ewin=n;
00157 
00158     memcpy(pptr, &(d[awin]), sizeof(double)*(ewin-awin));
00159     for(j=0; j<(ewin-awin); j++){
00160       wptr[j] = fabs(i-awin+j);
00161     }
00162 /*   dprintf("i=%i, awin=%i, ewin=%i\n", i, awin, ewin); */
00163     med = weighted_median_from_unsorted(pptr, wptr, ewin-awin);
00164      
00165     d[i] = med;
00166   }
00167   free(pptr);
00168   free(wptr);
00169 
00170   return d;
00171 }
00172 
00173 
00174 
00312 EEG* eeg_filter_fidlib( EEG *eeg, const char *spec, bool alloc ){
00313 #ifdef FIXEEG
00314   int len;
00315   int c,i,j;
00316   FidFilter *filt;
00317   char *filtspec, *orgspec;
00318   FidRun *run;
00319   FidFunc *funcp;
00320   void **fbuf;
00321 
00322   EEG *eeg_out;
00323   if( alloc ){
00324      eeg_out = eeg_clone( eeg, EEG_CLONE_ALL );
00325   } else {
00326      eeg_out = eeg;
00327   }
00328 
00329   fbuf = (void**) malloc( eeg->ntrials*sizeof(void*));
00330 
00331   filtspec = (char*) malloc( (strlen(spec)+10)*sizeof(char));
00332   orgspec = filtspec;
00333   memset( filtspec, 0, (strlen(spec)+10)*sizeof(char));
00334   strcpy( filtspec, spec );
00335   fid_parse( eeg_out->sampling_rate, &filtspec, &filt);
00336   run= fid_run_new(filt, &funcp);
00337 
00338   len= fid_run_bufsize(run);
00339   for( i=0; i<eeg_out->ntrials; i++ ){
00340      fbuf[i]= fid_run_newbuf(run);
00341   }
00342   dprintf("Buffer initialized\n");\
00343   for( c=0; c<eeg_out->nbchan; c++ ){
00344      for( i=0; i<eeg_out->ntrials; i++ ){
00345         for( j=0; j<eeg_out->n; j++ ){
00346           //        dprintf("Sample %i, channel %i\n", i, j );
00347           eeg_out->data[c][i][j] = funcp( fbuf[i], eeg_out->data[c][i][j] );
00348         }
00349         fid_run_zapbuf( fbuf[i] );
00350      }
00351   }
00352   for( i=0; i<eeg_out->ntrials; i++ ){
00353      fid_run_freebuf(fbuf[i]);
00354   }
00355   free( fbuf );
00356   fid_run_free(run);
00357   free( filt );
00358   free( orgspec ) ;
00359 
00360   return eeg_out;
00361 #endif
00362 }

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