Python Tutorial : Visualizing sound waves

Learn to convert 48 kHz audio files into arrays and plot them with Matplotlib. The tutorial shows how 'Good Morning' and 'Good Afternoon' waveforms match on the shared word 'good' but diverge on 'morning' and 'afternoon', giving visual intuition for speech processing.
Chapters
It took a few conversion steps, but now you've seen what it takes to transform an audio file into numbers. Because of your efforts, we'll now be able to visualize our Good Morning sound wave using the plotting library matplotlib. To add to the visualization we're creating, we'll bring in another sound wave. Good Afternoon. This will highlight the difference between two similar sound waves and set up the intuition for the rest of the course. Both the Good Morning and Good Afternoon audio files are 48 kilohertz or 48,000 frames per second. You'll see in future lessons having your audio files at the same frame rate and ensuring the same data transformations are made on each of them is important. This is because if they're different, we've got the potential for data mismatches, which will prevent us from further processing. In the previous lesson, we used the frame rate to calculate the timestamps of where each piece of audio information appears behind the scenes. We've done the same calculations with the Good Afternoon audio file as the Good Morning file. Now we've got both soundwave arrays and timestamps ready. We can plot them. To set up a plot, we first import the matplotlib pyplot module under the common alias plt. We can then start creating a plot by calling title on PLT and passing it a string. The string will be the title of our plot. Then we can add some labels for the X and Y axises using the X label and Y label methods. The X axis will be the timestamps we've calculated, measured in seconds, and the Y axis is the amplitude or how much the sound wave displaces air particles as it moves through the air. A value of 0 indicates no sound at all. Now we've got the plot label set up, we can add both of the sound weights. We'll make sure each of them has a label so we can differentiate them using a legend. And to make sure we can highlight the differences, we'll add an alpha parameter of 0.5 to the good Morning values. This is so they appear slightly transparent on the plot. Since we've given our data labels, we can create a legend by calling the legend method on our plot. And now everything is set up. We can see our plot by calling the show method. Let's see it. Whoa, that looks nice. In the beginning, you can see how the two sound waves are similar, where the word good would be. But then they begin to differ as morning and afternoon get uttered. These differences are what we'll be working with throughout the rest of the course as we convert sound wave integers to words.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







