Let's take a look at how to read an audio file and visualize the signal. This will be a good starting point, and it will give us a good understanding about the basic structure of audio signals. Before we start, we need to understand that audio files are digitized versions of actual audio signals. Actual audio signals are complex continuous-valued waves. In order to save a digital version, we sample the signal and convert it into numbers. For example, speech is commonly sampled at 44100 Hz. This means that each second of the signal is broken down into 44100 parts, and the values at these timestamps are stored. In other words, you store a value every 1/44100 seconds. As the sampling rate is high, we feel that the signal is continuous when we listen to it on our media players.
import numpy as np import matplotlib.pyplot as plt from scipy.io import wavfile
wavfile
package to read the audio file from the input_read.wav
input file that is already provided to you:# Read the input file sampling_freq, audio = wavfile.read('input_read.wav')
# Print the params print ' Shape:', audio.shape print 'Datatype:', audio.dtype print 'Duration:', round(audio.shape[0] / float(sampling_freq), 3), 'seconds'
# Normalize the values audio = audio / (2.**15)
# Extract first 30 values for plotting audio = audio[:30]
# Build the time axis x_values = np.arange(0, len(audio), 1) / float(sampling_freq)
# Convert to seconds x_values *= 1000
# Plotting the chopped audio signal plt.plot(x_values, audio, color='black') plt.xlabel('Time (ms)') plt.ylabel('Amplitude') plt.title('Audio signal') plt.show()
read_plot.py
file. If you run this code, you will see the following signal:18.226.98.166