Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Help writing a function

Help writing a function

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NietzscheDisciple
    wrote on last edited by
    #1

    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?

    R 1 Reply Last reply
    0
    • N NietzscheDisciple

      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?

      R Offline
      R Offline
      Ravi Bhavnani
      wrote on last edited by
      #2

      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 of y(0)? /ravi My new year's resolution: 2048 x 1536 Home | Articles | Freeware | Music ravib@ravib.com

      N 1 Reply Last reply
      0
      • R Ravi Bhavnani

        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 of y(0)? /ravi My new year's resolution: 2048 x 1536 Home | Articles | Freeware | Music ravib@ravib.com

        N Offline
        N Offline
        NietzscheDisciple
        wrote on last edited by
        #3

        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!

        R 1 Reply Last reply
        0
        • N NietzscheDisciple

          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!

          R Offline
          R Offline
          Ravi Bhavnani
          wrote on last edited by
          #4

          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 of n, whose definition includes the value y(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). Assuming y(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

          N 1 Reply Last reply
          0
          • R Ravi Bhavnani

            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 of n, whose definition includes the value y(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). Assuming y(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

            N Offline
            N Offline
            NietzscheDisciple
            wrote on last edited by
            #5

            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.

            R 1 Reply Last reply
            0
            • N NietzscheDisciple

              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.

              R Offline
              R Offline
              Ravi Bhavnani
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups