数据生成问题

时间:2017-03-13 20:11:25

标签: c++ filtering signal-processing

由于我开发的功能存在问题,我在使用C ++向文件写入数据时遇到了问题。我将来自外部源的.dat文件加载到我的C ++项目中。正确开发的while循环逐行读取每个数据元素并将其传递给FIR滤波器函数,该函数采用三个参数,即滤波器长度,滤波器系数std::vector<float>float data

float f0 = 8000;
float fs = 32000;
int filt_length = 21;
float filtered,data;
fir_lowpass windowed_filter(f0,fs);

std::ifstream input;
std::ofstream output;
input.open("chirp.dat");
output.open("filter_test_sig.dat");

std::vector<float> blackman_choice,basic_lpf;

basic_lpf = windowed_filter.fir_sinc_filter_weights(filt_length);

blackman_choice = windowed_filter.blackman_window(filt_length, basic_lpf);

if (input.fail() && output.fail()) {
    std::cout << "Files not opened." << std::endl;
}
else{
    while (input >> data) {
        filtered = windowed_filter.generic_fir_filter(filt_length, blackman_choice, data);
        output << filtered << std::endl;
        std::cout << filtered << std::endl;
    }
}

我已经确认这个循环确实按预期工作,因为我有一个复杂得多的FIR过滤器头文件。以下是与上述代码一起使用的原始函数:

float lpf_filter(float input){

    forward_buffer[0] = input;
    float output;

    for (int i = 0; i<filter_length; i++) {
        output += B[i]*forward_buffer[i];
    }
    for (int i = filter_length-1; i>0; i--) {
        forward_buffer[i] = forward_buffer[i-1];
    }
    return output;
}

请注意,array B是预定义的,在 Matlab 中计算并导出。在下一个函数中,我试图重写此函数以从另一个函数中获取已计算的系数。这是代码和它下面的示例系数函数(更新float output = 0):

float generic_fir_filter(const std::vector<float> &coefficients, float input){
    std::vector<float> buffer(coefficients.size(),0.0f);
    buffer[0] = input;
    float output = 0;

    for (int i = 0; i<(int)coefficients.size(); i++) {
        output += coefficients[i]*buffer[i];
    }
    for (int i = (int)coefficients.size()-1; i>0; i--) {
        buffer[i] = buffer[i-1];
    }
    return output;
}
std::vector<float> fir_sinc_filter_weights(int filt_length){
    std::vector<float> w(filt_length);
    M = filt_length-1;

    for (int i = 0; i<filt_length; i++) {
        if (i == M/2) {
            w[i] = 2*fn;
        }
        else{
            w[i] = sin(2*M_PI*fn*(i-M/2))/(M_PI*(i-M/2));
        }
    }
    return w;
}

当我在.cpp文件中的内部while循环中运行此函数时,它只为每个循环迭代生成相同的过滤值,而我的原始函数没有。我还将加载的.dat文件转换为std::vector<float>,并尝试一次加载一个样本,但这也不起作用。任何有关此事的帮助将不胜感激!

编辑:

我现在已将std::vector<float> buffer移至private:,以尝试将之前的剩余状态保留在缓冲区内。我还在构造函数中添加了一个小的补充,告诉std::vector<float> buffer它应该有多少个数组元素,并将它们全部初始化为零。现在的问题是,当我在generic_fir_filter()内部调用缓冲区时,当我在上述函数中调用EXC_BAD_ACCESS时出现buffer[0] = input;错误:

private:
   int M;
   float fn;
   std::vector<float> buffer;
public:
fir_lowpass(float f0,float fs,int weight_size){
    fn = f0/fs;
    std::vector<float> buffer(weight_size, 0.0f);
}
float generic_fir_filter(const std::vector<float> &coefficients, float input){
    //std::vector<float> buffer(coefficients.size(),0.0f);
    buffer[0] = input;
    float output = 0;

    for (int i = 0; i<(int)coefficients.size(); i++) {
        output += coefficients[i]*buffer[i];
    }
    for (int i = (int)coefficients.size()-1; i>0; i--) {
        buffer[i] = buffer[i-1];
    }
    return output;
}

1 个答案:

答案 0 :(得分:0)

我找到了调用' refresh all Pivot Tables in "DD" worksheet For Each PvtTbl In Worksheets("DD").PivotTables ' Create/Update Pivot Cache Set PvtCache = ActiveWorkbook.PivotCaches.Add(xlDatabase, PvtDataRng) With PvtTbl .ChangePivotCache PvtCache .RefreshTable End With Set PvtCache = Nothing ' <-- clear the Pivot Cache Next PvtTbl 并将其数组大小设置为我需要的任何问题的解决方案。诀窍是在std:vector<float> buffer中声明缓冲区,然后在构造函数设置中声明缓冲区,并通过构造函数名称旁边的private:操作初始化缓冲区:

:

感谢大家的帮助!