plectrum

Plectrum: instrument tuner for Android
Log | Files | Refs | README | LICENSE

WindowFunction.java (4443B)


      1 /*
      2 *      _______                       _____   _____ _____  
      3 *     |__   __|                     |  __ \ / ____|  __ \ 
      4 *        | | __ _ _ __ ___  ___  ___| |  | | (___ | |__) |
      5 *        | |/ _` | '__/ __|/ _ \/ __| |  | |\___ \|  ___/ 
      6 *        | | (_| | |  \__ \ (_) \__ \ |__| |____) | |     
      7 *        |_|\__,_|_|  |___/\___/|___/_____/|_____/|_|     
      8 *                                                         
      9 * -------------------------------------------------------------
     10 *
     11 * TarsosDSP is developed by Joren Six at IPEM, University Ghent
     12 *  
     13 * -------------------------------------------------------------
     14 *
     15 *  Info: http://0110.be/tag/TarsosDSP
     16 *  Github: https://github.com/JorenSix/TarsosDSP
     17 *  Releases: http://0110.be/releases/TarsosDSP/
     18 *  
     19 *  TarsosDSP includes modified source code by various authors,
     20 *  for credits and info, see README.
     21 * 
     22 */
     23 
     24 /*
     25  *  Copyright (c) 2007 - 2008 by Damien Di Fede <ddf@compartmental.net>
     26  *
     27  *   This program is free software; you can redistribute it and/or modify
     28  *   it under the terms of the GNU Library General Public License as published
     29  *   by the Free Software Foundation; either version 2 of the License, or
     30  *   (at your option) any later version.
     31  *
     32  *   This program is distributed in the hope that it will be useful,
     33  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
     34  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     35  *   GNU Library General Public License for more details.
     36  *
     37  *   You should have received a copy of the GNU Library General Public
     38  *   License along with this program; if not, write to the Free Software
     39  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
     40  */
     41 
     42 package be.tarsos.dsp.util.fft;
     43 
     44 /**
     45  * A Window function represents a curve which is applied to a sample buffer to
     46  * reduce the introduction of spectral leakage in the Fourier transform.
     47  * 
     48  * <p>
     49  * <b>Windowing</b>
     50  * <p>
     51  * Windowing is the process of shaping the audio samples before transforming
     52  * them to the frequency domain. The Fourier Transform assumes the sample buffer
     53  * is is a repetitive signal, if a sample buffer is not truly periodic within
     54  * the measured interval sharp discontinuities may arise that can introduce
     55  * spectral leakage. Spectral leakage is the speading of signal energy across
     56  * multiple FFT bins. This "spreading" can drown out narrow band signals and
     57  * hinder detection.
     58  * <p>
     59  * A <a href="http://en.wikipedia.org/wiki/Window_function">windowing
     60  * function</a> attempts to reduce spectral leakage by attenuating the measured
     61  * sample buffer at its end points to eliminate discontinuities. If you call the
     62  * <code>window()</code> function with an appropriate WindowFunction, such as
     63  * <code>HammingWindow()</code>, the sample buffers passed to the object for
     64  * analysis will be shaped by the current window before being transformed. The
     65  * result of using a window is to reduce the leakage in the spectrum somewhat.
     66  * <p>
     67  * <code>WindowFunction</code> handles work associated with various window
     68  * functions such as the Hamming window. To create your own window function you
     69  * must extend <code>WindowFunction</code> and implement the
     70  * {@link #value(int, int) value} method which defines the shape of the window
     71  * at a given offset. <code>WindowFunction</code> will call this method to apply
     72  * the window to a sample buffer. The number passed to the method is an offset
     73  * within the length of the window curve.
     74  * 
     75  * @author Damien Di Fede
     76  * @author Corban Brook
     77  * 
     78  */
     79 public abstract class WindowFunction {
     80 
     81 	/** The float value of 2*PI. Provided as a convenience for subclasses. */
     82 	protected static final float TWO_PI = (float) (2 * Math.PI);
     83 	protected int length;
     84 
     85 	public WindowFunction() {
     86 	}
     87 
     88 	/**
     89 	 * Apply the window function to a sample buffer.
     90 	 * 
     91 	 * @param samples
     92 	 *            a sample buffer
     93 	 */
     94 	public void apply(float[] samples) {
     95 		this.length = samples.length;
     96 
     97 		for (int n = 0; n < samples.length; n++) {
     98 			samples[n] *= value(samples.length, n);
     99 		}
    100 	}
    101 
    102 	/**
    103 	 * Generates the curve of the window function.
    104 	 * 
    105 	 * @param length
    106 	 *            the length of the window
    107 	 * @return the shape of the window function
    108 	 */
    109 	public float[] generateCurve(int length) {
    110 		float[] samples = new float[length];
    111 		for (int n = 0; n < length; n++) {
    112 			samples[n] = 1f * value(length, n);
    113 		}
    114 		return samples;
    115 	}
    116 
    117 	protected abstract float value(int length, int index);
    118 }