source: tags/release-1.1.5/src/Utils/Hanning.cc @ 1441

Last change on this file since 1441 was 419, checked in by MatthewWhiting, 16 years ago

Some minor fixes to correct compilation warnings. Moved Object3D::order() into the .cc file.

File size: 3.8 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 <duchamp/Utils/Hanning.hh>
31
32Hanning::Hanning(){
33  allocated=false;
34}
35
36Hanning::~Hanning(){
37  if(allocated) delete [] coeffs;
38}
39
40Hanning::Hanning(const Hanning& h)
41{
42  operator=(h);
43}
44
45Hanning& Hanning::operator=(const Hanning& h)
46{
47  if(this==&h) return *this;
48  this->hanningSize = h.hanningSize;
49  this->allocated = h.allocated;
50  if(h.allocated){
51    this->coeffs = new float[this->hanningSize];
52    for(int i=0;i<h.hanningSize;i++) this->coeffs[i] = h.coeffs[i];
53  }
54  return *this;
55}
56
57Hanning::Hanning(int size){
58  /**
59   * Constructor that sets the Hanning width and calculates the
60   * coefficients. Does this by simply calling the
61   * Hanning::define(int) function.
62   * \param size The full width of the filter. The parameter \f$a\f$
63   * is defined as (size+1)/2.
64   */
65
66  this->allocated=false;
67  this->define(size);
68}
69
70void Hanning::define(int size)
71{
72  /**
73   * Function that sets the Hanning width and calculates the coefficients.
74   * \param size The full width of the filter. The parameter \f$a\f$ is
75   *  defined as (size+1)/2.
76   */
77  if(size%2==0){
78    std::cerr << "Hanning: need an odd number for the size. "
79              << "Changing "<< size << " to " << size+1<<".\n";
80    size++;
81  }
82  this->hanningSize = size;
83  if(this->allocated) delete [] this->coeffs;
84  this->coeffs = new float[size];
85  this->allocated = true;
86  float a = (size+1.)/2.;
87  for(int i=0;i<size;i++){
88    float x = i-(size-1)/2.;
89    this->coeffs[i] = 0.5 + 0.5*cos(x * M_PI / a);
90  }
91}
92
93
94
95float *Hanning::smooth(float *array, int npts){
96  /**
97   * This smooths an array of float by doing a discrete convolution of
98   * the input array with the filter coefficients.
99   *
100   * Currently only works for C arrays of floats, as that is all I
101   * need it for.  Could be made more general if needs be.
102   *
103   * \param array The input array. Needs to be defined -- no memory
104   * checks are done! 
105   * \param npts The size of the input array.
106   * \return Returns an array of the same size. If filter coefficients
107   * have not been allocated, the input array is returned.
108   */
109  if(!this->allocated) return array;
110  else{
111    float *newarray = new float[npts];
112    float scale = (hanningSize+1.)/2.;
113    for(int i=0;i<npts;i++){
114      newarray[i] = 0.;
115      for(int j=0;j<hanningSize;j++){
116        int x = j-(hanningSize-1)/2;
117        if((i+x>0)&&(i+x<npts)) newarray[i]+=coeffs[j] * array[i+x];
118      }
119      newarray[i] /= scale;
120    }
121    return newarray;
122  }
123}
Note: See TracBrowser for help on using the repository browser.