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 | |
---|
32 | Hanning::Hanning(){ |
---|
33 | allocated=false; |
---|
34 | } |
---|
35 | |
---|
36 | Hanning::~Hanning(){ |
---|
37 | if(allocated) delete [] coeffs; |
---|
38 | } |
---|
39 | |
---|
40 | Hanning::Hanning(const Hanning& h) |
---|
41 | { |
---|
42 | operator=(h); |
---|
43 | } |
---|
44 | |
---|
45 | Hanning& 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 | |
---|
57 | Hanning::Hanning(int size) |
---|
58 | { |
---|
59 | /// @details |
---|
60 | /// Constructor that sets the Hanning width and calculates the |
---|
61 | /// coefficients. Does this by simply calling the |
---|
62 | /// Hanning::define(int) function. |
---|
63 | /// \param size The full width of the filter. The parameter \f$a\f$ |
---|
64 | /// is defined as (size+1)/2. |
---|
65 | |
---|
66 | this->allocated=false; |
---|
67 | this->define(size); |
---|
68 | } |
---|
69 | |
---|
70 | void Hanning::define(int size) |
---|
71 | { |
---|
72 | /// @details |
---|
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 | |
---|
95 | float *Hanning::smooth(float *array, int npts) |
---|
96 | { |
---|
97 | /// @details |
---|
98 | /// This smooths an array of float by doing a discrete convolution of |
---|
99 | /// the input array with the filter coefficients. |
---|
100 | /// |
---|
101 | /// Currently only works for C arrays of floats, as that is all I |
---|
102 | /// need it for. Could be made more general if needs be. |
---|
103 | /// |
---|
104 | /// \param array The input array. Needs to be defined -- no memory |
---|
105 | /// checks are done! |
---|
106 | /// \param npts The size of the input array. |
---|
107 | /// \return Returns an array of the same size. If filter coefficients |
---|
108 | /// have not been allocated, the input array is returned. |
---|
109 | |
---|
110 | if(!this->allocated) return array; |
---|
111 | else{ |
---|
112 | float *newarray = new float[npts]; |
---|
113 | float scale = (hanningSize+1.)/2.; |
---|
114 | for(int i=0;i<npts;i++){ |
---|
115 | newarray[i] = 0.; |
---|
116 | for(int j=0;j<hanningSize;j++){ |
---|
117 | int x = j-(hanningSize-1)/2; |
---|
118 | if((i+x>0)&&(i+x<npts)) newarray[i]+=coeffs[j] * array[i+x]; |
---|
119 | } |
---|
120 | newarray[i] /= scale; |
---|
121 | } |
---|
122 | return newarray; |
---|
123 | } |
---|
124 | } |
---|