[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").

Recommended Posts

[Python] How to FFT mp3 data
How to use "deque" for Python data
How to install Python
How to install python
[Python] How to read data from CIFAR-10 and CIFAR-100
[Introduction to Python] How to handle JSON format data
How to install Python [Windows]
python3: How to use bottle (2)
[Python] How to use list 1
How to update Python Tkinter to 8.6
How to use Python argparse
Python: How to use pydub
[Python] How to use checkio
How to run Notepad ++ Python
How to change Python version
How to develop in Python
How to handle data frames
[python] How to judge scalar
[Python] How to use input ()
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
[For beginners] How to study Python3 data analysis exam
How to scrape image data from flickr with python
How to measure mp3 file playback time with python
How to install python using anaconda
How to write a Python class
[Python] How to do PCA in Python
Python: How to use async with
How to read e-Stat subregion data
[Python] How to derive nCk (ABC156-D)
[Python] How to use Pandas Series
How to collect images in Python
How to use Requests (Python Library)
How to use SQLite in Python
[Introduction to Python] How to parse JSON
How to deal with imbalanced data
How to deal with imbalanced data
How to get the Python version
How to generate exponential pulse time series data in python
MP3 to WAV conversion with Python
How to get started with Python
[Python] How to import the library
How to convert Youtube to mp3 and download it super-safely [Python]
[Python] How to use list 3 Added
How to use Mysql in python
How to use OpenPose's Python API
How to Data Augmentation with PyTorch
[Python] How to swap array values
How to wrap C in Python
How to use ChemSpider in Python
How to use FTP with Python
Python: How to use pydub (playback)
How to use PubChem in Python
How to speed up Python calculations
How to calculate date with python
How to access wikipedia from python
How to use python zip function
[Nanonets] How to post Memo [Python]