# [Python] How to FFT mp3 data

# 0. Overview

How to FFT mp3 data using Python. Since pydub and ffmpeg are used as libraries, please download them in advance.

# 1. Download library

```
sudo apt install ffmpeg
pip install pydub
```

# 2. Code

`FFT.py`

```
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from pydub import AudioSegment as AS
import ffmpeg
import numpy as np
import matplotlib.pyplot as plt
file = "Enter the file name"
sound = AS.from_file(file + ".mp3", "mp3")
data = np.array(sound.get_array_of_samples())
spec = np.fft.fft(data) #A two-dimensional array(Real part, imaginary part)
freq = np.fft.fftfreq(data.shape[0], 1.0/sound.frame_rate)
spec = spec[:int(spec.shape[0]/2 + 1)] #Removal of spectral elements with negative frequencies
freq = freq[:int(freq.shape[0]/2 + 1)] #Removal of frequency elements that have a negative frequency
max_spec=max(np.abs(spec)) #Get maximum sound pressure(Used to normalize sound pressure)
plt.plot(freq, np.abs(spec)/max_spec)
plt.grid()
plt.xlim([0,4000]) #Frequency range to output to the graph[Hz]
plt.xlabel("Frequency[Hz]")
plt.ylabel("Sound Pressure[-]")
#plt.yscale("log")
plt.savefig(file + ".png ") #Output as png file
```

# 3. Supplement

You can change to the log scale by uncommenting plt.yscale ("log").