RTXI 1.3
plugins/include/DSP/croscorr.cpp
Go to the documentation of this file.
00001 #include <stdlib.h> 
00002 #include <iostream> 
00003 #include <fstream>
00004 #include <math.h>
00005 #include "misdefs.h"
00006 #include "croscorr.h"
00007 
00008 extern std::ofstream DebugFile;
00009 
00010 void CrossCorrelation( int seg_len,
00011                        double* input_segment, 
00012                        double* output_segment,
00013                        double* gain,
00014                        double* phase_delta,
00015                        int* indx_of_peak)
00016 {
00017   double input_peak, output_peak;
00018   int indx_of_input_peak, indx_of_output_peak;
00019   int i;
00020 
00021   //-----------------------------------
00022   // Find peak in each segment to use
00023   // for coarse alignment
00024 
00025   input_peak = 0.0;
00026   output_peak = 0.0;
00027   indx_of_input_peak = 0;
00028   indx_of_output_peak = 0;
00029 
00030   for(i=int(0.05*seg_len); i<int(0.9*seg_len); i++)
00031   {
00032     if(input_segment[i] > input_peak){
00033       input_peak = input_segment[i];
00034       indx_of_input_peak = i;
00035     }
00036     if(output_segment[i] > output_peak){
00037       output_peak = output_segment[i];
00038       indx_of_output_peak = i;
00039     }
00040   }
00041  
00042   //-----------------------------------------
00043   // Find first rising zero-crossing.
00044   // Then find next falling zero crossing
00045   // followed by next rising zero-crossing.
00046 
00047 #ifdef NOT_DEFINED
00048   for(i=10; i<seg_len; i++){
00049     if(input_segment[i] > 0.0) continue;
00050       beg_indx = i;
00051       break;
00052   }
00053   for(i=beg_indx; i<seg_len; i++){
00054     if(input_segment[i] < 0.0) continue;
00055       beg_input_cycle = i;
00056       break;
00057   }
00058   for(i=beg_input_cycle; i<seg_len; i++) {
00059     if(input_segment[i] > input_peak)
00060     {
00061       input_peak = input_segment[i];
00062       indx_of_input_peak = i;
00063     }
00064     if(input_segment[i] > 0.0) continue;
00065       mid_input_cycle = i;
00066       break;
00067   }
00068   for(i=mid_input_cycle; i<seg_len; i++) {
00069     if(input_segment[i] < 0.0) continue;
00070       end_input_cycle = i;
00071       break;
00072   }
00073 
00074   for(i=10; i<seg_len; i++){
00075     if(output_segment[i] > 0.0) continue;
00076       beg_indx = i;
00077       break;
00078   }
00079   for(i=beg_indx; i<seg_len; i++){
00080     if(output_segment[i] < 0.0) continue;
00081       beg_output_cycle = i;
00082       break;
00083   }
00084   for(i=beg_output_cycle; i<seg_len; i++) {
00085     if(output_segment[i] > output_peak)
00086     {
00087       output_peak = output_segment[i];
00088       indx_of_output_peak = i;
00089     }
00090     if(output_segment[i] > 0.0) continue;
00091       mid_output_cycle = i;
00092       break;
00093   }
00094   for(i=mid_output_cycle; i<seg_len; i++) {
00095     if(output_segment[i] < 0.0) continue;
00096       end_output_cycle = i;
00097       break;
00098   }
00099 #endif
00100   *gain = output_peak;
00101   *indx_of_peak = indx_of_output_peak;
00102 #ifdef NOT_DEFINED
00103   work = 360.0*(beg_output_cycle-beg_input_cycle)/
00104                  double(end_input_cycle-beg_input_cycle);
00105   while(work > 180.0) {
00106     work -= 360.0;
00107   }
00108   while(work < -180.0) {
00109     work += 360.0;
00110   }
00111   *phase_delta = work;
00112 #endif
00113   *phase_delta = 0.0;
00114 
00115   return;
00116 }  
00117 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines