-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComplex.java
More file actions
241 lines (237 loc) · 6.12 KB
/
Copy pathComplex.java
File metadata and controls
241 lines (237 loc) · 6.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//complex
package seuImage;
import java.lang.Math;
public class Complex extends Object{
public double im;
public double re;
public Complex(){};
public Complex(double r,double i){
im=i;
re=r;
};
public Complex(Complex a){
re=a.re;
im=a.im;
}
//exp(jseta), seta is in radians
public Complex(double seta){
re=Math.cos(seta);
im=Math.sin(seta);
}
//convert integer to complex
public static Complex int2Complex(int x){
Complex ret=new Complex();
ret.re=(double)x;
ret.im=0.0;
return ret;
}
//return WN(k),ie. exp(-k*j*(2*pi/N));
public static Complex wnk(int N,int k){
Complex ret=new Complex();
double kD=(double)k;
double ND=(double)N;
double seta=-1.0*kD*2.0*Math.PI/ND;
ret =new Complex(seta);
return ret;
}
//return length
public double length(){
return Math.hypot(re,im);
}
//real part
public double real(){
return re;
}
//img part
public double image(){
return im;
}
//return ang
public double angle(){
return Math.atan(im/re);
}
public Complex add(Complex c){
Complex ret=new Complex();
ret.im=im+c.im;
ret.re=re+c.re;
return ret;
}
public Complex minus(Complex c){
Complex ret=new Complex();
ret.im=im-c.im;
ret.re=re-c.re;
return ret;
}
public Complex mul(Complex c){
Complex ret=new Complex();
ret.im=re*c.im+im*c.re;
ret.re=re*c.re-im*c.im;
return ret;
}
//FFT
public static Complex[] FFT(Complex[] input){
//get log2(complexLength)
int complexLength=input.length;
int tmp,i,level=0,less=0,length=0;//level is fft level num,less is number of added number,length is real length
//double id;
for(i=0;i<32;i++){//suppose lagest level is 32 to avoid error
tmp=pow2(i);
if(tmp>=complexLength){
length=pow2(i);
level=i;
less=tmp-complexLength;
break;//stop
}
}
Complex []newIn=new Complex[complexLength+less];
Complex []output=new Complex[complexLength];
//System.out.println("dump input[]");
for(i=0;i<complexLength;i++){
newIn[i]=input[i];
//dump input[];
//input[i].print();
}
for(;i<complexLength+less;i++){
newIn[i]=new Complex(0.0,0.0);
}
//dump test
/*
for(i=0;i<complexLength;i++){
System.out.println("dump newIn[]");
newIn[i].print();
}
*/
//System.out.printf("complexLength: %d\n",complexLength);
//System.out.printf("level: %d\n",level);
//fft flow
int j,k,step;
Complex com1=new Complex();
Complex com2=new Complex();
for(i=0;i<level;i++){//i is level, left to right
for(j=0,k=0;j<length;/*see below for j*/){//j is the input order, up to down
// x chart
//set k
step=length/pow2(i+1);
//k=j>>(level-i);
//k=bitRevise(k,level);
//k=k+pow2(i);
//System.out.printf("level: %d\tindex: %d\nstep: %d\tk: %d\nnumIndex: %d\t%d\n",i,j,step,k,j,j+step);
/*newIn[j]*/com1=xChart(newIn[j],newIn[j+step],true,step,j);
/*newIn[j+step]*/com2=xChart(newIn[j],newIn[j+step],false,step,j);
newIn[j]=com1;
newIn[j+step]=com2;
j++;
//k=k+pow2(i);
//System.out.printf("j: %d\npow2(level-i-1): %d\n",j,pow2(level-i-1));
if(j%step==0){j=j+step;//j jump if j==k*2^(length-i-1)
//k=0;//and set k=0
//System.out.printf("j: %d\n",j);
}
}
}
//rearrangement,bit revise
//dump output
//System.out.println("dump output");
for(i=0;i<complexLength;i++){
output[i]=newIn[bitRevise(i,level)];
//System.out.printf("input index: %d\t out index: %d\n",i,bitRevise(i,level));
//output[i].print();
}
return output;
}
//bit revise of an integer with bitWidth width
public static int bitRevise(int in,int bitWidth){
int []bitArray =new int[bitWidth];
int i;
for(i=0;i<bitWidth;i++)bitArray[i]=0;//init
for(i=0;true;i++){
bitArray[i]=in%2;
in/=2;
if(in==0)break;
}
int ret=0;
for(i=bitWidth-1;i>=0;i--){
if(bitArray[i]!=0)ret+=pow2(bitWidth-1-i);
}
return ret;
}
//2^p;p>=0;
public static int pow2(int p){
/*
if(p<0)return -1;
int ret=1;
for(int i=0;i<p;i++)ret=ret*2;
return ret;
*/
return 1<<p;
}
//x chart,in1=in1+in2;in2=(in1-in2)*Wn(k);
public static Complex xChart(Complex in1,Complex in2,boolean isFirst,int n,int k){
Complex ret1,ret2;
ret1=new Complex(in1.add(in2));
ret2=new Complex(in1.minus(in2).mul(wnk(n,k)));
/*
if(isFirst){
System.out.printf("dump xChat: n: %d k: %d\n",n,k);
in1.print();
in2.print();
ret1.print();
ret2.print();
System.out.println("ok");
}*/
if(isFirst)return ret1;
else return ret2;
}
//dump
public void print(){
System.out.printf("%f +j*%f\n",re,im);
}
public static double[] DCT(double[]in){
int inLength=in.length;
double [] out=new double[inLength];
Complex[] inCom=new Complex[inLength*2];
for(int i=0;i<inLength;i++){
inCom[i]=new Complex(in[i],0.0);
}
for(int i=inLength;i<2*inLength;i++){
inCom[i]=new Complex(0.0,0.0);
}
inCom=FFT(inCom);
out[0]=0;
for(int i=0;i<inLength;i++){
out[0]+=in[i];
}
double length=(double)inLength;
double k=Math.sqrt(2.0/length);
out[0]*=k/Math.sqrt(2.0);
for(int i=1;i<inLength;i++){
Complex tmpCom=new Complex();
tmpCom=wnk(4*inLength,i).mul(inCom[i]);
out[i]=k*tmpCom.length();
}
return out;
}
/*
//test main
public static void main(String argv[]){
Complex[] duan=new Complex[8];
Complex d=new Complex();
//duan={Complex(1.0,0.0),Complex(2.0,0.0),Complex(3.0,0.0),Complex(4.0,0.0)};
duan[0]=new Complex(1.0,2.0);
duan[1]=new Complex(2.0,-3.0);
duan[2]=new Complex(1.0,0.0);
duan[3]=new Complex(1.0,0.0);
duan[4]=new Complex(4.0,0.0);
duan[5]=new Complex(3.0,0.0);
duan[6]=new Complex(2.0,0.0);
duan[7]=new Complex(1.0,0.0);
for(int i=0;i<duan.length;i++){
System.out.printf("before FFT\nindex %d: \n%f +j*%f\n",i,duan[i].re,duan[i].im);
}
duan=d.FFT(duan);
for(int i=0;i<duan.length;i++){
System.out.printf("index %d: \n%f +j*%f\n",i,duan[i].re,duan[i].im);
}
}
*/
}