<jupyter_start><jupyter_text>[&larr; Back to Index](index.html)  Segmentation In audio processing, it is common to operate on one frame at a time using a constant frame size and hop size (i.e. increment). Frames are typically chosen to be 10 to 100 ms in duration. Let's create an audio signal consisting of a pure tone that gradually gets louder. Then, we will segment the signal and compute the **root mean square (RMS) energy** for each frame.First, set our parameters:<jupyter_code>T = 3.0      # duration in seconds
sr = 22050   # sampling rate in Hertz
amplitude = numpy.logspace(-3, 0, int(T*sr), endpoint=False, base=10.0) # time-varying amplitude
print(amplitude.min(), amplitude.max()) # starts at 110 Hz, ends at 880 Hz<jupyter_output>0.001 0.999895579824<jupyter_text>Create the signal:<jupyter_code>t = numpy.linspace(0, T, int(T*sr), endpoint=False)
x = amplitude*numpy.sin(2*numpy.pi*440*t)<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>Segmentation Using Python List Comprehensions In Python, you can use a standard [list comprehension](https://docs.python.org/2/tutorial/datastructures.htmllist-comprehensions) to perform segmentation of a signal and compute RMSE at the same time. Initialize segmentation parameters:<jupyter_code>frame_length = 1024
hop_length = 512<jupyter_output><empty_output><jupyter_text>Define a helper function:<jupyter_code>def rmse(x):
    return numpy.sqrt(numpy.mean(x**2))<jupyter_output><empty_output><jupyter_text>Using a list comprehension, plot the RMSE for each frame on a log-y axis:<jupyter_code>plt.semilogy([rmse(x[i:i+frame_length])
              for i in range(0, len(x), hop_length)])<jupyter_output><empty_output><jupyter_text>`librosa.util.frame` Given a signal, [`librosa.util.frame`](https://librosa.github.io/librosa/generated/librosa.util.frame.htmllibrosa.util.frame) will produce a list of uniformly sized frames:<jupyter_code>frames = librosa.util.frame(x, frame_length=frame_length, hop_length=hop_length)
plt.semilogy([rmse(frame) for frame in frames.T])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11243913235945109, 'fraction_numerical': 0.02434705621956618, 'mean_word_length': 6.793103448275862, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, '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': '55734', 'license': 'MIT', 'n_tokens_mistral': 834, 'n_tokens_neox': 763, 'n_words': 196, 'path': 'segmentation.ipynb', 'repo_name': 'asantos-6/musicinformationretrieval.com', 'size': '2259'}