Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Normalised Least Mean Squares - C Implemetation

  • 29-01-2014 10:00am
    #1
    Registered Users, Registered Users 2 Posts: 8,004 ✭✭✭


    Hi Everyone,

    I'm looking to implement the Normalised Least Mean Squares (NLMS) in C. My issue is in the weight update (I think) As I'm running it against a standard MATLAB library. This is the MATLAB code (That works):
    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
    
    

    My issue is translating this to C, and this is what I have so far:
    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
    

    Can anyone shed some light? This isn't homework / assignment. I have a feeling its a way I'm handling the weight updates :(


Comments

  • Registered Users, Registered Users 2 Posts: 360 ✭✭eddyc


    I've only had a brief look at the code there, but whenever I am translating Matlab to C, I try to use functions as much as possible to make the structure of the main C function as close to the Matlab one. Things like vector/matrix multiplication, dot product etc., if you can make these functions and you know that the functions are accurate it will make it easier to debug than a function with a load of nested for loops and the like.

    Or you can of course use a library, armadillo is a C++ one so I don't know if that would be suitable, but it manages to get very close to Matlab syntax http://arma.sourceforge.net/


Advertisement