+ //---------------------------------------------------------------------
+ void clitk::Signal::Shift(int d, int length) {
+ std::cout << "Shift d=" << d << " length = " << length << std::endl;
+ // int d = (int)lrint(s/GetSamplingPeriod()); // closest integer delta
+ // DD(d);
+ clitk::Signal temp;
+ temp.resize(length);//(uint)size());
+ temp.SetSamplingPeriod(GetSamplingPeriod());
+ for(uint i=0; i<temp.size(); i++) {
+ int j = (i+d) % size();
+ // DD(i);
+ // DD(j);
+ temp[i] = (*this)[j];
+ }
+ CopyFrom(temp);
+ }
+ //---------------------------------------------------------------------
+ //---------------------------------------------------------------------
+ double Signal::UnormalizedXcov(const Signal &input2, Signal &output) const {
+ unsigned int N;
+ N=size();
+ unsigned int max;
+ max = 2*N;
+ output.resize(max);
+ assert(size() == input2.size()); //les signaux à comparer doivent avoir la même taille
+
+
+ //calcul de la moyenne
+ double mean1=0.,mean2=0.;
+ mean1=this->GetGlobalMean();
+ mean2=input2.GetGlobalMean();
+
+ for (unsigned int k= 0;k<N;k++){
+ double value=0.;
+ for (unsigned int n=0;n<=k;n++){
+ value=value+((*this)[n] - mean1)*(input2[n+N-k-1] - mean2);
+ }
+ output[k]=value;
+ }
+ for (unsigned int k=N;k<max-1;k++){
+ double value=0.;
+ for (unsigned int n=0;n<max-k-1;n++){
+ value=value+((*this)[n+k-N+1] - mean1)*(input2[n] - mean2); //*input2??
+ }
+ output[k]=value;
+ }
+ double Coef;
+ Coef = output[N-1];
+ return Coef;
+
+ }
+
+ //---------------------------------------------------------------------
+ //---------------------------------------------------------------------
+ double Signal::UnormalizedCorrelation(const Signal &input2) const {
+ unsigned int N;
+ N=size();
+ Signal output;
+ output.resize(N);
+ assert(size() == input2.size()); //les signaux à comparer doivent avoir la même taille
+
+ //calcul de la moyenne
+ double mean1=0.,mean2=0.;
+ mean1=this->GetGlobalMean();
+ mean2=input2.GetGlobalMean();
+ double value=0.;
+ for (unsigned int n=0;n<N;n++){
+ value=value+((*this)[n] - mean1)*(input2[n] - mean2);
+ }
+ return value;
+
+ }
+ //---------------------------------------------------------------------
+ //---------------------------------------------------------------------
+ double Signal::Correlation(const Signal &input2) const {
+
+ assert(size() == input2.size()); //les signaux à comparer doivent avoir la même taille
+
+ double cc11,cc22,cc;
+ Signal input1(*this);
+
+ cc=this->UnormalizedCorrelation(input2);
+ cc11=this->UnormalizedCorrelation(input1);
+ cc22=input2.UnormalizedCorrelation (input2);
+ cc=cc/sqrt(cc11*cc22);
+ std::cout<<"coefficient de corrélation : "<<cc<<std::endl;
+
+ return cc;
+
+ }
+
+
+
+ //---------------------------------------------------------------------
+ //---------------------------------------------------------------------
+
+ Signal Signal::Xcov(const Signal &input2 , int &shift, double &coef, double &cc) {
+
+ Signal temp_output,output;
+ unsigned int N;
+ N=size();
+ unsigned int max;
+ max = 2*N;
+ output.resize(max);
+ temp_output.resize(max);
+ double cc11,cc22;
+ Signal input1(*this);
+
+ cc=this->UnormalizedXcov(input2,output);
+ cc11=this->UnormalizedCorrelation(input1);
+ cc22=input2.UnormalizedCorrelation (input2);
+
+ unsigned int tailleOutput;
+ tailleOutput=output.size();
+ for (unsigned int k= 0;k<tailleOutput;k++){
+ output[k]=output[k]/sqrt(cc11 * cc22 );
+ }
+
+ coef=output[0];
+ shift=0;
+ for (unsigned int k= 0;k<tailleOutput;k++){
+ if(output[k]>coef){
+ coef=output[k];
+ shift=k;
+ }
+ }
+ cc=this->Correlation(input2);
+ shift=shift-N+1;
+ std::cout<<"shift: "<<shift<<std::endl;
+ std::cout<<"XcovMax: "<<coef<<std::endl;