# HG changeset patch # User Chris Cannam # Date 1356791773 0 # Node ID 00b604d7faf9a342df62886f6e3e87dbf8b06c37 # Parent 1e7430a9592681c42369dea100e4583bf9fb04c8 Use complex class for output of FFT diff -r 1e7430a95926 -r 00b604d7faf9 complex.yeti --- 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, } diff -r 1e7430a95926 -r 00b604d7faf9 fft.yeti --- 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, } diff -r 1e7430a95926 -r 00b604d7faf9 test/all.yeti --- 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; diff -r 1e7430a95926 -r 00b604d7faf9 test/test_fft.yeti --- 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])) ), ];