SpectralPeakArrayAdder.cxx

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
00003  *                         UNIVERSITAT POMPEU FABRA
00004  *
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  */
00021 
00022 #include "Complex.hxx"
00023 #include "SpecTypeFlags.hxx"
00024 #include "SpectralPeakArrayAdder.hxx"
00025 #include "BPF.hxx"
00026 #include "Point.hxx"
00027 #include "Spectrum.hxx"
00028 
00029 namespace CLAM {
00030 
00031 
00032         SpectralPeakArrayAdder::SpectralPeakArrayAdder()
00033                 : mIn1("Input 1",this),
00034                   mIn2("Input 2",this),
00035                   mOut("Output",this)
00036         {
00037                 Configure(PeaksAddConfig());
00038         }
00039 
00040         SpectralPeakArrayAdder::SpectralPeakArrayAdder(const PeaksAddConfig &c)
00041                 : mIn1("Input 1",this),
00042                   mIn2("Input 2",this),
00043                   mOut("Output",this)
00044         {
00045                 Configure(c);
00046         }
00047 
00048 
00049         bool SpectralPeakArrayAdder::ConcreteConfigure(const ProcessingConfig&c)
00050         {
00051                 CopyAsConcreteConfig(mConfig, c);
00052 
00053                 return true;
00054         }
00055 
00056         // Unsupervised Do() function.
00057         bool SpectralPeakArrayAdder::Do(const SpectralPeakArray& in1, const SpectralPeakArray& in2, SpectralPeakArray& out)
00058         {
00059                 CLAM_DEBUG_ASSERT(IsRunning(),
00060                         "SpectralPeakArrayAdder::Do(): Not in execution mode");
00061 
00062                 CLAM_ASSERT((&out)!=(&in1) && (&out)!=(&in1), "SpectralPeakAdder cannot process inplace");
00063                         
00064                 //we initialize output peak array making sure index array is present
00065                 out.AddIndexArray();
00066                 out.UpdateData();
00067                 out.SetnPeaks(0);
00068                 
00069                 int nPeaks1=in1.GetnPeaks();
00070                 int nPeaks2=in2.GetnPeaks();
00071 
00072                 if(nPeaks1==0)
00073                 {
00074                         out=in2;
00075                         return true;
00076                 }
00077                 if(nPeaks2==0)
00078                 {
00079                         out=in1;
00080                         return true;
00081                 }
00082 
00083                 IndexArray& in1Index = in1.GetIndexArray();
00084                 /*we first multiply indices in second input by 1000 in order
00085                 to avoid aliasing between indices. Note though that if this
00086                 process is applied recursively indices may end up getting out
00087                 of bounds*/
00088                 IndexArray& in2Index = in2.GetIndexArray();
00089                 int i;
00090                 for (i=0; i<nPeaks2;i++) in2Index[i]*=1000;
00091                                 
00092                 int nSelected1, nSelected2;
00093                 nSelected1 = nSelected2 = 0;
00097                 do
00098                 {
00099                         /* TODO?: if peaks have exactly the same frequency we could think
00100                          * on adding the magnitudes and adding a single peak. The problem
00101                          * would then be that indices would get mixed up. Appart from that,
00102                          * the synthesis process will work by adding their energy */
00103                         if(in1.GetFreq(nSelected1)<in2.GetFreq(nSelected2))
00104                         {
00105                                 out.AddSpectralPeak(in1.GetSpectralPeak(nSelected1), true, in1Index[nSelected1]);
00106                                 //std::cout<<"peak index"<<in1Index[nSelected1]<<std::endl;
00107                                 nSelected1++;
00108                         }
00109                         else
00110                         {
00111                                 out.AddSpectralPeak(in2.GetSpectralPeak(nSelected2), true, in2Index[nSelected2]);
00112                                 nSelected2++;
00113                         }
00114                         
00115                 }while(nPeaks1-nSelected1 >0 && nPeaks2-nSelected2>0);
00116                 
00117                 return true;
00118         }
00119 
00120 };
00121 

Generated on Tue Aug 12 22:33:45 2008 for CLAM by  doxygen 1.5.5