Analyze the frequency and strength of sound in Android 1 Purpose In order to get the distance between two phones, one method is to let the phone generate high frequency tone and the other one detect the signal strength of the sound in the specific frequency range. In our experiment we use two android phones to generate high frequency (>15500Hz) sound and get it so that its frequency and strength can be analyzed. 2 Generate sound in specific frequency AudioTrack class allows to stream PCM audio buffers to the audio hardware for playback. So here we use it to generate the high frequency sound. According to the theory of “Nyquist Frequency” (Fmax = SampleRate / 2), we use 44100Hz as the sample rate so that later we can have a band from 0 to 22050Hz. // construct audio stream in 16bit format with sample rate of 44100Hz int minSize = AudioTrack.getMinBufferSize(sampleRate, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT); AudioTrack track = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, minSize, AudioTrack.MODE_STREAM); ... //use formula to get the wave data in specific frequency (15500Hz) void genTone(){ int angle = 0; int increment = 2 * Math.PI * freqOfTone / sampleRate; // angular increment for (int i = 0; i < sample.length; i++) { sample[i] = Math.sin(angle) * Short.MAX_VALUE; angle += increment; } track.write(sample, 0, sample.length); // write data to audio hardware track.play(); // play an AudioTrack }
3 Get sound from microphone AudioRecord class records data from the audio input hardware. In order to get the correct data, the sample rate for recording should be the same as that of input radio. // construct AudioRecord to record audio from microphone with sample rate of 44100Hz int minSize = AudioRecord.getMinBufferSize(sampleRate,AudioFormat. CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT); AudioRecord audioInput = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRate, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT); minSize); ... short[] buffer = new short[readSize]; audioInput.startRecording(); audioInput.read(buffer, index, readSize); // record data from mic into buffer
4 Analyze the frequency and strength by using FFT The data in the buffer from read() are digital signals of the sound which is sampled every 22us (≈ 1/44100Hz). In order to plot the frequency spectrum, we need FFT (Fast Fourier Transform) to process the data. FFT is an effective algorithm to convert signals from time or space domain to the frequency domain. The input of FFT is a complex vector and we use the data in the buffer as its real part and 0 as its image part. The output of the algorithm returns a complex array. Then we calculate the absolute value of the array and plot the frequency spectrum with spectrum bars, each of which contains the magnitude(energy) of a frequency range. Say sample rate is 44100Hz and the buffer size is 4096. Then a frequency range of 22050Hz is equally split into 2048 bars and each bar is 22050/2048 ≈ 10.77 Hz wide which is also called frequency resolution. In order to get the strength in dB unit, we can first calculate the sum of the data and the sum of the squared values. Then use the formula “(squaredsum‐ sum*sum/number)/number” and conversion of log10() to get the power value. In the application we use the user interface provided by moonblink and Fig 1 shows some screenshots. The left part shows the normal situation and the right one is got when the other phone nearby generates a tone with frequency of 1000Hz.
(a) (b) Figure 1. Frequency spectrum and strength value
5 Unsolved problem
Right now we get some obstruction to get the pitch in the specific high frequency since microphone of Nexus One filters out the high frequency tone and cannot pick it up. We looked up the technical specifications and the detailed frequency range is still unknown. Reference: [1] http://developer.android.com/index.html [2] Examples of AudioRecord http://code.google.com/p/moonblink/wiki/Audalyzer [3] Introduction of FFT http://www.vlf.it/fft_beginners/fft_beginners.html [4] FFT source code http://www.cs.princeton.edu/introcs/97data/FFT.java [5] Examples of AudioTrack http://apistudios.com/hosted/marzec/badlogic/wordpress/?p=228
Thank you for interesting in our services. We are a non-profit group that run this website to share documents. We need your help to maintenance this website.