// Copyright (C) 1999 Jean-Marc Valin #include "BufferedNode.h" #include "Buffer.h" #include "Vector.h" #include #include using namespace std; namespace FD { class NLMS; DECLARE_NODE(NLMS) /*Node * * @name NLMS * @category DSP:Adaptive * @description Normalized LMS algorithm * * @input_name INPUT * @input_description The input of the adaptive FIR filter * * @input_name REF * @input_description The signal being tracked * * @output_name OUTPUT * @output_description The output of the adaptive FIR filter (not the residue) * * @parameter_name FILTER_LENGTH * @parameter_description Length of the adaptive FIR filter * * @parameter_name ALPHA * @parameter_description Adaptation rate of the filter coefficients * * @parameter_name BETA * @parameter_description Adaptation rate of the normalization energy estimate * * @parameter_name POWER * @parameter_description Normalization power * END*/ class NLMS : public BufferedNode { int inputID; int refID; int outputID; int size; Vector a; float alpha; float beta; float E; float power; //Vector w; //Vector grad; public: NLMS(string nodeName, ParameterSet params) : BufferedNode(nodeName, params) { inOrder = true; inputID = addInput("INPUT"); refID = addInput("REF"); outputID = addOutput("OUTPUT"); size = dereference_cast (parameters.get("FILTER_LENGTH")); alpha = dereference_cast (parameters.get("ALPHA")); beta = dereference_cast (parameters.get("BETA")); power = dereference_cast (parameters.get("POWER")); a.resize(size,0.0); //w.resize(size,1.0); //grad.resize(size,0.0); inputsCache[inputID].lookBack=1; } void initialize() { for (int j=0;j &in = object_cast > (inputValue); const Vector &ref = object_cast > (refValue); int inputLength = in.size(); Vector &output = *Vector::alloc(inputLength); out[count] = &output; for (int i=0;i *past; bool can_look_back = false; if (count > 0) { ObjectRef pastInputValue = getInput(inputID, count-1); can_look_back=true; past = &object_cast > (pastInputValue); } DYN_VEC(float, inputLength+size-1, _x); //float _x[inputLength+size-1]; float *x=_x+size-1; if (can_look_back) { for (int i=0;i