2013-05-13 38 views
2

我想创建一个函数,在保持其“形状”的同时改变整数数组的大小。Java中的数组修改(改变数组“分辨率”)

目的是加快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; 
} 
+0

你想通过插值使数组变大,对吗?如果'y 2013-05-13 14:57:24

+1

我们需要更多关于您正在实施的“想法”的数据。在旧数组中,“覆盖”是什么意思?根据什么规则,你决定'z [0] =(1 * w [0] + 0.5 * w [1])...''但'z [1] =(0.5 * w [1] + 1 * w [ 2])...'?为什么'w [1]'在它之前总是有'0.5 *'? – Pshemo 2013-05-13 15:01:53

+0

请发布您尝试过的。 – 2013-05-13 15:06:58

回答

1

所以,你想一个过滤器应用于阵列?这是一个天真的实现... 我认为关键在于如何对过滤器进行编码......我将通过将其表示为一系列浮点数来代表我想要应用于原始值的百分比产值。这是过滤器的标准。

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; 

} 

而像您的问题指定来调用滤波器,....

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

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

处理的从[1]由1/2比例的情况下,可以通过预处理来hadled数组然后再应用过滤器。像这样:

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); 
    } 
} 
0

可以使用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.