Tuesday, January 17, 2012

FFT based simple Spectrum Analyzer with Source Code

i am writing this blogpost to pay homage to my alma mater... i was not at my level best to understand all aspects of Electonics & Telecommunication, the Microprocessor, the Digital Communication, the Digital Signal processing as taught by my professors... however, after so many years while playing around with different software, i understand how valuable was their contribution to make me an engineer. i remember, how important was the lesson of digital communication, and digital signal processing taught by Dr. T.K.Sen... i remember how important was the lesson of Microprocessor taught by A.R.... i remember how important was the lectures of Maths during our engineering course... i remember how important were the concepts of Fourier Transformation, FFT, DFT, Laplace Transformation, Matrix algebra and Complex Algebra...Hats off to all those professors who in spite of all odds helped a small town boy see the outer world...

Here goes the apk of my FFT based simple Spectrum Analyzer for an Android device with a sampling frequency of 8 KHz and audio encoding as 16 bit PCM...

i was so excited when i finished the coding part (of course with the help of Google...) and saw its working on a real device...

here are the two screenshots i have taken from an Android device...


and here goes the source code which may help someone interested in Android...

20 comments:

Hazel Ann Manlapaz said...

hi! thanks for sharing your code with us who also need and interested with this FFT based project. Is it okay if I ask you a question? Since I am very new in Android and doesn't have much knowledge in programming, I would like to ask you If is it possible to also extract the voice from which I utter through your project? Because I have seen and tried your code and it's a perfect step toward the project which I am trying to make. I plan to get the pitch and tone from voice then implement a feature extraction algorithm to match to the corresponding features I want to attain. Is it possible? your help will truly be appreciated! :)

Hazel Ann Manlapaz said...

and oh I forgot also to ask where did the audio goes? here in your project? what happened to the voice which I utter? thanks again and sorry for asking so much :)

Somenath Mukhopadhyay said...

see the function AudioRecord. This android function actually converts your Audio data in some discrete form using Pulse Code Modulation (remember Nyquist theory of Sampling) and stores in a buffer the size of which we calculate using the AudioRecord.getMinBuffersize function. These discrete values are then passed through a library function called RealDoubleFFT.ft to get the fourier co-efficients. These co-efficients are then plotted in the graph. So, in short to answer your question your voice goes to a buffer of discrete values.

Rina Rivera said...

Sorry to bother, can I ask what's the use of different classes you have made under the ca.uol.aig.fftpack package especially the
RealDoubleFFT_Mixed.java class. What are those many letters was for? Great work by the way. thanks for sharing!

Mike said...

Hi! Could I ask why it's necessary to divide each element of buffer array by 32768.0? Where did this nuber came from?
Line 85 of SoundRecordAndAnalysisActivity.java

Mike said...

... hmm I believe I've added 3 coments instead of 1 by mistake. Sorry :(

Mike said...

Sorry for bothering you again. Of course 32768 came from 16 bit int maximum size... Wake up brain! Ehh...

Somenath Mukhopadhyay said...

@Mike, its a scaling factor. as we have taken 16 bit PCM, if we divide each element by 2 to the power 15, the values that will be fed to the FT function will vary from 0 to 1...

dev said...
This comment has been removed by the author.
Hwan Hee Gil said...

I used both this and "Pro android Media" but I think this works better.
But when I touch a button for stop and touch one more for restart, the spectrum usually goes down - it lives less than 5 seconds and doesn't work -.
Could you give me an answer about the matter?

Somenath Mukhopadhyay said...

@Hwan it is working in the same manner in my device as well. once you start the app and rotate you will have to press the start-stop-start button. then it works. i will look into the matter.

Hwan Hee Gil said...

@Somenath Yes, I just press button one more time and it changed start-stop-start. However it still doesn't work. I wonder if you are cell is different with me. My cell is Galuxy III and the system version is 4.1.2(Jelly bean) I can show you the screenshot and I appreciate to you if you show it.

Somenath Mukhopadhyay said...

@Hwan, my device is Samsung Galaxy Y having Android 2.3.6. However, i would like to say you that i tested this app earlier on a different Galaxy Y phone which i think had Android 2.1 or 2.2 and it worked perfectly there. I am not sure what is happening. anyway i will look into the matter.

Hwan Hee Gil said...

I got it what you mean but maybe I should read the project more time. And if you okay, please visit my blog (http://myconfession.tistory.com/139) and see the screenshot. thanks~

Hữu Tài Lê said...

Hi, Somenath Mukhopadhyay.
Thank for your sharing.
It's so good.
Is it ok if I ask you a question?
Instead of showing the frequency on the "imageView1"?
Is there any way to save as file?
And as you know, is there any way to company the frequency based on Spectrum Analyzer?
I will appreciate much if you can help me.

Thank you,
Thomas

Somenath Mukhopadhyay said...

@Hữu Tài Lê see this link

http://stackoverflow.com/questions/9078715/how-to-save-a-bitmap-image-with-imageview-onclick

Rajith Raj said...

Thank you so much..This code helping me lot..Can you please present the detailed tutorials...

Omer Ahmed said...

You was a great help! Thanks :)

keith ngai said...

Dear Sir,
It is great to see your code. I'm an Android beginner and I would like to make an App to detect audio frequency up to 20Khz. Your code is really helpful. I find the int frequency 8000 is limited to audio input of 4Khz. In case of detect up to 20Khz, I change the int frequency from 8000 to 16000 and works fine for audio up to 8Khz. But when set frequency to 40000 ( bcoz I want to detect max 20KHz ), the screen cannot show the graph. Could you please advise how I can modify the code to achieve such wider range frequency ? thank you very much in-advance.

Somenath Mukhopadhyay said...

@Keith, i think at present the maximum sampling frequency supported by Android is 44.1 khz. hence according to the nyquist theorem the maximum audio frequency it can support is approximately 22 khz. however, it all depends on the actual hardware device. i am not sure which device you are using. but please do a bit research on your device capability vis-a-vis audio frequency.

 
Bookmark and Share