Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to separate audio frequencies python

from scipy import fft, arange
import numpy as np
import matplotlib.pyplot as plt
from scipy.io import wavfile
import os


def frequency_spectrum(x, sf):
    """
    Derive frequency spectrum of a signal from time domain
    :param x: signal in the time domain
    :param sf: sampling frequency
    :returns frequencies and their content distribution
    """
    x = x - np.average(x)  # zero-centering

    n = len(x)
    k = arange(n)
    tarr = n / float(sf)
    frqarr = k / float(tarr)  # two sides frequency range

    frqarr = frqarr[range(n // 2)]  # one side frequency range

    x = fft(x) / n  # fft computing and normalization
    x = x[range(n // 2)]

    return frqarr, abs(x)


# Sine sample with a frequency of 1hz and add some noise
sr = 32  # sampling rate
y = np.linspace(0, 2*np.pi, sr)
y = np.tile(np.sin(y), 5)
y += np.random.normal(0, 1, y.shape)
t = np.arange(len(y)) / float(sr)

plt.subplot(2, 1, 1)
plt.plot(t, y)
plt.xlabel('t')
plt.ylabel('y')

frq, X = frequency_spectrum(y, sr)

plt.subplot(2, 1, 2)
plt.plot(frq, X, 'b')
plt.xlabel('Freq (Hz)')
plt.ylabel('|X(freq)|')
plt.tight_layout()


# wav sample from https://freewavesamples.com/files/Alesis-Sanctuary-QCard-Crickets.wav
here_path = os.path.dirname(os.path.realpath(__file__))
wav_file_name = 'Alesis-Sanctuary-QCard-Crickets.wav'
wave_file_path = os.path.join(here_path, wav_file_name)
sr, signal = wavfile.read(wave_file_path)

y = signal[:, 0]  # use the first channel (or take their average, alternatively)
t = np.arange(len(y)) / float(sr)

plt.figure()
plt.subplot(2, 1, 1)
plt.plot(t, y)
plt.xlabel('t')
plt.ylabel('y')

frq, X = frequency_spectrum(y, sr)

plt.subplot(2, 1, 2)
plt.plot(frq, X, 'b')
plt.xlabel('Freq (Hz)')
plt.ylabel('|X(freq)|')
plt.tight_layout()

plt.show()
Comment

PREVIOUS NEXT
Code Example
Python :: convert a python object like dict, list, etc to a json object 
Python :: how to convert nonetype to list in python 
Python :: get inverse of bool value python 
Python :: get first element of each group 
Python :: time vs timeit 
Python :: python concat list multiple times 
Python :: manipulate list using slice assignment 
Python :: plotly dcc.interval bar graph with time 
Python :: # filter a list 
Python :: pairplot markersize 
Python :: Default rows values display 
Python :: change xlabel size 
Python :: unittest run one test 
Python :: custom port odoo 
Python :: find factors of a number using while loop 
Python :: Errors while using os.makedirs() method 
Python :: form handling in django 
Python :: copy string x times python 
Python :: jdoodle python 
Python :: python adding an item 
Python :: pyt last of range of numbers 
Python :: Python NumPy broadcast_arrays() Function Syntax 
Python :: Django merge duplicate rows 
Python :: Python NumPy asanyarray Function Example List to an array 
Python :: python function arguments multiple lines 
Python :: como saber si un string es un numero python 
Python :: object at being output python 
Python :: python string josin 
Python :: ax bar different colors 
Python :: how to calculate iqr in pandas 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =