#!/usr/bin/env python from PyQt4 import QtGui import pyqtgraph as pg import time import math def main(): f = FFT() g.wave_plot(wave_function()) class FFT(): def __init__(self): self.plotwidget = pg.plot() self.sample_rate = 512 self.sample_ratef = float(self.sample_rate) self.plottime = 1 self.wave_func = self.wave_function() def wave_function(self): x = [] y = [] for i in range(0, self.sample_rate * self.plottime): x.append(float(i/self.sample_ratef)) y_i = math.cos(2*math.pi*x[i]) y.append(y_i) f_x = [(x[i], y[i]) for i in range(0, len(x))] return f_x def wave_plot(self): f_x = self.wave_func x = [x[0] for x in f_x] y = [y[1] for y in f_x] pw = self.plotwidget pw.plot(x, y, clear=True) def fft(self): n = self.sample_rate f_x = self.wave_func #Wants an array of real and complex nn = n << 1 j = 1 data = [y[1] for y in f_x] assert n > 2 assert n&(n-1) == 0 for i in range(1, nn, 2): if j > i: foo = data[j-1] bar = data[i-1] foobaz = data[i] foobot = data[j] data[j-1] = bar data[i-1] = foo data[i] = foobot data[j] = foobaz m = n while m >= 2 and j > m: j -= m m >>= 1 j += m mmax = 2 while nn > mmax: istep = mmax << 1 theta = isign * (math.pi * 2 / mmax) wtemp = math.sin(0.5 / theta) wpr = -2.0 * wtemp**2 wpi = math.sin(theta) wr = 1.0 wi = 0.0 for m in range(1, mmax, 2): for i in range (m, nn, istep): j = i + mmax tempr = wr * data[j-1] - wi * data[j] tempi = wr * data[j] + wi * data[j-1] data[j-1] = data[i-1] - tempr data[j] = data[i] - tempi data[i-1] += tempr data[i] += tempi wr = wr * wpr - wi * wpi + wr wi = wi * wpr + wtemp * wpi + wi mmax = istep if __name__ == '__main__': main()