我有一个数组,它在for循环中,首先转换为一个列表,然后分成两半,列表的第一部分存储在s1列表中,第二部分存储在w1中,这个是递归完成,直到循环结束,在方法结束时,我将返回s1和w1这是我到目前为止所做的代码 - :
public Pair daubTrans( double s[] ) throws Exception
{
final int N = s.length;
int n;
//double t1[] = new double[100000];
//List<Double> t1 = new ArrayList<Double>();
// double s1[] = new double[100000];
List<double[]> w1 = new ArrayList<double[]>();
List<double[]> s1 = new ArrayList<double[]>();
List<double[]> lList = new ArrayList<double[]>();
//List<double[]> t1 = new ArrayList<double[]>();
for (n = N; n >= 4; n >>= 1) {
double[] t1= transform( s, n );
int length = t1.length;
// System.out.println(n);
// LinkedList<double> t1 =new LinkedList<double>( Arrays.asList(t1));
/* for(double[] d: t1)
{
t1.add(d);
}*/
lList = Arrays.asList(t1);
length=lList.size();
//System.out.print(lList.size());
// System.arraycopy(src, srcPos, dest, destPos, length)
/* s1= t1.subList(0, 1);
w1= t1.subList(0, 1); */
/* if(n==N)
{
s1= lList.subList(0, length/2-1);
w1= lList.subList(length/2-1, length);
}
else
{
s1=lList.subList(( length/2), length);
w1=lList.subList(( length/2), length);
} */
// System.arraycopy(t1,0, s1, n==N?0:t1.size()/2-1, t1.size()/2-1);
// System.arraycopy(t1,(length/2), w1, n==N?0:t1.size()/2-1, t1.size()/2-1);
// System.out.println(w1.length);
}
return new Pair(s1, w1);
}
其中定义了对类,以便返回2列表,并且transform返回一个double类型的数组,该数组存储在t1数组中。 现在我遇到了将t1数组转换为列表类型以及如何将由t1元素形成的列表拆分为2部分的问题。变革守则是 - :
protected double[] transform( double a[], int n )
{
if (n >= 4) {
int i, j;
int half = n >> 1;
double tmp[] = new double[n];
i = 0;
for (j = 0; j < n-3; j = j + 2) {
tmp[i] = a[j]*h0 + a[j+1]*h1 + a[j+2]*h2 + a[j+3]*h3;
tmp[i+half] = a[j]*g0 + a[j+1]*g1 + a[j+2]*g2 + a[j+3]*g3;
i++;
}
// System.out.println(i);
tmp[i] = a[n-2]*h0 + a[n-1]*h1 + a[0]*h2 + a[1]*h3;
tmp[i+half] = a[n-2]*g0 + a[n-1]*g1 + a[0]*g2 + a[1]*g3;
for (i = 0; i < n; i++) {
a[i] = tmp[i];
}
}
return a;
} // transform
这是整个代码 - :
import java.util.Arrays;
import java.util.List;
import java.util.*;
import java.lang.Math.*;
class daub {
protected final double sqrt_3 = Math.sqrt( 3 );
protected final double denom = 4 * Math.sqrt( 2 );
//
// forward transform scaling (smoothing) coefficients
//
protected final double h0 = (1 + sqrt_3)/denom;
protected final double h1 = (3 + sqrt_3)/denom;
protected final double h2 = (3 - sqrt_3)/denom;
protected final double h3 = (1 - sqrt_3)/denom;
//
// forward transform wavelet coefficients
//
protected final double g0 = h3;
protected final double g1 = -h2;
protected final double g2 = h1;
protected final double g3 = -h0;
//
// Inverse transform coefficients for smoothed values
//
protected final double Ih0 = h2;
protected final double Ih1 = g2; // h1
protected final double Ih2 = h0;
protected final double Ih3 = g0; // h3
//
// Inverse transform for wavelet values
//
protected final double Ig0 = h3;
protected final double Ig1 = g3; // -h0
protected final double Ig2 = h1;
protected final double Ig3 = g1; // -h2
List<Double> doubleList = new ArrayList<Double>();
/**
<p>
Forward wavelet transform.
protected double[] transform( double a[], int n )
{
if (n >= 4) {
int i, j;
int half = n >> 1;
double tmp[] = new double[n];
i = 0;
for (j = 0; j < n-3; j = j + 2) {
tmp[i] = a[j]*h0 + a[j+1]*h1 + a[j+2]*h2 + a[j+3]*h3;
tmp[i+half] = a[j]*g0 + a[j+1]*g1 + a[j+2]*g2 + a[j+3]*g3;
i++;
}
// System.out.println(i);
tmp[i] = a[n-2]*h0 + a[n-1]*h1 + a[0]*h2 + a[1]*h3;
tmp[i+half] = a[n-2]*g0 + a[n-1]*g1 + a[0]*g2 + a[1]*g3;
for (i = 0; i < n; i++) {
a[i] = tmp[i];
}
}
return a;
} //转换
protected void invTransform( double a[], int n )
{
if (n >= 4) {
int i, j;
int half = n >> 1;
int halfPls1 = half + 1;
double tmp[] = new double[n];
// last smooth val last coef. first smooth first coef
tmp[0] = a[half-1]*Ih0 + a[n-1]*Ih1 + a[0]*Ih2 + a[half]*Ih3;
tmp[1] = a[half-1]*Ig0 + a[n-1]*Ig1 + a[0]*Ig2 + a[half]*Ig3;
j = 2;
for (i = 0; i < half-1; i++) {
// smooth val coef. val smooth val coef. val
tmp[j++] = a[i]*Ih0 + a[i+half]*Ih1 + a[i+1]*Ih2 + a[i+halfPls1]*Ih3;
tmp[j++] = a[i]*Ig0 + a[i+half]*Ig1 + a[i+1]*Ig2 + a[i+halfPls1]*Ig3;
}
for (i = 0; i < n; i++) {
a[i] = tmp[i];
}
}
}
/**
Forward Daubechies D4 transform
*/
public Pair daubTrans( double s[] ) throws Exception
{
final int N = s.length;
int n;
//double t1[] = new double[100000];
//List<Double> t1 = new ArrayList<Double>();
// double s1[] = new double[100000];
List<double[]> w1 = new ArrayList<double[]>();
List<double[]> s1 = new ArrayList<double[]>();
List<double[]> lList = new ArrayList<double[]>();
//List<double[]> t1 = new ArrayList<double[]>();
for (n = N; n >= 4; n >>= 1) {
double[] t1= transform( s, n );
int length = t1.length;
// System.out.println(n);
// LinkedList<double> t1 =new LinkedList<double>( Arrays.asList(t1));
/* for(double[] d: t1)
{
t1.add(d);
}*/
lList = Arrays.asList(t1);
length=lList.size();
//System.out.print(lList.size());
// System.arraycopy(src, srcPos, dest, destPos, length)
/* s1= t1.subList(0, 1);
w1= t1.subList(0, 1); */
if(n==N)
{
s1= lList.subList(0, length/2-1);
w1= lList.subList(length/2-1, length);
}
else
{
s1=lList.subList(( length/2), length);
w1=lList.subList(( length/2), length);
}
// System.arraycopy(t1,0, s1, n==N?0:t1.size()/2-1, t1.size()/2-1);
// System.arraycopy(t1,(length/2), w1, n==N?0:t1.size()/2-1, t1.size()/2-1);
// System.out.println(w1.length);
}
return new Pair(s1, w1);
}
/**
答案 0 :(得分:0)
请在此问题中添加transform(s,n)
的代码。
为什么这样? for (n = N; n >= 4; n >>= 1) {}
似乎更容易:for (int n = N; n >= 4; n--) {}
这很疯狂:List<double[]>
如果您想使用双打列表,请使用此代码:List<double>
您希望看到什么结果?