Sei sulla pagina 1di 2

AnalyzethefrequencyandstrengthofsoundinAndroid

1Purpose Inordertogetthedistancebetweentwophones,onemethodistoletthephone 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 frequencyandstrengthcanbeanalyzed. 2Generatesoundinspecificfrequency AudioTrack class allows to stream PCM audio buffers to the audio hardware for playback.Sohereweuseittogeneratethehighfrequencysound.Accordingtothe theory of Nyquist Frequency (Fmax = SampleRate / 2), we use 44100Hz as the sampleratesothatlaterwecanhaveabandfrom0to22050Hz.
// 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 }

3Getsoundfrommicrophone AudioRecord class records data from the audio input hardware. In order to get thecorrectdata,thesamplerateforrecordingshouldbethesameasthatofinput 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

4AnalyzethefrequencyandstrengthbyusingFFT 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 needFFT(FastFourierTransform)toprocessthedata. FFTisaneffectivealgorithmtoconvertsignalsfromtimeorspacedomaintothe frequencydomain.TheinputofFFTisacomplexvectorandweusethedatainthe bufferasitsrealpartand0asitsimagepart.Theoutputofthealgorithmreturnsa 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)ofafrequencyrange.Saysamplerateis44100Hzandthebuffer sizeis4096.Thenafrequencyrangeof22050Hzisequallysplitinto2048barsand eachbaris22050/204810.77Hzwidewhichisalsocalledfrequencyresolution. InordertogetthestrengthindBunit,wecanfirstcalculatethesumofthedata 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 somescreenshots.Theleftpartshowsthenormalsituationandtherightoneisgot whentheotherphonenearbygeneratesatonewithfrequencyof1000Hz.

5Unsolvedproblem

(a) (b) Figure1.Frequencyspectrumandstrengthvalue

Right now we get some obstruction to get the pitch in the specific high frequency sincemicrophoneofNexusOnefiltersoutthehighfrequencytoneandcannotpickit up. We looked up the technical specifications and the detailed frequency range is stillunknown. Reference:
[1]http://developer.android.com/index.html [2]ExamplesofAudioRecord http://code.google.com/p/moonblink/wiki/Audalyzer [3]IntroductionofFFT http://www.vlf.it/fft_beginners/fft_beginners.html [4]FFTsourcecode http://www.cs.princeton.edu/introcs/97data/FFT.java [5]ExamplesofAudioTrack http://apistudios.com/hosted/marzec/badlogic/wordpress/?p=228

Potrebbero piacerti anche