CLAM-Development  1.4.0
SpectralPeakArrayAdder.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 "SpecTypeFlags.hxx"
25 #include "BPF.hxx"
26 #include "Point.hxx"
27 #include "Spectrum.hxx"
28 
29 namespace CLAM {
30 
32  : mIn1("Input 1",this),
33  mIn2("Input 2",this),
34  mOut("Output",this)
35  {
36  Configure(c);
37  }
38 
39 
40  // Unsupervised Do() function.
42  {
44  "SpectralPeakArrayAdder::Do(): Not in execution mode");
45 
46  CLAM_ASSERT((&out)!=(&in1) && (&out)!=(&in1), "SpectralPeakAdder cannot process inplace");
47 
48  //we initialize output peak array making sure index array is present
49  out.AddIndexArray();
50  out.UpdateData();
51  out.SetnPeaks(0);
52 
53  int nPeaks1=in1.GetnPeaks();
54  int nPeaks2=in2.GetnPeaks();
55 
56  if(nPeaks1==0)
57  {
58  out=in2;
59  return true;
60  }
61  if(nPeaks2==0)
62  {
63  out=in1;
64  return true;
65  }
66 
67  IndexArray& in1Index = in1.GetIndexArray();
68  /*we first multiply indices in second input by 1000 in order
69  to avoid aliasing between indices. Note though that if this
70  process is applied recursively indices may end up getting out
71  of bounds*/
72  IndexArray& in2Index = in2.GetIndexArray();
73  int i;
74  for (i=0; i<nPeaks2;i++) in2Index[i]*=1000;
75 
76  int nSelected1, nSelected2;
77  nSelected1 = nSelected2 = 0;
81  do
82  {
83  /* TODO?: if peaks have exactly the same frequency we could think
84  * on adding the magnitudes and adding a single peak. The problem
85  * would then be that indices would get mixed up. Appart from that,
86  * the synthesis process will work by adding their energy */
87  if(in1.GetFreq(nSelected1)<in2.GetFreq(nSelected2))
88  {
89  out.AddSpectralPeak(in1.GetSpectralPeak(nSelected1), true, in1Index[nSelected1]);
90  //std::cout<<"peak index"<<in1Index[nSelected1]<<std::endl;
91  nSelected1++;
92  }
93  else
94  {
95  out.AddSpectralPeak(in2.GetSpectralPeak(nSelected2), true, in2Index[nSelected2]);
96  nSelected2++;
97  }
98 
99  }while(nPeaks1-nSelected1 >0 && nPeaks2-nSelected2>0);
100 
101  return true;
102  }
103 
104 };
105