gstlal  0.8.1
 All Classes Namespaces Files Functions Variables Pages
matrixmixer_test_01.py
1 #!/usr/bin/env python
2 # Copyright (C) 2013 Kipp Cannon
3 #
4 # This program is free software; you can redistribute it and/or modify it
5 # under the terms of the GNU General Public License as published by the
6 # Free Software Foundation; either version 2 of the License, or (at your
7 # option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
12 # Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with this program; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 
18 #
19 # =============================================================================
20 #
21 # Preamble
22 #
23 # =============================================================================
24 #
25 
26 
27 import numpy
28 import sys
29 from gstlal import pipeparts
30 import test_common
31 import cmp_nxydumps
32 
33 
34 #
35 # =============================================================================
36 #
37 # Pipelines
38 #
39 # =============================================================================
40 #
41 
42 
43 #
44 # is the matrixmixer element an identity transform when given an identity
45 # matrix?
46 #
47 
48 
49 def matrixmixer_test_01(pipeline, name, width, channels):
50  #
51  # try changing these. test should still work!
52  #
53 
54  rate = 2048 # Hz
55  gap_frequency = 13.0 # Hz
56  gap_threshold = 0.8 # of 1
57  buffer_length = 1.0 # seconds
58  test_duration = 10.0 # seconds
59 
60  #
61  # build pipeline
62  #
63 
64  assert 1 <= channels <= 2
65  head = test_common.gapped_test_src(pipeline, buffer_length = buffer_length, rate = rate, width = width, channels = channels, test_duration = test_duration, gap_frequency = gap_frequency, gap_threshold = gap_threshold, control_dump_filename = "%s_control.dump" % name)
66  head = tee = pipeparts.mktee(pipeline, head)
67 
68  head = pipeparts.mkmatrixmixer(pipeline, head, numpy.identity(channels, dtype = "double"))
69  head = pipeparts.mkchecktimestamps(pipeline, head)
70  pipeparts.mknxydumpsink(pipeline, pipeparts.mkqueue(pipeline, head), "%s_out.dump" % name)
71  pipeparts.mknxydumpsink(pipeline, pipeparts.mkqueue(pipeline, tee), "%s_in.dump" % name)
72 
73  #
74  # done
75  #
76 
77  return pipeline
78 
79 
80 #
81 # test the transformation of a specific buffer with a specific matrix
82 #
83 
84 
85 def matrixmixer_test_02(name, dtype, samples, channels_in, channels_out):
86  numpy.random.seed(0)
87  input_array = numpy.random.random((samples, channels_in)).astype(dtype)
88  # element always ingests mix matrix as double-precision floats
89  mix = numpy.random.random((channels_in, channels_out)).astype("float64")
90  # element will cast mix matrix to the appropriate type internally
91  # for the matrix-matrix multiply
92  output_reference = numpy.mat(input_array) * mix.astype(dtype)
93 
94  output_array, = test_common.transform_arrays([input_array], pipeparts.mkmatrixmixer, name, matrix = mix)
95 
96  if (output_array != output_reference).any():
97  raise ValueError("incorrect output: expected %s, got %s\ndifference = %s" % (output_reference, output_array, output_array - output_reference))
98 
99 
100 #
101 # =============================================================================
102 #
103 # Main
104 #
105 # =============================================================================
106 #
107 
108 
109 test_common.build_and_run(matrixmixer_test_01, "matrixmixer_test_01a", width = 64, channels = 1)
110 cmp_nxydumps.compare("matrixmixer_test_01a_in.dump", "matrixmixer_test_01a_out.dump", flags = cmp_nxydumps.COMPARE_FLAGS_EXACT_GAPS)
111 test_common.build_and_run(matrixmixer_test_01, "matrixmixer_test_01b", width = 64, channels = 2)
112 cmp_nxydumps.compare("matrixmixer_test_01b_in.dump", "matrixmixer_test_01b_out.dump", flags = cmp_nxydumps.COMPARE_FLAGS_EXACT_GAPS)
113 test_common.build_and_run(matrixmixer_test_01, "matrixmixer_test_01c", width = 32, channels = 1)
114 cmp_nxydumps.compare("matrixmixer_test_01c_in.dump", "matrixmixer_test_01c_out.dump", flags = cmp_nxydumps.COMPARE_FLAGS_EXACT_GAPS)
115 test_common.build_and_run(matrixmixer_test_01, "matrixmixer_test_01d", width = 32, channels = 2)
116 cmp_nxydumps.compare("matrixmixer_test_01d_in.dump", "matrixmixer_test_01d_out.dump", flags = cmp_nxydumps.COMPARE_FLAGS_EXACT_GAPS)
117 
118 
119 matrixmixer_test_02("matrixmixer_test_02a", "float64", samples = 6, channels_in = 4, channels_out = 3)
120 matrixmixer_test_02("matrixmixer_test_02b", "float32", samples = 6, channels_in = 4, channels_out = 3)