Tuesday, May 1, 2018

Calculate the entropy of a signal emulating the classic Shannon definition.


signal.entropy <- function(x)
{ 
    ## Purpose: Calculate the entropy of a signal, emulating the classic Shannon definition. 
    ## Arguments:
    ##   x: A signal vector
    ## Return: An entropy number
    ## Author: Feiming Chen, Date:  1 May 2018, 13:07
    ## ________________________________________________

    x2 <- x^2
    tot <- sum(x2) 
    if (tot == 0) {
        H <- NA
    } else {
        p <- x2 / tot
        H <- - sum(p * log10(p))
    }
    H
}
if (F) {                                # Unit Test
    x <- rnorm(100)
    signal.entropy(x)
    signal.entropy(rep(1,100))          # 2
    signal.entropy(rep(1,10000))        # 4
}

No comments:

Post a Comment