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

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

Mostly adding the distribution text to the start of files, with a few additional comments added too.

File size: 3.4 KB
Line 
1// -----------------------------------------------------------------------
2// Hanning.cc: Member functions for the Hanning class.
3// -----------------------------------------------------------------------
4// Copyright (C) 2006, Matthew Whiting, ATNF
5//
6// This program is free software; you can redistribute it and/or modify it
7// under the terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 2 of the License, or (at your
9// option) any later version.
10//
11// Duchamp is distributed in the hope that it will be useful, but WITHOUT
12// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14// for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with Duchamp; if not, write to the Free Software Foundation,
18// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
19//
20// Correspondence concerning Duchamp may be directed to:
21//    Internet email: Matthew.Whiting [at] atnf.csiro.au
22//    Postal address: Dr. Matthew Whiting
23//                    Australia Telescope National Facility, CSIRO
24//                    PO Box 76
25//                    Epping NSW 1710
26//                    AUSTRALIA
27// -----------------------------------------------------------------------
28#include <iostream>
29#include <math.h>
30#include <Utils/Hanning.hh>
31
32Hanning::Hanning(){
33  allocated=false;
34}
35
36Hanning::~Hanning(){
37  if(allocated) delete [] coeffs;
38}
39
40Hanning::Hanning(int size){
41  /**
42   * Constructor that sets the Hanning width and calculates the
43   * coefficients. Does this by simply calling the
44   * Hanning::define(int) function.
45   * \param size The full width of the filter. The parameter \f$a\f$
46   * is defined as (size+1)/2.
47   */
48
49  this->allocated=false;
50  this->define(size);
51};
52
53void Hanning::define(int size)
54{
55  /**
56   * Function that sets the Hanning width and calculates the coefficients.
57   * \param size The full width of the filter. The parameter \f$a\f$ is
58   *  defined as (size+1)/2.
59   */
60  if(size%2==0){
61    std::cerr << "Hanning: need an odd number for the size. "
62              << "Changing "<< size << " to " << ++size<<".\n";
63  }
64  this->hanningSize = size;
65  if(this->allocated) delete [] this->coeffs;
66  this->coeffs = new float[size];
67  this->allocated = true;
68  float a = (size+1.)/2.;
69  for(int i=0;i<size;i++){
70    float x = i-(size-1)/2.;
71    this->coeffs[i] = 0.5 + 0.5*cos(x * M_PI / a);
72  }
73}
74
75
76
77float *Hanning::smooth(float *array, int npts){
78  /**
79   * This smooths an array of float by doing a discrete convolution of
80   * the input array with the filter coefficients.
81   *
82   * Currently only works for C arrays of floats, as that is all I
83   * need it for.  Could be made more general if needs be.
84   *
85   * \param array The input array. Needs to be defined -- no memory
86   * checks are done! 
87   * \param npts The size of the input array.
88   * \return Returns an array of the same size. If filter coefficients
89   * have not been allocated, the input array is returned.
90   */
91  if(!this->allocated) return array;
92  else{
93    float *newarray = new float[npts];
94    float scale = (hanningSize+1.)/2.;
95    for(int i=0;i<npts;i++){
96      newarray[i] = 0.;
97      for(int j=0;j<hanningSize;j++){
98        int x = j-(hanningSize-1)/2;
99        if((i+x>0)&&(i+x<npts)) newarray[i]+=coeffs[j] * array[i+x];
100      }
101      newarray[i] /= scale;
102    }
103    return newarray;
104  }
105}
Note: See TracBrowser for help on using the repository browser.