Home

Performing a Fast Fourier Transform (FFT) on a Sound File

|
|  Updated:  
2016-03-26 07:41:05
|   From The Book:  
No items found.
Python Essentials For Dummies
Explore Book
Buy On Amazon

Data analysis takes many forms. Sometimes, you need to look for patterns in data in a manner that you might not have initially considered. One common way to perform such an analysis is to use a Fast Fourier Transform (FFT) to convert the sound from the frequency domain to the time domain. Doing this lets you plot the sound in a new way.

For example, think about a mechanic who takes a sound sample of an engine and then relies on a machine to analyze that sample, looking for potential engine problems. The diagnostic can find some problems and visual inspection can find others, but sometimes the sound of an engine reveals issues that you can’t find in any other way.

Here’s the code you use to perform an FFT:

import matplotlib.pyplot as plt
from scipy.io import wavfile as wav
from scipy.fftpack import fft
import numpy as np
rate, data = wav.read('bells.wav')
fft_out = fft(data)
%matplotlib inline
plt.plot(data, np.abs(fft_out))
plt.show()

In this case, you begin by reading in the sound file and extracting the data from it. The rate information isn’t important because you don’t need to know how fast to play the data, you simply need to know what values the sound contains. The sound values consist of frequency (the tone of the sound) and amplitude (how loud to play it).

The next step is to perform the FFT by calling fft() with data. This particular analysis is a simplification of a much larger process. The point is that the output displays the strongest detected frequencies over time.

image0.jpg

About This Article

This article is from the book: 

No items found.

About the book author:

John Paul Mueller is a freelance author and technical editor. He has writing in his blood, having produced 100 books and more than 600 articles to date. The topics range from networking to home security and from database management to heads-down programming. John has provided technical services to both Data Based Advisor and Coast Compute magazines.

Luca Massaron is a data scientist specialized in organizing and interpreting big data and transforming it into smart data by means of the simplest and most effective data mining and machine learning techniques. Because of his job as a quantitative marketing consultant and marketing researcher, he has been involved in quantitative data since 2000 with different clients and in various industries, and is one of the top 10 Kaggle data scientists.