Tummas Tomasson / Mbed 2 deprecated PingDetector_mbedadc

Dependencies:   mbed

PingDetect/FFT.cpp

Committer:
tummastt
Date:
2012-07-30
Revision:
0:efb27fbc92c0
Child:
1:227db871d328

File content as of revision 0:efb27fbc92c0:

#include "FFT.h"

// Using the Mellen FFT
extern "C" void fftR4(short *y, short *x, int N);

int FFT::DetectSignal(short data[]) {

    CalculateFFT(data);

    // adding the center frequencies with the 4 closesed frequencies
//    amp = my[centerFreq-2]+my[centerFreq]+my[centerFreq+2];
    amp = my[centerFreq-2]+my[centerFreq]+my[centerFreq+2];
//    return amp;
    if (amp >= AMPLITUDE_LIMIT)
        return found; // indicating pinger signal found

    else
        return notFound; // indicating no pinger signal found

}

void FFT::CalculateFFT(short data[]) {

    // Clearing array
    for (i=0; i<SAMPLES*2; i++)
        mx[i]=0;

    // Add data to array
    for (i=0; i<SAMPLES; i=i+1)
        mx[i*2]=data[i]-DCOFFSET;

    // calculate fft
    fftR4(my, mx, SAMPLES);

    // make all values positive
    for (i=0; i<SAMPLES; i=i+2) {
        my[i] = abs(my[i]);
    }
}