Skip to content

Instantly share code, notes, and snippets.

@nicovillanueva
Last active May 23, 2016 05:46
Show Gist options
  • Save nicovillanueva/56d48b527b250c5f6f9c472eb41ed6dc to your computer and use it in GitHub Desktop.
Save nicovillanueva/56d48b527b250c5f6f9c472eb41ed6dc to your computer and use it in GitHub Desktop.
Audio analysis
#!/bin/bash
wget -O stereo_2205k_16bit.wav http://download.wavetlan.com/SVV/Media/HTTP/test_stereo_22050Hz_16bit_PCM.wav
wget -O mono_441k_8bit.wav http://download.wavetlan.com/SVV/Media/HTTP/test_mono_44100Hz_8bit_PCM.wav
#!/usr/bin/python3
# Get the test files (run the 'getfiles.sh') and
# install scipy: "pip install scipy"
# (or "pip install -r requirements.txt")
# -----
# Based on:
# http://stackoverflow.com/questions/10222812/python-numpy-fft-and-inverse-fft
# http://stackoverflow.com/questions/23377665/python-scipy-fft-wav-files
from scipy.fftpack import rfft, irfft
from scipy.io import wavfile
import numpy
target = 'mono_441k_8bit.wav'
#target = 'stereo_2205k_16bit.wav'
fs, data = wavfile.read(target)
print('Analysing: {}'.format(target))
print("Frame rate: {}/sec".format(fs))
print("Bitrate: {}".format(data.T.dtype.name))
if len(data.T) == 2:
print("Original data (left channel): ", data.T[0])
print("Original data (right channel): ", data.T[1])
else:
print("Original data: {}".format(data.T))
rf = rfft(data.T, axis=0)
print("FFT:")
print(rf)
irf = irfft(rf, axis=0)
irf = numpy.round(irf).astype('int16')
print("IFFT:")
print(irf)
newname = target.split('.')[0] + '_post.wav'
print("Saving IFFT to {}".format(newname))
wavfile.write(newname, fs, irf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment