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#
  4. float to and from frequency

float to and from frequency

Scheduled Pinned Locked Moved C#
csharpcomtutorialquestion
15 Posts 4 Posters 2 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.
  • S Sascha Lefevre

    Are you sure that your given values (for .25, .5, .75) take into account that the range starts at 20Hz ? Because they all seem to be off by roughly 20 Hz here:

    x a b f tgt xrev

    0,00 10,00 1,00 20,0 20 0,00
    0,25 7,75 0,89 62,1 42 0,25
    0,50 5,50 0,74 118,7 89 0,50
    0,75 3,25 0,51 205,5 189 0,75
    1,00 1,00 0,00 400,0 400 1,00

    x : input
    a = 10 - x * 9
    b = log(a)
    f = 400 - b * 380
    tgt : target
    xrev = (10-10^((400-f)/380))/9

    But maybe I'm missing something.

    If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

    D Offline
    D Offline
    DaveyM69
    wrote on last edited by
    #5

    I've double checked the displayed results for those values and they are correct. All the spec says is:

    logf [20.000, 400.000, 101] Hz

    which means: log float with a range of 20-400Hz, 101 valid steps

    Dave
    Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
    BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

    S 1 Reply Last reply
    0
    • D DaveyM69

      I've double checked the displayed results for those values and they are correct. All the spec says is:

      logf [20.000, 400.000, 101] Hz

      which means: log float with a range of 20-400Hz, 101 valid steps

      Dave
      Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

      S Offline
      S Offline
      Sascha Lefevre
      wrote on last edited by
      #6

      I'll give it another thought tomorrow. Maybe someone who's more talented in math will solve it in the meantime ;)

      If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

      D 1 Reply Last reply
      0
      • D DaveyM69

        Mathematics is not my strong point! I'm coding against a protocol (OSC) that connects to an external device that takes a float between 0.0 and 1.0 to represent a frequency between 20Hz and 400Hz. The frequency scale is an inverse log curve so for example 0.5 represents approximately 89Hz. How on earth do I convert from the float to the frequency - and back again too?

        Dave
        Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

        M Offline
        M Offline
        Mycroft Holmes
        wrote on last edited by
        #7

        Frame the problem as a puzzle and drop it into the Lounge, I've seen a number of them get some detailed responses as long as they don't consider it a coding question you might get away with it :laugh:

        Never underestimate the power of human stupidity RAH

        D 1 Reply Last reply
        0
        • D DaveyM69

          Mathematics is not my strong point! I'm coding against a protocol (OSC) that connects to an external device that takes a float between 0.0 and 1.0 to represent a frequency between 20Hz and 400Hz. The frequency scale is an inverse log curve so for example 0.5 represents approximately 89Hz. How on earth do I convert from the float to the frequency - and back again too?

          Dave
          Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

          K Offline
          K Offline
          Kenneth Haugland
          wrote on last edited by
          #8

          Hmm, is this what you are asking? Generating a logarithmically spaced numbers[^]

          D 1 Reply Last reply
          0
          • K Kenneth Haugland

            Hmm, is this what you are asking? Generating a logarithmically spaced numbers[^]

            D Offline
            D Offline
            DaveyM69
            wrote on last edited by
            #9

            Brilliant find Kenneth! I have tweaked the C# method on that page to:

            float[] logValues = CreateLogValues(20, 400, 101);

            public static float[] CreateLogValues(float minimum, float maximum, int valuesCount)
            {
            double logarithmicBase = Math.E;
            double logMinimum = Math.Log(minimum);
            double logMaximum = Math.Log(maximum);
            double delta = (logMaximum - logMinimum) / (valuesCount - 1);
            double accumulatedDelta = 0;
            float[] values = new float[valuesCount];
            for (int i = 0; i < valuesCount; ++i)
            {
            values[i] = (float)Math.Pow(logarithmicBase, logMinimum + accumulatedDelta);
            accumulatedDelta += delta;
            }
            return values;
            }

            which is a good start. I should be able to sort the rest from here :) Thanks :thumbsup:

            Dave
            Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

            K 2 Replies Last reply
            0
            • S Sascha Lefevre

              I'll give it another thought tomorrow. Maybe someone who's more talented in math will solve it in the meantime ;)

              If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

              D Offline
              D Offline
              DaveyM69
              wrote on last edited by
              #10

              Thanks for your help Sascha - I've got a solution, see my response to Kenneth below :thumbsup:

              Dave
              Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
              BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

              S 1 Reply Last reply
              0
              • M Mycroft Holmes

                Frame the problem as a puzzle and drop it into the Lounge, I've seen a number of them get some detailed responses as long as they don't consider it a coding question you might get away with it :laugh:

                Never underestimate the power of human stupidity RAH

                D Offline
                D Offline
                DaveyM69
                wrote on last edited by
                #11

                Ha ha! I don't think I'd be that brave ;) Nearly there now thanks to Kenneth's find.

                Dave
                Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
                BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                1 Reply Last reply
                0
                • D DaveyM69

                  Brilliant find Kenneth! I have tweaked the C# method on that page to:

                  float[] logValues = CreateLogValues(20, 400, 101);

                  public static float[] CreateLogValues(float minimum, float maximum, int valuesCount)
                  {
                  double logarithmicBase = Math.E;
                  double logMinimum = Math.Log(minimum);
                  double logMaximum = Math.Log(maximum);
                  double delta = (logMaximum - logMinimum) / (valuesCount - 1);
                  double accumulatedDelta = 0;
                  float[] values = new float[valuesCount];
                  for (int i = 0; i < valuesCount; ++i)
                  {
                  values[i] = (float)Math.Pow(logarithmicBase, logMinimum + accumulatedDelta);
                  accumulatedDelta += delta;
                  }
                  return values;
                  }

                  which is a good start. I should be able to sort the rest from here :) Thanks :thumbsup:

                  Dave
                  Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
                  BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                  K Offline
                  K Offline
                  Kenneth Haugland
                  wrote on last edited by
                  #12

                  I have education in acoustics, were this comes up quite frequent :)

                  1 Reply Last reply
                  0
                  • D DaveyM69

                    Brilliant find Kenneth! I have tweaked the C# method on that page to:

                    float[] logValues = CreateLogValues(20, 400, 101);

                    public static float[] CreateLogValues(float minimum, float maximum, int valuesCount)
                    {
                    double logarithmicBase = Math.E;
                    double logMinimum = Math.Log(minimum);
                    double logMaximum = Math.Log(maximum);
                    double delta = (logMaximum - logMinimum) / (valuesCount - 1);
                    double accumulatedDelta = 0;
                    float[] values = new float[valuesCount];
                    for (int i = 0; i < valuesCount; ++i)
                    {
                    values[i] = (float)Math.Pow(logarithmicBase, logMinimum + accumulatedDelta);
                    accumulatedDelta += delta;
                    }
                    return values;
                    }

                    which is a good start. I should be able to sort the rest from here :) Thanks :thumbsup:

                    Dave
                    Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
                    BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                    K Offline
                    K Offline
                    Kenneth Haugland
                    wrote on last edited by
                    #13

                    Ah, you should be careful though, LogE and Log10 gives you quite different results! And log10 is the one that is used in acoustics. I would know, I have a Masters degree in it :-D

                    D 1 Reply Last reply
                    0
                    • K Kenneth Haugland

                      Ah, you should be careful though, LogE and Log10 gives you quite different results! And log10 is the one that is used in acoustics. I would know, I have a Masters degree in it :-D

                      D Offline
                      D Offline
                      DaveyM69
                      wrote on last edited by
                      #14

                      I'll keep my eye on it for other parameters that use the logf type. For the 20-400HZ one Math.E is giving the correct result. Thanks for the heads up.

                      Dave
                      Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
                      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                      1 Reply Last reply
                      0
                      • D DaveyM69

                        Thanks for your help Sascha - I've got a solution, see my response to Kenneth below :thumbsup:

                        Dave
                        Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
                        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                        S Offline
                        S Offline
                        Sascha Lefevre
                        wrote on last edited by
                        #15

                        DaveyM69 wrote:

                        Thanks for your help Sascha

                        Let's call it an attempt ;) Good to see you got a solution - I'll take a look at it myself, might come in handy some time. cheers, Sascha

                        If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                        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