# [C#] pink noise algorithm

by btb4198
Tags: algorithm, noise, pink
 P: 272 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
 P: 3,002 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.
 P: 272 This is not homework: sampleRate is 44100 Hz Amplitude = 1 db oh it is in C#
 P: 272 [C#] pink noise algorithm is that right? I feel like I am going that with my code but it is not coming out right
 P: 272 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?
 P: 272 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; } } }
 P: 3,002 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.
 P: 3,002 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)
 P: 272 I did that, and i got a sound for like a sec and then it stops
 P: 3,002 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.
 P: 272 would the Voss algorithm work ? if so can someone explain to me how it works ?
 P: 3,002
 P: 329 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.
 P: 272 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
 P: 272 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];
 P: 272 it should sound like this right? http://mynoise.net/NoiseMachines/oce...eGenerator.php
 P: 272 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?

 Related Discussions Introductory Physics Homework 4 Math & Science Software 0 Engineering, Comp Sci, & Technology Homework 0 Engineering, Comp Sci, & Technology Homework 9 Set Theory, Logic, Probability, Statistics 1