/**
* JWave - Java implementation of wavelet transform algorithms
*
* Copyright 2010-2012 Christian Scheiblich
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This file WaveletPacketTransform.java is part of JWave.
*
* @author Christian Scheiblich
* date 23.02.2010 13:44:05
* contact graetz@mailfish.de
*/
package math.transform.jwave.handlers;
import math.transform.jwave.handlers.wavelets.Wavelet;
import math.transform.jwave.handlers.wavelets.WaveletInterface;
/**
* Base class for the forward and reverse Wavelet Packet Transform (WPT) also
* called Wavelet Packet Decomposition (WPD) using a specified Wavelet by
* inheriting class.
*
* @date 23.02.2010 13:44:05
* @author Christian Scheiblich
*/
public class WaveletPacketTransform extends WaveletTransform {
/**
* Constructor receiving a Wavelet object.
*
* @date 23.02.2010 13:44:05
* @author Christian Scheiblich
* @param wavelet
* object of type Wavelet; Haar02, Daub02, Coif06, ...
*/
public WaveletPacketTransform( WaveletInterface wavelet ) {
super(wavelet);
} // WaveletPacketTransform
/**
* Constructor receiving a Wavelet object.
*
* @date 23.02.2010 13:44:05
* @author Christian Scheiblich
* @param wavelet
* object of type Wavelet; Haar02, Daub02, Coif06, ...
*/
public WaveletPacketTransform( WaveletInterface wavelet, int iteration ) {
super(wavelet, iteration);
} // WaveletPacketTransform
/**
* Implementation of the 1-D forward wavelet packet transform for arrays of
* dim N by filtering with the longest wavelet first and then always with both
* sub bands -- low and high (approximation and details) -- by the next
* smaller wavelet.
*
* @date 23.02.2010 13:44:05
* @author Christian Scheiblich
* @see math.transform.jwave.handlers.BasicTransform#forward(double[])
*/
@Override
public double[ ] forwardWavelet( double[ ] arrTime ) {
double[ ] arrHilb = new double[ arrTime.length ];
for( int i = 0; i < arrTime.length; i++ )
arrHilb[ i ] = arrTime[ i ];
int level = 0;
int k = arrTime.length;
int h = arrTime.length;
int minWaveLength = _wavelet.getWaveLength( );
if( h >= minWaveLength ) {
while( h >= minWaveLength ) {
int g = k / h; // 1 -> 2 -> 4 -> 8 -> ...
for( int p = 0; p < g; p++ ) {
double[ ] iBuf = new double[ h ];
for( int i = 0; i < h; i++ )
iBuf[ i ] = arrHilb[ i + ( p * h ) ];
double[ ] oBuf = _wavelet.forward( iBuf );
for( int i = 0; i < h; i++ )
arrHilb[ i + ( p * h ) ] = oBuf[ i ];
} // packets
h = h >> 1;
level++;
} // levels
} // if
return arrHilb;
} // forward
/**
* Implementation of the 1-D reverse wavelet packet transform for arrays of
* dim N by filtering with the smallest wavelet for all sub bands -- low and
* high bands (approximation and details) -- and the by the next greater
* wavelet combining two smaller and all other sub bands.
*
* @date 23.02.2010 13:44:05
* @author Christian Scheiblich
* @see math.transform.jwave.handlers.BasicTransform#reverse(double[])
*/
@Override
public double[ ] reverseWavelet( double[ ] arrHilb ) {
double[ ] arrTime = new double[ arrHilb.length ];
for( int i = 0; i < arrHilb.length; i++ )
arrTime[ i ] = arrHilb[ i ];
int level = 0;
int minWaveLength = _wavelet.getWaveLength( );
int k = arrTime.length;
int h = minWaveLength;
if( arrHilb.length >= minWaveLength ) {
while( h <= arrTime.length && h >= minWaveLength ) {
int g = k / h; // ... -> 8 -> 4 -> 2 -> 1
for( int p = 0; p < g; p++ ) {
double[ ] iBuf = new double[ h ];
for( int i = 0; i < h; i++ )
iBuf[ i ] = arrTime[ i + ( p * h ) ];
double[ ] oBuf = _wavelet.reverse( iBuf );
for( int i = 0; i < h; i++ )
arrTime[ i + ( p * h ) ] = oBuf[ i ];
} // packets
h = h << 1;
level++;
} // levels
} // if
return arrTime;
} // reverse
/**
* Implementation of the 1-D forward wavelet packet transform for arrays of
* dim N by filtering with the longest wavelet first and then always with both
* sub bands -- low and high (approximation and details) -- by the next
* smaller wavelet. Stopping at the given level.
*
* @date 15.07.2010 13:43:44
* @author Christian Scheiblich
* @see math.transform.jwave.handlers.BasicTransform#forward(double[], int)
*/
@Override
public double[ ] forwardWavelet( double[ ] arrTime, int toLevel ) {
double[ ] arrHilb = new double[ arrTime.length ];
for( int i = 0; i < arrTime.length; i++ )
arrHilb[ i ] = arrTime[ i ];
int level = 0;
int k = arrTime.length;
int h = arrTime.length;
int minWaveLength = _wavelet.getWaveLength( );
if( h >= minWaveLength ) {
while( h >= minWaveLength && level < toLevel ) {
int g = k / h; // 1 -> 2 -> 4 -> 8 -> ...
for( int p = 0; p < g; p++ ) {
double[ ] iBuf = new double[ h ];
for( int i = 0; i < h; i++ )
iBuf[ i ] = arrHilb[ i + ( p * h ) ];
double[ ] oBuf = _wavelet.forward( iBuf );
for( int i = 0; i < h; i++ )
arrHilb[ i + ( p * h ) ] = oBuf[ i ];
} // packets
h = h >> 1;
level++;
} // levels
} // if
return arrHilb;
} // forward
/**
* Implementation of the 1-D reverse wavelet packet transform for arrays of
* dim N by filtering with the smallest wavelet for all sub bands -- low and
* high bands (approximation and details) -- and the by the next greater
* wavelet combining two smaller and all other sub bands. Starting from a
* given level.
*
* @date 15.07.2010 13:44:03
* @author Christian Scheiblich
* @date 20.06.2011 13:05:15
* @author Pol Kennel
* @see math.transform.jwave.handlers.BasicTransform#reverse(double[], int)
*/
@Override
public double[ ] reverseWavelet( double[ ] arrHilb, int fromLevel ) {
double[ ] arrTime = new double[ arrHilb.length ];
for( int i = 0; i < arrHilb.length; i++ )
arrTime[ i ] = arrHilb[ i ];
int level = 0;
int minWaveLength = _wavelet.getWaveLength( );
int k = arrTime.length;
// int h = minWaveLength; // bug ... 20110620
int h = (int) (arrHilb.length / (Math.pow(2, fromLevel-1))); // added by Pol
if( arrHilb.length >= minWaveLength ) {
while( h <= arrTime.length && h >= minWaveLength && level < fromLevel ) {
int g = k / h; // ... -> 8 -> 4 -> 2 -> 1
for( int p = 0; p < g; p++ ) {
double[ ] iBuf = new double[ h ];
for( int i = 0; i < h; i++ )
iBuf[ i ] = arrTime[ i + ( p * h ) ];
double[ ] oBuf = _wavelet.reverse( iBuf );
for( int i = 0; i < h; i++ )
arrTime[ i + ( p * h ) ] = oBuf[ i ];
} // packets
h = h << 1;
level++;
} // levels
} // if
return arrTime;
} // reverse
} // class