Java中的数组修改(更改数组“分辨率”)

Java中的数组修改(更改数组“分辨率”),java,arrays,Java,Arrays,我想创建一个函数,在保持其“形状”的同时更改整数数组的大小 其目的是加速FFT的计算 它返回一个大小为y的新数组,每个元素都是它在旧数组中“覆盖”的元素的平均值。例如,如果我有一个包含3个元素的数组w,并且想要创建另一个包含2个元素的数组z,那么第一个元素将是:z[0]=(1*w[0]+0.5*w[1])*2/3,第二个元素将是:z[1]=(0.5*w[1]+1*w[2])*2/3。这有点像改变阵列的“分辨率”。(当然,对于较小的数字,舍入有丢失信息的风险,但对于较大的数字,我需要舍入,因为两位

我想创建一个函数,在保持其“形状”的同时更改整数数组的大小

其目的是加速FFT的计算

它返回一个大小为
y
的新数组,每个元素都是它在旧数组中“覆盖”的元素的平均值。例如,如果我有一个包含3个元素的数组
w
,并且想要创建另一个包含2个元素的数组
z
,那么第一个元素将是:
z[0]=(1*w[0]+0.5*w[1])*2/3
,第二个元素将是:
z[1]=(0.5*w[1]+1*w[2])*2/3
。这有点像改变阵列的“分辨率”。(当然,对于较小的数字,舍入有丢失信息的风险,但对于较大的数字,我需要舍入,因为两位数并不重要。)

感觉这是一个非常直截了当的问题,但我花了太多时间在静脉上。我确实有一些代码,尽管我几乎可以使用,但我认为我走错了方向(行太多了)。基本上,它在原始数组中循环,计算如何对每个元素进行除法,并使用局部变量跟踪将其放在何处

而且,我的搜索结果都是动态改变数组大小之类的,这不是我想要的

因此,这里有一个可能的骨架:

public int[] the_function (int[] w, int y) {
    int[] z = new int[y];

    // Some code looping through the array

    return z;
}

那么您想对数组应用过滤器吗?这是一个幼稚的实现。。。 我认为关键是要聪明地对过滤器进行编码。。。我将用一系列浮点数来表示它,这些浮点数表示我要应用于输出值的原始值的百分比。这对于过滤器来说是相当标准的

public static int[] applyFilter( int[] from , float[] filter ) { 
    if (filter.length > from.lenth){
        throw new IllegalArgumentException("Filter to large to apply to array!");
    } 

    int [] to = new int[from.length + 1 - filter.length];

    for ( int i = 0; i < from.length + 1 - filter.length; i++) { 
        float newValue = 0.0f;

        for( int j = 0; j < filter.length; j++){
           newValue += filter[j] * from[i+j]; 
        }

        to[i] = Math.round(newValue);
    }
    return to;

}
处理from[1]按1/2缩放的情况,可以通过预处理数组然后应用第二个过滤器来进行阴影处理。像这样:

 public static void main (String ... args){
    float[] filter = new float[] { 0.66f, 0.66f }; 
    int[] from = new int[] { 1, 2, 3, 4, 5, 6};

            // Preprocess to apply static scalars to the source array.
            int[] frompp = from.clone();
            frompp[1] = Math.round((float) from[i] / 0.5f);

    int[] to = applyFilter(from, filterpp);
    for (int i : to){
        System.out.println(i);
    }
}

您可以使用最小公倍数lcm来使用积分算法。 将源阵列(w)和目标阵列映射/缩放到 最小公倍数

public static int gcd(int a, int b) {
    while (a != b) {
        if (a > b) a -= b; else b -= a;
    }
    return a;
}

public static int lcm(int a, int b) {
    return a * (b / gcd(a, b);
}

One then can achieve a high precision in averaging.
For image scaling ideal and fast.
For your purpose `double` math will do, I think, and saves a bit on index juggling.

你想通过插值使数组变大,对吗?如果
y
,我们需要更多关于您要实施的“想法”的数据。您所说的旧数组中的“cover”是什么意思?您根据什么规则决定
z[0]=(1*w[0]+0.5*w[1])…
z[1]=(0.5*w[1]+1*w[2])…
?为什么
w[1]
之前总是
0.5*
呢?请把你尝试过的东西贴出来。在我看来,这取决于你开发一个算法。。。。或
public static int gcd(int a, int b) {
    while (a != b) {
        if (a > b) a -= b; else b -= a;
    }
    return a;
}

public static int lcm(int a, int b) {
    return a * (b / gcd(a, b);
}

One then can achieve a high precision in averaging.
For image scaling ideal and fast.
For your purpose `double` math will do, I think, and saves a bit on index juggling.