View Javadoc

1   /**
2    * Copyright (c) 2011, The University of Southampton and the individual contributors.
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without modification,
6    * are permitted provided that the following conditions are met:
7    *
8    *   * 	Redistributions of source code must retain the above copyright notice,
9    * 	this list of conditions and the following disclaimer.
10   *
11   *   *	Redistributions in binary form must reproduce the above copyright notice,
12   * 	this list of conditions and the following disclaimer in the documentation
13   * 	and/or other materials provided with the distribution.
14   *
15   *   *	Neither the name of the University of Southampton nor the names of its
16   * 	contributors may be used to endorse or promote products derived from this
17   * 	software without specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  /**
31   * 
32   */
33  package org.openimaj.audio.analysis;
34  
35  import org.openimaj.audio.SampleChunk;
36  import org.openimaj.audio.processor.AudioProcessor;
37  
38  import edu.emory.mathcs.jtransforms.fft.FloatFFT_1D;
39  
40  /**
41   *	An implementation of the power cepstrum of an audio signal. The
42   *	power cepstrum of an audio signal is the squared magnitude of the Fourier 
43   *	transform of the logarithm of the squared magnitude of the Fourier transform 
44   *	of a signal. Yeah, I know.
45   *
46   *	@author David Dupplaw (dpd@ecs.soton.ac.uk)
47   *  @created 18 Jul 2012
48   *	@version $Author$, $Revision$, $Date$
49   */
50  public class PowerCepstrumTransform extends AudioProcessor
51  {
52  	/** The last generated cepstrum */
53  	private float[][] lastCepstrum = null;
54  
55  	@Override
56      public SampleChunk process( final SampleChunk sample ) throws Exception
57      {
58  		final FourierTransform fft = new FourierTransform();
59  		
60  		//
61  		// The squared magnitude of the Fourier transform of the logarithm 
62  		// of the squared magnitude of the Fourier transform of a signal...
63  		//
64  		// Working backwards...
65  		// ... the FFT of a signal...
66  		//
67  		fft.process( sample );
68  		final float[][] fftCoeffs = fft.getLastFFT();
69  		
70  		// ...the logarithm of the squared magnitude...
71  		final float logMags[][] = new float[fftCoeffs.length][];
72  		for( int c = 0; c < fftCoeffs.length; c++ )
73  		{
74  			logMags[c] = new float[fftCoeffs[c].length/4];
75  			for( int i = 0; i < fftCoeffs[c].length/4; i++ )
76  			{
77  				// Calculate magnitude
78  				final float re = fftCoeffs[c][i*2];
79  				final float im = fftCoeffs[c][i*2+1];
80  				float mag = (float)Math.log(Math.sqrt( re*re + im*im )+1);
81  				
82  				// Square
83  				mag *= mag;
84  				
85  				// Logarithm
86  				final float logMag = (float)Math.log( mag );
87  				
88  				// Store
89  				logMags[c][i] = logMag;
90  			}
91  		}
92  		
93  		// ... the Fast Fourier (of the log-squared-mags)
94  		this.lastCepstrum  = new float[ logMags.length ][];
95  		final FloatFFT_1D fft2 = new FloatFFT_1D( logMags[0].length/4 );
96  		for( int c = 0; c < logMags.length; c++ )
97  		{
98  			fft2.complexForward( logMags[c] );
99  			
100 			this.lastCepstrum[c] = new float[ logMags[c].length/4 ];
101 			
102 			// ...the squared magnitude of...
103 			for( int i = 0; i < logMags[c].length/4; i++ )
104 			{
105 				// Calculate magnitude
106 				final float re = logMags[c][i*2];
107 				final float im = logMags[c][i*2+1];
108 				float mag = (float)Math.log(Math.sqrt( re*re + im*im )+1);
109 				
110 				// Square
111 				mag *= mag;
112 				
113 				this.lastCepstrum[c][i] = mag;
114 			}
115 		}
116 		
117 	    return sample;
118     }
119 	
120 	/**
121 	 * 	Returns the last generated cepstrum
122 	 *	@return The last generated cepstrum
123 	 */
124 	public float[][] getLastCepstrum()
125 	{
126 		return this.lastCepstrum;
127 	}
128 }