This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
fastdev:python_signals [2009/11/11 11:05] – memeruiz | fastdev:python_signals [2009/11/11 11:33] (current) – memeruiz | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Python + Filters + FFT + Gnuplot ====== | ||
+ | |||
+ | ==== Noise ==== | ||
+ | |||
+ | {{: | ||
+ | |||
+ | ==== Filters ==== | ||
+ | |||
+ | === Linear filters === | ||
+ | |||
+ | There are five basic parameters to design a filter: wp (pass frequency), ws (stop frequency), gpass (pass gain), gstop (stop gain), filter type | ||
+ | |||
+ | {{: | ||
+ | {{: | ||
+ | |||
+ | {{: | ||
+ | {{: | ||
+ | |||
+ | {{: | ||
+ | |||
+ | === Non linear filters === | ||
+ | |||
+ | == Median filter == | ||
+ | |||
+ | |||
+ | {{: | ||
+ | {{: | ||
+ | |||
+ | {{: | ||
+ | |||
+ | == Sharpness: high pass filter == | ||
+ | |||
+ | {{: | ||
+ | |||
+ | ==== Fast Fourier Transform ==== | ||
+ | |||
+ | {{: | ||
+ | |||
+ | ==== Example ==== | ||
+ | |||
+ | The following example code takes data from a phidget analog input and filters this signal using first a IIR filter, then a median and then it calculates the FFT of the whole signal. | ||
+ | |||
+ | import scipy.signal as filters | ||
+ | class filter: | ||
+ | def __init__(self, | ||
+ | self.b, | ||
+ | self.z=filters.lfiltic(self.b, | ||
+ | self.M=len(self.b)-1 | ||
+ | self.N=len(self.a)-1 | ||
+ | self.K=max(self.M, | ||
+ | def get_output(self, | ||
+ | ''' | ||
+ | self.output, | ||
+ | return(self.output) | ||
+ | def get_N(self): | ||
+ | return self.N | ||
+ | def get_ab(self): | ||
+ | return([self.a, | ||
+ | |||
+ | N=property(get_N) | ||
+ | | ||
+ | from Phidgets.PhidgetException import * | ||
+ | from Phidgets.Events.Events import * | ||
+ | from Phidgets.Devices import * | ||
+ | import numpy as n | ||
+ | ik=InterfaceKit.InterfaceKit() | ||
+ | ik.openPhidget() | ||
+ | ik.waitForAttach(10000) | ||
+ | | ||
+ | my_filter=filter(0.1, | ||
+ | N=my_filter.N | ||
+ | time_array=[] | ||
+ | total_input=[] | ||
+ | total_output=[] | ||
+ | import time | ||
+ | inittime=time.time() | ||
+ | while True: | ||
+ | t=[] | ||
+ | input=[] | ||
+ | for i in range(N): | ||
+ | t.append(time.time()-inittime) | ||
+ | input.append(ik.getSensorValue(0)) | ||
+ | time.sleep(0.05) | ||
+ | output=my_filter.get_output(input) | ||
+ | time_array=n.concatenate((time_array, | ||
+ | total_input=n.concatenate((total_input, | ||
+ | total_output=n.concatenate((total_output, | ||
+ | if t[-1]> | ||
+ | break | ||
+ | | ||
+ | total_output_median=filters.medfilt(total_input, | ||
+ | | ||
+ | import Gnuplot | ||
+ | g=Gnuplot.Gnuplot() | ||
+ | g.title(" | ||
+ | g('set data style linespoints' | ||
+ | g.plot(zip(time_array, | ||
+ | raw_input() | ||
+ | import scipy | ||
+ | from math import pi | ||
+ | fft=scipy.fft(total_input) | ||
+ | g.plot(zip(n.arange(0, | ||
+ | raw_input() | ||
+ | g.plot(zip(time_array, | ||
+ | a, | ||
+ | w, | ||
+ | from numpy import log10 | ||
+ | h_db=20*log10(abs(h)) | ||
+ | raw_input() | ||
+ | g.plot(zip(w/ | ||
+ | | ||
+ | while True: | ||
+ | time.sleep(0.1) | ||