User:Wyonghao

From Audacity Wiki
Jump to: navigation, search

My userpage

Audacity user
My open source project: ARM simulator
Links:
Audacity Web
HowAudacityWorks
audacity forum
http://audacity.cvs.sourceforge.net/audacity/


Algorithm explained:

  • White noise is generated by rand() function.
    rand() will generate random number at range (0~RAND_MAX). RAND_MAX is a constant depends on the implementation of standard C++ library. Variable div = RAND_MAX/2; therefor "rand() / div" creates random values between 0~2; the code buffer[i] = amplitude * ((rand() / div) - 1.0f); shall create random number between -1~+1.
  • pink noise uses a weighted sum of six first-order IIR filters. The variable "white" is current input sample, using loop for(i=0; i<len; i++) to fill output signal:
       buf0=0.997f * buf0 + 0.029591f * white;
       buf1=0.985f * buf1 + 0.032534f * white;
       buf2=0.950f * buf2 + 0.048056f * white;
       buf3=0.850f * buf3 + 0.090579f * white;
       buf4=0.620f * buf4 + 0.108990f * white;
       buf5=0.250f * buf5 + 0.255784f * white;
       buffer[i] = amplitude * (buf0 + buf1 + buf2 + buf3 + buf4 + buf5);
The above code can be write into difference equation as following:
      y1(n)=0.997f * y1(n-1) + 0.029591f * x(n);
      y2(n)=0.985f * y2(n-1) + 0.032534f * x(n);
      y3(n)=0.950f * y3(n-1) + 0.048056f * x(n);
      y4(n)=0.850f * y4(n-1) + 0.090579f * x(n);
      y5(n)=0.620f * y5(n-1) + 0.108990f * x(n);
      y6(n)=0.250f * y6(n-1) + 0.255784f * x(n);
      y(n) = amplitude * (y1(n) + y2(n) + y3(n) + y4(n) + y5(n) + y6(n));
If user input amplitude is "1". then the final z-transform format of this filter will be:
  • Brown noise using 1st order IIR filter.
The structure is shown in comment already: y[n]=a0*x[n] + b1*y[n-1]; where b1=exp(-2*pi*fc/fs); and a0 = 1.0-b1;;

code

bool EffectNoise::MakeNoise(float *buffer, sampleCount len, float fs, float amplitude)
{
   float white, buf0, buf1, buf2, buf3, buf4, buf5;
   float a0, b1, fc, y;
   sampleCount i;
   float div = ((float)RAND_MAX) / 2.0f;

   switch (noiseType) {
   default:
   case 0: // white
       for(i=0; i<len; i++)
          buffer[i] = amplitude * ((rand() / div) - 1.0f);
       break;

   case 1: // pink
       white=buf0=buf1=buf2=buf3=buf4=buf5=0;

       // 0.55f is an experimental normalization factor: thanks to Martyn
       amplitude *= 0.55f;
       for(i=0; i<len; i++) {
        white=(rand() / div) - 1.0f;
        buf0=0.997f * buf0 + 0.029591f * white;
        buf1=0.985f * buf1 + 0.032534f * white;
        buf2=0.950f * buf2 + 0.048056f * white;
        buf3=0.850f * buf3 + 0.090579f * white;
        buf4=0.620f * buf4 + 0.108990f * white;
        buf5=0.250f * buf5 + 0.255784f * white;
        buffer[i] = amplitude * (buf0 + buf1 + buf2 + buf3 + buf4 + buf5);
       };
       break;

   case 2: // brown
       // fc=100 Hz,
       // y[n]=a0*x[n] + b1*y[n-1];
       white=a0=b1=fc=y=0;
       fc=100; //fs=44100;
       b1=exp(-2*M_PI*fc/fs);
       a0=1.0f-b1;

       // 6.2f is an experimental normalization factor: thanks to Martyn
       amplitude *= 6.2f;
       for(i=0; i<len; i++){
         white=(rand() / div) - 1.0f;
         y = (a0 * white + b1 * y);
         buffer[i] = amplitude * y;
       };
       break;
   }
   return true;
}