CLAM-Development  1.4.0
SpectrumInterpolator.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"
24 #include "SpectrumInterpolator.hxx"
25 #include "BPF.hxx"
26 #include "Point.hxx"
27 
28 namespace CLAM {
29 
31  {
32  AddAll();
33  UpdateData();
34  }
35 
37  : mSize(0)
38  , mIn1("Input 1",this)
39  , mIn2("Input 2",this)
40  , mOut("Output",this)
41  , mProtoState(SOther)
42  , mInterpolationFactorCtl("InterpolationFactor",this)
43  {
44  Configure(c);
45  }
46 
47  bool SpectrumInterpolator::ConcreteConfigure(const ProcessingConfig&c)
48  {
49  CopyAsConcreteConfig(mConfig, c);
50  //Initialize interpolation factor control from value in the configuration
51  mInterpolationFactorCtl.DoControl(mConfig.GetInterpolationFactor());
52 
53  return true;
54  }
55 
56  // Unsupervised Do() function.
57 // TODO use 'const Spectrum& in1, const Spectrum& in2' and spread it to the rest
59  {
61  "SpectrumInterpolator::Do(): Not in execution mode");
62 
63  switch (mProtoState) {
64  // Fast prototype configurations
65  case SMagPhase:
66  InterpolateMagPhase(in1,in2,out);
67  break;
68  case SComplex:
69  InterpolateComplex(in1,in2,out);
70  break;
71  case SPolar:
72  InterpolatePolar(in1,in2,out);
73  break;
74  case SBPF:
75  InterpolateBPF(in1,in2,out);
76  break;
77  case SBPFMagPhase:
78  InterpolateBPFMagPhase(in1,in2,out);
79  break;
80  case SBPFComplex:
81  InterpolateBPFComplex(in1,in2,out);
82  break;
83  case SBPFPolar:
84  InterpolateBPFPolar(in1,in2,out);
85  break;
86  case SMagPhaseBPF:
87  InterpolateMagPhaseBPF(in1,in2,out);
88  break;
89  case SComplexBPF:
90  InterpolateComplexBPF(in1,in2,out);
91  break;
92  case SPolarBPF:
93  InterpolatePolarBPF(in1,in2,out);
94  break;
95  // Slow type configurations
96  case SOther:
97  Interpolate(in1,in2,out);
98  break;
99  default:
100  CLAM_ASSERT(false,"Do(...) : internal inconsistency (invalid mProtoState)");
101  }
102 
103  return true;
104  }
105 
107  {
108  CLAM_ASSERT(false, "SpectrumInterpolator::Do(): Not implemented");
109 
110  return true;
111  }
112 
113  // This function analyses the inputs and decides which prototypes to use
114  // For the interpolation computation.
115  bool SpectrumInterpolator::SetPrototypes(const Spectrum& in1,const Spectrum& in2,const Spectrum& out)
116  {
117  // Check common attributes
118  SpecTypeFlags t1;
119  in1.GetType(t1);
120  SpecTypeFlags t2;
121  in2.GetType(t2);
122  SpecTypeFlags to;
123  out.GetType(to);
124 
125  // Sanity check:
126  CLAM_ASSERT((t1.bMagPhase || t1.bComplex || t1.bPolar || t1.bMagPhaseBPF),
127  "SpectrumInterpolator: First spectrum has no content");
128  CLAM_ASSERT((t2.bMagPhase || t2.bComplex || t2.bPolar || t2.bMagPhaseBPF),
129  "SpectrumInterpolator: Second spectrum has no content");
130  CLAM_ASSERT((to.bMagPhase || to.bComplex || to.bPolar || to.bMagPhaseBPF),
131  "SpectrumInterpolator: Output spectrum has no content");
132 
133  // Interpolateer size. "pure" BPFs are not considered here.
134  mSize = 0;
135  if (t1.bMagPhase || t1.bComplex || t1.bPolar) {
136  mSize = in1.GetSize();
137  CLAM_ASSERT(mSize,"SpectrumInterpolator::SetPrototypes: Zero size spectrum");
138  }
139  if (t2.bMagPhase || t2.bComplex || t2.bPolar)
140  {
141  if (mSize) {
142  CLAM_ASSERT(mSize == in2.GetSize(),"SpectrumInterpolator::SetPrototypes:Size mismatch in spectrum interpolation");
143  }
144  else {
145  mSize = in2.GetSize();
146  CLAM_ASSERT(mSize,"SpectrumInterpolator::SetPrototypes: Zero size spectrum");
147  }
148  }
149  if (to.bMagPhase || to.bComplex || to.bPolar)
150  {
151  if (mSize) {
152  CLAM_ASSERT(mSize == out.GetSize(),"SpectrumInterpolator::SetPrototypes:Size mismatch in spectrum interpolation");
153  }
154  else {
155  mSize = out.GetSize();
156  CLAM_ASSERT(mSize,"SpectrumInterpolator::SetPrototypes: Zero size spectrum");
157  }
158  }
159  // Spectral Range.
160  // We could also ignore BPF-only objects here, but in
161  // practice, if a BPF is designed for a certain spectral
162  // range, error will probably be too big out of the range, out
163  // we always force range matching
164  CLAM_ASSERT(in1.GetSpectralRange() == in2.GetSpectralRange() &&
165  in1.GetSpectralRange() == out.GetSpectralRange() ,"SpectrumInterpolator::SetPrototypes: Spectral range mismatch in spectrum interpolation");
166 
167  // Scale.
168  if (in1.GetScale() == EScale::eLinear)
169  if (in2.GetScale() == EScale::eLinear)
170  mScaleState=Slinlin;
171  else
172  mScaleState=Slinlog;
173  else
174  if (in2.GetScale() == EScale::eLinear)
175  mScaleState=Sloglin;
176  else
177  mScaleState=Sloglog;
178  // Log scale output might be useful, for example when working
179  // with BPF objects at the three ports. But right for now...
180  CLAM_ASSERT(out.GetScale() != EScale::eLog,"SpectrumInterpolator: Log Scale Output not implemented");
181 
182  // Prototypes.
183 
184  // BPF Interpolation.
185  bool i1BPF=false, i2BPF=false, oBPF=false;
186  if (t1.bMagPhaseBPF && !t1.bComplex && !t1.bPolar && !t1.bMagPhase)
187  i1BPF=true;
188  if (t2.bMagPhaseBPF && !t2.bComplex && !t2.bPolar && !t2.bMagPhase)
189  i2BPF=true;
190  if (to.bMagPhaseBPF && !to.bComplex && !to.bPolar && !to.bMagPhase)
191  oBPF=true;
192 
193  if (oBPF) {
194  // BPF output requires interpolating the inputs.
195  mProtoState=SBPF;
196  return true;
197  }
198  if (i1BPF) {
199  // States with direct BPF implementation.
200  if (t2.bMagPhase && to.bMagPhase) {
201  mProtoState=SBPFMagPhase;
202  return true;
203  }
204  if (t2.bComplex && to.bComplex) {
205  mProtoState=SBPFComplex;
206  return true;
207  }
208  if (t2.bPolar && to.bPolar) {
209  mProtoState=SBPFPolar;
210  return true;
211  }
212  // States requiring 1 conversion:
213  if (t2.bMagPhase || to.bMagPhase) {
214  mProtoState=SBPFMagPhase;
215  return true;
216  }
217  if (t2.bComplex || to.bComplex) {
218  mProtoState=SBPFComplex;
219  return true;
220  }
221  if (t2.bPolar || to.bPolar) {
222  mProtoState=SBPFPolar;
223  return true;
224  }
225  // Should never get here:
226  CLAM_ASSERT(false,
227  "SpectrumInterpolator::SetPrototypes: Data flags internal inconsistency");
228  }
229  if (i2BPF) {
230  // States with direct BPF implementation.
231  if (t1.bMagPhase && to.bMagPhase) {
232  mProtoState=SMagPhaseBPF;
233  return true;
234  }
235  if (t1.bComplex && to.bComplex) {
236  mProtoState=SComplexBPF;
237  return true;
238  }
239  if (t1.bPolar && to.bPolar) {
240  mProtoState=SPolarBPF;
241  return true;
242  }
243  // States requiring 1 conversion:
244  if (t1.bMagPhase || to.bMagPhase) {
245  mProtoState=SMagPhaseBPF;
246  return true;
247  }
248  if (t1.bComplex || to.bComplex) {
249  mProtoState=SComplexBPF;
250  return true;
251  }
252  if (t1.bPolar || to.bPolar) {
253  mProtoState=SPolarBPF;
254  return true;
255  }
256  // Should never get here:
257  CLAM_ASSERT(false,
258  "SpectrumInterpolator::SetPrototypes:"
259  " invalid data flags");
260  }
261  // Direct non-BPF states.
262  if (t1.bMagPhase && t2.bMagPhase && to.bMagPhase) {
263  mProtoState=SMagPhase;
264  return true;
265  }
266  if (t1.bComplex && t2.bComplex && to.bComplex) {
267  mProtoState=SComplex;
268  return true;
269  }
270  if (t1.bPolar && t2.bPolar && to.bPolar) {
271  mProtoState=SPolar;
272  return true;
273  }
274  // States Requiring 1 Conversion
275  if ( (t1.bMagPhase && t2.bMagPhase) ||
276  (t1.bMagPhase && to.bMagPhase) ||
277  (t2.bMagPhase && to.bMagPhase)) {
278  mProtoState=SMagPhase;
279  return true;
280  }
281  if ( (t1.bComplex && t2.bComplex) ||
282  (t1.bComplex && to.bComplex) ||
283  (t2.bComplex && to.bComplex)) {
284  mProtoState=SComplex;
285  return true;
286  }
287  if ( (t1.bPolar && t2.bPolar) ||
288  (t1.bPolar && to.bPolar) ||
289  (t2.bPolar && to.bPolar)) {
290  mProtoState=SPolar;
291  return true;
292  }
293  // Bad luck. We require 2 conversions...
294  mProtoState=SMagPhase;
295  return true;
296  }
297 
298 
300  {
301  CLAM_ASSERT(false, "SpectrumInterpolator::SetPrototypes(): Not implemented");
302 
303  return true;
304  }
305 
307  {
308  mProtoState=SOther;
309  return true;
310  }
311 
312 
313  void SpectrumInterpolator::Interpolate(Spectrum& in1, Spectrum& in2, Spectrum& out)
314  {
315  PrototypeState state_copy = mProtoState;
316  ScaleState state2_copy = mScaleState;
317 
318  SetPrototypes(in1,in2,out);
319  Do(in1,in2,out);
320 
321  mProtoState = state_copy;
322  mScaleState = state2_copy;
323  }
324 
325 
326  void SpectrumInterpolator::InterpolateMagPhase(Spectrum& in1, Spectrum& in2, Spectrum& out)
327  {
328  switch(mScaleState) {
329  case Slinlin:
330  InterpolateMagPhaseLin(in1,in2,out);
331  break;
332  case Sloglog:
333  InterpolateMagPhaseLog(in1,in2,out);
334  break;
335  case Slinlog:
336  InterpolateMagPhaseLinLog(in1,in2,out);
337  break;
338  case Sloglin:
339  InterpolateMagPhaseLinLog(in2,in1,out);
340  break;
341  }
342  }
343 
344  void SpectrumInterpolator::InterpolateMagPhaseLin(Spectrum& in1, Spectrum& in2, Spectrum& out)
345  {
346  bool remove1=false,remove2=false,remove3=false;
347  SpecTypeFlags f;
348 
349  // This function was choosed because some of the data objects had
350  // their MagPhase attribute instantiated. We don't know which of
351  // them, though, out we must check and instantiate the attribute
352  // it it is missed. This could be optimised out by Interpolateing more
353  // States, see coments on this in the class declaration.
354  in1.GetType(f);
355  if (!f.bMagPhase) {
356  remove1=true;
357  f.bMagPhase=true;
358  in1.SetTypeSynchronize(f);
359  }
360  in2.GetType(f);
361  if (!f.bMagPhase) {
362  remove2=true;
363  f.bMagPhase=true;
364  in2.SetTypeSynchronize(f);
365  }
366  out.GetType(f);
367  if (!f.bMagPhase) {
368  remove3=true;
369  f.bMagPhase=true;
370  out.SetType(f);
371  }
372 
373  TData *m1 = in1.GetMagBuffer().GetPtr();
374  TData *f1 = in1.GetPhaseBuffer().GetPtr();
375  TData *m2 = in2.GetMagBuffer().GetPtr();
376  TData *f2 = in2.GetPhaseBuffer().GetPtr();
377  TData *mo = out.GetMagBuffer().GetPtr();
378  TData *fo = out.GetPhaseBuffer().GetPtr();
379 /*****************************/
380 //OPERATION: MAGPHASE AND MAGPHASE
381 
383  if(intFactor>1) intFactor=1;
384  if(intFactor<0) intFactor=0;
385  TData invIntFactor=1-intFactor;
386 
387  Polar polInvIntFactor=Polar(invIntFactor);
388  Polar polIntFactor=Polar(intFactor);
389 
390  for (int i=0;i<mSize;i++) {
391  Polar po=polInvIntFactor*Polar(m1[i],f1[i])+polIntFactor*Polar(m2[i],f2[i]);
392  mo[i]=po.Mag();
393  fo[i]=po.Ang();
394  }
395 
396  f.bComplex=f.bPolar=f.bMagPhaseBPF=false;
397  f.bMagPhase=true;
398  out.SynchronizeTo(f);
399 
400  if (remove1) {
401  in1.RemoveMagBuffer();
402  in1.RemovePhaseBuffer();
403  in1.UpdateData();
404  }
405  if (remove2) {
406  in2.RemoveMagBuffer();
407  in2.RemovePhaseBuffer();
408  in2.UpdateData();
409  }
410  if (remove3) {
411  out.RemoveMagBuffer();
412  out.RemovePhaseBuffer();
413  out.UpdateData();
414  }
415 
416  }
417 
418  void SpectrumInterpolator::InterpolateComplex(Spectrum& in1, Spectrum& in2, Spectrum& out)
419  {
420  switch(mScaleState) {
421  case Slinlin:
422  InterpolateComplexLin(in1,in2,out);
423  break;
424  case Sloglog:
425  InterpolateComplexLog(in1,in2,out);
426  break;
427  case Slinlog:
428  InterpolateComplexLinLog(in1,in2,out);
429  break;
430  case Sloglin:
431  InterpolateComplexLinLog(in2,in1,out);
432  break;
433  }
434  }
435 
436  void SpectrumInterpolator::InterpolateComplexLin(Spectrum& in1, Spectrum& in2, Spectrum& out)
437  {
438  bool remove1=false,remove2=false,remove3=false;
439  SpecTypeFlags f;
440 
441  // This function was choosed because some of the data objects had
442  // their Complex attribute instantiated. We don't know which of
443  // them, though, out we must check and instantiate the attribute
444  // it it is missed. This could be optimised out by Interpolateing more
445  // States, see coments on this in the class declaration.
446  in1.GetType(f);
447  if (!f.bComplex) {
448  remove1=true;
449  f.bComplex=true;
450  in1.SetTypeSynchronize(f);
451  }
452  in2.GetType(f);
453  if (!f.bComplex) {
454  remove2=true;
455  f.bComplex=true;
456  in2.SetTypeSynchronize(f);
457  }
458  out.GetType(f);
459  if (!f.bComplex) {
460  remove3=true;
461  f.bComplex=true;
462  out.SetType(f);
463  }
464 
465  Complex *c1 = in1.GetComplexArray().GetPtr();
466  Complex *c2 = in2.GetComplexArray().GetPtr();
467  Complex *co = out.GetComplexArray().GetPtr();
468 /*****************************/
469 //OPERATION: COMPLEX AND COMPLEX
471  if(intFactor>1) intFactor=1;
472  if(intFactor<0) intFactor=0;
473  TData invIntFactor=1-intFactor;
474 
475  for (int i=0;i<mSize;i++)
476  co[i]=c1[i]*invIntFactor+c2[i]*intFactor;
477 
478  f.bMagPhase=f.bPolar=f.bMagPhaseBPF=false;
479  f.bComplex=true;
480  out.SynchronizeTo(f);
481 
482  if (remove1) {
483  in1.RemoveComplexArray();
484  in1.UpdateData();
485  }
486  if (remove2) {
487  in2.RemoveComplexArray();
488  in2.UpdateData();
489  }
490  if (remove3) {
491  out.RemoveComplexArray();
492  out.UpdateData();
493  }
494  }
495 
496 
497  void SpectrumInterpolator::InterpolatePolar(Spectrum& in1, Spectrum& in2, Spectrum& out)
498  {
499  switch(mScaleState) {
500  case Slinlin:
501  InterpolatePolarLin(in1,in2,out);
502  break;
503  case Sloglog:
504  InterpolatePolarLog(in1,in2,out);
505  break;
506  case Slinlog:
507  InterpolatePolarLinLog(in1,in2,out);
508  break;
509  case Sloglin:
510  InterpolatePolarLinLog(in2,in1,out);
511  break;
512  }
513  }
514 
515  void SpectrumInterpolator::InterpolatePolarLin(Spectrum& in1, Spectrum& in2, Spectrum& out)
516  {
517  bool remove1=false,remove2=false,remove3=false;
518  SpecTypeFlags f;
519 
520  // This function was choosed because some of the data objects had
521  // their Polar attribute instantiated. We don't know which of
522  // them, though, out we must check and instantiate the attribute
523  // it it is missed. This could be optimised out by Interpolateing more
524  // States, see coments on this in the class declaration.
525  in1.GetType(f);
526  if (!f.bPolar) {
527  remove1=true;
528  f.bPolar=true;
529  in1.SetTypeSynchronize(f);
530  }
531  in2.GetType(f);
532  if (!f.bPolar) {
533  remove2=true;
534  f.bPolar=true;
535  in2.SetTypeSynchronize(f);
536  }
537  out.GetType(f);
538  if (!f.bPolar) {
539  remove3=true;
540  f.bPolar=true;
541  out.SetType(f);
542  }
543 
544  Polar *p1 = in1.GetPolarArray().GetPtr();
545  Polar *p2 = in2.GetPolarArray().GetPtr();
546  Polar *po = out.GetPolarArray().GetPtr();
547 /*****************************/
548 //OPERATION: POLAR AND POLAR
550  if(intFactor>1) intFactor=1;
551  if(intFactor<0) intFactor=0;
552  TData invIntFactor=1-intFactor;
553 
554  Polar polInvIntFactor=Polar(invIntFactor);
555  Polar polIntFactor=Polar(intFactor);
556 
557  for (int i=0;i<mSize;i++)
558  po[i]=polInvIntFactor*p1[i]+polIntFactor*p2[i];
559 
560  f.bComplex=f.bMagPhase=f.bMagPhaseBPF=false;
561  f.bPolar=true;
562  out.SynchronizeTo(f);
563 
564  if (remove1) {
565  in1.RemovePolarArray();
566  in1.UpdateData();
567  }
568  if (remove2) {
569  in2.RemovePolarArray();
570  in2.UpdateData();
571  }
572  if (remove3) {
573  out.RemovePolarArray();
574  out.UpdateData();
575  }
576  }
577 
578 
579  void SpectrumInterpolator::InterpolateBPFMagPhase(Spectrum& in1, Spectrum& in2, Spectrum& out)
580  {
581  switch(mScaleState) {
582  case Slinlin:
583  InterpolateBPFMagPhaseLin(in1,in2,out);
584  break;
585  case Sloglog:
586  InterpolateBPFMagPhaseLog(in1,in2,out);
587  break;
588  case Slinlog:
589  CLAM_ASSERT(false,"InterpolateBPFMagPhaseLinLog: Not implemented");
590  break;
591  case Sloglin:
592  InterpolateBPFMagPhaseLogLin(in1,in2,out);
593  break;
594  }
595  }
596 
597  void SpectrumInterpolator::InterpolateMagPhaseBPF(Spectrum& in1, Spectrum& in2, Spectrum& out)
598  {
599  switch(mScaleState) {
600  case Slinlin:
601  InterpolateBPFMagPhaseLin(in2,in1,out);
602  break;
603  case Sloglog:
604  InterpolateBPFMagPhaseLog(in2,in1,out);
605  break;
606  case Slinlog:
607  InterpolateBPFMagPhaseLogLin(in2,in1,out);
608  break;
609  case Sloglin:
610  CLAM_ASSERT(false,"InterpolateBPFMagPhaseLinLog: Not implemented");
611  break;
612  }
613  }
614 
615  void SpectrumInterpolator::InterpolateBPFMagPhaseLin(Spectrum& in1, Spectrum& in2, Spectrum& out)
616  {
617  bool remove2=false,remove3=false;
618  SpecTypeFlags f;
619 
620  // This function was choosed because in1 is a BPF Spectrum,
621  // and some of the non-BPF data objects have their MagPhase
622  // attribute instantiated. We don't know which of them,
623  // though, out we must check and instantiate the attribute it
624  // it is missed. This could be optimised out by Interpolateing more
625  // States, see coments on this in the class declaration.
626  in2.GetType(f);
627  if (!f.bMagPhase) {
628  remove2=true;
629  f.bMagPhase=true;
630  in2.SetTypeSynchronize(f);
631  }
632  out.GetType(f);
633  if (!f.bMagPhase) {
634  remove3=true;
635  f.bMagPhase=true;
636  out.SetType(f);
637  }
638 
639  TData pos = 0.0;
640  TData delta = out.GetSpectralRange() /
641  ((TData)out.GetSize()-TData(1.0));
642  BPF &m1 = in1.GetMagBPF();
643  BPF &f1 = in1.GetPhaseBPF();
644  TData *m2 = in2.GetMagBuffer().GetPtr();
645  TData *f2 = in2.GetPhaseBuffer().GetPtr();
646  TData *mo = out.GetMagBuffer().GetPtr();
647  TData *fo = out.GetPhaseBuffer().GetPtr();
648 /*****************************/
649 //OPERATION: BPF AND MAGPHASE
651  if(intFactor>1) intFactor=1;
652  if(intFactor<0) intFactor=0;
653  TData invIntFactor=1-intFactor;
654 
655  Polar polInvIntFactor=Polar(invIntFactor);
656  Polar polIntFactor=Polar(intFactor);
657 
658  for (int i=0;i<mSize;i++) {
659  Polar po = polInvIntFactor*Polar(m1.GetValue(pos),f1.GetValue(pos)) +
660  polIntFactor*Polar(m2[i],f2[i]);
661  mo[i]=po.Mag();
662  fo[i]=po.Ang();
663  pos+=delta;
664  }
665 
666  f.bComplex=f.bPolar=f.bMagPhaseBPF=false;
667  f.bMagPhase=true;
668  out.SynchronizeTo(f);
669 
670  if (remove2) {
671  in2.RemoveMagBuffer();
672  in2.RemovePhaseBuffer();
673  in2.UpdateData();
674  }
675  if (remove3) {
676  out.RemoveMagBuffer();
677  out.RemovePhaseBuffer();
678  out.UpdateData();
679  }
680  }
681 
682  void SpectrumInterpolator::InterpolateBPFMagPhaseLogLin(Spectrum& in1, Spectrum& in2, Spectrum& out)
683  {
684  bool remove2=false,remove3=false;
685  SpecTypeFlags f;
686 
687  // This function was choosed because in1 is a BPF Spectrum,
688  // and some of the non-BPF data objects have their MagPhase
689  // attribute instantiated. We don't know which of them,
690  // though, out we must check and instantiate the attribute it
691  // it is missed. This could be optimised out by Interpolateing more
692  // States, see coments on this in the class declaration.
693  in2.GetType(f);
694  if (!f.bMagPhase) {
695  remove2=true;
696  f.bMagPhase=true;
697  in2.SetTypeSynchronize(f);
698  }
699  out.GetType(f);
700  if (!f.bMagPhase) {
701  remove3=true;
702  f.bMagPhase=true;
703  out.SetType(f);
704  }
705 
706  TData pos = 0.0;
707  TData delta = out.GetSpectralRange() /
708  ((TData)out.GetSize()-TData(1.0));
709  BPF &m1 = in1.GetMagBPF();
710  BPF &f1 = in1.GetPhaseBPF();
711  TData *m2 = in2.GetMagBuffer().GetPtr();
712  TData *f2 = in2.GetPhaseBuffer().GetPtr();
713  TData *mo = out.GetMagBuffer().GetPtr();
714  TData *fo = out.GetPhaseBuffer().GetPtr();
715 /*****************************/
716 //OPERATION: BPF(LOG) AND MAGPHASE
718  if(intFactor>1) intFactor=1;
719  if(intFactor<0) intFactor=0;
720  TData invIntFactor=1-intFactor;
721  Polar polInvIntFactor=Polar(invIntFactor);
722  Polar polIntFactor=Polar(intFactor);
723 
724  for (int i=0;i<mSize;i++) {
725  Polar po = polInvIntFactor*Polar(CLAM_pow(TData(10),m1.GetValue(pos)/TData(10.0)),f1.GetValue(pos)) +
726  polIntFactor*Polar(m2[i],f2[i]);
727  mo[i]=po.Mag();
728  fo[i]=po.Ang();
729  pos+=delta;
730  }
731 
732  f.bComplex=f.bPolar=f.bMagPhaseBPF=false;
733  f.bMagPhase=true;
734  out.SynchronizeTo(f);
735 
736  if (remove2) {
737  in2.RemoveMagBuffer();
738  in2.RemovePhaseBuffer();
739  in2.UpdateData();
740  }
741  if (remove3) {
742  out.RemoveMagBuffer();
743  out.RemovePhaseBuffer();
744  out.UpdateData();
745  }
746  }
747 
748  void SpectrumInterpolator::InterpolateBPFComplex(Spectrum& in1, Spectrum& in2, Spectrum& out)
749  {
750  switch(mScaleState) {
751  case Slinlin:
752  InterpolateBPFComplexLin(in1,in2,out);
753  break;
754  case Sloglog:
755  InterpolateBPFComplexLog(in1,in2,out);
756  break;
757  case Slinlog:
758  CLAM_ASSERT(false,"InterpolateBPFMagPhaseLinLog: Not implemented");
759  break;
760  case Sloglin:
761  InterpolateBPFComplexLogLin(in1,in2,out);
762  break;
763  }
764  }
765 
766  void SpectrumInterpolator::InterpolateComplexBPF(Spectrum& in1, Spectrum& in2, Spectrum& out)
767  {
768  switch(mScaleState) {
769  case Slinlin:
770  InterpolateBPFComplexLin(in2,in1,out);
771  break;
772  case Sloglog:
773  InterpolateBPFComplexLog(in2,in1,out);
774  break;
775  case Slinlog:
776  InterpolateBPFComplexLogLin(in2,in1,out);
777  break;
778  case Sloglin:
779  CLAM_ASSERT(false,"InterpolateBPFMagPhaseLinLog: Not implemented");
780  break;
781  }
782  }
783 
784  void SpectrumInterpolator::InterpolateBPFComplexLin(Spectrum& in1, Spectrum& in2, Spectrum& out)
785  {
786  bool remove2=false,remove3=false;
787  SpecTypeFlags f;
788 
789  // This function was choosed because in1 is a BPF Spectrum,
790  // and some of the non-BPF data objects have their Complex
791  // attribute instantiated. We don't know which of them,
792  // though, out we must check and instantiate the attribute it
793  // it is missed. This could be optimised out by Interpolateing more
794  // States, see coments on this in the class declaration.
795  in2.GetType(f);
796  if (!f.bComplex) {
797  remove2=true;
798  f.bComplex=true;
799  in2.SetTypeSynchronize(f);
800  }
801  out.GetType(f);
802  if (!f.bComplex) {
803  remove3=true;
804  f.bComplex=true;
805  out.SetType(f);
806  }
807 
808  TData pos = 0.0;
809  TData delta = out.GetSpectralRange() /
810  ((TData)out.GetSize()-TData(1.0));
811  BPF &m1 = in1.GetMagBPF();
812  BPF &f1 = in1.GetPhaseBPF();
813  Complex *c2 = in2.GetComplexArray().GetPtr();
814  Complex *co = out.GetComplexArray().GetPtr();
815 /*****************************/
816 //OPERATION: BPF AND COMPLEX
818  if(intFactor>1) intFactor=1;
819  if(intFactor<0) intFactor=0;
820  TData invIntFactor=1-intFactor;
821 
822  for (int i=0;i<mSize;i++) {
823  TData BRe = fabs(m1.GetValue(pos)) * CLAM_cos(f1.GetValue(pos));
824  TData BIm = fabs(m1.GetValue(pos)) * CLAM_sin(f1.GetValue(pos));
825  co[i]= Complex(BRe,BIm)*invIntFactor + c2[i]*intFactor;
826  pos+=delta;
827  }
828 
829  f.bMagPhase=f.bPolar=f.bMagPhaseBPF=false;
830  f.bComplex=true;
831  out.SynchronizeTo(f);
832 
833  if (remove2) {
834  in2.RemoveComplexArray();
835  in2.UpdateData();
836  }
837  if (remove3) {
838  out.RemoveComplexArray();
839  out.UpdateData();
840  }
841  }
842 
843  // This is probably one of the most used methods, because it can be used
844  // to apply a BPF filter in log scale to a linear complex spectrum, as the
845  // one naturaly generated from a FFT
846  void SpectrumInterpolator::InterpolateBPFComplexLogLin(Spectrum& in1, Spectrum& in2, Spectrum& out)
847  {
848  bool remove2=false,remove3=false;
849  SpecTypeFlags f;
850 
851  // This function was choosed because in1 is a BPF Spectrum,
852  // and some of the non-BPF data objects have their Complex
853  // attribute instantiated. We don't know which of them,
854  // though, out we must check and instantiate the attribute it
855  // it is missed. This could be optimised out by Interpolateing more
856  // States, see coments on this in the class declaration.
857  in2.GetType(f);
858  if (!f.bComplex) {
859  remove2=true;
860  f.bComplex=true;
861  in2.SetTypeSynchronize(f);
862  }
863  out.GetType(f);
864  if (!f.bComplex) {
865  remove3=true;
866  f.bComplex=true;
867  out.SetType(f);
868  }
869 
870  TData pos = 0.0;
871  TData delta = out.GetSpectralRange() /
872  ((TData)out.GetSize()-TData(1.0));
873  BPF &m1 = in1.GetMagBPF();
874  BPF &f1 = in1.GetPhaseBPF();
875  Complex *c2 = in2.GetComplexArray().GetPtr();
876  Complex *co = out.GetComplexArray().GetPtr();
877 /*****************************/
878 //OPERATION: BPF(LOG) AND COMPLEX
880  if(intFactor>1) intFactor=1;
881  if(intFactor<0) intFactor=0;
882  TData invIntFactor=1-intFactor;
883 
884  for (int i=0;i<mSize;i++) {
885  TData BRe = CLAM_pow(TData(10),fabs(m1.GetValue(pos))/TData(10.0)) * CLAM_cos(f1.GetValue(pos));
886  TData BIm = CLAM_pow(TData(10),fabs(m1.GetValue(pos))/TData(10.0)) * CLAM_sin(f1.GetValue(pos));
887  co[i]= Complex(BRe,BIm)*invIntFactor + c2[i]*intFactor;
888  pos+=delta;
889  }
890 
891  f.bMagPhase=f.bPolar=f.bMagPhaseBPF=false;
892  f.bComplex=true;
893  out.SynchronizeTo(f);
894 
895 
896  if (remove2) {
897  in2.RemoveComplexArray();
898  in2.UpdateData();
899  }
900  if (remove3) {
901  out.RemoveComplexArray();
902  out.UpdateData();
903  }
904  }
905 
906 
907  void SpectrumInterpolator::InterpolateBPFPolar(Spectrum& in1, Spectrum& in2, Spectrum& out)
908  {
909  switch(mScaleState) {
910  case Slinlin:
911  InterpolateBPFPolarLin(in1,in2,out);
912  break;
913  case Sloglog:
914  InterpolateBPFPolarLog(in1,in2,out);
915  break;
916  case Slinlog:
917  CLAM_ASSERT(false,"InterpolateBPFPolarLinLog: Not implemented");
918  break;
919  case Sloglin:
920  InterpolateBPFPolarLogLin(in1,in2,out);
921  break;
922  }
923  }
924 
925  void SpectrumInterpolator::InterpolatePolarBPF(Spectrum& in1, Spectrum& in2, Spectrum& out)
926  {
927  switch(mScaleState) {
928  case Slinlin:
929  InterpolateBPFPolarLin(in2,in1,out);
930  break;
931  case Sloglog:
932  InterpolateBPFPolarLog(in2,in1,out);
933  break;
934  case Slinlog:
935  InterpolateBPFPolarLogLin(in2,in1,out);
936  break;
937  case Sloglin:
938  CLAM_ASSERT(false,"InterpolateBPFPolarLinLog: Not implemented");
939  break;
940  }
941  }
942 
943  void SpectrumInterpolator::InterpolateBPFPolarLin(Spectrum& in1, Spectrum& in2, Spectrum& out)
944  {
945  bool remove2=false,remove3=false;
946  SpecTypeFlags f;
947 
948  // This function was choosed because in1 is a BPF Spectrum,
949  // and some of the non-BPF data objects have their Polar
950  // attribute instantiated. We don't know which of them,
951  // though, out we must check and instantiate the attribute it
952  // it is missed. This could be optimised out by Interpolateing more
953  // States, see coments on this in the class declaration.
954  in2.GetType(f);
955  if (!f.bPolar) {
956  remove2=true;
957  f.bPolar=true;
958  in2.SetTypeSynchronize(f);
959  }
960  out.GetType(f);
961  if (!f.bPolar) {
962  remove3=true;
963  f.bPolar=true;
964  out.SetType(f);
965  }
966 
967  TData pos = 0.0;
968  TData delta = out.GetSpectralRange() /
969  ((TData)out.GetSize()-TData(1.0));
970  BPF &m1 = in1.GetMagBPF();
971  BPF &f1 = in1.GetPhaseBPF();
972  Polar *p2 = in2.GetPolarArray().GetPtr();
973  Polar *po = out.GetPolarArray().GetPtr();
974 /*****************************/
975 //OPERATION: BPF AND POLAR
977  if(intFactor>1) intFactor=1;
978  if(intFactor<0) intFactor=0;
979  TData invIntFactor=1-intFactor;
980  Polar polInvIntFactor=Polar(invIntFactor);
981  Polar polIntFactor=Polar(intFactor);
982 
983  for (int i=0;i<mSize;i++) {
984  po[i]=polInvIntFactor*Polar(m1.GetValue(pos),f1.GetValue(pos))+polIntFactor*p2[i];
985  pos+=delta;
986  }
987 
988  f.bMagPhase=f.bComplex=f.bMagPhaseBPF=false;
989  f.bPolar=true;
990  out.SynchronizeTo(f);
991 
992  if (remove2) {
993  in2.RemovePolarArray();
994  in2.UpdateData();
995  }
996  if (remove3) {
997  out.RemovePolarArray();
998  out.UpdateData();
999  }
1000  }
1001 
1002  void SpectrumInterpolator::InterpolateBPFPolarLogLin(Spectrum& in1, Spectrum& in2, Spectrum& out)
1003  {
1004  bool remove2=false,remove3=false;
1005  SpecTypeFlags f;
1006 
1007  // This function was choosed because in1 is a BPF Spectrum,
1008  // and some of the non-BPF data objects have their Polar
1009  // attribute instantiated. We don't know which of them,
1010  // though, out we must check and instantiate the attribute it
1011  // it is missed. This could be optimised out by Interpolateing more
1012  // States, see coments on this in the class declaration.
1013  in2.GetType(f);
1014  if (!f.bPolar) {
1015  remove2=true;
1016  f.bPolar=true;
1017  in2.SetTypeSynchronize(f);
1018  }
1019  out.GetType(f);
1020  if (!f.bPolar) {
1021  remove3=true;
1022  f.bPolar=true;
1023  out.SetType(f);
1024  }
1025 
1026  TData pos = 0.0;
1027  TData delta = out.GetSpectralRange() /
1028  ((TData)out.GetSize()-TData(1.0));
1029  BPF &m1 = in1.GetMagBPF();
1030  BPF &f1 = in1.GetPhaseBPF();
1031  Polar *p2 = in2.GetPolarArray().GetPtr();
1032  Polar *po = out.GetPolarArray().GetPtr();
1033 /*****************************/
1034 //OPERATION: BPF(LOG) AND POLAR
1036  if(intFactor>1) intFactor=1;
1037  if(intFactor<0) intFactor=0;
1038  TData invIntFactor=1-intFactor;
1039  Polar polInvIntFactor=Polar(invIntFactor);
1040  Polar polIntFactor=Polar(intFactor);
1041 
1042  for (int i=0;i<mSize;i++) {
1043  TData BMag = CLAM_pow(TData(10),m1.GetValue(pos)/TData(10.0));
1044  TData BPha = f1.GetValue(pos);
1045  po[i]=polInvIntFactor*Polar(BMag,BPha)+polIntFactor*p2[i];
1046  pos+=delta;
1047  }
1048 
1049  f.bMagPhase=f.bComplex=f.bMagPhaseBPF=false;
1050  f.bPolar=true;
1051  out.SynchronizeTo(f);
1052 
1053  if (remove2) {
1054  in2.RemovePolarArray();
1055  in2.UpdateData();
1056  }
1057  if (remove3) {
1058  out.RemovePolarArray();
1059  out.UpdateData();
1060  }
1061  }
1062 
1063  void SpectrumInterpolator::InterpolateBPF(Spectrum& in1, Spectrum& in2, Spectrum& out)
1064  {
1065 
1067  if(intFactor>1) intFactor=1;
1068  if(intFactor<0) intFactor=0;
1069  TData invIntFactor=1-intFactor;
1070 
1071  // First we check if the abcisas agree
1072 
1073  for (int i=0;i<mSize;i++) {
1074  Point &pm1=in1.GetMagBPF().GetPointArray()[i];
1075  Point &pm2=in2.GetMagBPF().GetPointArray()[i];
1076  Point &pmo=out.GetMagBPF().GetPointArray()[i];
1077  Point &pf1=in1.GetPhaseBPF().GetPointArray()[i];
1078  Point &pf2=in2.GetPhaseBPF().GetPointArray()[i];
1079  Point &pfo=out.GetPhaseBPF().GetPointArray()[i];
1080  CLAM_ASSERT(pm1.GetX() == pm2.GetX(), "InterpolateBPF: input BPF abcisas do not match "
1081  "(and BPF merging not yet iplemented)");
1082  CLAM_ASSERT(pm1.GetX() == pmo.GetX(), "InterpolateBPF: ouput BPF abcisas do not match with imput "
1083  "(and BPF merging not yet iplemented)");
1084 /*****************************/
1085 //OPERATION: BPF AND BPF
1086  pmo.SetY(invIntFactor*pm1.GetY()+intFactor*pm2.GetY());
1087  pfo.SetY(invIntFactor*pf1.GetY()+intFactor*pf2.GetY());
1088  }
1089 
1090  }
1091 
1092  // UNINMPLEMENTED METHODS. some day...
1093  void SpectrumInterpolator::InterpolateMagPhaseLog(Spectrum& in1, Spectrum& in2, Spectrum& out)
1094  {
1095  CLAM_ASSERT(false,"InterpolateMagPhaseLog: Not implemented");
1096  }
1097  void SpectrumInterpolator::InterpolateMagPhaseLinLog(Spectrum& in1, Spectrum& in2, Spectrum& out)
1098  {
1099  CLAM_ASSERT(false,"InterpolateMagPhaseLinLog: Not implemented");
1100  }
1101  void SpectrumInterpolator::InterpolateComplexLog(Spectrum& in1, Spectrum& in2, Spectrum& out)
1102  {
1103  CLAM_ASSERT(false,"InterpolateComplexLog: Not implemented");
1104  }
1105  void SpectrumInterpolator::InterpolateComplexLinLog(Spectrum& in1, Spectrum& in2, Spectrum& out)
1106  {
1107  CLAM_ASSERT(false,"InterpolateComplexLinLog: Not implemented");
1108  }
1109  void SpectrumInterpolator::InterpolatePolarLog(Spectrum& in1, Spectrum& in2, Spectrum& out)
1110  {
1111  CLAM_ASSERT(false,"InterpolatePolarLog: Not implemented");
1112  }
1113  void SpectrumInterpolator::InterpolatePolarLinLog(Spectrum& in1, Spectrum& in2, Spectrum& out)
1114  {
1115  CLAM_ASSERT(false,"InterpolatePolarLinLog: Not implemented");
1116  }
1117  void SpectrumInterpolator::InterpolateBPFComplexLog(Spectrum& in1, Spectrum& in2, Spectrum& out)
1118  {
1119  CLAM_ASSERT(false,"InterpolateBPFComplexLog: Not implemented");
1120  }
1121  void SpectrumInterpolator::InterpolateBPFComplexLinLog(Spectrum& in1, Spectrum& in2, Spectrum& out)
1122  {
1123  CLAM_ASSERT(false,"InterpolateBPFComplexLinLog: Not implemented");
1124  }
1125  void SpectrumInterpolator::InterpolateBPFPolarLog(Spectrum& in1, Spectrum& in2, Spectrum& out)
1126  {
1127  CLAM_ASSERT(false,"InterpolateBPFPolarLog: Not implemented");
1128  }
1129  void SpectrumInterpolator::InterpolateBPFPolarLinLog(Spectrum& in1, Spectrum& in2, Spectrum& out)
1130  {
1131  CLAM_ASSERT(false,"InterpolateBPFPolarLinLog: Not implemented");
1132  }
1133  void SpectrumInterpolator::InterpolateBPFMagPhaseLog(Spectrum& in1, Spectrum& in2, Spectrum& out)
1134  {
1135  CLAM_ASSERT(false,"InterpolateBPFMagPhaseLog: Not implemented");
1136  }
1137  void SpectrumInterpolator::InterpolateBPFMagPhaseLinLog(Spectrum& in1, Spectrum& in2, Spectrum& out)
1138  {
1139  CLAM_ASSERT(false,"InterpolateBPFMagPhaseLinLog: Not implemented");
1140  }
1141 }
1142