001/** 002 * Copyright (c) 2011, The University of Southampton and the individual contributors. 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, 006 * are permitted provided that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, 009 * this list of conditions and the following disclaimer. 010 * 011 * * Redistributions in binary form must reproduce the above copyright notice, 012 * this list of conditions and the following disclaimer in the documentation 013 * and/or other materials provided with the distribution. 014 * 015 * * Neither the name of the University of Southampton nor the names of its 016 * contributors may be used to endorse or promote products derived from this 017 * software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030/** 031 * 032 */ 033package org.openimaj.audio.analysis; 034 035import org.openimaj.audio.SampleChunk; 036import org.openimaj.audio.processor.AudioProcessor; 037 038import edu.emory.mathcs.jtransforms.fft.FloatFFT_1D; 039 040/** 041 * An implementation of the power cepstrum of an audio signal. The 042 * power cepstrum of an audio signal is the squared magnitude of the Fourier 043 * transform of the logarithm of the squared magnitude of the Fourier transform 044 * of a signal. Yeah, I know. 045 * 046 * @author David Dupplaw (dpd@ecs.soton.ac.uk) 047 * @created 18 Jul 2012 048 * @version $Author$, $Revision$, $Date$ 049 */ 050public class PowerCepstrumTransform extends AudioProcessor 051{ 052 /** The last generated cepstrum */ 053 private float[][] lastCepstrum = null; 054 055 @Override 056 public SampleChunk process( final SampleChunk sample ) throws Exception 057 { 058 final FourierTransform fft = new FourierTransform(); 059 060 // 061 // The squared magnitude of the Fourier transform of the logarithm 062 // of the squared magnitude of the Fourier transform of a signal... 063 // 064 // Working backwards... 065 // ... the FFT of a signal... 066 // 067 fft.process( sample ); 068 final float[][] fftCoeffs = fft.getLastFFT(); 069 070 // ...the logarithm of the squared magnitude... 071 final float logMags[][] = new float[fftCoeffs.length][]; 072 for( int c = 0; c < fftCoeffs.length; c++ ) 073 { 074 logMags[c] = new float[fftCoeffs[c].length/4]; 075 for( int i = 0; i < fftCoeffs[c].length/4; i++ ) 076 { 077 // Calculate magnitude 078 final float re = fftCoeffs[c][i*2]; 079 final float im = fftCoeffs[c][i*2+1]; 080 float mag = (float)Math.log(Math.sqrt( re*re + im*im )+1); 081 082 // Square 083 mag *= mag; 084 085 // Logarithm 086 final float logMag = (float)Math.log( mag ); 087 088 // Store 089 logMags[c][i] = logMag; 090 } 091 } 092 093 // ... the Fast Fourier (of the log-squared-mags) 094 this.lastCepstrum = new float[ logMags.length ][]; 095 final FloatFFT_1D fft2 = new FloatFFT_1D( logMags[0].length/4 ); 096 for( int c = 0; c < logMags.length; c++ ) 097 { 098 fft2.complexForward( logMags[c] ); 099 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}