1 2 Previous Next 20 Replies Latest reply on Jan 1, 2011 3:07 PM by sabre150

    Perform Hamming window and FFT with Java

      Hi all,

      I am trying to apply Hamming window on my lengthy sound data soundSample[1000] with window length 100. Then proceed with FFT.

      And i found that java does have the package for Hamming window and FFT. As below,


      But i hardly find an example that illustrate how to use the package in a java program. So no idea how should i use the method in package.

      Please advise and appreciate if reference is provided.

      Thank you.
        • 1. Re: Perform Hamming window and FFT with Java
          Interesting that you can't find examples ! Backing up from the the first link you give provides examples that include how to apply a Hamming Window!

          I'm not keen on the code provided in that distribution; little 'separation of concerns' is evident. As example, the FFT class includes code that handles the spectrum and the sample rate but the FFT should only be concerned with performing the DFT leaving the interpretation of the results to another more appropriate class.
          • 2. Re: Perform Hamming window and FFT with Java

            Thanks for your reply.

            The example that i meant is a complete Java program example that show the usage of Hamming Window and FFT... Not the method usage elaboration that shown in the link provided.

            Do you have any program example that using Hamming Window and FFT? I hardly find a Java code that using those function.

            Many thanks
            • 3. Re: Perform Hamming window and FFT with Java
              So you don't see any example in http://code.compartmental.net/minim/ ? OK, the examples hide some of the detail but only a little detective work is required find where the Hamming windows is specified.

              Since the application of a window function to sound samples is a very simple concept that is easy to understand if one understands the signal analysis associated with the Discrete Fourier Transform, I get the feeling that what you actually want is a tutorial on the application of the FFT to sound samples. I'm not willing at this time to create such a tutorial but I will outline the steps you need to implement.

              1) Read 2*N sound samples from the Audio stream and convert to double or float according to whether your FFT implementation works on double arrays or float arrays. I always scale the values so that they are in [-1.0,1.0]. Choose N to be of length that can be transformed by your FFT and such that the sample-rate / N is an acceptable frequency resolution. My experience with audio data is that a resolution of about 5 Hz is normally good enough for what I need but it depends on your application.

              2) Save the last N samples for use later.

              3) Compute the complex array needed for the DFT by using the 2N sample values as the real parts and zero for the imaginary parts.

              4) Apply the window function by multiplying the sample values by the windows function values. For Hamming this is 1.07672 - 0.92328 * Math.cos(index * deltaTheta) where index is in [0,2N) and deltaTheta is 2*PI/2N . You can pre-compute these values.

              5) Perform the DFT using the FFT algorithm creating 2N complex transform values from the 2N complex input values.

              6) Compute the spectral density estimates from the complex values as the modulus squared of the FFT results. Only the first N complex values need to be processed.

              7) Process the spectral density estimates in any way you choose.

              8) Copy the N saved value to the input array and read the next N samples from the audio input stream. Save this latest N values for use in the next segment.

              9) Process this new data by repeating from 3).

              Notes -

              i) by doing this you will be using 50% overlapping segments which means that you are throwing away less of your data by using a window.
              ii) your actual resolution will be about sample-rate * 1.2 / 2N and your neighbouring sample values will have a correlation of about 20%
              iii) you might want to consider removing the best straight line from the data segment prior to windowing. This helps reduce the distortion from very low frequencies below the segment sample period. i.e. frequencies below the sample-rate / 2N.
              iv) though the maximum usable frequency is the Nyquist frequency of the sample-rate / 2, unless you have samples from bat echo location, in practice it is rarely worth processing anything about about 18 KHz.

              P.S. I know you won't want to hear this but you should really spend some time learning about Signal Analysis. All the steps I have given will then become pretty obvious.
              1 person found this helpful
              • 4. Re: Perform Hamming window and FFT with Java
                Many thanks for your guidance!!

                I will try to work out the steps that you have shown previously..

                Hopefully later will have your advise again if i encountered any problem..
                • 5. Re: Perform Hamming window and FFT with Java
                  Cross posted

                  Any more?



                  Was advised to provide links to all cross posts on DaniWeb.

                  Edited by: Darryl Burke on 28 Dec, 2010 4:48 AM
                  • 6. Re: Perform Hamming window and FFT with Java

                    I tried to run the minim example, but i get stucked how should i compile and run the file for minim package...

                    I am referring to this example: http://code.compartmental.net/minim/examples/Minim/createSample/createSample.pde

                    My questions:

                    1) This example isn't a Java Class, what kind of Java file i should create for it? Usually we create a class file for an Java program then proceed with the coding...
                    But this example starts with "void setup" and i nvr come across with this kind form.

                    2) Does ddf package comes together with Netbean IDE? I tried to import this package... but i got a little comment message showing me ddf package is not exist...
                    What can i do to have ddf package in my program? Should i download its .jar (But it is not a library... i m blur with this...)

                    Please advise...

                    Thank you.
                    • 7. Re: Perform Hamming window and FFT with Java
                      Hi ,

                      I have tried to download the jar file from the Webpage that u shown me.. I just found out the attached jar file...

                      Now can import the package already...
                      And now working on how to use the fft and hamming features that are provided by Minim...

                      Hope to have your advise later on...
                      • 8. Re: Perform Hamming window and FFT with Java
                        cielle wrote:
                        I have tried to download the jar file from the Webpage that u shown me..
                        You mystify me! All I did was to go to towards the root of the web page YOU posted!

                        Now can import the package already...
                        And now working on how to use the fft and hamming features that are provided by Minim...

                        Hope to have your advise later on...
                        My advice - stop right now. You don't have the Java and maths and signal processing and software engineering and research and study skills to do this project. To do this project you needs some basic background knowledge. You need to understand basic Java. You need to understand basic Java sound. You need to understand basic complex numbers. You need to understand basic signal processing. You don't need to be an expert at any of these but you MUST MUST MUST have a basic understanding of all of them. You don't seem to have a basic understanding of any of them and don't seem to be willing to obtain any understanding by studying. You are just thrashing around and have re-enforced my view that those posting here about how to use an FFT for processing sound are just far to far outside of their comfort zone.

                        I don't know what is pushing you to do this project. It can't be a school project since it is too advanced. It can't be a university project since you are not showing any of the required background knowledge. I do know that I can't help any further.

                        • 9. Re: Perform Hamming window and FFT with Java

                          1) Belwo is the FFT algorthm that i am studying...
                              void correctAndRecombine(
                                      double realSample,double imagSample,
                                      int position,int length,double scale,
                                      double[] realOut,double[] imagOut){
                              //Calculate the complex transform values for
                              // each sample in the complex output series.
                               for(int cnt = 0; cnt < length; cnt++){
                                double angle =  (2.0*Math.PI*cnt/length)*position;
                              //Calculate output based on real input
                                realOut[cnt] +=  realSample*Math.cos(angle)/scale;
                                imagOut[cnt] += realSample*Math.sin(angle)/scale;
                                //Calculate output based on imag input
                                realOut[cnt] -=  imagSample*Math.sin(angle)/scale;
                                imagOut[cnt] +=  imagSample*Math.cos(angle)/scale;
                              }//end for loop
                            }//end correctAndRecombine
                          I noticed that the result from this algorithm sometimes has the sign difference between the MATLAB result.. The magnitude is the same...
                          Can tell me what is the mistake i have done?

                          2) May i know is it a must to mapped the sound data to the range of [-1,+1] before proceed to FFT? If yes, how can i map it... i should divide the sound data by?

                          Thank you again.
                          • 10. Re: Perform Hamming window and FFT with Java
                            诸葛 wrote:
                            1) Belwo is the FFT algorthm that i am studying...
                            That is not an FFT! It is a Discrete Fourier Transform (DFT) implementation using just about the most inaccurate and slow approach possible.
                            1 person found this helpful
                            • 11. Re: Perform Hamming window and FFT with Java
                              I noticed mostly FFT works with Radix 2.. the FFT buffer has to be in power of 2.

                              Sound data array is lengthy.. Says as 441000 (10s 44100kHz sampling).

                              If I starts by buffer as 262144 (2 power of 18) .. I will loop the buffer for only one time.
                              Then (441000-262144=178856) the remaining 178856 sampled data is not a power of 2...

                              What should i do for this circumstances? Should i add perform zero padding? fill in all the remaining buffer space with zero before perform with FFT?

                              Edited by: 诸葛 on Dec 29, 2010 4:11 AM

                              I really hope to work out this thing...
                              Highly hope to have your guidance again.

                              I am now searching hard the FFT tutorial to get myself understand it...
                              Frequency extraction are actually done with MATLAB.. but i wish to work out the same thing with JAVA.
                              Thanks again...

                              Edited by: 诸葛 on Dec 29, 2010 4:12 AM
                              • 12. Re: Perform Hamming window and FFT with Java
                                诸葛 wrote:
                                I noticed mostly FFT works with Radix 2.. the FFT buffer has to be in power of 2.
                                There are mixed radix FFT out there! For example, mine allows mixed radix 2,3 and 5. The C FFT implementation FFTW can transform any number of data points. You could always write a JNI wrapper for FFTW if you really do need to transform any number of data points but I doubt if you need to. I can't be sure because I don't have access to your project requirements.

                                Sound data array is lengthy.. Says as 441000 (10s 44100kHz sampling).

                                If I starts by buffer as 262144 (2 power of 18) .. I will loop the buffer for only one time.
                                Then (441000-262144=178856) the remaining 178856 sampled data is not a power of 2...
                                It is unusual to transform the whole of the data at one go unless the data is just one single frequency. Easy enough though with 441,000 points as long as you use an FFT that works on 'double' and not 'float' since 'float' tends not to be accurate enough for around half a million data points.

                                What should i do for this circumstances? Should i add perform zero padding? fill in all the remaining buffer space with zero before perform with FFT?
                                Since you have ignored the list of steps I posted earlier it seems you are not trying to do what I thought. Therefore I can't say what you need to do since you still have not specified what your input is (just saying it is an audio file is not enough, the characteristics of the audio are important) and what you are trying to extract from that input. You need to stop thrashing around with code, start studying and make sure you have a clear set of requirements.
                                • 13. Re: Perform Hamming window and FFT with Java

                                  I have spent sometimes to study and do the coding for FFT on audio samples.
                                  My project is to study the FFT working by performing the FFT on audio sampled data.
                                  After this is done, then only proceed to extract the pitch value in the audio data.

                                  Below is the coding that i have done by referring to the provided steps:

                                  In my main, i call this method and past the retrieved sampled sound data to this method.
                                   public static void Analyze(int[] soundSample,float sample_rate ) {
                                          int N = (int)sample_rate/5;
                                          int Number_Sample = soundSample.length;
                                          Complex[] fftBuffer = new Complex [2*N];
                                          Complex[] fftResult = new Complex [2*N];
                                          Complex [] lastN = new Complex [N];   // The array to save the last N sample
                                          int delay = 0;
                                          double delta = 2*Math.PI/(2*N);
                                          // I have no idea how can i convert my sample array to double so that it will be in the range of [-1,+1]
                                          while(delay <=soundSample.length){
                                              //Extract the 2N sample for FFT analysis and convert the data to complex number.
                                              for (int z=0; z<2*N; z++){
                                                  fftBuffer[z] = new Complex(soundSample[z+delay],0) ;                 
                                              for (int i=N-1;i>=N/2; i-- ){
                                                  lastN[N-1-i] = fftBuffer;

                                  for (int z=0; z<2*N; z++){
                                  fftBuffer[z] = fftBuffer[z].times(0.54-0.46*Math.cos(z*delta));

                                  fftResult = FFT1.fft(fftBuffer);
                                  delay = 2*N + delay;

                                  1) I was trying to perform FFT with 2N samples then keep on looping the FFT method until 2N reaches the ends of sampled data.
                                  But the FFT that i am working with is radix 2... It doesn't work with my 2N samples... Please teach me how should i work out FFT regardless the number of sample?
                                  2) The hamming window coefficient i m using is based on http://www.mathworks.com/help/toolbox/signal/hamming.html . I am working on index [0:2N] ..
                                  Is it appropriate?
                                  3) According to your No1 steps, the acceptable frequency resolution is 5Hz. May i know what is this representing? And is it application for most of the FFT application? How can i determine the frequency resolution that i should used in my project?
                                  Sorry for late reply as i was trying to work out the thing..
                                  Hereby attach to your the my coding.. and hopes to have your guidance and tutorial how to extract the pitch for recording audio file with Java.
                                  I have done the pitch extraction with MATLAB.. but Matlab as built-in FFT function...  
                                  So i m now get stucked how to perform FFT on audio sound sample regardless the N value of sound sample for FFT buffer.
                                  Many thanks for your former advise... and
                                  Looking forward for your replies again.
                                  Happy New Year 2011 :)
                                  Edited by: 诸葛 on Dec 31, 2010 9:29 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
                                  • 14. Re: Perform Hamming window and FFT with Java
                                    诸葛 wrote:

                                    Below is the coding that i have done by referring to the provided steps:

                                    My FFT class:
                                    public static Complex[] fft(Complex[] x) {
                                    // removed
                                    I have to admire your nerve - http://www.cs.princeton.edu/introcs/97data/FFT.java.html.

                                    Edited by: Darryl Burke -- copyright code removed as the OP has indirectly acknowledged that posting it was not authorized.
                                    1 person found this helpful
                                    1 2 Previous Next