samer@0
|
1 /*
|
samer@0
|
2 * Copyright (c) 2000, Samer Abdallah, King's College London.
|
samer@0
|
3 * All rights reserved.
|
samer@0
|
4 *
|
samer@0
|
5 * This software is provided AS iS and WITHOUT ANY WARRANTY;
|
samer@0
|
6 * without even the implied warranty of MERCHANTABILITY or
|
samer@0
|
7 * FITNESS FOR A PARTICULAR PURPOSE.
|
samer@0
|
8 */
|
samer@0
|
9
|
samer@0
|
10 package samer.units;
|
samer@0
|
11
|
samer@0
|
12 import samer.core.*;
|
samer@0
|
13 import samer.maths.*;
|
samer@0
|
14 import samer.tools.*;
|
samer@0
|
15 import samer.functions.*;
|
samer@0
|
16
|
samer@0
|
17 /**
|
samer@0
|
18 Manages a fourier transform from a given Vec into
|
samer@0
|
19 another vector. Can create tasks appropriate for
|
samer@0
|
20 a power spectrum or an arbitrary function of a
|
samer@0
|
21 power spectrum.
|
samer@0
|
22 */
|
samer@0
|
23
|
samer@0
|
24 public class FFTVector extends FFT implements Task
|
samer@0
|
25 {
|
samer@0
|
26 Vec in;
|
samer@0
|
27
|
samer@0
|
28 public FFTVector(Vec in) throws Exception { this(in.size()); setInput(in); }
|
samer@0
|
29 public FFTVector(int N) throws Exception
|
samer@0
|
30 {
|
samer@0
|
31 super(N);
|
samer@0
|
32
|
samer@0
|
33 // setup window function
|
samer@0
|
34 String window = Shell.getString("ft.window","hamming");
|
samer@0
|
35 if (window.equals("rectangular")) setWindow(new Constant(1.0));
|
samer@0
|
36 else if (window.equals("hanning")) setWindow(new Hanning());
|
samer@0
|
37 else setWindow(new Hamming());
|
samer@0
|
38 }
|
samer@0
|
39
|
samer@0
|
40 public void setInput(Vec in) { this.in = in; }
|
samer@0
|
41 public String toString() { return "FT("+in+")"; }
|
samer@0
|
42 public Vec real() { return new Vec.ForArray(real); }
|
samer@0
|
43 public Vec imag() { return new Vec.ForArray(imag); }
|
samer@0
|
44
|
samer@0
|
45 public void starting() {}
|
samer@0
|
46 public void stopping() {}
|
samer@0
|
47 public void run() { input(in.iterator()); calculate(); }
|
samer@0
|
48 public void dispose() {}
|
samer@0
|
49
|
samer@0
|
50 public Task calcTask() {
|
samer@0
|
51 if (in.array()!=null) {
|
samer@0
|
52 return new AnonymousTask() {
|
samer@0
|
53 double [] b=in.array();
|
samer@0
|
54 public void run() throws Exception {
|
samer@0
|
55 input(b); calculate();
|
samer@0
|
56 }
|
samer@0
|
57 public String toString() { return FFTVector.this.toString()+": compute"; }
|
samer@0
|
58 };
|
samer@0
|
59 } else return this;
|
samer@0
|
60 }
|
samer@0
|
61
|
samer@0
|
62 public Task getPower(final VVector A)
|
samer@0
|
63 {
|
samer@0
|
64 final double [] a=A.array();
|
samer@0
|
65 return new AnonymousTask() {
|
samer@0
|
66 public void run() throws Exception {
|
samer@0
|
67 outputPower(a); A.changed();
|
samer@0
|
68 }
|
samer@0
|
69 public String toString() { return FFTVector.this.toString()+":power->"+A; }
|
samer@0
|
70 };
|
samer@0
|
71 }
|
samer@0
|
72
|
samer@0
|
73 public Task getFnPower(final Function f, final VVector A)
|
samer@0
|
74 {
|
samer@0
|
75 final double [] a=A.array();
|
samer@0
|
76 return new AnonymousTask() {
|
samer@0
|
77 public void run() throws Exception {
|
samer@0
|
78 outputPower(a); f.apply(a);
|
samer@0
|
79 A.changed();
|
samer@0
|
80 }
|
samer@0
|
81 public void dispose() { f.dispose(); super.dispose(); }
|
samer@0
|
82 public String toString() {
|
samer@0
|
83 return FFTVector.this.toString()+":"+f.format("power")+"->"+A;
|
samer@0
|
84 }
|
samer@0
|
85 };
|
samer@0
|
86 }
|
samer@0
|
87
|
samer@0
|
88 public Task getLinearFT(final VVector A)
|
samer@0
|
89 {
|
samer@0
|
90 return new AnonymousTask() {
|
samer@0
|
91 double[] a=A.array();
|
samer@0
|
92
|
samer@0
|
93 public void run() throws Exception
|
samer@0
|
94 {
|
samer@0
|
95 int i=1,j=1;
|
samer@0
|
96 a[0]=real[0];
|
samer@0
|
97 for (; i<N/2; i++) {
|
samer@0
|
98 a[j++] = real[i];
|
samer@0
|
99 a[j++] = imag[i];
|
samer@0
|
100 }
|
samer@0
|
101 a[j]=real[i]; // i=N/2, j=N-1 (I hope)
|
samer@0
|
102 A.changed();
|
samer@0
|
103 }
|
samer@0
|
104 public String toString() { return FFTVector.this.toString()+"->"+A; }
|
samer@0
|
105 };
|
samer@0
|
106 }
|
samer@0
|
107 }
|
samer@0
|
108
|