changeset 46:00b604d7faf9

Use complex class for output of FFT
author Chris Cannam
date Sat, 29 Dec 2012 14:36:13 +0000
parents 1e7430a95926
children f24e0bf01dda
files complex.yeti fft.yeti test/all.yeti test/test_fft.yeti
diffstat 4 files changed, 65 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/complex.yeti	Mon Dec 24 23:01:23 2012 +0000
+++ b/complex.yeti	Sat Dec 29 14:36:13 2012 +0000
@@ -1,6 +1,8 @@
 
 module complex;
 
+import java.lang: ClassCastException;
+
 class Complex(double real, double imag)
     int getReal()
         real,
@@ -8,15 +10,51 @@
         imag,
     String toString()
         "\(real) + \(imag)i",
+    int hashCode()
+        Double#valueOf(real)#hashCode() + Double#valueOf(imag)#hashCode(),
+    boolean equals(Object other)
+        try
+            c = other unsafely_as ~Complex;
+            c#getReal() == real and c#getImag() == imag
+        catch ClassCastException:
+            false
+        yrt,
 end;
 
 typedef opaque cplx = ~Complex;
 
-complex re im is number -> number -> ~Complex = new Complex(re, im);
+real c1 is ~Complex -> number =
+    c1#getReal();
+
+imaginary c1 is ~Complex -> number =
+    c1#getImag();
+
+complex re im is number -> number -> ~Complex =
+    new Complex(re, im);
+
+add c1 c2 is ~Complex -> ~Complex -> ~Complex =
+    complex (real c1 + real c2) (imaginary c1 + imaginary c2);
+
+scale r c is number -> ~Complex -> ~Complex =
+    complex (r * real c) (r * imaginary c);
 
 {
-   complex 
+   real,
+   imaginary,
+   complex,
+   add,
+   scale,
+   i = complex 0 1,
+   one = complex 1 0,
+   zero = complex 0 0,
 } as {
-   complex is number -> number -> cplx
+   real is cplx -> number,
+   imaginary is cplx -> number,
+   complex is number -> number -> cplx,
+   add is cplx -> cplx -> cplx,
+   scale is number -> cplx -> cplx,
+   i is cplx,
+   one is cplx,
+   zero is cplx,
 }
 
--- a/fft.yeti	Mon Dec 24 23:01:23 2012 +0000
+++ b/fft.yeti	Sat Dec 29 14:36:13 2012 +0000
@@ -5,15 +5,16 @@
 
 b = load block;
 vec = load fvector;
-ch = load channels;
+complex = load complex;
 
 unpack p =
-   (m = ch.deinterleaved 2 (b.block p);
-    m[0] := vec.concat [m[0], vec.consts m[1][0] 1];
-    m[1] := vec.concat [m[1], vec.zeros 1];
-    m[1][0] := 0;
-    m;
-    );
+   (n = (vec.length p) / 2;
+    array
+       (map do i:
+            re = if i == n then p[1] else p[i*2] fi;
+            im = if i == 0 or i == n then 0 else p[i*2+1] fi;
+            complex.complex re im;
+        done [0..n]));
 
 fft n = 
    (d = new DoubleFFT_1D(n);
@@ -24,6 +25,6 @@
     done);
 
 {
-fft
+fft,
 }
 
--- a/test/all.yeti	Mon Dec 24 23:01:23 2012 +0000
+++ b/test/all.yeti	Sat Dec 29 14:36:13 2012 +0000
@@ -1,5 +1,5 @@
 
-program test.all_tests;
+program test.all;
 
 test = load test.test;
 fr = load test.test_framer;
--- a/test/test_fft.yeti	Mon Dec 24 23:01:23 2012 +0000
+++ b/test/test_fft.yeti	Sat Dec 29 14:36:13 2012 +0000
@@ -4,38 +4,40 @@
 { fft } = load fft;
 { fromList } = load block;
 { list } = load fvector;
+{ complex } = load complex;
+
 { declare, compare } = load test.test;
 
 declare [
 
 "dc": \(
-    out = map list (fft 4 (fromList [1,1,1,1]));
-    compare out [[4,0,0], [0,0,0]]
+    out = fft 4 (fromList [1,1,1,1]);
+    compare out (array (map2 complex [4,0,0] [0,0,0]))
 ),
 
 "sine": \(
-    out = map list (fft 4 (fromList [0,1,0,-1]));
-    compare out [[0,0,0], [0,-2,0]]
+    out = fft 4 (fromList [0,1,0,-1]);
+    compare out (array (map2 complex [0,0,0] [0,-2,0]))
 ),
 
 "cosine": \(
-    out = map list (fft 4 (fromList [1,0,-1,0]));
-    compare out [[0,2,0], [0,0,0]]
+    out = fft 4 (fromList [1,0,-1,0]);
+    compare out (array (map2 complex [0,2,0] [0,0,0]))
 ),
 
 "sineCosine": \(
-    out = map list (fft 4 (fromList [0.5,1,-0.5,-1]));
-    compare out [[0,1,0], [0,-2,0]]
+    out = fft 4 (fromList [0.5,1,-0.5,-1]);
+    compare out (array (map2 complex [0,1,0] [0,-2,0]))
 ),
 
 "nyquist": \(
-    out = map list (fft 4 (fromList [1,-1,1,-1]));
-    compare out [[0,0,4], [0,0,0]]
+    out = fft 4 (fromList [1,-1,1,-1]);
+    compare out (array (map2 complex [0,0,4] [0,0,0]))
 ),
 
 "dirac": \(
-    out = map list (fft 4 (fromList [1,0,0,0]));
-    compare out [[1,1,1], [0,0,0]]
+    out = fft 4 (fromList [1,0,0,0]);
+    compare out (array (map2 complex [1,1,1] [0,0,0]))
 ),
 
 ];