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,

http://code.compartmental.net/minim/javadoc/ddf/minim/analysis/FFT.html
http://marf.sourceforge.net/api/marf/math/Algorithms.Hamming.html

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.

minim package:
based on ForwardFFT.pde example
jingle = minim.loadFile("jingle.mp3", 2048);
in this case bufferSize = 2048
and this size is also a size of Hamming window
look at sources

Attachments
/**
 * This sketch demonstrates how to use an FFT to analyze an 
 * AudioBuffer and draw the resulting spectrum. <br />
 * It also allows you to turn windowing on and off, 
 * but you will see there is not much difference in the spectrum.<br />
 * Press 'w' to turn on windowing, press 'e' to turn it off.
 */
/*
import ddf.minim.analysis.*;
import ddf.minim.*;

Minim minim;
AudioPlayer jingle;
FFT fft;
String windowName;

void setup()
{
size(512, 200, P3D);
textMode(SCREEN);

minim = new Minim(this);

jingle = minim.loadFile("jingle.mp3", 2048);
jingle.loop();

// create an FFT object that has a time-domain buffer 
// the same size as jingle's sample buffer
// note that this needs to be a power of two 
// and that it means the size of the spectrum
// will be 512. see the online tutorial for more info.
fft = new FFT(jingle.bufferSize(), jingle.sampleRate());

textFont(createFont("Arial", 16));

windowName = "None";
}

void draw()
{
background(0);
stroke(255);
// perform a forward FFT on the samples in jingle's left buffer
// note that if jingle were a MONO file, 
// this would be the same as using jingle.right or jingle.left
fft.forward(jingle.mix);
for(int i = 0; i < fft.specSize(); i++)
{
// draw the line for frequency band i, scaling it by 4 so we can see it a bit better
line(i, height, i, height - fft.getBand(i)*4);
}
fill(255);
// keep us informed about the window being used
text("The window being used is: " + windowName, 5, 20);
}

void keyReleased()
{
if ( key == 'w' ) 
{
// a Hamming window can be used to shape the sample buffer that is passed to the FFT
// this can reduce the amount of noise in the spectrum
fft.window(FFT.HAMMING);
windowName = "Hamming";
}

if ( key == 'e' ) 
{
fft.window(FFT.NONE);
windowName = "None";
}
}

void stop()
{
// always close Minim audio classes when you finish with them
jingle.close();
minim.stop();

super.stop();
}





 */
package tes2;

/*
 * Test1.java
 *
 * Created on 2010-12-27, 09:53:32
 */
/**
 *
 * @author j3c
 */
import java.awt.*;
import javax.swing.*;
import java.awt.Font;
import java.awt.event.*;
import processing.core.PApplet;
import ddf.minim.analysis.*;
import ddf.minim.*;

public class ForwardFFT extends JPanel implements Runnable {//PApplet {//

    Minim minim;
    AudioPlayer jingle;
    FFT fft;
    String windowName;
    Thread t;

    public ForwardFFT() {
        setup();
    }

    void setup() {
        t = new Thread(this);
        t.start();
        minim = new Minim(new PApplet());// the PApplet that will be used for loading files
        int bufferSize = 128; 
        jingle = minim.loadFile("jingle.mp3", bufferSize); 
        ////minim.createSample(samples, format, bufferSize) 
        //Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: FFT: timeSize must be a power of two.
        jingle.loop();

// create an FFT object that has a time-domain buffer 
// the same size as jingle's sample buffer
// note that this needs to be a power of two 
// and that it means the size of the spectrum
// will be 512. see the online tutorial for more info.
        fft = new FFT(jingle.bufferSize(), jingle.sampleRate());
        System.out.println("jingle.bufferSize()=" + jingle.bufferSize());
        System.out.println("jingle.sampleRate()=" + jingle.sampleRate());
        System.out.println("jingle.length()=" + jingle.length());
        System.out.println("jingle.mix.size()=" + jingle.mix.size());

        System.out.println("fft.timeSize()=" + fft.timeSize());
        System.out.println("fft.specSize()=" + fft.specSize());

        windowName = "None";

    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        // perform a forward FFT on the samples in jingle's left buffer
        // note that if jingle were a MONO file, 
        // this would be the same as using jingle.right or jingle.left
        fft.forward(jingle.mix);
        Font font = new Font("Serif", Font.PLAIN, 32);
        g2.setFont(font);
        g2.drawString(("jingle.position()=" + jingle.position()), 260, 80);
        for (int i = 0; i < fft.specSize(); i++) {
            // draw the line for frequency band i, scaling it by 4 so we can see it a bit better
            g2.drawLine(i * 4, this.getHeight(), i * 4, (int) (this.getHeight() - fft.getBand(i) * 32));

        }
// keep us informed about the window being used
        font = new Font("Arial", Font.PLAIN, 16);
        g2.setFont(font);
        g2.drawString("The window being used is: " + windowName, 5, 20);
        g2.drawString("jingle.bufferSize()=" + jingle.bufferSize(), 5, 40);
        g2.drawString("jingle.sampleRate()=" + jingle.sampleRate(), 5, 60);
        g2.drawString("jingle.length()[msec.]=" + jingle.length(), 5, 80);
        g2.drawString("jingle.position()=" + jingle.position(), 5, 100);
        g2.drawString("jingle.mix.size()=" + jingle.mix.size(), 5, 120);
        g2.drawString("fft.timeSize()=" + fft.timeSize(), 5, 140);
        g2.drawString("fft.specSize()=" + fft.specSize(), 5, 160);
        g2.dispose();
    }

    public void keyReleased(KeyEvent e) {
        char key = e.getKeyChar();
        if (key == 'w') {
// a Hamming window can be used to shape the sample buffer that is passed to the FFT
// this can reduce the amount of noise in the spectrum
            fft.window(FFT.HAMMING);
            windowName = "Hamming";
        }

        if (key == 'e') {
            fft.window(FFT.NONE);
            windowName = "None";
            repaint();
        }
    }

    public void run() {
        while (true) {
            repaint();
            try {
                Thread.sleep(20);
            } catch (InterruptedException ex) {
            }
        }
    }

    static class Ramka extends JFrame {

        private final ForwardFFT comp;

        Ramka(String s, final ForwardFFT comp) {
            super(s);
            this.comp = comp;
            requestFocusInWindow();
            addKeyListener(new KeyAdapter()   {

                @Override
                public void keyReleased(KeyEvent e) {
                    comp.keyReleased(e);
                }
            });
        }
    }

    private static void createAndShowGUI() {
        ForwardFFT newContentPane = new ForwardFFT();
        //Create and set up the window.
        JFrame frame = new Ramka("ForwardFFT", newContentPane);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.setSize(800, 400);
        frame.setLocation(250, 50);
        //frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        /* Use an appropriate Look and Feel */
        try {
            //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            //UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
            //UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
            UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel ");
        } catch (UnsupportedLookAndFeelException ex) {
        } catch (IllegalAccessException ex) {
        } catch (InstantiationException ex) {
        } catch (ClassNotFoundException ex) {
        }
        /* Turn off metal's use of bold fonts */
        UIManager.put("swing.boldMetal", Boolean.FALSE);

        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable()                                   {

            public void run() {
                createAndShowGUI();
            }
        });
    }
}

minim package:
based on ForwardFFT.pde example
jingle = minim.loadFile("jingle.mp3", 2048);
in this case bufferSize = 2048
and this size is also a size of Hamming window
look at sources

the minim package not available in Netbean IDE java..

Can advise where can i got the package?

This article has been dead for over six months. Start a new discussion instead.