[C#] pink noise algorithm

  1. Can someone go over the pink noise algorithm for ?
    I am not really understand how to make pink noise
    this is what I did :

    Code (Text):

            for (int n = 0; n < sampleCount; n++)
            {
                Frequency_Start = rnd1.Next(8);
                Frequency_End = Frequency_Start * 2;
                octaves = Math.Log(Frequency_End / Frequency_Start);
                float temp = (float)((Amplitude +( octaves * -2) ));
                buffer[n + offset] = temp;
                sample++;
                  n2++;
               
            }
            if (sample >= sampleRate) sample = 0;
            return sampleCount;
     
    but it does not sound right
     
  2. jcsd
  3. jedishrfu

    Staff: Mentor

    I can't really say whether its right or not as it appears pieces are missing like what is sampleRate and sampleCount and Amplitude.

    What language are you coding it in?

    Is this a class assignment? If so then you need to use the homework template.
     
  4. This is not homework:

    sampleRate is 44100 Hz
    Amplitude = 1 db
    oh it is in C#
     
    Last edited: Jun 11, 2014
  5. pinkNoiseGraph.png

    is that right?

    I feel like I am going that with my code but it is not coming out right
     
  6. ok i think I know why my code is not working:
    Code (Text):

     Frequency_Start = rnd1.Next(255);
                Frequency_End = Frequency_Start * 2;
                octaves = Math.Log(Frequency_End / Frequency_Start);
    this will alway return the same number...

    ok i have been reading this site:
    http://hyperphysics.phy-astr.gsu.edu/hbase/audio/equal.html
    and this one:
    http://en.wikipedia.org/wiki/Octave_(electronics)

    so i changed it to this :
    Code (Text):

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using NAudio.Wave;
    using NAudio;
    //using System.Numerics;
    namespace Isochronic_tones_generator
    {
        class PinkNoise : WaveProvider32
        {
            public float Frequency_Start { get; set; }
            public double Amplitude { get; set; }
            public float Frequency_End { get; set; }
            double octaves;
            int sample;
              Random rnd1;
        public PinkNoise()
        {
            Frequency_Start = 20;
            Frequency_End = 4000;
            Amplitude = 1;
            rnd1 = new System.Random();  
           
        }


        public override int Read(float[] buffer, int offset, int sampleCount)
        {
            for (int n = 0; n < sampleCount; n++)
            {

                Frequency_End = rnd1.Next(255);
                octaves = Math.Log(Frequency_End / Frequency_Start);
                float temp = (float)((Amplitude +( octaves * (-2)) ));
                buffer[n + offset] = temp;
                sample++;      
            }
            return sampleCount;
        }


        }
    }

     
    This is my code...
    it does not sound right...

    what is wrong with my logic?
     
  7. I also tried making Amplitude = to 20. but it is still not working..
    so you know a tick come out and then nothing ... there is not song at all...

    but white noise it working but for that one all i did was:
    Code (Text):

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using NAudio.Wave;
    using NAudio;
    namespace Isochronic_tones_generator
    {
        class WhiteNoise : WaveProvider32
        {
            int sample;

        public WhiteNoise()
        {
            Frequency = 1000;
            Amplitude = 0.5f; // let's not hurt our ears    
        }
     
        public float Frequency { get; set; }
        public float Amplitude { get; set; }
            static int n2;

        public override int Read(float[] buffer, int offset, int sampleCount)
        {
            int sampleRate = WaveFormat.SampleRate;
            Random rnd1 = new System.Random();  
       
            for (int n = 0; n < sampleCount; n++)
            {
                buffer[n + offset] = rnd1.Next(8);
                sample++;
                if (sample >= sampleRate) sample = 0;

            }
            return sampleCount;
        }
        }
    }

     
     
  8. jedishrfu

    Staff: Mentor

    Here's a pink noise generator written in java with some explanatory notes:

    http://sampo.kapsi.fi/PinkNoise/

    With respect to your code, perhaps you'll need to use a debugger to step through each line and make sure the calculations match what you expect. If you have to run many loop iterations then perhaps print statements or logging statements (log4j or log4c depends on what logger is supported for your language).

    You could also print the final result as a csv file and then import it into a plotting program or Excel to see how it plots. I don't see anything off the bat that I'd say is a problem and I don't have the programming environment that you are using so in your case you need to debug it and see where it goes wrong.

    Lastly, maybe it is a hardware issue where you've generated the data correctly but the hardware just can't play it. If so then there may be comments about the hardware somewhere on the web or vendors site.
     
  9. jedishrfu

    Staff: Mentor

    Look at the output of the Frequency_End = rnd1.next(255) you have initialized Frequency_End = 4000 and yet it looks like your random number generator will return something from 0 to 255? and the initialized value is never used.

    Perhaps your code should say:

    Code (Text):

     Frequency_End = rnd1.next(4000)
     
     
    Last edited: Jun 12, 2014
  10. I did that, and i got a sound for like a sec and then it stops
     
  11. jedishrfu

    Staff: Mentor

    perhaps you can add prints to see what the sampleCount is inside the Read() method as I don't see how the program calls the Read() method.
     
  12. would the Voss algorithm work ?
    if so can someone explain to me how it works ?
     
  13. jedishrfu

    Staff: Mentor

  14. FactChecker

    FactChecker 739
    Gold Member

    The only way I know to computer-generate pseudo random noise of a specific spectrum is to send a uniform random series of numbers through the correct Tustin transformation. The Tustin transformation will make each output depend on all the previous outputs so that the desired balance of frequencies is obtained. It will accentuate the desired frequency ranges and attenuate the undesired ones. As far as I can tell, that doesn't look like what your code is doing.
     
    Last edited: Jun 13, 2014
  15. ok I am trying to understand this code:

    Code (Text):


    class PinkNumber
    {
    private:
      int max_key;
      int key;
      unsigned int white_values[5]; // is this 16 bits
      unsigned int range;
    public:
      PinkNumber(unsigned int range = 128) // what is this ?
        {
          max_key = 0x1f; // Five bits set
          this->range = range;  // what does "this->" do?
          key = 0;
          for (int i = 0; i < 5; i++)
     white_values[i] = rand() % (range/5); // why mod by (range/5)
        }
      int GetNextValue()
        {
          int last_key = key;
          unsigned int sum;

          key++;
          if (key > max_key)
     key = 0;
          // Exclusive-Or previous value with current value. This gives
          // a list of bits that have changed.
          int diff = last_key ^ key;
          sum = 0;
          for (int i = 0; i < 5; i++)
     {
       // If bit changed get new random number for corresponding
       // white_value
       if (diff & (1 << i)) // why are they does this?  ?
         white_values[i] = rand() % (range/5);
       sum += white_values[i];
     }
          return sum;
        }
    };
    #ifdef DEBUG
    main()
    {
      PinkNumber pn;

      for (int i = 0; i < 100; i++)
        {
          cout << pn.GetNextValue() << endl;
        }
    }
    #endif // DEBUG
     



     
     
  16. ok that does not sound like pink noise is this right ?
    :

    Code (Text):


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using NAudio.Wave;
    using NAudio;
    //using System.Numerics;
    namespace Isochronic_tones_generator
    {
        class PinkNoise : WaveProvider32
        {
            int max_key;
      int key;
      float [] white_values;
      UInt16 range;
      Random  rnd1 ;
      int sample;
        public PinkNoise(UInt16 range = 128)
        {
            rnd1 = new System.Random();
            white_values = new float[5];
          max_key = 0x1f; // Five bits set
          this.range = range;
          key = 0;
          sample = 0;
          for (int i = 0; i < 5; i++)
     white_values[i] = (rnd1.Next(255) % (range/5));
        }
           
             
        public override int Read(float[] buffer, int offset, int sampleCount)
        {
            for (int n = 0; n < sampleCount; n++)
            {
                buffer[n + offset] = (float)(1.1 * GetNextValue());
                sample++;
            }
            return sample;
        }
         
         public float GetNextValue()
        {
          int last_key = key;
          float sum;

          key++;
          if (key > max_key)
     key = 0;
          // Exclusive-Or previous value with current value. This gives
          // a list of bits that have changed.
          int diff = last_key ^ key;
          sum = 0;
          for (int i = 0; i < 5; i++)
     {
       // If bit changed get new random number for corresponding
       // white_value
       if ((diff & (1 << i)) == 1)
           white_values[i] = rnd1.Next(255) % (range / 5);
       sum += white_values[i];
     }
          return sum;
        }



        }
    }

     
    I had to convert it in to c#
    i am not sure if I did this part right :
    if ((diff & (1 << i)) == 1)
    white_values = rnd1.Next(255) % (range / 5);
    sum += white_values;
     
  17. ok,

    I change the code some
    i have this now:
    Code (Text):


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using NAudio.Wave;
    using NAudio;
    //using System.Numerics;
    namespace Isochronic_tones_generator
    {
        class PinkNoise : WaveProvider32
        {
            int max_key;
      int key;
      float [] white_values;
      UInt16 range;
      Random  rnd1 ;
      int sample;
        public PinkNoise(UInt16 range = 128)
        {
            rnd1 = new System.Random();
            white_values = new float[5];
          max_key = 0x1f; // Five bits set
          this.range = range;
          key = 0;
          sample = 0;
          for (int i = 0; i < 5; i++)
     white_values[i] = (rnd1.Next(22) % (range/5));
        }
           
             
        public override int Read(float[] buffer, int offset, int sampleCount)
        {
            for (int n = 0; n < sampleCount; n++)
            {
                buffer[n + offset] = (float)( GetNextValue());
                sample++;
            }
            return sample;
        }
         

         public float GetNextValue()
        {
          int last_key = key;
          float sum;
          key++;
          if (key > max_key)
     key = 0;
          // Exclusive-Or previous value with current value. This gives
          // a list of bits that have changed.
          int diff = last_key ^ key;
          sum = 0;
          for (int i = 0; i < 5; i++)
     {
       // If bit changed get new random number for corresponding
       // white_value
       if ((diff & (1 << i)) != 0)
           white_values[i] = rnd1.Next(22) % (range / 5);
       sum += white_values[i];
     }
          return sum;
        }



        }
    }

     
    it sound more like a ocean .. which it what I think it should sound like.. but starts off low and getting louder which is ok, but it get too loud and my speakers die out.
    how should I fix that ?
    I am thinking of doing nd1.Next(9)
    what do you guys think?
     
Know someone interested in this topic? Share a link to this question via email, Google+, Twitter, or Facebook

Have something to add?

0
Draft saved Draft deleted