Register to reply

[C#] pink noise algorithm

by btb4198
Tags: algorithm, noise, pink
Share this thread:
btb4198
#1
Jun11-14, 06:58 PM
P: 230
Can someone go over the pink noise algorithm for ?
I am not really understand how to make pink noise
this is what I did :

        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
Phys.Org News Partner Science news on Phys.org
New model helps explain how provisions promote or reduce wildlife disease
Stress can make hard-working mongooses less likely to help in the future
Grammatical habits in written English reveal linguistic features of non-native speakers' languages
jedishrfu
#2
Jun11-14, 07:08 PM
P: 2,766
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.
btb4198
#3
Jun11-14, 09:20 PM
P: 230
This is not homework:

sampleRate is 44100 Hz
Amplitude = 1 db
oh it is in C#

btb4198
#4
Jun12-14, 10:11 AM
P: 230
[C#] pink noise algorithm

Click image for larger version

Name:	pinkNoiseGraph.png
Views:	8
Size:	6.4 KB
ID:	70552

is that right?

I feel like I am going that with my code but it is not coming out right
btb4198
#5
Jun12-14, 10:57 AM
P: 230
ok i think I know why my code is not working:
 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...dio/equal.html
and this one:
http://en.wikipedia.org/wiki/Octave_(electronics)

so i changed it to this :
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?
btb4198
#6
Jun12-14, 11:04 AM
P: 230
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:
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; 
    }
    }
}
jedishrfu
#7
Jun12-14, 12:05 PM
P: 2,766
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.
jedishrfu
#8
Jun12-14, 12:09 PM
P: 2,766
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:

 Frequency_End = rnd1.next(4000)
btb4198
#9
Jun12-14, 12:35 PM
P: 230
I did that, and i got a sound for like a sec and then it stops
jedishrfu
#10
Jun12-14, 02:42 PM
P: 2,766
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.
btb4198
#11
Jun12-14, 04:00 PM
P: 230
would the Voss algorithm work ?
if so can someone explain to me how it works ?
jedishrfu
#12
Jun12-14, 06:33 PM
P: 2,766
http://www.firstpr.com.au/dsp/pink-noise/
FactChecker
#13
Jun13-14, 08:47 PM
P: 275
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.
btb4198
#14
Jun14-14, 12:51 AM
P: 230
ok I am trying to understand this 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
btb4198
#15
Jun14-14, 01:51 AM
P: 230
ok that does not sound like pink noise is this right ?
:


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[i] = rnd1.Next(255) % (range / 5);
sum += white_values[i];
btb4198
#16
Jun14-14, 01:55 AM
P: 230
it should sound like this right?

http://mynoise.net/NoiseMachines/oce...eGenerator.php
btb4198
#17
Jun14-14, 02:13 AM
P: 230
ok,

I change the code some
i have this now:

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?


Register to reply

Related Discussions
Signal to noise ratio for thermal and shot noise Introductory Physics Homework 4
Pink noise in mathematica Math & Science Software 0
Help With Pseudo Coded Algorithm for The Diamond-Square Algorithm Engineering, Comp Sci, & Technology Homework 0
Noise handling algorithm in 8 wire touch screen Engineering, Comp Sci, & Technology Homework 9
Definition of dynamic noise and observational noise in finance Set Theory, Logic, Probability, Statistics 1