Mercurial > hg > camir-aes2014
annotate toolboxes/FullBNT-1.0.7/KPMtools/logsum_simple.m @ 0:e9a9cd732c1e tip
first hg version after svn
author | wolffd |
---|---|
date | Tue, 10 Feb 2015 15:05:51 +0000 |
parents | |
children |
rev | line source |
---|---|
wolffd@0 | 1 function result = logsum(logv) |
wolffd@0 | 2 |
wolffd@0 | 3 len = length(logv); |
wolffd@0 | 4 if (len<2); |
wolffd@0 | 5 error('Subroutine logsum cannot sum less than 2 terms.'); |
wolffd@0 | 6 end; |
wolffd@0 | 7 |
wolffd@0 | 8 % First two terms |
wolffd@0 | 9 if (logv(2)<logv(1)), |
wolffd@0 | 10 result = logv(1) + log( 1 + exp( logv(2)-logv(1) ) ); |
wolffd@0 | 11 else, |
wolffd@0 | 12 result = logv(2) + log( 1 + exp( logv(1)-logv(2) ) ); |
wolffd@0 | 13 end; |
wolffd@0 | 14 |
wolffd@0 | 15 % Remaining terms |
wolffd@0 | 16 for (i=3:len), |
wolffd@0 | 17 term = logv(i); |
wolffd@0 | 18 if (result<term), |
wolffd@0 | 19 result = term + log( 1 + exp( result-term ) ); |
wolffd@0 | 20 else, |
wolffd@0 | 21 result = result + log( 1 + exp( term-result ) ); |
wolffd@0 | 22 end; |
wolffd@0 | 23 end |