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():mFreq("Frequency",this) 00033 { 00034 mFreq.DoControl(0); 00035 Configure(FDCombFilterConfig()); 00036 } 00037 00038 FDCombFilter::FDCombFilter(const FDCombFilterConfig &c = FDCombFilterConfig()):mFreq("Frequency",this) 00039 { 00040 mFreq.DoControl(0); 00041 Configure(c); 00042 } 00043 00044 FDCombFilter::~FDCombFilter() 00045 {} 00046 00047 00048 /* Configure the Processing Object according to the Config object */ 00049 00050 bool FDCombFilter::ConcreteConfigure(const ProcessingConfig& c) 00051 { 00052 00053 CopyAsConcreteConfig(mConfig, c); 00054 return true; 00055 } 00056 00057 /* Setting Prototypes for faster processing */ 00058 00059 bool FDCombFilter::SetPrototypes(const Spectrum& input,Spectrum& output) 00060 { 00061 return true; 00062 } 00063 00064 bool FDCombFilter::SetPrototypes() 00065 { 00066 return true; 00067 } 00068 00069 bool FDCombFilter::UnsetPrototypes() 00070 { 00071 return true; 00072 } 00073 00074 /* The supervised Do() function */ 00075 00076 bool FDCombFilter::Do(void) 00077 { 00078 CLAM_ASSERT(false,CLASS"::Do(): Supervised mode not implemented"); 00079 return false; 00080 } 00081 00082 00083 /* The unsupervised Do() function */ 00084 bool FDCombFilter::Do(const Spectrum& input, Spectrum& output) 00085 { 00086 if (mFreq.GetLastValue() > 0) 00087 { 00088 output.SetSize(input.GetSize()); 00089 Spectrum tmpSpec=input; 00090 bool wasDB=false; 00091 if(output.GetScale()==EScale::eLog) 00092 { 00093 wasDB=true; 00094 output.SetScale(EScale::eLinear); 00095 } 00096 00097 tmpSpec.ToLinear(); 00098 00099 00100 TData samplingRate=input.GetSpectralRange()*2; 00101 TSize sizeSpectrum=input.GetSize(); 00102 TData pitch=mFreq.GetLastValue(); 00103 TData period = pitch / samplingRate * sizeSpectrum * 2.0; 00104 int i; 00105 DataArray& inputMag = input.GetMagBuffer(); 00106 DataArray& inputPhase = input.GetPhaseBuffer(); 00107 DataArray& outputMag = output.GetMagBuffer(); 00108 DataArray& outputPhase = output.GetPhaseBuffer(); 00109 00110 TData twoPiOverPeriod = TWO_PI/period; 00111 TData oneOverTwo = 1./2.0; 00112 for(i=0; i<sizeSpectrum; i++) 00113 { 00114 //todo: this loop is very inefficient because of the sin and cos but there are ways of optimizing 00115 //these kind of iterative sine computations 00116 TData combReal = (1.f +CLAM_cos(i*twoPiOverPeriod)) * oneOverTwo; 00117 TData combImag = (1.f -CLAM_sin(i*twoPiOverPeriod)) * oneOverTwo; 00118 00119 TData mag=inputMag[i]; 00120 TData phase=inputPhase[i]; 00121 TData real=mag*CLAM_cos(phase); 00122 TData imag=mag*CLAM_sin(phase); 00123 00124 00125 TData newReal=real*combReal-imag*combImag; 00126 TData newImag=real*combImag+imag*combReal; 00127 TData newMag=CLAM_sqrt(newReal*newReal+newImag*newImag); 00128 TData newPhase=CLAM_atan2(newImag,newReal); 00129 00130 00131 00132 outputMag[i] = newMag; 00133 outputPhase[i] = newPhase; 00134 } 00135 00136 if(wasDB) output.ToDB(); 00137 00138 00139 return true; 00140 } 00141 else return false; 00142 } 00143 00144 00145 };//namespace CLAM 00146