od_covmat.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*Daala video codec
  2. Copyright (c) 2013 Daala project contributors. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. - Redistributions of source code must retain the above copyright notice, this
  6. list of conditions and the following disclaimer.
  7. - Redistributions in binary form must reproduce the above copyright notice,
  8. this list of conditions and the following disclaimer in the documentation
  9. and/or other materials provided with the distribution.
  10. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
  11. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  12. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  13. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  14. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  15. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  16. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  17. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  18. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  19. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include <math.h>
  24. #include <stdlib.h>
  25. #include "od_defs.h"
  26. #include "od_covmat.h"
  27. #define PRINT_COLLAPSE (0)
  28. void od_covmat_init(od_covmat *_this,int _sz){
  29. _this->sz=_sz;
  30. _this->mean=(double *)malloc(sizeof(*_this->mean)*_this->sz);
  31. _this->cov=(double *)malloc(sizeof(*_this->cov)*_this->sz*_this->sz);
  32. _this->work=(double *)malloc(sizeof(*_this->work)*_this->sz);
  33. od_covmat_reset(_this);
  34. }
  35. void od_covmat_clear(od_covmat *_this){
  36. free(_this->mean);
  37. free(_this->cov);
  38. free(_this->work);
  39. }
  40. void od_covmat_reset(od_covmat *_this){
  41. int j;
  42. int i;
  43. _this->w=0;
  44. for(j=0;j<_this->sz;j++){
  45. _this->mean[j]=0;
  46. for(i=0;i<_this->sz;i++){
  47. _this->cov[_this->sz*j+i]=0;
  48. }
  49. }
  50. }
  51. void cov_update(double *_mean,double *_cov,double *_work,double *_weight,int _n,
  52. const double *_data,double _w){
  53. double s;
  54. int j;
  55. int i;
  56. if(_w==0){
  57. return;
  58. }
  59. (*_weight)+=_w;
  60. s=_w/(*_weight);
  61. for(i=0;i<_n;i++){
  62. _work[i]=_data[i]-_mean[i];
  63. #if FAST_MATH
  64. _mean[i]+=_work[i]*s;
  65. #else
  66. _mean[i]+=_work[i]*_w/(*_weight);
  67. #endif
  68. }
  69. s*=(*_weight)-_w;
  70. for(j=0;j<_n;j++){
  71. for(i=0;i<_n;i++){
  72. #if FAST_MATH
  73. _cov[_n*j+i]+=_work[j]*_work[i]*s;
  74. #else
  75. _cov[_n*j+i]+=_work[j]*_work[i]*((*_weight)-_w)/(*_weight);
  76. #endif
  77. }
  78. }
  79. }
  80. void od_covmat_add(od_covmat *_this,const double *_data,double _w){
  81. /* why does this cause performance penalty?? 15.8% at -03 and 8.7% at -O2 */
  82. #if 0
  83. cov_update(_this->mean,_this->cov,_this->work,&_this->w,_this->sz,_data,_w);
  84. #else
  85. double s;
  86. int i;
  87. int j;
  88. if(_w==0){
  89. return;
  90. }
  91. _this->w+=_w;
  92. s=_w/_this->w;
  93. for(i=0;i<_this->sz;i++){
  94. _this->work[i]=_data[i]-_this->mean[i];
  95. #if FAST_MATH
  96. _this->mean[i]+=_this->work[i]*s;
  97. #else
  98. _this->mean[i]+=_this->work[i]*_w/_this->w;
  99. #endif
  100. }
  101. s*=_this->w-_w;
  102. for(j=0;j<_this->sz;j++){
  103. for(i=0;i<_this->sz;i++){
  104. #if FAST_MATH
  105. _this->cov[_this->sz*j+i]+=_this->work[j]*_this->work[i]*s;
  106. #else
  107. _this->cov[_this->sz*j+i]+=
  108. _this->work[j]*_this->work[i]*(_this->w-_w)/_this->w;
  109. #endif
  110. }
  111. }
  112. #endif
  113. }
  114. void od_covmat_combine(od_covmat *_a,const od_covmat *_b){
  115. od_covmat_update(_a,_b->cov,_b->mean,_b->w);
  116. }
  117. void od_covmat_update(od_covmat *_this,const double *_cov,const double *_mean,
  118. double _w){
  119. double s;
  120. int i;
  121. int j;
  122. if(_w==0){
  123. return;
  124. }
  125. s=_w/(_this->w+_w);
  126. for(i=0;i<_this->sz;i++){
  127. _this->work[i]=_mean[i]-_this->mean[i];
  128. #if FAST_MATH
  129. _this->mean[i]+=_this->work[i]*s;
  130. #else
  131. _this->mean[i]+=_this->work[i]*_w/(_this->w+_w);
  132. #endif
  133. }
  134. s*=_this->w;
  135. for(i=0;i<_this->sz;i++){
  136. for(j=0;j<_this->sz;j++){
  137. #if FAST_MATH
  138. _this->cov[_this->sz*i+j]+=_cov[_this->sz*i+j]+
  139. _this->work[i]*_this->work[j]*s;
  140. #else
  141. _this->cov[_this->sz*i+j]+=_cov[_this->sz*i+j]+
  142. _this->work[i]*_this->work[j]*_this->w*_w/(_this->w+_w);
  143. #endif
  144. }
  145. }
  146. _this->w+=_w;
  147. }
  148. void od_covmat_correct(od_covmat *_this){
  149. double s;
  150. int j;
  151. int i;
  152. s=1/_this->w;
  153. for(j=0;j<_this->sz;j++){
  154. for(i=0;i<_this->sz;i++){
  155. #if FAST_MATH
  156. _this->cov[_this->sz*j+i]*=s;
  157. #else
  158. _this->cov[_this->sz*j+i]/=_this->w;
  159. #endif
  160. }
  161. }
  162. }
  163. void od_covmat_normalize(od_covmat *_this){
  164. int i;
  165. int j;
  166. for(i=0;i<_this->sz;i++){
  167. _this->work[i]=sqrt(_this->cov[_this->sz*i+i]);
  168. }
  169. for(j=0;j<_this->sz;j++){
  170. for(i=0;i<_this->sz;i++){
  171. _this->cov[_this->sz*j+i]/=_this->work[j]*_this->work[i];
  172. }
  173. }
  174. }
  175. static void covariance_collapse(const double *_cov,int _sz,int _n,double *_r,
  176. double *_work){
  177. int m;
  178. int i;
  179. int j;
  180. int u;
  181. int v;
  182. m=_sz/_n;
  183. for(i=0;i<_sz;i++){
  184. _r[i]=0;
  185. _work[i]=0;
  186. }
  187. for(v=0;v<_n;v++){
  188. for(u=v;u<_n;u++){
  189. for(j=0;j<m;j++){
  190. for(i=j;i<m;i++){
  191. _r[(u-v)*m+(i-j)]+=_cov[(v*m+j)*_sz+(u*m+i)];
  192. _r[(u-v)*m+(i-j)]+=_cov[(v*m+i)*_sz+(u*m+j)];
  193. _r[(u-v)*m+(i-j)]+=_cov[(u*m+j)*_sz+(v*m+i)];
  194. _r[(u-v)*m+(i-j)]+=_cov[(u*m+i)*_sz+(v*m+j)];
  195. _work[(u-v)*m+(i-j)]+=4;
  196. }
  197. }
  198. }
  199. }
  200. for(i=0;i<_sz;i++){
  201. _r[i]/=_work[i];
  202. }
  203. }
  204. void od_covmat_collapse(od_covmat *_this,int _n,double *_r){
  205. covariance_collapse(_this->cov,_this->sz,_n,_r,_this->work);
  206. #if PRINT_COLLAPSE
  207. for(i=0;i<_this->sz;i++){
  208. fprintf(stderr,"%s %- 24.18G",i>0?",":"",_r[i]);
  209. }
  210. fprintf(stderr,"\n");
  211. #endif
  212. }
  213. static void covariance_expand(double *_cov,int _sz,int _n,const double *_r){
  214. int i;
  215. int j;
  216. int u;
  217. int v;
  218. int m;
  219. m=_sz/_n;
  220. for(v=0;v<_n;v++){
  221. for(u=0;u<_n;u++){
  222. for(j=0;j<m;j++){
  223. for(i=0;i<m;i++){
  224. _cov[(v*m+j)*_sz+u*m+i]=_r[abs(v-u)*m+abs(i-j)];
  225. }
  226. }
  227. }
  228. }
  229. }
  230. void od_covmat_expand(od_covmat *_this,int _n,const double *_r){
  231. covariance_expand(_this->cov,_this->sz,_n,_r);
  232. }
  233. void od_covmat_print(od_covmat *_this,FILE *_fp){
  234. int i;
  235. int j;
  236. for(i=0;i<_this->sz;i++){
  237. fprintf(_fp,"%s %- 24.18G",i>0?",":"",_this->mean[i]);
  238. }
  239. fprintf(_fp,"\n");
  240. for(j=0;j<_this->sz;j++){
  241. for(i=0;i<_this->sz;i++){
  242. fprintf(_fp,"%s %- 24.18G",i>0?",":"",_this->cov[_this->sz*j+i]);
  243. }
  244. fprintf(_fp,"\n");
  245. }
  246. }