Skip to content

Instantly share code, notes, and snippets.

@rxa254
Created February 4, 2022 06:24
Show Gist options
  • Save rxa254/5cf7a302c1110e966b3d64d3ae25cc41 to your computer and use it in GitHub Desktop.
Save rxa254/5cf7a302c1110e966b3d64d3ae25cc41 to your computer and use it in GitHub Desktop.
How to plot the cumulative RMS of a power spectral density (or time series)

Sometime you want to know the RMS of only some port of your signal or some part of your power spectral density.

In that case what you may is to start from the highest frequency bin, and add the power in each bin as you go. T his quantity is then the cumulative RMS of the spectrum. At each frequency point, f, the cumulative RMS is the RMS of the signal from the highest frequency to f.

def myrms(f, y_of_f):
    df = np.diff(f)
    df = np.append(df[0],df)
    #print(y_of_f.shape)
    y = df * (y_of_f)**2 #normalize the PSD
    y = np.cumsum(np.flipud(y)) # integrate the PSD from high frequency down to low freq to get the mean-squared
    rms = np.flipud(np.sqrt(y)) # flip it back to plot the RMS
    
    return rms.flatten()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment