0

我正在用 C++ 实现 Goertzel 算法,到目前为止我已经做到了:

#include <iostream>
#include <fstream>
#include <string>
#include <math.h>

const double pi = 3.14159;

class digitalFilter{
    private:
        int     k,i;
        float   temp;
        float   scalingFactor;
        float   floatnumSamples;
        float   omega,sine,cosine,coeff,q0,q1,q2,magnitude,real,imag;
        int const static limit = 205;
    public:
        digitalFilter();
        void readDataFromFile();
        float goertzel_mag(int numSamples,int TARGET_FREQUENCY,int SAMPLING_RATE, float* data);
};

digitalFilter::digitalFilter(){
    float* data = new float[limit];
}

void digitalFilter::readDataFromFile(){
    //get 205 values from txt file and store this in an array
    std::ifstream dataFile ("data_files/datad.txt");
    if (dataFile.is_open()){
        for (int i = 0; i < limit; i++){
            dataFile >> temp;
            data[i] = temp;
        }
        dataFile.close();
    }else{
        std::cout << "Unable to open file\n";
    }
}

float digitalFilter::goertzel_mag(int numSamples,int TARGET_FREQUENCY,int SAMPLING_RATE, float* data){
    scalingFactor = numSamples / 2.0;

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

    for(int 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);
    std::cout << "a" << TARGET_FREQUENCY << " = " << magnitude << std::endl;
    return magnitude;
}


int main()
{
    digitalFilter ObjOne;
    //compute amplitude magnitude of DFT
    ObjOne.readDataFromFile();
    ObjOne.goertzel_mag(205,697,8000,data);
    ObjOne.goertzel_mag(205,770,8000,data);
    ObjOne.goertzel_mag(205,852,8000,data);
    ObjOne.goertzel_mag(205,941,8000,data);
    ObjOne.goertzel_mag(205,1209,8000,data);
    ObjOne.goertzel_mag(205,1336,8000,data);
    ObjOne.goertzel_mag(205,1477,8000,data);
    ObjOne.goertzel_mag(205,1633,8000,data);

    return 0;
}

但我得到了这些愚蠢的错误代码:

goertzel_mag-v3.cpp: In member function 'void digitalFilter::readDataFromFile()':
goertzel_mag-v3.cpp:41:4: error: 'data' was not declared in this scope
goertzel_mag-v3.cpp: In function 'int main()':
goertzel_mag-v3.cpp:85:35: error: 'data' was not declared in this scope

我完全迷失了,为什么我的成员函数readDataFromFile()找不到我的数组data[]?似乎构造函数构建了数组,然后在程序离开构造函数后将其销毁?我该如何解决这些明显愚蠢的错误?

4

2 回答 2

4
digitalFilter::digitalFilter(){
    float* data = new float[limit];
}

退出构造函数后,data数组不再存在。使其成为类成员变量或全局变量以保留它。

于 2013-10-16T18:30:04.620 回答
0

将 float *data 初始化为成员函数将解决第一个问题。因为它是在构造函数中初始化的,并且仅对该块可用并且超出范围

对于主要部分,一旦您将其设为成员函数,就可以使用该对象访问它。例如:obj.data

于 2013-10-16T18:32:34.427 回答