Mercurial > hg > segmenter-vamp-plugin
comparison armadillo-3.900.4/include/armadillo_bits/glue_cor_meat.hpp @ 49:1ec0e2823891
Switch to using subrepo copies of qm-dsp, nnls-chroma, vamp-plugin-sdk; update Armadillo version; assume build without external BLAS/LAPACK
author | Chris Cannam |
---|---|
date | Thu, 13 Jun 2013 10:25:24 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
48:69251e11a913 | 49:1ec0e2823891 |
---|---|
1 // Copyright (C) 2009-2012 NICTA (www.nicta.com.au) | |
2 // Copyright (C) 2009-2012 Conrad Sanderson | |
3 // Copyright (C) 2009-2010 Dimitrios Bouzas | |
4 // | |
5 // This Source Code Form is subject to the terms of the Mozilla Public | |
6 // License, v. 2.0. If a copy of the MPL was not distributed with this | |
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/. | |
8 | |
9 | |
10 //! \addtogroup glue_cor | |
11 //! @{ | |
12 | |
13 | |
14 | |
15 template<typename eT> | |
16 inline | |
17 void | |
18 glue_cor::direct_cor(Mat<eT>& out, const Mat<eT>& A, const Mat<eT>& B, const uword norm_type) | |
19 { | |
20 arma_extra_debug_sigprint(); | |
21 | |
22 if(A.is_empty() || B.is_empty() ) | |
23 { | |
24 out.reset(); | |
25 return; | |
26 } | |
27 | |
28 if(A.is_vec() && B.is_vec()) | |
29 { | |
30 arma_debug_check( (A.n_elem != B.n_elem), "cor(): the number of elements in the two vectors must match" ); | |
31 | |
32 const eT* A_ptr = A.memptr(); | |
33 const eT* B_ptr = B.memptr(); | |
34 | |
35 eT A_acc = eT(0); | |
36 eT B_acc = eT(0); | |
37 eT out_acc = eT(0); | |
38 | |
39 const uword N = A.n_elem; | |
40 | |
41 for(uword i=0; i<N; ++i) | |
42 { | |
43 const eT A_tmp = A_ptr[i]; | |
44 const eT B_tmp = B_ptr[i]; | |
45 | |
46 A_acc += A_tmp; | |
47 B_acc += B_tmp; | |
48 | |
49 out_acc += A_tmp * B_tmp; | |
50 } | |
51 | |
52 out_acc -= (A_acc * B_acc)/eT(N); | |
53 | |
54 const eT norm_val = (norm_type == 0) ? ( (N > 1) ? eT(N-1) : eT(1) ) : eT(N); | |
55 | |
56 out.set_size(1,1); | |
57 out[0] = out_acc/norm_val; | |
58 | |
59 const Mat<eT> stddev_A = (A.n_rows == 1) ? Mat<eT>(stddev(trans(A))) : Mat<eT>(stddev(A)); | |
60 const Mat<eT> stddev_B = (B.n_rows == 1) ? Mat<eT>(stddev(trans(B))) : Mat<eT>(stddev(B)); | |
61 | |
62 out /= stddev_A * stddev_B; | |
63 } | |
64 else | |
65 { | |
66 arma_debug_assert_mul_size(A, B, true, false, "cor()"); | |
67 | |
68 const uword N = A.n_rows; | |
69 const eT norm_val = (norm_type == 0) ? ( (N > 1) ? eT(N-1) : eT(1) ) : eT(N); | |
70 | |
71 out = trans(A) * B; | |
72 out -= (trans(sum(A)) * sum(B))/eT(N); | |
73 out /= norm_val; | |
74 out /= trans(stddev(A)) * stddev(B); | |
75 } | |
76 } | |
77 | |
78 | |
79 | |
80 template<typename T> | |
81 inline | |
82 void | |
83 glue_cor::direct_cor(Mat< std::complex<T> >& out, const Mat< std::complex<T> >& A, const Mat< std::complex<T> >& B, const uword norm_type) | |
84 { | |
85 arma_extra_debug_sigprint(); | |
86 | |
87 typedef typename std::complex<T> eT; | |
88 | |
89 if(A.is_empty() || B.is_empty() ) | |
90 { | |
91 out.reset(); | |
92 return; | |
93 } | |
94 | |
95 if(A.is_vec() && B.is_vec()) | |
96 { | |
97 arma_debug_check( (A.n_elem != B.n_elem), "cor(): the number of elements in the two vectors must match" ); | |
98 | |
99 const eT* A_ptr = A.memptr(); | |
100 const eT* B_ptr = B.memptr(); | |
101 | |
102 eT A_acc = eT(0); | |
103 eT B_acc = eT(0); | |
104 eT out_acc = eT(0); | |
105 | |
106 const uword N = A.n_elem; | |
107 | |
108 for(uword i=0; i<N; ++i) | |
109 { | |
110 const eT A_tmp = A_ptr[i]; | |
111 const eT B_tmp = B_ptr[i]; | |
112 | |
113 A_acc += A_tmp; | |
114 B_acc += B_tmp; | |
115 | |
116 out_acc += std::conj(A_tmp) * B_tmp; | |
117 } | |
118 | |
119 out_acc -= (std::conj(A_acc) * B_acc)/eT(N); | |
120 | |
121 const eT norm_val = (norm_type == 0) ? ( (N > 1) ? eT(N-1) : eT(1) ) : eT(N); | |
122 | |
123 out.set_size(1,1); | |
124 out[0] = out_acc/norm_val; | |
125 | |
126 const Mat<T> stddev_A = (A.n_rows == 1) ? Mat<T>(stddev(trans(A))) : Mat<T>(stddev(A)); | |
127 const Mat<T> stddev_B = (B.n_rows == 1) ? Mat<T>(stddev(trans(B))) : Mat<T>(stddev(B)); | |
128 | |
129 out /= conv_to< Mat<eT> >::from( stddev_A * stddev_B ); | |
130 } | |
131 else | |
132 { | |
133 arma_debug_assert_mul_size(A, B, true, false, "cor()"); | |
134 | |
135 const uword N = A.n_rows; | |
136 const eT norm_val = (norm_type == 0) ? ( (N > 1) ? eT(N-1) : eT(1) ) : eT(N); | |
137 | |
138 out = trans(A) * B; // out = strans(conj(A)) * B; | |
139 out -= (trans(sum(A)) * sum(B))/eT(N); // out -= (strans(conj(sum(A))) * sum(B))/eT(N); | |
140 out /= norm_val; | |
141 out /= conv_to< Mat<eT> >::from( trans(stddev(A)) * stddev(B) ); | |
142 } | |
143 } | |
144 | |
145 | |
146 | |
147 template<typename T1, typename T2> | |
148 inline | |
149 void | |
150 glue_cor::apply(Mat<typename T1::elem_type>& out, const Glue<T1,T2,glue_cor>& X) | |
151 { | |
152 arma_extra_debug_sigprint(); | |
153 | |
154 typedef typename T1::elem_type eT; | |
155 | |
156 const unwrap_check<T1> A_tmp(X.A, out); | |
157 const unwrap_check<T2> B_tmp(X.B, out); | |
158 | |
159 const Mat<eT>& A = A_tmp.M; | |
160 const Mat<eT>& B = B_tmp.M; | |
161 | |
162 const uword norm_type = X.aux_uword; | |
163 | |
164 if(&A != &B) | |
165 { | |
166 glue_cor::direct_cor(out, A, B, norm_type); | |
167 } | |
168 else | |
169 { | |
170 op_cor::direct_cor(out, A, norm_type); | |
171 } | |
172 } | |
173 | |
174 | |
175 | |
176 //! @} |