Skip to content Skip to sidebar Skip to footer

Frequency Analysis In Python

I'm trying to use Python to retrieve the dominant frequencies of a live audio input. For the moment I am experimenting using the audio stream my Laptop's built in microphone, but w

Solution 1:

Hello find the maximum computing the FFT for real-time analysis becomes a little slow.

If you will not work with complex waveforms to find the frequencies you can use any method based on Time-domain such as zero-crossing where the performance will be better.

In the last year i make one simple function to calc the frequency by Zero-crossing.

#Eng Eder de Souza 01/12/2011#ederwanderfrom matplotlib.mlab import find
import pyaudio
import numpy as np
import math


chunk = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
RECORD_SECONDS = 20defPitch(signal):
    signal = np.fromstring(signal, 'Int16');
    crossing = [math.copysign(1.0, s) for s in signal]
    index = find(np.diff(crossing));
    f0=round(len(index) *RATE /(2*np.prod(len(signal))))
    return f0;


p = pyaudio.PyAudio()

stream = p.open(format = FORMAT,
channels = CHANNELS,
rate = RATE,
input = True,
output = True,
frames_per_buffer = chunk)

for i inrange(0, RATE / chunk * RECORD_SECONDS):
    data = stream.read(chunk)
    Frequency=Pitch(data)
    print"%f Frequency" %Frequency

ederwander

Solution 2:

There is also the function scipy.signal.lombscargle that calculates the Lomb-Scargle periodogram and is available since v0.10.0. This method should work even for unevenly sampled signals. It seems that the data's mean has to be subtracted for this method to work properly although this is not mentioned in the documentation. More information can be found in the scipy reference guide: http://docs.scipy.org/doc/scipy/reference/tutorial/signal.html#lomb-scargle-periodograms-spectral-lombscargle

Post a Comment for "Frequency Analysis In Python"