Search code examples
rplotspectral

How to create a sensible Hilbert Spectrum plot with R (computing environment)


I am using the EMD package for R. This package has a spectrogram function for displaying a Hilbert Spectrum (calculated with hilbertspec). The output however, is really vague and black-white.

This function does not seem to have an option for outputting color images. How can I get the spectrum displayed clearly and if possible in color.


Solution

  • The gray levels are hardcoded in the spectrogram function (gray()), to override this setting you could use, for instance, the following:

    # define a color palette
    colors <- colorRampPalette(c("#007FFF", "blue", "#000077"))  
    gray <- function(x) colors(255*x)  # redefine gray palette
    spectrogram(test1$amplitude[,1], test1$instantfreq[,1])
    gray <- grDevices::gray  # reset gray palette function
    

    Another option is to use the source of the spectrogram function to define your own plot function which has an argument for the color palette.