views:

63

answers:

2

I have read a code for pitch determination using autocorrelation method. Can anybody please tell what would be the input data (passed as argument to DetectPitch()) function here:

double DetectPitch(short* data)
{
    int sampleRate = 2048;

    //Create sine wave
    double *buffer = malloc(1024*sizeof(short));
    double amplitude = 0.25 * 32768; //0.25 * max length of short
    double frequency = 726.0;
    for (int n = 0; n < 1024; n++)
    {
        buffer[n] = (short)(amplitude * sin((2 * 3.14159265 * n * frequency) / sampleRate));
    }

    doHighPassFilter(data);

    printf("Pitch from sine wave: %f\n",detectPitchCalculation(buffer, 50.0, 1000.0, 1, 1));
    printf("Pitch from mic: %f\n",detectPitchCalculation(data, 50.0, 1000.0, 1, 1));
    return 0;
}
+2  A: 

It looks like you need to at least change:

double *buffer = malloc(1024*sizeof(short));

to:

short *buffer = malloc(1024*sizeof(short));

Paul R
+2  A: 

It seems that "data" is used exactly the same way as the locally allocated "buffer", so I suppose it's something like short data[1024], i.e. 1024 signal samples between -32768 and 32767 (the way "amplitude" is calculated makes suppose that the "short" type is 16 bit here). By the way, as "max length of short" (as the comment says) I would expect 32767, not 32768 (there is a theoretical overflow with maximum positive values).

Regards

Giuseppe Guerrini
As Paul R noted, the type of "buffer" would probably be "short *".
Giuseppe Guerrini
+1: You're right about the first part regarding the input data but the last part about the overflow is not an issue - it's just an amplitude calculation and the sine wave values will vary between +/- 8192.
Paul R
Right, my note was only about the comment "max length of short", since it's in general safer to use 32767, by a theoretical point of view.
Giuseppe Guerrini