<jupyter_start><jupyter_text>[&larr; Back to Index](index.html)  Energy and RMSE The **energy** ([Wikipedia](https://en.wikipedia.org/wiki/Energy_(signal_processing%29); FMP, p. 66) of a signal corresponds to the total magntiude of the signal. For audio signals, that roughly corresponds to how loud the signal is. The energy in a signal is defined as$$ \sum_n \left| x(n) \right|^2 $$ The **root-mean-square energy (RMSE)** in a signal is defined as$$ \sqrt{ \frac{1}{N} \sum_n \left| x(n) \right|^2 } $$ Let's load a signal:<jupyter_code>x, sr = librosa.load('audio/simple_loop.wav')
sr
x.shape
librosa.get_duration(x, sr)<jupyter_output><empty_output><jupyter_text>Listen to the signal:<jupyter_code>ipd.Audio(x, rate=sr)<jupyter_output><empty_output><jupyter_text>Plot the signal:<jupyter_code>librosa.display.waveplot(x, sr=sr)<jupyter_output><empty_output><jupyter_text>Compute the short-time energy using a list comprehension:<jupyter_code>hop_length = 256
frame_length = 512
energy = numpy.array([
    sum(abs(x[i:i+frame_length]**2))
    for i in range(0, len(x), hop_length)
])
energy.shape<jupyter_output><empty_output><jupyter_text>Compute the RMSE using [`librosa.feature.rmse`](https://librosa.github.io/librosa/generated/librosa.feature.rmse.html):<jupyter_code>rmse = librosa.feature.rmse(x, frame_length=frame_length, hop_length=hop_length, center=True)
rmse.shape
rmse = rmse[0]<jupyter_output><empty_output><jupyter_text>Plot both the energy and RMSE along with the waveform:<jupyter_code>frames = range(len(energy))
t = librosa.frames_to_time(frames, sr=sr, hop_length=hop_length)
librosa.display.waveplot(x, sr=sr, alpha=0.4)
plt.plot(t, energy/energy.max(), 'r--')             # normalized for visualization
plt.plot(t[:len(rmse)], rmse/rmse.max(), color='g') # normalized for visualization
plt.legend(('Energy', 'RMSE'))<jupyter_output><empty_output><jupyter_text>Questions Write a function, `strip`, that removes leading silence from a signal. Make sure it works for a variety of signals recorded in different environments and with different signal-to-noise ratios (SNR).<jupyter_code>def strip(x, frame_length, hop_length):

    # Compute RMSE.
    rmse = librosa.feature.rmse(x, frame_length=frame_length, hop_length=hop_length, center=True)
    
    # Identify the first frame index where RMSE exceeds a threshold.
    thresh = 0.01
    frame_index = 0
    while rmse[0][frame_index] < thresh:
        frame_index += 1
        
    # Convert units of frames to samples.
    start_sample_index = librosa.frames_to_samples(frame_index, hop_length=hop_length)
    
    # Return the trimmed signal.
    return x[start_sample_index:]<jupyter_output><empty_output><jupyter_text>Let's see if it works.<jupyter_code>y = strip(x, frame_length, hop_length)
ipd.Audio(y, rate=sr)
librosa.display.waveplot(y, sr=sr)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12797202797202797, 'fraction_numerical': 0.008391608391608392, 'mean_word_length': 5.860911270983213, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 33, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '43461', 'license': 'MIT', 'n_tokens_mistral': 1061, 'n_tokens_neox': 1007, 'n_words': 228, 'path': 'energy.ipynb', 'repo_name': 'mcferrenm/musicinformationretrieval.com', 'size': '2860'}