简体   繁体   English

将Complex转换为ArrayList <Float> 在Java中

[英]Converting Complex to ArrayList<Float> in Java

I have an input signal that I want to store in an ArrayList then convert it into Complex, which goes something like this 我有一个输入信号,我想存储在ArrayList中,然后将其转换为Complex,就像这样

-0.03480425839330703
0.07910192950176387
0.7233322451735928
0.1659819820667019

and this outputs its FFT like this 这就像这样输出它的FFT

0.9336118983487516
-0.7581365035668999 + 0.08688005256493803i
0.44344407521182005
-0.7581365035668999 - 0.08688005256493803i

This is in a complex structure, I want to convert this into an ArrayList type. 这是一个复杂的结构,我想将其转换为ArrayList类型。 while dropping the + 0.08688005256493803i value. 同时降低+ 0.08688005256493803i值。

So All I need are these values 所以我需要的只是这些价值观

0.9336118983487516
-0.7581365035668999
0.44344407521182005
-0.7581365035668999

What is the best way of going about this? 解决这个问题的最佳方式是什么?

And this is the code that I am using 这是我正在使用的代码

public static Complex[] fft(Complex[] x) {
        int N = x.length;

        // base case
        if (N == 1) return new Complex[] { x[0] };

        // radix 2 Cooley-Tukey FFT
        if (N % 2 != 0) { throw new RuntimeException("N is not a power of 2"); }

        // fft of even terms
        Complex[] even = new Complex[N/2];
        for (int k = 0; k < N/2; k++) {
            even[k] = x[2*k];
        }
        Complex[] q = fft(even);

        // fft of odd terms
        Complex[] odd  = even;  // reuse the array
        for (int k = 0; k < N/2; k++) {
            odd[k] = x[2*k + 1];
        }
        Complex[] r = fft(odd);

        // combine
        Complex[] y = new Complex[N];
        for (int k = 0; k < N/2; k++) {
            double kth = -2 * k * Math.PI / N;
            Complex wk = new Complex(Math.cos(kth), Math.sin(kth));
            y[k]       = q[k].plus(wk.times(r[k]));
            y[k + N/2] = q[k].minus(wk.times(r[k]));
        }
        return y;
    }

All you want to do is just drop imaginary part of your Complex data structure. 您要做的只是删除Complex数据结构的虚构部分。

As you not show us Complex class assume it has member for real part (eg double real; ) To drop imaginary part just call something like complex.getRealPart() , or access complex.real (substitute with your real member name). 正如你没有向我们展示Complex类假设它具有真实部分的成员(例如double real; )要删除虚部,只需调用complex.getRealPart() ,或访问complex.real (用你的真实成员名替换)。

To compose ArrayList<Double> use the following snippet: 要编写ArrayList<Double>使用以下代码段:

ArrayList<Double> list = new ArrayList<Double>();
for (Complex c : complexes) {  // complexes your array of complexes returned from for fft
  list.add(c.getRealpart());
}

Note: Just in case, I can be wrong, but I assume that instead of real part you need absolute value of complex number. 注意:以防万一,我可能是错的,但我认为你需要复数的绝对值,而不是真正的部分。 To calculate it use: 要计算它使用:

Math.sqrt(c.getRealPart() * c.getRealPart() + c.getImPart() * c.getImPart());

From what I understand you just want the real part of the complex value. 根据我的理解,你只想要复杂价值的真实部分。 If that's the case, presumably your Complex class also has getReal() and getImaginary() (or similar) methods - so just use getReal(). 如果是这种情况,可能你的Complex类也有getReal()和getImaginary()(或类似的)方法 - 所以只需使用getReal()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM