filter.h File Reference

Include file for filter library. More...

#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <misc.h>
Include dependency graph for filter.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void filter (double *bufferf, double *buffer, char *type, int width, int ni, int nj, int nt)
 Filter master subroutine.
void filter_window (double **filter_window, char *type, int width)
 Filter window subroutine.

Detailed Description

Include file for filter library.

Definition in file filter.h.


Function Documentation

void filter ( double *  bufferf,
double *  buffer,
char *  type,
int  width,
int  ni,
int  nj,
int  nt 
)

Filter master subroutine.

Uses wrap edges.

Parameters:
[out] bufferf Filtered version of buffer input matrix.
[in] buffer Input matrix.
[in] type Type of filter. Possible values: hanning.
[in] width Width of filter.
[in] ni Horizontal dimension of buffer input matrix.
[in] nj Horizontal dimension of buffer input matrix.
[in] nt Temporal dimension of buffer input matrix.

Definition at line 58 of file filter.c.

References alloc_error(), filter(), and filter_window().

Referenced by filter(), main(), and remove_seasonal_cycle().

00058                                                                                        {
00069   double *filter = NULL; /* Filter window vector */
00070   double *tmpvec = NULL; /* Temporary vector */
00071 
00072   int half_width; /* Half-width of filter window. */
00073   int i; /* Loop counter for ni. */
00074   int j; /* Loop counter for nj. */
00075   int t; /* Loop counter. */
00076   int tt; /* Loop counter. */
00077 
00078   double sum; /* To sum values over filter window width. */
00079 
00080   /*  (void) fprintf(stdout, "%s: Filtering data with a %s filter.\n", __FILE__, type);*/
00081 
00082   if ( !strcmp(type, "hanning") ) {
00083     /* Hanning filter implementation */
00084 
00085     /* Compute filter window vector */
00086     (void) filter_window(&filter, type, width);
00087     
00088     /* Half-width */
00089     half_width = ( width - 1 ) / 2;
00090     
00091     /* Expanded version of vector: wrapping edges. */
00092     tmpvec = (double *) calloc(nt*2, sizeof(double));
00093     if (tmpvec == NULL) alloc_error(__FILE__, __LINE__);
00094 
00095     for (j=0; j<nj; j++)
00096       for (i=0; i<ni; i++) {
00097         
00098         for (t=0; t<half_width; t++) {
00099           tmpvec[t] = buffer[i+j*ni+(nt-half_width+t)*ni*nj];
00100         }
00101         for (t=half_width; t<(half_width+nt); t++) {
00102           tmpvec[t] = buffer[i+j*ni+(t-half_width)*ni*nj];
00103         }
00104         for (t=(half_width+nt); t<(nt*2); t++) {
00105           tmpvec[t] = buffer[i+j*ni+(t-(half_width+nt))*ni*nj];
00106         }
00107         
00108         /* Apply filter. */
00109         for (t=0; t<nt; t++) {
00110           sum = 0.0;
00111           for (tt=t; tt<(t+width-1); tt++)
00112             sum += (filter[tt-t] * tmpvec[tt]);
00113           bufferf[i+j*ni+t*ni*nj] = sum;
00114         }
00115       }
00116     
00117     /* Free memory */
00118     (void) free(filter);
00119     (void) free(tmpvec);
00120   }
00121   else {
00122     /* Unknown filter type */
00123     (void) fprintf(stderr, "%s: ABORT: Unknown filtering type: %s\n", __FILE__, type);
00124     (void) abort();
00125   }
00126 }

void filter_window ( double **  filter_window,
char *  type,
int  width 
)

Filter window subroutine.

Uses hanning.

Parameters:
[out] filter_window Output filter window vector.
[in] type Type of filter. Possible values: hanning.
[in] width Width of filter.

We are using a hanning filter.

Definition at line 58 of file filter_window.c.

References alloc_error().

Referenced by filter().

00058                                                              {
00065   double scale_factor; /* Hanning filter scale factor. */
00066   double alpha = 0.5; /* alpha value for hanning filter. */
00067   double sum; /* Sum for normalizing filter window. */
00068   int i; /* Loop counter. */
00069 
00070   /* Check if number is odd. If it is, make it even by adding one. */
00071   if (width % 2 != 0) width++;
00072 
00073   /* Allocate memory */
00074   (*filter_window) = (double *) calloc(width, sizeof(double));
00075   if ((*filter_window) == NULL) alloc_error(__FILE__, __LINE__);
00076   
00077   if ( !strcmp(type, "hanning") ) {
00080     /* Scale factor */
00081     scale_factor = 2.0 * M_PI / (double) width;
00082     
00083     /* Compute filter window. */
00084     sum = 0.0;
00085     for (i=0; i<width; i++) {
00086       /* Hanning definition */
00087       (*filter_window)[i] = (alpha - 1.0) * cos( ((double) i) * scale_factor) + alpha;
00088       sum += (*filter_window)[i];
00089     }
00090     
00091     /* Normalizing to 1.0 */
00092     for (i=0; i<width; i++)
00093       (*filter_window)[i] /= sum;
00094   }
00095   else {
00096     /* Unknown filter type */
00097     (void) fprintf(stderr, "%s: ABORT: Unknown filtering type: %s\n", __FILE__, type);
00098     (void) abort();
00099   }
00100 }


Generated on 12 May 2016 for DSCLIM by  doxygen 1.6.1