Gary Sieling

Apply FFT to a list of wav files with Python

import pickle
import pandas

from os import listdir
from os.path import isfile, join
import os

import scipy.io.wavfile as wavfile
import scipy
import scipy.fftpack

import numpy as np

def process(fileWav):
  signal = wavfile.read(fileWav)[1]

  t = scipy.linspace(0,120,4000)

  FFT = abs(scipy.fft(signal))
  freqs = scipy.fftpack.fftfreq(signal.size, t[1]-t[0])

  return np.array(freqs)

def all_files(mypath):
  return [f for f in listdir(mypath) if isfile(join(mypath, f))]

def get_and_save_data(path, file): 
  print("Processing... " + file)
  data = [process(path + "\\" + file)]
  towrite = "ffts\\" + file + ".pickled"
  print(towrite)

  pickle.dump(data, open(towrite, "wb"))

def process_all_files(path):
  print("Loading directory " + path) 
  found = [file for file in all_files(path)]

  random.shuffle(found)

  for file in found:
    if not isfile("ffts\\" + file + ".pickled"):
      print("Starting a thread for " + file)
      get_and_save_data(path, file)

process_all_files("samples")
Exit mobile version