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 "FDCombFilter.hxx" 00024 00025 #define CLASS "FDCombFilter" 00026 00027 namespace CLAM { 00028 00029 00030 /* Processing object Method implementations */ 00031 00032 FDCombFilter::FDCombFilter(const Config &c) 00033 : mFreq("Frequency",this) 00034 { 00035 mFreq.DoControl(0); 00036 Configure(c); 00037 } 00038 00039 FDCombFilter::~FDCombFilter() 00040 {} 00041 00042 00043 /* The supervised Do() function */ 00044 00045 bool FDCombFilter::Do() 00046 { 00047 CLAM_ASSERT(false,CLASS"::Do(): Supervised mode not implemented"); 00048 return false; 00049 } 00050 00051 00052 /* The unsupervised Do() function */ 00053 bool FDCombFilter::Do(const Spectrum& input, Spectrum& output) 00054 { 00055 if (mFreq.GetLastValue() <= 0) return false; 00056 00057 output.SetSize(input.GetSize()); 00058 Spectrum tmpSpec=input; 00059 bool wasDB=false; 00060 if(output.GetScale()==EScale::eLog) 00061 { 00062 wasDB=true; 00063 output.SetScale(EScale::eLinear); 00064 } 00065 00066 tmpSpec.ToLinear(); 00067 00068 00069 TData samplingRate=input.GetSpectralRange()*2; 00070 const TSize sizeSpectrum=input.GetSize(); 00071 TData pitch=mFreq.GetLastValue(); 00072 TData period = pitch / samplingRate * sizeSpectrum * 2.0; 00073 DataArray & inputMag = input.GetMagBuffer(); 00074 DataArray & inputPhase = input.GetPhaseBuffer(); 00075 DataArray & outputMag = output.GetMagBuffer(); 00076 DataArray & outputPhase = output.GetPhaseBuffer(); 00077 00078 TData twoPiOverPeriod = TWO_PI/period; 00079 TData oneOverTwo = 1./2.0; 00080 for(unsigned i=0; i<sizeSpectrum; i++) 00081 { 00082 //todo: this loop is very inefficient because of the sin and cos but there are ways of optimizing 00083 //these kind of iterative sine computations 00084 TData combReal = (1.f +CLAM_cos(i*twoPiOverPeriod)) * oneOverTwo; 00085 TData combImag = (1.f -CLAM_sin(i*twoPiOverPeriod)) * oneOverTwo; 00086 00087 TData mag=inputMag[i]; 00088 TData phase=inputPhase[i]; 00089 TData real=mag*CLAM_cos(phase); 00090 TData imag=mag*CLAM_sin(phase); 00091 00092 TData newReal=real*combReal-imag*combImag; 00093 TData newImag=real*combImag+imag*combReal; 00094 TData newMag=CLAM_sqrt(newReal*newReal+newImag*newImag); 00095 TData newPhase=CLAM_atan2(newImag,newReal); 00096 00097 outputMag[i] = newMag; 00098 outputPhase[i] = newPhase; 00099 } 00100 00101 if(wasDB) output.ToDB(); 00102 00103 return true; 00104 } 00105 00106 00107 };//namespace CLAM 00108