Mercurial > hg > may
changeset 229:ac1373067054
Add vector max/min, matrix difference/abs, update audiofile ref tests
author | Chris Cannam |
---|---|
date | Sun, 12 May 2013 18:04:48 +0100 |
parents | 1b02d903aa79 |
children | a8c84f59f7c0 |
files | yetilab/matrix/matrix.yeti yetilab/matrix/test/test_matrix.yeti yetilab/stream/test/test_audiofile.yeti yetilab/vector/blockfuncs.yeti yetilab/vector/test/test_blockfuncs.yeti |
diffstat | 5 files changed, 97 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/yetilab/matrix/matrix.yeti Sun May 12 14:56:40 2013 +0100 +++ b/yetilab/matrix/matrix.yeti Sun May 12 18:04:48 2013 +0100 @@ -176,6 +176,16 @@ generate do row col: getAt row col m1 + getAt row col m2 done (size m1); fi; +difference m1 m2 = //!!! doc: m1 - m2, not m2 - m1 + if (size m1) != (size m2) + then failWith "Matrices are not the same size: \(size m1), \(size m2)"; + else + generate do row col: getAt row col m1 - getAt row col m2 done (size m1); + fi; + +abs' m = + generate do row col: abs (getAt row col m) done (size m); + product m1 m2 = if (size m1).columns != (size m2).rows then failWith "Matrix dimensions incompatible: \(size m1), \(size m2) (\((size m1).columns != (size m2).rows)"; @@ -307,6 +317,8 @@ asRows, asColumns, sum = sum', + difference, + abs = abs', product, concat, rowSlice, @@ -345,6 +357,8 @@ asRows is matrix -> list<vector>, asColumns is matrix -> list<vector>, sum is matrix -> matrix -> matrix, + difference is matrix -> matrix -> matrix, + abs is matrix -> matrix, product is matrix -> matrix -> matrix, concat is (Horizontal () | Vertical ()) -> list<matrix> -> matrix, rowSlice is number -> number -> matrix -> matrix,
--- a/yetilab/matrix/test/test_matrix.yeti Sun May 12 14:56:40 2013 +0100 +++ b/yetilab/matrix/test/test_matrix.yeti Sun May 12 18:04:48 2013 +0100 @@ -102,6 +102,19 @@ not mat.equal m r ), +"equalUnder-\(name)": \( + p = newMatrix (ColumnMajor ()) [[1,2,3],[4,5,6]]; + q = newMatrix (ColumnMajor ()) [[1,2,3],[4,5,6]]; + r = newMatrix (ColumnMajor ()) [[4,3,1],[3,1,2]]; + s = newMatrix (ColumnMajor ()) [[1,2,5],[6,7,8]]; + t = newMatrix (ColumnMajor ()) [[1,2,5],[6,7,9]]; + mat.equalUnder (==) p p and + mat.equalUnder (==) p q and + mat.equalUnder (!=) p r and + mat.equalUnder do a b: a % 2 == b % 2 done p s and + not mat.equalUnder do a b: a % 2 == b % 2 done p t +), + "getAt-\(name)": \( generator row col = row * 10 + col; m = generate generator { rows = 2, columns = 3 }; @@ -225,6 +238,29 @@ yrt ), +"difference-\(name)": \( + compareMatrices + (mat.difference (constMatrix 2 { rows = 3, columns = 4 }) + (constMatrix 1 { rows = 3, columns = 4 })) + (constMatrix 1 { rows = 3, columns = 4 }) +), + +"differenceFail-\(name)": \( + try + \() (mat.difference (constMatrix 2 { rows = 3, columns = 4 }) + (constMatrix 1 { rows = 3, columns = 5 })); + false; + catch FailureException e: + true + yrt +), + +"abs-\(name)": \( + compareMatrices + (mat.abs (newMatrix (ColumnMajor ()) [[-1,4],[2,-5],[-3,0]])) + (newMatrix (ColumnMajor ()) [[1,4],[2,5],[3,0]]) +), + "product-\(name)": \( compareMatrices (mat.product (constMatrix 2 { rows = 4, columns = 2 })
--- a/yetilab/stream/test/test_audiofile.yeti Sun May 12 14:56:40 2013 +0100 +++ b/yetilab/stream/test/test_audiofile.yeti Sun May 12 18:04:48 2013 +0100 @@ -4,6 +4,7 @@ af = load yetilab.stream.audiofile; vec = load yetilab.vector.vector; mat = load yetilab.matrix.matrix; +bf = load yetilab.vector.blockfuncs; ref = load yetilab.stream.test.audiofile_reference; @@ -37,10 +38,18 @@ fi; done); +maxOf m = + bf.max (vec.fromList (map bf.max (mat.asRows m))); + testReferenceFile rate channels bitdepth = (test = readAll (af.open (testfile "\(rate)-\(channels)-\(bitdepth)")); ref = readAll (ref.afReference rate channels); - compareUsing (mat.equalUnder (bitdepthComparator bitdepth)) test ref); + if mat.equalUnder (bitdepthComparator bitdepth) test ref then + true + else + println "** peak difference: \(maxOf (mat.difference ref test))"; + false + fi); [
--- a/yetilab/vector/blockfuncs.yeti Sun May 12 14:56:40 2013 +0100 +++ b/yetilab/vector/blockfuncs.yeti Sun May 12 18:04:48 2013 +0100 @@ -19,6 +19,26 @@ done; tot[0]); +max' v = + (dat = raw v; + var mx = 0; + for [0..length dat - 1] do i: + if i == 0 or dat[i] > mx then + mx := dat[i]; + fi + done; + mx); + +min' v = + (dat = raw v; + var mn = 0; + for [0..length dat - 1] do i: + if i == 0 or dat[i] < mn then + mn := dat[i]; + fi + done; + mn); + mean v = case vec.length v of 0: 0; @@ -65,6 +85,8 @@ sqr is vector -> vector, sqrt is vector -> vector = sqrt', rms is vector -> number, +max is vector -> number = max', +min is vector -> number = min', fftshift is vector -> vector, ifftshift is vector -> vector, }
--- a/yetilab/vector/test/test_blockfuncs.yeti Sun May 12 14:56:40 2013 +0100 +++ b/yetilab/vector/test/test_blockfuncs.yeti Sun May 12 18:04:48 2013 +0100 @@ -4,7 +4,7 @@ stdSqrt = sqrt; { zeros, consts, ones, fromList, list } = load yetilab.vector.vector; -{ sum, mean, multiply, divideBy, sqr, sqrt, rms, fftshift, ifftshift } = load yetilab.vector.blockfuncs; +{ sum, max, min, mean, multiply, divideBy, sqr, sqrt, rms, fftshift, ifftshift } = load yetilab.vector.blockfuncs; { compare } = load yetilab.test.test; [ @@ -16,6 +16,20 @@ compare ((sum . fromList) [1,-2,3,0]) 2 ), +"max": \( + compare ((max . fromList) [1,-2,3,0]) 3 and + compare ((max . fromList) [-1,-2,-3]) (-1) and + compare ((max . fromList) [4,1]) 4 and + compare ((max . fromList) []) 0 +), + +"min": \( + compare ((min . fromList) [1,-2,3,0]) (-2) and + compare ((min . fromList) [-1,-2,-3]) (-3) and + compare ((min . fromList) [4,1]) 1 and + compare ((min . fromList) []) 0 +), + "mean": \( compare ((mean . zeros) 0) 0 and compare ((mean . zeros) 5) 0 and