c - 归一化最小均方 - C 实现

标签 c matlab math signal-processing

我希望在 C 语言中实现归一化最小均方 (NLMS)。我的问题在于权重更新(我认为),因为我正在针对标准 MATLAB 库运行它。这是 MATLAB 代码(有效):

function [e,w,y]=nlmsFunc(mu,M,u,d,a);
% Normalized LMS
% Call:
% [e,w]=nlms(mu,M,u,d,a);
%
% Input arguments:
% mu = step size, dim 1x1
% M = filter length, dim 1x1
% u = input signal, dim Nx1
% d = desired signal, dim Nx1
% a = constant, dim 1x1
%
% Output arguments:
% e = estimation error, dim Nx1
% w = final filter coefficients, dim Mx1
%intial value 0

w=zeros(M,1); %This is a vertical column

%input signal length
N=length(u);
%make sure that u and d are colon vectors
u=u(:);
d=d(:);
%NLMS
for n=M:N %Start at M (Filter Length) and Loop to N (Length of Sample)
uvec=u(n:-1:n-M+1); %Array, start at n, decrement to n-m+1
e(n)=d(n)-w'*uvec;
w=w+mu/(a+uvec'*uvec)*uvec*conj(e(n));
y(n) = w'*uvec; %In ALE, this will be the narrowband noise.
end

我的问题是将它翻译成 C,这是我目前所拥有的:

float mu = 0.05; //Set up mu
    int a = 1; //Constant

    int inputSigSize = numSamples;
    float outputYSignal[inputSigSize];
    float desiredSignal[inputSigSize];
    float error[inputSigSize];


    float inputSignal[inputSigSize];

    //Initialise Weights to Zero
    if (weights[0] == 0) {

        for (int k = 0; k<=filterLength; k++) {
            weights[k]=0;
        }
    }

    float X[filterLength+1];
    float Y = 0;
    float E = 0;

    //Start NLMS Loop
    for (int t = 0; t<numSamples; t++) {

        X[0] = inputSignal[t];

        for (int i = 0; i<=filterLength; i++) {
            Y += (weights[i]*X[i]);
        }

        E = desiredSignal[t] - Y;

        for (int i = filterLength; i>=0; i--) {
            weights[i] = weights[i] + (mu*E*X[i]);

            if (i!=0) {
                X[i]=X[i-1];
            }
        }

        outputYSignal[t] = Y;
        error[t] = E;
    }
    //END NLMS Loop

我感觉这是我处理权重更新的一种方式。

最佳答案

NLMS C 代码实现:

#define inputSize 800

#define N 64 // filter size

double stepsize = 0.0; //Set up mu
double x[inputSize];
double d[inputSize];
double y[inputSize];
double h[N];
double e[inputSize];
int M = inputSize;

void nlmsFilter() {

//Initialise Weights to Zero
memset(y, 0, inputSize);
memset(e, 0, inputSize);
memset(h, 0, N);

double X1[N];
int t, j, i;

for (t = N; t <= M; t++) {
    for (j = (t - 1); j >= (t - N); j--) {
        X1[t-j-1] = x[j];
        printf("%d %lf %lf \n", (t-j-1), X1[t-j-1], x[j]);
    }

    for (i = 0; i < N; i++) {
        y[t-1] += (h[i] * X1[i]);
    }

    e[t-1] = d[t-1] - y[t-1];

    for (i = 0; i < N; i++) {
        stepsize += (X1[i] * X1[i]);
    }

    stepsize = 1/stepsize;

    for (i = 0; i < N; i++) {
        h[i] = h[i] + (stepsize * e[t-1] * X1[i]);
    }
}
return;
}

关于c - 归一化最小均方 - C 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21428996/

相关文章:

C:如何在不使用 malloc 的情况下编辑函数中的 C 字符串

c - PAHO 嵌入式 C 库 Socket 接收实现

php - 将 N 名员工安置在具有角色的 M 个职位的算法

algorithm - 在排序列表中搜索饱和度值的最佳方法

c++ - ffmpeg/libx264 C API : frames dropped from end of short MP4

c - memset 清除不应该的内容

arrays - 区分 'inf'和 '-inf'

matlab - 如何自动为 fmincon 找到合适的初始值?

performance - 如何向量化许多 rank-1 外积的形成?

c - 我们如何计算多重集的 r 组合?