CS411X - Lecture 5

homeContentsIndexPrevNext

Lecture Topics:


 
Bit Reversal Algorithm :

 int bitr(int j) {
     int ans = 0;
     for (int i = 0; i< nu; i++) {
        ans = (ans <<1) + (j&1);
        j = j>>1;     
     }     
     return ans;
 }
  public void swap(int i) {
       double tempr;
       int j = bitr(i);
       String js = Integer.toBinaryString(j);
       String is = Integer.toBinaryString(i);
 
    //System.out.println("swap "+is+","+js);
   // System.out.println(Integer.toBinaryString(i)+"bitr="+Integer.toBinaryString(bitr(i)));
    tempr = r_data[j];
    r_data[j] = r_data[i];
    r_data[i] = tempr;
    tempr = i_data[j];
    i_data[j] = i_data[i];
    i_data[i] = tempr;
  }

Alternate representation of Swap :

void swap(i,j){
i = i^j;
j = i^j;
i = i^j;
}

Saw Waves :

for current playback fc =8khz N=#of samples = 8000
then T = N/fs = 1sec

So if you want 440hz ., ie 440 distinct cycles in the 8000 sample table .
Since the wave tables is only N samples long, you must exactly
N/f = 8000/440 = 18.2 intervals
for every sample, find a number,

    public void sawWave() {
        // The number of cycles per second is
        double frequency = 440.0;
        //
        // Three partial summations of sine waves to
        // make a sawtooth wave approximation
        // These three forms show the harmonic
        // content of a sawtooth wave.
        //
        doubleData = new double[8000];
        
 
        // f = 1/(waveLength/8000);
        // waveLength/ 8000 = 1/f
        // waveLength = 8000/f
        // waveLength = (int)( 1+ 2/step )
        // (waveLength - 1)* step = 2
        // step = 2/(waveLength) 
        // step = 2/(8000/f) = 2*f/8000 = f/4000;
        // and it sounds flat!
        // I wonder if it is being resampled at the 8125 rate.
        double sample_rate = 8125.;
        double step = frequency/(sample_rate/2);
        int waveLength = (int)( 2/step );
        int i = 0;
        int number_of_waves = 1 + doubleData.length / waveLength;
 
        do 
        done: for (double n = -1; n < 1; n = n + step) {
                doubleData[i] = n;
                i++;
                if (i >= doubleData.length) { break done;}
            }
        while (i < doubleData.length);
        
        ulawData=Audio.encodeUlaw(doubleData);
        audioDataStream = new AudioDataStream(new AudioData(ulawData));
        play();
    }
    

The step varies inversely with N and is proportional to 2f and in general
step = 2f/N

ulawDate = Audio.encodeulaw(doubleDate);
audioDataStream = new AudioDataStream( new AudioData( ulawData ) );
play( );

Sine Waves :
Make use of the static method called

Byte ulaw.to-ulaw( double d )

ulaw data is a Byte array.
Convert on the fly. No double data stored

    public void sineWave() {
 
        int length = 16;
        ulawData = new byte[length];
        double t = 0;
        // The number of cycles per second is
        double frequency = 440.0;
        double dt = delta_t();
 
        // t will range from 0.. delta_t * length
        // Assume that there are 8k samples/second
        // Then the waveform will clock out
        // it will also clock out 8000/length times per second
        // so, if you want the wave form to clock out at
        // 440 hz, then 8000/length = 440, and length = 8000/440
        // = 18 bytes.. If length = 8000, then you need
        // 440 cycles from 0..2*pi
        for (int i = 0; i < length; i++) {
            ulawData[i] = Ulaw.to_ulaw(sin(pi_2 *t));
            t = t + dt;
        }
        audioDataStream = new AudioDataStream(new AudioData(ulawData));
        play();
    }

so far we have learned

Triangle Wave :

    public void triangleWave() {
            // The number of cycles per second is
        double frequency = 880.0;
        //
        doubleData = new double[8000];
        // f = 1/(waveLength/8000);
 
        // waveLength/ 8000 = 1/f
 
        // waveLength = 8000/f
        // waveLength = (int)( 1+ 2/step )
        // (waveLength - 1)* step = 2
        // step = 2/(waveLength) 
        // step = 2/(8000/f) = 2*f/8000 = f/4000;
 
        double sample_rate = 8125.;
        double step = frequency/(sample_rate/2);
        int waveLength = (int)( 2/step );
        int i = 0;
        int number_of_waves = 1 + doubleData.length / waveLength;
 
 
    do
      {
 
        done: for (double n = -1; n < 1; n = n + step) {
                doubleData[i] = n;
                i++;
                if (i >= doubleData.length) 
                    { break done;}
            }
 
         // Do second half of triangle only if there is room.
         // Simply use same type of loop to generate triangle
         // wave as used for saw tooth wave.
           if (i < doubleData.length) {
           done2: for (double n = 1; n > -1; n = n - step) {
                      doubleData[i] = n;
                      i++;
                      if (i >= doubleData.length) 
                      { break done2;}
                  }
         }
      }
     
    while (i < doubleData.length);      
        ulawData=Audio.encodeUlaw(doubleData);
        audioDataStream = new AudioDataStream(new AudioData(ulawData));
        play();
    }

NEW TOPICS :
AUDIO PROCESSING :

Sampling :
r( t ) = continuously varying voltage which is real valued function of time.

via, the principle of superposition


-sin( x ) = sin( x )

Sine is an odd function

Cosine is an even function

f( -x ) = f( x ) implies f is an even function

Sample Quiz

1. Sine & Cosine are


 
homeContentsIndexPrevNext



UBLOGOLast Update: 04/09/97
Copyright © 1997- Douglas Lyon
Lyon@cse.bridgeport.edu