source: trunk/src/Utils/Hanning.cc @ 258

Last change on this file since 258 was 258, checked in by Matthew Whiting, 17 years ago

Merging pixel-map-branch revisions 236:257 back into trunk.
The use of the PixelMap? functions is sorted now, so we put everything back into a uniform location.

File size: 1.7 KB
Line 
1#include <iostream>
2#include <math.h>
3#include <Utils/Hanning.hh>
4
5Hanning::Hanning(){
6  allocated=false;
7};
8Hanning::~Hanning(){
9  if(allocated) delete [] coeffs;
10};
11
12Hanning::Hanning(int size){
13  /**
14   * Constructor that sets the Hanning width and calculates the coefficients.
15   * \param size The full width of the filter. The parameter \f$a\f$ is
16   *  defined as (size+1)/2.
17   */
18  if(size%2==0){
19    std::cerr << "Hanning: need an odd number for the size. "
20              << "Changing "<< size << " to " << ++size<<".\n";
21  }
22  hanningSize = size;
23  coeffs = new float[size];
24  allocated = true;
25  float a = (size+1.)/2.;
26  for(int i=0;i<size;i++){
27    float x = i-(size-1)/2.;
28    coeffs[i] = 0.5 + 0.5*cos(x * M_PI / a);
29  }
30};
31
32
33float *Hanning::smooth(float *array, int npts){
34  /**
35   * This smooths an array of float by doing a discrete convolution of
36   * the input array with the filter coefficients.
37   *
38   * Currently only works for C arrays of floats, as that is all I
39   * need it for.  Could be made more general if needs be.
40   *
41   * \param array The input array. Needs to be defined -- no memory
42   * checks are done!  \param npts The size of the input array.
43   * \return Returns an array of the same size. If filter coefficients
44   * have not been allocated, the input array is returned.
45   */
46  if(!this->allocated) return array;
47  else{
48    float *newarray = new float[npts];
49    float scale = (hanningSize+1.)/2.;
50    for(int i=0;i<npts;i++){
51      newarray[i] = 0.;
52      for(int j=0;j<hanningSize;j++){
53        int x = j-(hanningSize-1)/2;
54        if((i+x>0)&&(i+x<npts)) newarray[i]+=coeffs[j] * array[i+x];
55      }
56      newarray[i] /= scale;
57    }
58    return newarray;
59  }
60};
Note: See TracBrowser for help on using the repository browser.