Forums

Autocorrelation function, Java or C code

Started by Thomas Magma June 16, 2006
Does anyone have any optimized Java or C code for an autocorrelation 
function. It is my first time needing to do autocorrelation and it seems 
straight forward enough to be able to write the code myself, but due to the 
amount of iterations it would be wise to ask for code that already has it's 
fat trimmed.

Thanks in advance.
Thomas 


#include "stdafx.h"
#include <sstream>
#include <iostream>
#include <fstream>
#include <istream>
#include <string>
#include <vector>

#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;
}

Its in C++ and is very simple but should get you started.

Shlomo Kashani.

Thomas Magma wrote:
> Does anyone have any optimized Java or C code for an autocorrelation > function. It is my first time needing to do autocorrelation and it seems > straight forward enough to be able to write the code myself, but due to the > amount of iterations it would be wise to ask for code that already has it's > fat trimmed. > > Thanks in advance. > Thomas
For history, here is the Java version of Kalman's below C++ code for the 
autocorrelation function. Works good!

public void autoCorrelation(int size){
    float[] R = new R[size];
    float sum;

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

"Kalman" <shlomo_kashani@yahoo.com> wrote in message 
news:1150638032.157626.100330@f6g2000cwb.googlegroups.com...
> #include "stdafx.h" > #include <sstream> > #include <iostream> > #include <fstream> > #include <istream> > #include <string> > #include <vector> > > #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; > } > > Its in C++ and is very simple but should get you started. > > Shlomo Kashani. > > Thomas Magma wrote: >> Does anyone have any optimized Java or C code for an autocorrelation >> function. It is my first time needing to do autocorrelation and it seems >> straight forward enough to be able to write the code myself, but due to >> the >> amount of iterations it would be wise to ask for code that already has >> it's >> fat trimmed. >> >> Thanks in advance. >> Thomas >
Just for the record, I don't remember if i wrote it or found out
sometime ago on the web.
Anyway, I am glad you found it helpful.

Shlomo Kashani.

Thomas Magma wrote:
> For history, here is the Java version of Kalman's below C++ code for the > autocorrelation function. Works good! > > public void autoCorrelation(int size){ > float[] R = new R[size]; > float sum; > > for (int i=0;i<size;i++) { > sum=0; > for (int j=0;j<size-i;j++) { > sum+=x[j]*x[j+i]; > } > R[i]=sum; > } > } > > "Kalman" <shlomo_kashani@yahoo.com> wrote in message > news:1150638032.157626.100330@f6g2000cwb.googlegroups.com... > > #include "stdafx.h" > > #include <sstream> > > #include <iostream> > > #include <fstream> > > #include <istream> > > #include <string> > > #include <vector> > > > > #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; > > } > > > > Its in C++ and is very simple but should get you started. > > > > Shlomo Kashani. > > > > Thomas Magma wrote: > >> Does anyone have any optimized Java or C code for an autocorrelation > >> function. It is my first time needing to do autocorrelation and it seems > >> straight forward enough to be able to write the code myself, but due to > >> the > >> amount of iterations it would be wise to ask for code that already has > >> it's > >> fat trimmed. > >> > >> Thanks in advance. > >> Thomas > >
Thanks Kalman,

How about an algorithm for crosscorrelation that I can convert to java (or 
is that too much to ask)? I imagine that it will be slightly different than 
the autocorrelation function. In crosscorrelation your regenerated data has 
to start on one side and slide completely passed your sampled data, but in 
the autocorrelation function because the two data sets are equal they can 
start square and can just slip off to one side. It should be twice (-1) as 
much data output in the cosscorrelation function shouldn't it?

Thanks again,
Thomas

"Kalman" <shlomo_kashani@yahoo.com> wrote in message 
news:1151508013.558259.191120@x69g2000cwx.googlegroups.com...
> Just for the record, I don't remember if i wrote it or found out > sometime ago on the web. > Anyway, I am glad you found it helpful. > > Shlomo Kashani. >
Hi I'm trying to do pitch tracking (work out if the current spectrum buffer
thing is higher or lower frequency than the previous one), using a human
voice input from a microphone.

  I'm using Java written in the processing environment and using the ESS
library (ESS has built in fast fourier transforms. check it out at-
http://processing.org ).  I've little math and programming skill, so would
really appreciate if someone could walk me through the next step from
Thomas's code (or Thomas if you're listening!).

  I've modified it so as to not be a class and fit in with what i've done
so far.  I've got an array 32 floats long which is the spectrum data.  I'm
assuming that the array x in thomas's code is the equivalent to my spectrum
data?  
i.e. 

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

becomes...

    for (int i=0; i<myFFT.spectrum.length; i++) {
      sum=0;
      for (int j=0; j<myFFT.spectrum.length-i; j++) {
        sum += myFFT.spectrum[j] * myFFT.spectrum[j+i];
      }
      R[i]=sum;
    }

This works fine, and i get an array R with 32 floats which change from
about 10e-11 to 10e-5 when i sing into the microphone, but i can't dicern
any difference in them when i sing high pitched or low.  Now i assume i'm
missing another all important step to extract the approximate overall
frequency from my voice input.  Would anyone out there care to enlighten
me?? Please!!!

Any help appreciated,
Thomas.

>For history, here is the Java version of Kalman's below C++ code for the
>autocorrelation function. Works good! > >public void autoCorrelation(int size){ > float[] R = new R[size]; > float sum; > > for (int i=0;i<size;i++) { > sum=0; > for (int j=0;j<size-i;j++) { > sum+=x[j]*x[j+i]; > } > R[i]=sum; > } >} > >"Kalman" <shlomo_kashani@yahoo.com> wrote in message >news:1150638032.157626.100330@f6g2000cwb.googlegroups.com... >> #include "stdafx.h" >> #include <sstream> >> #include <iostream> >> #include <fstream> >> #include <istream> >> #include <string> >> #include <vector> >> >> #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; >> } >> >> Its in C++ and is very simple but should get you started. >> >> Shlomo Kashani. >> >> Thomas Magma wrote: >>> Does anyone have any optimized Java or C code for an autocorrelation >>> function. It is my first time needing to do autocorrelation and it
seems
>>> straight forward enough to be able to write the code myself, but due
to
>>> the >>> amount of iterations it would be wise to ask for code that already has
>>> it's >>> fat trimmed. >>> >>> Thanks in advance. >>> Thomas >> > > >