Created
March 23, 2018 14:57
-
-
Save tuttelikz/8fcc542ee220c091984438c046cc4884 to your computer and use it in GitHub Desktop.
Script to analyze sound in terms of fft and amplitude
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from __future__ import print_function | |
import scipy.io.wavfile as wavfile | |
import scipy | |
import scipy.fftpack | |
import numpy as np | |
from matplotlib import pyplot as plt | |
fs_rate, signal = wavfile.read("20180323_225212.wav") | |
print ("Frequency sampling", fs_rate) | |
l_audio = len(signal.shape) | |
print ("Channels", l_audio) | |
if l_audio == 2: | |
signal = signal.sum(axis=1) / 2 | |
N = signal.shape[0] | |
print ("Complete Samplings N", N) | |
secs = N / float(fs_rate) | |
print ("secs", secs) | |
Ts = 1.0/fs_rate # sampling interval in time | |
print ("Timestep between samples Ts", Ts) | |
t = scipy.arange(0, secs, Ts) # time vector as scipy arange field / numpy.ndarray | |
FFT = abs(scipy.fft(signal)) | |
FFT_side = FFT[range(int(N/2))] # one side FFT range | |
freqs = scipy.fftpack.fftfreq(signal.size, t[1]-t[0]) | |
fft_freqs = np.array(freqs) | |
freqs_side = freqs[range(int(N/2))] # one side frequency range | |
fft_freqs_side = np.array(freqs_side) | |
plt.subplot(211) | |
p1 = plt.plot(t, signal, "g") # plotting the signal | |
plt.xlabel('Time') | |
plt.ylabel('Amplitude') | |
plt.subplot(212) | |
p3 = plt.plot(freqs_side, abs(FFT_side), "b") # plotting the positive fft spectrum | |
plt.xlabel('Frequency (Hz)') | |
plt.ylabel('Count single-sided') | |
plt.show() | |
''' | |
p2 = plt.plot(freqs, FFT, "r") # plotting the complete fft spectrum | |
plt.xlabel('Frequency (Hz)') | |
plt.ylabel('Count dbl-sided') | |
plt.subplot(313) | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment