CLAM-Development  1.4.0
FDCombFilter.cxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 #include "Complex.hxx"
23 #include "FDCombFilter.hxx"
24 
25 #define CLASS "FDCombFilter"
26 
27 namespace CLAM {
28 
29 
30  /* Processing object Method implementations */
31 
33  : mFreq("Frequency",this)
34  {
35  mFreq.DoControl(0);
36  Configure(c);
37  }
38 
40  {}
41 
42 
43  /* The supervised Do() function */
44 
46  {
47  CLAM_ASSERT(false,CLASS"::Do(): Supervised mode not implemented");
48  return false;
49  }
50 
51 
52  /* The unsupervised Do() function */
53  bool FDCombFilter::Do(const Spectrum& input, Spectrum& output)
54  {
55  if (mFreq.GetLastValue() <= 0) return false;
56 
57  output.SetSize(input.GetSize());
58  Spectrum tmpSpec=input;
59  bool wasDB=false;
60  if(output.GetScale()==EScale::eLog)
61  {
62  wasDB=true;
63  output.SetScale(EScale::eLinear);
64  }
65 
66  tmpSpec.ToLinear();
67 
68 
69  TData samplingRate=input.GetSpectralRange()*2;
70  const TSize sizeSpectrum=input.GetSize();
71  TData pitch=mFreq.GetLastValue();
72  TData period = pitch / samplingRate * sizeSpectrum * 2.0;
73  DataArray & inputMag = input.GetMagBuffer();
74  DataArray & inputPhase = input.GetPhaseBuffer();
75  DataArray & outputMag = output.GetMagBuffer();
76  DataArray & outputPhase = output.GetPhaseBuffer();
77 
78  TData twoPiOverPeriod = TWO_PI/period;
79  TData oneOverTwo = 1./2.0;
80  for(unsigned i=0; i<sizeSpectrum; i++)
81  {
82  //todo: this loop is very inefficient because of the sin and cos but there are ways of optimizing
83  //these kind of iterative sine computations
84  TData combReal = (1.f +CLAM_cos(i*twoPiOverPeriod)) * oneOverTwo;
85  TData combImag = (1.f -CLAM_sin(i*twoPiOverPeriod)) * oneOverTwo;
86 
87  TData mag=inputMag[i];
88  TData phase=inputPhase[i];
89  TData real=mag*CLAM_cos(phase);
90  TData imag=mag*CLAM_sin(phase);
91 
92  TData newReal=real*combReal-imag*combImag;
93  TData newImag=real*combImag+imag*combReal;
94  TData newMag=CLAM_sqrt(newReal*newReal+newImag*newImag);
95  TData newPhase=CLAM_atan2(newImag,newReal);
96 
97  outputMag[i] = newMag;
98  outputPhase[i] = newPhase;
99  }
100 
101  if(wasDB) output.ToDB();
102 
103  return true;
104  }
105 
106 
107 };//namespace CLAM
108