# Determining the Amplitude of a .wav File in Python

Audio processing has become an essential part of many fields. From virtual assistants like Siri, Google Home, and Alexa to noise-cancelling headphones and machine learning models, audio processing is everywhere. Aduio processing enhances the audio signal and sound wave quality, helping us analyze their amplitude, frequency, and wavelength.

Python has made audio processing a lot easier. Using the Scipy module, we can easily analyze audio waves and extract useful information from them. In this article, we’ll find the amplitude of a .wav file using Python.

We’ll explore the concept of amplitude and discuss how to extract and analyze it using Scipy. So, let’s get started and dive into the world of audio processing with Python!

## Parts Of A Sound Wave

In this section, we will discuss the pitch, frequency, wavelength, and amplitude of a sound wave. These components help users understand the characteristics of a sound wave and how it propagates in a medium.

A sound wave or a audio wave is the way which sound is propagated in a medium, mostly air. A sound wave which is a longitudinal wave that comprises of rarefaction and compression, in which the particles of the medium vibrate in the same direction. Rarefaction is the area where the volume of the particles of the medium increase and they are momentarily apart whereas when the particles are compressed the particles are closer and less in volume.

There are four basic components to a sound wave:

• The pitch: The pitch is the vibrations that are emitted from a sound wave. It is the way in which our ears interpret the frequency of a sound wave.
• The frequency: It is the number of oscillations of a wave per unit of time.
• The wavelength: It is the horizontal distance between two successive equivalent points in a wave.
• The amplitude: It is the vertical distance or the height of the wave of the sound. During loud sounds, amplitude is high and waves are large. The opposite happens when there is a low sound.

## Understanding .WAV Files and Their Amplitude

A .wav file is a audio storage format bitwise. It stores audio in an uncompressed manner. It is developed by IBM and Microsoft and is the default file format for storing audio.

We can determine the amplitude of a .wav file in python using the scipy.io module but it is just a bunch of numbers in a ndarray in the form of data and channels. Hence, we can use the matplotlib library to plot it and get a better picture for analyzing and studying sound waves.

Suggested: How to Extract Audio from Video files (Mp3 From Mp4).

## Using Scipy and Matplotlib to Analyze Amplitude of Audio Waves

To determine the amplitude of a .wav file in Python, you can use the Scipy library’s scipy.io.wavfile.read() function to read the audio file and obtain its sample rate and data. Then, use Matplotlib to plot the amplitude against time for better visualization and analysis of the audio signal.

The scipy library consists of the scipy.io.wavfile.read(name_of_file) function that reads a .wav file and returns the required file’s sample rate.

The syntax of the function is:

`scipy.io.wavfile.read(file_name`)

It returns the sample rate of the audio and also data that contains the amplitude, the channels and various other stuff that isn’t that important in this article.

We will use this function to determine the amplitude of the audio file and plot it using matplotlib for better visualization.

## Implementing the Code

First and foremost, you will need to have the bunch of the following libraries in your system for audio analysis. If not, install them by running the following in your command prompt:

```pip install scipy
pip install numpy
pip install matplotlib
```

I have also used a 3 second starwars audio sample. You can use whatever audio you want or visit this website for audio samples in .wav format.

Now let’s look at how at our code to analyze and plot amplitudes using scipy and matplotlib.

```#importing required modules
from scipy.io import wavfile
import matplotlib.pyplot as plt
import numpy as np
#reading the sample audio rate and the data
print(f"number of channels = {data}")
length = data.shape[0] / rate
print(f"length = {length}s")
#plotting the audio time and amplitude
time = np.linspace(0., length, data.shape[0])
plt.plot(time, data, label="Audio")
plt.legend()
plt.xlabel("Time [s]")
plt.ylabel("Amplitude")
plt.show()
```

The output is:

```number of channels = [    0     0     0 ... -1502    26   414]
length = 3.0s
```

The plotting would be something like this:

The above amplitude plot shows the variation of the audio signal’s amplitude with respect to time. Higher amplitudes indicate louder sounds, while lower amplitudes indicate softer sounds. This visualization helps us understand the audio signal’s characteristics and can be useful for various audio processing tasks.

Related: How to Make Beep Noise in Python.

## Conclusion

In this article we have tried to come up with a solution for analyzing audio samples for sound processing purposes in python. A .wav file is a particular format in which sound is stored and it can be analyzed using the scipy library. We can also use the matplotlib library in addition to it for plotting purposes otherwise it is just an ndarray with numerical values which are hard to interpret.