Help writing a function
-
Hello All, I am new to C++ and am trying to do some DSP programming. I would like to write the following function y(n) = (1-a)*x(n-1) + a*y(n-1) The arguments to this function are all floats - "x", "y" and "a". Could somebody help me with this?
-
Hello All, I am new to C++ and am trying to do some DSP programming. I would like to write the following function y(n) = (1-a)*x(n-1) + a*y(n-1) The arguments to this function are all floats - "x", "y" and "a". Could somebody help me with this?
I could help you if you can answer these 2 questions: (1) is
x
a function (if so, what is it)? (2) what's the definition ofy(0)
? /ravi My new year's resolution: 2048 x 1536 Home | Articles | Freeware | Music ravib@ravib.com -
I could help you if you can answer these 2 questions: (1) is
x
a function (if so, what is it)? (2) what's the definition ofy(0)
? /ravi My new year's resolution: 2048 x 1536 Home | Articles | Freeware | Music ravib@ravib.comThanks, Ravi. To answer your questions, this function which I'll call SmoothingFunction, is called by another function by name, SmoothCoeffs(). I have two arrays, Coeff[] and Smooth[], each with 5 elements. Values in the array Coeff[] are "smoothed" and then stored in Smooth[]. Here is the SmoothCoeffs() function: NUM_ITER is 5.
inline void cDSP::SmoothCoeffs() { float *nStart = &gnCoeff[mnCoeffIndex]; float *nFin = &gnSmooth[mnCoeffIndex]; for (int i = 0; i < NUM_ITER; i++) { // CALL SMOOTHING ROUTINE SmoothingFunction(nStart, nFin, mnSmoothCoeff); } mnCoeffIndex += NUM_ITER; // increment coeff index if (mnCoeffIndex >= NUM_COEFF) mnCoeffIndex = 0; // reset coeff index if finished with buffer }
"x" represents an element of Coeff() and "y" represents an element of Smooth(). Hope that helps! -
Thanks, Ravi. To answer your questions, this function which I'll call SmoothingFunction, is called by another function by name, SmoothCoeffs(). I have two arrays, Coeff[] and Smooth[], each with 5 elements. Values in the array Coeff[] are "smoothed" and then stored in Smooth[]. Here is the SmoothCoeffs() function: NUM_ITER is 5.
inline void cDSP::SmoothCoeffs() { float *nStart = &gnCoeff[mnCoeffIndex]; float *nFin = &gnSmooth[mnCoeffIndex]; for (int i = 0; i < NUM_ITER; i++) { // CALL SMOOTHING ROUTINE SmoothingFunction(nStart, nFin, mnSmoothCoeff); } mnCoeffIndex += NUM_ITER; // increment coeff index if (mnCoeffIndex >= NUM_COEFF) mnCoeffIndex = 0; // reset coeff index if finished with buffer }
"x" represents an element of Coeff() and "y" represents an element of Smooth(). Hope that helps!OK, so that implies x and y are simply floating point values and not functions. But then I'm confused by your original definition, viz:
y(n) = (1-a)*x(n-1) + a*y(n-1)
To me, that says
y
is a function ofn
, whose definition includes the valuey(n-1)
. This is pretty easy to code recursively, but then I need to know the exit condition (eg:y(0)
is arbitrarily defined as some value). Assumingy(0)
is zero, I think the function you want is:#define EPSILON 0.0000001
double y
(float n,
float a,
float x)
{
if (fabs (n) < EPSILON)
return (0.0); // exit condition
return ((1.0 - a)*x*(n - 1.0) + (a*y(n-1)));
}/ravi My new year's resolution: 2048 x 1536 Home | Articles | Freeware | Music ravib@ravib.com
-
OK, so that implies x and y are simply floating point values and not functions. But then I'm confused by your original definition, viz:
y(n) = (1-a)*x(n-1) + a*y(n-1)
To me, that says
y
is a function ofn
, whose definition includes the valuey(n-1)
. This is pretty easy to code recursively, but then I need to know the exit condition (eg:y(0)
is arbitrarily defined as some value). Assumingy(0)
is zero, I think the function you want is:#define EPSILON 0.0000001
double y
(float n,
float a,
float x)
{
if (fabs (n) < EPSILON)
return (0.0); // exit condition
return ((1.0 - a)*x*(n - 1.0) + (a*y(n-1)));
}/ravi My new year's resolution: 2048 x 1536 Home | Articles | Freeware | Music ravib@ravib.com
Hi Ravi, I should've probably defined it as follows:
y[n] = (1-a)*x[n-1] + a*y[n-1]
y[n] and x[n] are the elements of two arrays, Smooth[] and Coeff[] respectively.
-
Hi Ravi, I should've probably defined it as follows:
y[n] = (1-a)*x[n-1] + a*y[n-1]
y[n] and x[n] are the elements of two arrays, Smooth[] and Coeff[] respectively.
OK, it looks like you've answered your own question, then? :) /ravi My new year's resolution: 2048 x 1536 Home | Articles | Freeware | Music ravib@ravib.com