将matlab的AGC函数转换为C

时间:2019-11-15 02:21:11

标签: c matlab embedded simulink telecommunication

我正在尝试从Matlab的通讯工具箱中转换“自动增益控制”模块。 Here is the documentation,但相关部分总结如下:

框图

Block Diagram

位置

enter image description here

enter image description here

enter image description here

这是我编写的代码:

  baseCharge = float(daysRented) * budget_charge

但是我得到的输出与Matlab中相同的值不一样。我在做什么错了?

谢谢!

2 个答案:

答案 0 :(得分:1)

也许这不是唯一的错误,但我注意到,AGC检测器的总和在Matlab文档中从ndx*N(ndx+1)N-1,但是在您的C代码中,它从 ndx*N(ndx+1)(N-1)

答案 1 :(得分:1)

我知道了。下面的代码针对给定的一组复杂数据提供与matlab相同的输出。

double AGC_Detector(double complex input, double* sum, double state[], unsigned int* idx, int N)
{
    unsigned int first;
    unsigned int nth;
    double output;
    double val = cabs(input)*cabs(input);
    *sum += val;
    output = (*sum)*(1.0/N);
    *sum -= state[*idx-1];

    if(*sum < 0.0){*sum = 0.0;}

    state[*idx-1] = val;
    first = *idx;
    nth = first + 1;

    if(nth < first){nth = 0xFFFFFFFF;}

    *idx = nth;

    if(*idx > N-1){*idx = 1;}

    return output;
}


void use_AGC(double complex* input, double complex* output, double step, double desired_pwr, int avrg_len, int max_pwr, int len)
{
    unsigned int idx = 1;
    int filt_len = avrg_len-1;
    double K = step;
    double g = 0;
    double sum = 0;
    double dtctr = 0;
    double filterState[filt_len];

    for(int i = 0; i < filt_len; i++){filterState[i] = 0;}

    for(int i = 0; i < avrg_len; i++)
    {
        dtctr = AGC_Detector(input[i], &sum, filterState, &idx, avrg_len);
        output[i] = input[i] * exp(g);
        dtctr = log(dtctr);
        g += K*(desired_pwr - (dtctr + 2.0*g));
        if(g > max_pwr){g = max_pwr;}
    }
}