[Scipy-svn] r5448 - trunk/scipy/fftpack/tests

scipy-svn@scip... scipy-svn@scip...
Mon Jan 12 20:00:46 CST 2009


Author: cdavid
Date: 2009-01-12 20:00:41 -0600 (Mon, 12 Jan 2009)
New Revision: 5448

Added:
   trunk/scipy/fftpack/tests/gendata.m
   trunk/scipy/fftpack/tests/test_real_transforms.py
Log:
Start test for DCT.

Added: trunk/scipy/fftpack/tests/gendata.m
===================================================================
--- trunk/scipy/fftpack/tests/gendata.m	2009-01-12 22:28:15 UTC (rev 5447)
+++ trunk/scipy/fftpack/tests/gendata.m	2009-01-13 02:00:41 UTC (rev 5448)
@@ -0,0 +1,21 @@
+x0 = linspace(0, 10, 11);
+x1 = linspace(0, 10, 15);
+x2 = linspace(0, 10, 16);
+x3 = linspace(0, 10, 17);
+
+x4 = randn(32, 1);
+x5 = randn(64, 1);
+x6 = randn(128, 1);
+x7 = randn(256, 1);
+
+y0 = dct(x0);
+y1 = dct(x1);
+y2 = dct(x2);
+y3 = dct(x3);
+y4 = dct(x4);
+y5 = dct(x5);
+y6 = dct(x6);
+y7 = dct(x7);
+
+save('test.mat', 'x0', 'x1', 'x2', 'x3', 'x4', 'x5', 'x6', 'x7', ...
+                 'y0', 'y1', 'y2', 'y3', 'y4', 'y5', 'y6', 'y7');

Added: trunk/scipy/fftpack/tests/test_real_transforms.py
===================================================================
--- trunk/scipy/fftpack/tests/test_real_transforms.py	2009-01-12 22:28:15 UTC (rev 5447)
+++ trunk/scipy/fftpack/tests/test_real_transforms.py	2009-01-13 02:00:41 UTC (rev 5448)
@@ -0,0 +1,42 @@
+#!/usr/bin/env python
+from os.path import join, dirname
+
+import numpy as np
+from numpy.fft import fft as numfft
+from numpy.testing import assert_array_almost_equal
+
+from scipy.io import loadmat
+
+TDATA = loadmat(join(dirname(__file__), 'test.mat'), 
+                squeeze_me=True,  struct_as_record=True)
+X = [TDATA['x%d' % i] for i in range(8)]
+Y = [TDATA['y%d' % i] for i in range(8)]
+
+def direct_dct(x):
+    """Compute a Discrete Cosine Transform, type II.
+
+    The DCT type II is defined as:
+
+        \forall u \in 0...N-1, 
+        dct(u) = a(u) sum_{i=0}^{N-1}{f(i)cos((i + 0.5)\pi u}
+
+    Where a(0) = sqrt(1/(4N)), a(u) = sqrt(1/(2N)) for u > 0
+    """
+    x = np.asarray(x)
+    if not np.isrealobj(x):
+        raise ValueError("Complex input not supported")
+    n = x.size
+    y = np.zeros(n * 4, x.dtype)
+    y[1:2*n:2] = x
+    y[2*n+1::2] = x[-1::-1]
+    y = np.real(numfft(y))[:n]
+    y[0] *= np.sqrt(.25 / n)
+    y[1:] *= np.sqrt(.5 / n)
+    return y
+
+def test_ref():
+    for i in range(len(X)):
+        assert_array_almost_equal(direct_dct(X[i]), Y[i])
+
+if __name__ == "__main__":
+    np.testing.run_module_suite()



More information about the Scipy-svn mailing list