Goertzel algorithm giving infinite result

0

I have a sinewave at 20hz - 1 amplitude that I have created using Audacity software. It is also only 500ms.

I am using following algorithm to detect the frequency.

All I want to detect if tone amplitude passes a threshold and gives me positive result at 20 hz frequency cycles.

static float goertzel_mag(int numSamples,int TARGET_FREQUENCY,int SAMPLING_RATE, float* data)
{
    int     k,i;
    float   floatnumSamples;
    float   omega,sine,cosine,coeff,q0,q1,q2,magnitude,real,imag;

    float   scalingFactor = numSamples / 2.0;

    floatnumSamples = (float) numSamples;
    k = (int) (0.5 + ((floatnumSamples * TARGET_FREQUENCY) / SAMPLING_RATE));
    omega = (2.0 * M_PI * k) / floatnumSamples;
    sine = sin(omega);
    cosine = cos(omega);
    coeff = 2.0 * cosine;
    q0=0;
    q1=0;
    q2=0;

    for(i=0; i<numSamples; i++)
    {
        q0 = coeff * q1 - q2 + data[i];
        q2 = q1;
        q1 = q0;
    }

    // calculate the real and imaginary results
    // scaling appropriately
    real = (q1 - q2 * cosine) / scalingFactor;
    imag = (q2 * sine) / scalingFactor;

    magnitude = sqrtf(real*real + imag*imag);
    return magnitude;
}

call the function

// If there's more packets, read them
        inCompleteAQBuffer->mAudioDataByteSize = numBytes;
        CheckError(AudioQueueEnqueueBuffer(inAQ,
                                           inCompleteAQBuffer,
                                           (sound->packetDescs?nPackets:0),
                                           sound->packetDescs),
                   "couldn't enqueue buffer");
        sound->packetPosition += nPackets;

        NSLog(@"number of packets %i",nPackets);

        float *data=(float*)inCompleteAQBuffer->mAudioData;
        int nn = sizeof(data)/sizeof(float);
        float gort = goertzel_mag(nn, 20, 44100, data);
        NSLog(@"gort:%f", gort);
        if (gort == INFINITY)
             NSLog(@"positive infinity");  

break point inside of the function enter image description here

output

number of packets 8192
gort:36029896530591744.000000
number of packets 8192
gort:inf
positive infinity
number of packets 5666
gort:inf
positive infinity 

Why I am getting inf result? I don't know how to read the return value, I understand magnitude always has to be positive value but I create the file with 1 amplitude, shouldn't I be getting 0 to 1 results?

EDIT; Aduio info

afinfo 500ms.aiff
File:           500ms.aiff
File type ID:   AIFF
Num Tracks:     1
----
Data format:     1 ch,  44100 Hz, 'lpcm' (0x0000000E) 16-bit big-endian signed integer
                no channel layout.
estimated duration: 0.500000 sec
audio bytes: 44100
audio packets: 22050
bit rate: 705600 bits per second
packet size upper bound: 2
maximum packet size: 2
audio data file offset: 54
optimized
source bit depth: I16
ios
c
audio
signal-processing
dft
asked on Stack Overflow Mar 9, 2015 by Mord Fustang • edited Mar 9, 2015 by Mord Fustang

1 Answer

2

I think one problem is with these lines:

float *data=(float*)inCompleteAQBuffer->mAudioData;
int nn = sizeof(data)/sizeof(float);

which I believe is intended to tell you the number of samples. I don't have the information or resources to reproduce your code, but can reproduce the bug with this:

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    float *data=malloc(sizeof(float) * 10);
    printf ("Sizeof 'data' = %d\n", sizeof(data));
    return 0;
}

Program output

Sizeof 'data' = 4

which on my 32-bit compilation is the size of the array pointer, not the array. And using sizeof(*data) won't get you anywhere since that just tells you the size of the data type float, not the array.

There is no way you can ascertain the size of the array, or number of elements, from its pointer, so my answer is, sadly, you need more information, perhaps numBytes? Or numBytes/sizeof(float)?

answered on Stack Overflow Mar 9, 2015 by Weather Vane • edited Mar 9, 2015 by Weather Vane

User contributions licensed under CC BY-SA 3.0