annotate yetilab/signal/test/test_window.yeti @ 265:c7efd12c27c5

Window fixes and tests
author Chris Cannam
date Thu, 23 May 2013 13:21:05 +0100
parents
children 46d2923a04ab
rev   line source
Chris@265 1
Chris@265 2 module yetilab.signal.test.test_window;
Chris@265 3
Chris@265 4 win = load yetilab.signal.window;
Chris@265 5 vec = load yetilab.vector.vector;
Chris@265 6
Chris@265 7 { compare, compareUsing } = load yetilab.test.test;
Chris@265 8
Chris@265 9 functions = [
Chris@265 10 Hann () : win.hann,
Chris@265 11 Hamming () : win.hamming,
Chris@265 12 Blackman () : win.blackman,
Chris@265 13 Nuttall () : win.nuttall,
Chris@265 14 BlackmanNuttall () : win.blackmanNuttall,
Chris@265 15 BlackmanHarris () : win.blackmanHarris,
Chris@265 16 Boxcar () : win.boxcar,
Chris@265 17 Bartlett () : win.bartlett,
Chris@265 18 ];
Chris@265 19
Chris@265 20 close aa bb = all id (map2 do a b: (abs (a - b) < 0.00001) done aa bb);
Chris@265 21
Chris@265 22 isSymmetric a =
Chris@265 23 (len = (vec.length a);
Chris@265 24 b = if len % 2 == 0 then
Chris@265 25 half = vec.slice a 0 (len/2);
Chris@265 26 vec.concat [half, vec.reversed half];
Chris@265 27 else
Chris@265 28 half = vec.slice a 0 (int (len/2));
Chris@265 29 mid = vec.slice a (int (len/2)) (int (len/2) + 1);
Chris@265 30 vec.concat [half, mid, vec.reversed half];
Chris@265 31 fi;
Chris@265 32 compareUsing close (vec.list a) (vec.list b));
Chris@265 33
Chris@265 34 [
Chris@265 35
Chris@265 36 "windowFunction": \(
Chris@265 37 all id (map do type:
Chris@265 38 f = functions[type];
Chris@265 39 a = f 10;
Chris@265 40 b = win.windowFunction type [ Symmetric false ] 10;
Chris@265 41 compareUsing close (vec.list a) (vec.list b)
Chris@265 42 or (eprintln "** failed window type: \(type)"; false)
Chris@265 43 done (keys functions));
Chris@265 44 ),
Chris@265 45
Chris@265 46 "symmetric-even": \(
Chris@265 47 len = 10;
Chris@265 48 all id (map do type:
Chris@265 49 f = win.windowFunction type [ Symmetric true ];
Chris@265 50 v = f len;
Chris@265 51 (compare (vec.length v) len and isSymmetric v) or
Chris@265 52 (eprintln "** failed window type: \(type)"; false);
Chris@265 53 done (keys functions));
Chris@265 54 ),
Chris@265 55
Chris@265 56 "symmetric-odd": \(
Chris@265 57 len = 11;
Chris@265 58 all id (map do type:
Chris@265 59 f = win.windowFunction type [ Symmetric true ];
Chris@265 60 v = f len;
Chris@265 61 (compare (vec.length v) len and isSymmetric v) or
Chris@265 62 (eprintln "** failed window type: \(type)"; false);
Chris@265 63 done (keys functions));
Chris@265 64 ),
Chris@265 65
Chris@265 66 "periodic-even": \(
Chris@265 67 // We can't actually test whether a function is periodic, given
Chris@265 68 // only one cycle of it! But we can make sure that all but the
Chris@265 69 // first sample is symmetric, which is what a symmetric window
Chris@265 70 // becomes when generated in periodic mode
Chris@265 71 len = 10;
Chris@265 72 all id (map do type:
Chris@265 73 f = win.windowFunction type [ Symmetric false ];
Chris@265 74 v = f len;
Chris@265 75 (compare (vec.length v) len and isSymmetric (vec.slice v 1 len)) or
Chris@265 76 (eprintln "** failed window type: \(type)"; false);
Chris@265 77 done (keys functions));
Chris@265 78 ),
Chris@265 79
Chris@265 80 "periodic-odd": \(
Chris@265 81 len = 11;
Chris@265 82 all id (map do type:
Chris@265 83 f = win.windowFunction type [ Symmetric false ];
Chris@265 84 v = f len;
Chris@265 85 (compare (vec.length v) len and isSymmetric (vec.slice v 1 len)) or
Chris@265 86 (eprintln "** failed window type: \(type)"; false);
Chris@265 87 done (keys functions));
Chris@265 88 ),
Chris@265 89
Chris@265 90 "bartlett-periodic": \(
Chris@265 91 compare (vec.list (win.bartlett 1)) [1] and
Chris@265 92 compare (vec.list (win.bartlett 2)) [0,1] and
Chris@265 93 compare (vec.list (win.bartlett 3)) [0,2/3,2/3] and
Chris@265 94 compare (vec.list (win.bartlett 4)) [0,1/2,1,1/2]
Chris@265 95 ),
Chris@265 96
Chris@265 97 "bartlett-symmetric": \(
Chris@265 98 b = win.windowFunction (Bartlett ()) [ Symmetric true ];
Chris@265 99 compare (vec.list (b 1)) [1] and
Chris@265 100 compare (vec.list (b 2)) [0,0] and
Chris@265 101 compare (vec.list (b 3)) [0,1,0] and
Chris@265 102 compare (vec.list (b 4)) [0,2/3,2/3,0] and
Chris@265 103 compare (vec.list (b 5)) [0,1/2,1,1/2,0]
Chris@265 104 ),
Chris@265 105
Chris@265 106 ] is hash<string, () -> boolean>;
Chris@265 107