Autocorrelation

From emboxit
Jump to: navigation, search

Reverse engineering

File:Autocorrelation-1.jpg
extracted from C code
File:Autocorrelation-2.jpg
For the 3d term of Correlation R (lag = j = 1)
File:Autocorrelation-4.jpg
From Wikipedia, Discrete Autocorrelation
  • Reverse engineering from C code:

<cpp>

   for (i=0;i<order;i++) {
       sum=0;
       for (j=0;j<order-i;j++) {
           sum+=x[j]*x[j+i];
       }
       R[i]=sum;
   }
   return R;

</cpp>


















General

Autocorrelation is the cross-correlation of a signal with itself. 
Informally, it is the similarity between observations as a function of the time lag between them. 
It is a mathematical tool for finding repeating patterns, such as the presence of a periodic signal obscured by noise, 
or identifying the missing fundamental frequency in a signal implied by its harmonic frequencies. 
It is often used in signal processing for analyzing functions or series of values, such as time domain signals.
Autocorrelation is the cross-correlation of a signal with itself. Informally, 
it is the similarity between observations as a function of the time separation between them. 
It is a mathematical tool for finding repeating patterns, such as the presence of a periodic signal 
which has been buried under noise, or identifying the missing fundamental frequency 
in a signal implied by its harmonic frequencies. 
It is often used in signal processing for analyzing functions or series of values, such as time domain signals.


Publications

This report describes a robust method of Instantaneous Heart Rate (IHR) detection from noisy electrocardiogram (ECG) signals. 
Generally, the IHR is calculated from the interval of R-waves. Then, the R-waves are extracted from the ECG using a threshold. 
However, in wearable biosignal monitoring systems, various noises 
(e.g. muscle artifacts from myoelectric signals, electrode motion artifacts) increase incidences of misdetection
 and false detection because the power consumption and electrode distance of the wearable sensor 
are limited to reduce its size and weight. To prevent incorrect detection, we use a short-time autocorrelation technique. 
The proposed method uses similarity of the waveform of the QRS complex. 
Therefore, it has no threshold calculation Process and it is robust for noisy environment. 
Simulation results show that the proposed method improves the success rate of IHR detection by up to 37%.


C++

<cpp>

  1. include "stdafx.h"
  2. include <sstream>
  3. include <iostream>
  4. include <fstream>
  5. include <istream>
  6. include <string>
  7. include <vector>
  1. define PI 3.14159265

typedef std::vector<float> float_vec_t;

using namespace std;

class LPCAnalysis{

   public:
     float_vec_t LPCAnalysis::autoCorrelation(const float_vec_t &x);

};


/* Calculate the (un-normalized) autocorrelation for a frame of a signal */

float_vec_t LPCAnalysis::autoCorrelation(const float_vec_t &x) {

   short order=x.size();
   float_vec_t R(order);
   float sum;
   int i,j;
   for (i=0;i<order;i++) {
       sum=0;
       for (j=0;j<order-i;j++) {
           sum+=x[j]*x[j+i];
       }
       R[i]=sum;
   }
   return R;

} </cpp>


In C, created from Numerical recipies

<cpp> /*******************************************************************

Program: autocor.c Eric R. Weeks 3/10/97

link to this program: http://www.physics.emory.edu/~weeks/software/autocor.html email: weeks@physics.emory.edu

This program is public domain, but you are not allowed to remove the web or email information. No guarantees are made, but I think the program is bug-free.


autocor.c-----------

03-10-97: started from cor.c add -t option

*******************************************************************/
  1. include <stdio.h>
  2. include <stdlib.h>
  3. include <math.h>
  1. define MAX 500100

char *prgname;

main(argc,argv) int argc; char **argv; { int r,t; int c,numpts; FILE *fp, *fopen(); extern int optind; extern char *optarg; double sum[2],avg[2],sumsq[2],std[2],max[2],min[2]; double dat[MAX][2]; double sqrt(); double temp,cor; int tau; float deltat=-1;

tau = 100;

   while ((c = getopt(argc, argv, "ht:d:")) != EOF)
       switch (c)
       {
       case 'h':  

fprintf(stderr,"input should be one column of data\n\n"); fprintf(stderr,"Usage: cor [<] datafile\n"); fprintf(stderr,"\nOptions:\n"); fprintf(stderr," -h  : this help option\n"); fprintf(stderr," -d # : set maximum delay for autocorrelation [%d]\n",tau); fprintf(stderr," -t # : set delta-t between each point [1.0]\n");

              exit(1);
              break;

case 'd': tau = atoi(optarg); break; case 't': deltat = atof(optarg); break;

       }
   argc -= (optind-1) ; argv += (optind-1) ;
   fp = (argc > 1) ? fopen(*++argv, "r") : stdin;

numpts = 0; for (r=0;r<2;r++) { sum[r] = 0.0; sumsq[r] = 0.0; max[r] = -9.999e99; min[r] = +9.999e99; }

while (fscanf(fp,"%lf",&dat[numpts][0]) == 1) { for (r=0;r<1;r++) { sum[r] = sum[r] + dat[numpts][r]; sumsq[r] = sumsq[r] + dat[numpts][r]*dat[numpts][r]; max[r] = (max[r] > dat[numpts][r]) ? max[r] : dat[numpts][r]; min[r] = (min[r] < dat[numpts][r]) ? min[r] : dat[numpts][r]; } ++numpts; if (numpts >= MAX) { fprintf(stderr,"too many data points, limit is %d\n",MAX); exit(1); } }

fprintf(stderr," avg std max min number\n"); for (r=0;r<1;r++) { avg[r] = sum[r]/numpts; std[r] = sqrt((sumsq[r]/numpts)-avg[r]*avg[r]); fprintf(stderr,"%10g %10g %10g",avg[r],std[r],max[r]); fprintf(stderr," %10g %10d\n",min[r],numpts); std[r] = 1.0/std[r]; } cor = 0.0; for (t=0;t<numpts;t++) dat[t][0] = (dat[t][0]-avg[0])*std[0];

for (r=0;r<tau;r++) { cor = 0.0; for (t=0;t<(numpts-r);t++) cor += dat[t][0]*dat[t+r][0]; cor = cor / ((double)(numpts-r)); if (deltat<0.0) printf("%d %g\n",r,cor); else printf("%f %g\n",((float)r)*deltat,cor); } exit(0); } </cpp>



Projects

Pitch detection with Processing and Arduino, VIDEO