Can someone help me fix the loudness issue with my pink noise algorithm?

In summary: However, you should tell us the problem you are encountering and the results you are getting.In summary, the conversation discusses the creation of a pink noise algorithm in C# and the difficulties the coder is facing in getting it to produce the desired sound. They mention using a random number generator and calculating octaves and amplitude, but are unsure of their code's logic and effectiveness. They also mention having more success with a white noise generator.
  • #1
btb4198
572
10
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:
        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
 
Technology news on Phys.org
  • #2
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.
 
  • #3
This is not homework:

sampleRate is 44100 Hz
Amplitude = 1 db
oh it is in C#
 
Last edited:
  • #4
pinkNoiseGraph.png


is that right?

I feel like I am going that with my code but it is not coming out right
 
  • #5
ok i think I know why my code is not working:
Code:
 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:
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?
 
Last edited by a moderator:
  • #6
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:
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; 
    }
    }
}
 
  • #7
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.
 
  • #8
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:
 Frequency_End = rnd1.next(4000)
 
Last edited:
  • #9
I did that, and i got a sound for like a sec and then it stops
 
  • #10
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.
 
  • #11
would the Voss algorithm work ?
if so can someone explain to me how it works ?
 
  • #13
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:
  • #14
ok I am trying to understand this code:

Code:
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
 
  • #15
ok that does not sound like pink noise is this right ?
:

Code:
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 into 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:
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?
 

1. What is a pink noise algorithm?

A pink noise algorithm is a type of algorithm used in digital signal processing to generate a random signal with a power spectrum that follows a 1/f^2 frequency distribution. This means that it contains equal energy per octave and has a consistent decrease in power as the frequency increases.

2. How is pink noise different from white noise?

Pink noise and white noise both contain random signals, but they have different power distributions. White noise has equal energy across all frequencies, while pink noise has a power spectrum that decreases as the frequency increases. This makes pink noise sound less harsh and more pleasant to the human ear.

3. What is the purpose of using a pink noise algorithm?

Pink noise algorithms are often used in audio processing and testing to simulate natural and realistic sounds. They are also used in various scientific and engineering applications, such as in studying the behavior of complex systems and in designing filters and equalizers.

4. How does a pink noise algorithm work?

A pink noise algorithm typically works by generating a random sequence of numbers and then applying a filter that adjusts the amplitude of each frequency component to match the 1/f^2 distribution. This can be done using various methods, such as the Voss-McCartney algorithm or the Gardner algorithm.

5. Can pink noise be used for other applications besides audio?

Yes, pink noise can be used for a variety of applications besides audio. It has been used in studies of brain activity, as it has been found to have a calming effect and promote relaxation. It has also been used in environmental noise studies, as it mimics the sound of natural environments and can be used to mask unwanted noise.

Similar threads

  • Programming and Computer Science
Replies
21
Views
1K
  • Programming and Computer Science
Replies
16
Views
3K
  • Electrical Engineering
Replies
1
Views
2K
  • Electrical Engineering
Replies
15
Views
3K
  • Electrical Engineering
Replies
4
Views
909
  • Programming and Computer Science
Replies
4
Views
986
  • Programming and Computer Science
Replies
1
Views
4K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
5
Views
5K
  • Programming and Computer Science
Replies
2
Views
3K
Back
Top