<filename>old_code/plot_analysis.py
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from numpy import correlate
from scipy import fft, array

def generate_plots(wave):
     fig = Figure()        
             
     for j in range(wave.wave_quantity):
            canvas = FigureCanvas(fig)
            fig.set_canvas(canvas)
            ax = fig.add_subplot(1,1,1)
            fig.suptitle("Event " + str(j) + ", Relative Time: " + wave.time_difference_string[j]) 
            ax.plot(wave.wave_data[j], '-')
            plotdir = wave.dir_path + "\\plots"
            fname = plotdir + "\\event" + str(j) + ".png"
            fig.savefig(fname, format='png', dpi=100)
            canvas.close()
            fig.clear()
            
def generate_correlate_plots(wave):
    fig = Figure()
    
    for j in range(wave.wave_quantity):     
        
        canvas = FigureCanvas(fig)
        fig.set_canvas(canvas)
        
        ax1 = fig.add_subplot(2,1,1)
        fig.suptitle("Event " + str(j) + ", Relative Time: " + wave.time_difference_string[j]) 
        ax1.plot(wave.wave_data[j], '-')
        
        ax2 = fig.add_subplot(2,1,2)
        x = wave.wave_data[j]
        y = correlate(x,x,mode='full')[len(x)-1:]       
        ax2.plot(y, '-')
        
        plotdir = wave.dir_path + "\\correlate_plots"
        fname = plotdir + "\\event" + str(j) + ".png"
        fig.savefig(fname, format='png', dpi=100)
        canvas.close()
        fig.clear()
        
def generate_fft_plots(wave):
    fig = Figure()
    
    for j in range(wave.wave_quantity):
              
        canvas = FigureCanvas(fig)
        fig.set_canvas(canvas)
        
        ax1 = fig.add_subplot(2,1,1)
        
        fig.suptitle("Event " + str(j) + ", Relative Time: " + wave.time_difference_string[j]) 
        #fig.suptitle(wave.wave_times[j].strftime("%d/%m/%y %H:%M:%S %f"))
        ax1.plot( wave.wave_data[j] , '-')
        
        ax2 = fig.add_subplot(2,1,2)

        ax2.plot(wave.fft_period[j], wave.fft_power[j])
        ax2.set_xscale('log')
        
        plotdir = wave.dir_path + "\\fft_plots"
        fname = plotdir + "\\event" + str(j) + ".png"
        fig.savefig(fname, format='png', dpi=100)
        canvas.close()
        fig.clear()

def lpfilter_plot(cuttoff_freq, wave_data):

    from scipy import signal
    import math, numpy
    from matplotlib import pyplot
    
    ## some constants
    samp_rate = 2000.0
    sim_time = 0.200
    nsamps = samp_rate*sim_time
    
    fig = pyplot.figure()
    
    ## generate input signal
    t = numpy.linspace(0, sim_time, nsamps)
    #freqs = [0.1, 0.5, 1, 4]
    #x = 0
    #for i in range(len(freqs)):
    # x += numpy.cos(2*math.pi*freqs[i]*t)
    x = wave_data
    time_dom = fig.add_subplot(232)
    pyplot.plot(t, x)
    pyplot.title('Filter Input - Time Domain')
    pyplot.grid(True)
    #
    ## input signal spectrum
    xfreq = numpy.fft.fft(x)
    fft_freqs = numpy.fft.fftfreq( int(nsamps), d=1./samp_rate)
    fig.add_subplot(233)
    pyplot.loglog(fft_freqs[0:nsamps/2], numpy.abs(xfreq)[0:nsamps/2])
    pyplot.title('Filter Input - Frequency Domain')
    #pyplot.text(0.03, 0.01, "freqs: "+str(freqs)+" Hz")
    pyplot.grid(True)
    #
    ## design filter
    norm_pass = 2*math.pi*cuttoff_freq/samp_rate
    norm_stop = 1.5*norm_pass
    (N, Wn) = signal.buttord(wp=norm_pass, ws=norm_stop, gpass=2, gstop=30, analog=0)
    (b, a) = signal.butter(N, Wn, btype='low', analog=0, output='ba')
    b *= numpy.sum(a)/numpy.sum(b)
    print("b="+str(b)+", a="+str(a))
    #
    ## filter frequency response
    (w, h) = signal.freqz(b, a)
    fig.add_subplot(131)
    pyplot.loglog(w, numpy.abs(h))
    pyplot.title('Filter Frequency Response')
    pyplot.text(2e-3, 1e-5, str(N)+"-th order Butterworth filter")
    pyplot.grid(True)
    #
    ## filtered output
    ##zi = signal.lfiltic(b, a, x[0:5], x[0:5])
    ##(y, zi) = signal.lfilter(b, a, x, zi=zi)
    y = signal.lfilter(b, a, x)
    fig.add_subplot(235)
    pyplot.plot(t, y)
    pyplot.title('Filter output - Time Domain')
    pyplot.grid(True)
    #
    ## output spectrum
    yfreq = numpy.fft.fft(y)
    fig.add_subplot(236)
    pyplot.loglog(fft_freqs[0:nsamps/2], numpy.abs(yfreq)[0:nsamps/2])
    pyplot.title('Filter Output - Frequency Domain')
    pyplot.grid(True)
    
    pyplot.show()
<filename>old_code/plot_analysis.py
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from numpy import correlate
from scipy import fft, array

def generate_plots(wave):
     fig = Figure()        
             
     for j in range(wave.wave_quantity):
            canvas = FigureCanvas(fig)
            fig.set_canvas(canvas)
            ax = fig.add_subplot(1,1,1)
            fig.suptitle("Event " + str(j) + ", Relative Time: " + wave.time_difference_string[j]) 
            ax.plot(wave.wave_data[j], '-')
            plotdir = wave.dir_path + "\\plots"
            fname = plotdir + "\\event" + str(j) + ".png"
            fig.savefig(fname, format='png', dpi=100)
            canvas.close()
            fig.clear()
            
def generate_correlate_plots(wave):
    fig = Figure()
    
    for j in range(wave.wave_quantity):     
        
        canvas = FigureCanvas(fig)
        fig.set_canvas(canvas)
        
        ax1 = fig.add_subplot(2,1,1)
        fig.suptitle("Event " + str(j) + ", Relative Time: " + wave.time_difference_string[j]) 
        ax1.plot(wave.wave_data[j], '-')
        
        ax2 = fig.add_subplot(2,1,2)
        x = wave.wave_data[j]
        y = correlate(x,x,mode='full')[len(x)-1:]       
        ax2.plot(y, '-')
        
        plotdir = wave.dir_path + "\\correlate_plots"
        fname = plotdir + "\\event" + str(j) + ".png"
        fig.savefig(fname, format='png', dpi=100)
        canvas.close()
        fig.clear()
        
def generate_fft_plots(wave):
    fig = Figure()
    
    for j in range(wave.wave_quantity):
              
        canvas = FigureCanvas(fig)
        fig.set_canvas(canvas)
        
        ax1 = fig.add_subplot(2,1,1)
        
        fig.suptitle("Event " + str(j) + ", Relative Time: " + wave.time_difference_string[j]) 
        #fig.suptitle(wave.wave_times[j].strftime("%d/%m/%y %H:%M:%S %f"))
        ax1.plot( wave.wave_data[j] , '-')
        
        ax2 = fig.add_subplot(2,1,2)

        ax2.plot(wave.fft_period[j], wave.fft_power[j])
        ax2.set_xscale('log')
        
        plotdir = wave.dir_path + "\\fft_plots"
        fname = plotdir + "\\event" + str(j) + ".png"
        fig.savefig(fname, format='png', dpi=100)
        canvas.close()
        fig.clear()

def lpfilter_plot(cuttoff_freq, wave_data):

    from scipy import signal
    import math, numpy
    from matplotlib import pyplot
    
    ## some constants
    samp_rate = 2000.0
    sim_time = 0.200
    nsamps = samp_rate*sim_time
    
    fig = pyplot.figure()
    
    ## generate input signal
    t = numpy.linspace(0, sim_time, nsamps)
    #freqs = [0.1, 0.5, 1, 4]
    #x = 0
    #for i in range(len(freqs)):
    # x += numpy.cos(2*math.pi*freqs[i]*t)
    x = wave_data
    time_dom = fig.add_subplot(232)
    pyplot.plot(t, x)
    pyplot.title('Filter Input - Time Domain')
    pyplot.grid(True)
    #
    ## input signal spectrum
    xfreq = numpy.fft.fft(x)
    fft_freqs = numpy.fft.fftfreq( int(nsamps), d=1./samp_rate)
    fig.add_subplot(233)
    pyplot.loglog(fft_freqs[0:nsamps/2], numpy.abs(xfreq)[0:nsamps/2])
    pyplot.title('Filter Input - Frequency Domain')
    #pyplot.text(0.03, 0.01, "freqs: "+str(freqs)+" Hz")
    pyplot.grid(True)
    #
    ## design filter
    norm_pass = 2*math.pi*cuttoff_freq/samp_rate
    norm_stop = 1.5*norm_pass
    (N, Wn) = signal.buttord(wp=norm_pass, ws=norm_stop, gpass=2, gstop=30, analog=0)
    (b, a) = signal.butter(N, Wn, btype='low', analog=0, output='ba')
    b *= numpy.sum(a)/numpy.sum(b)
    print("b="+str(b)+", a="+str(a))
    #
    ## filter frequency response
    (w, h) = signal.freqz(b, a)
    fig.add_subplot(131)
    pyplot.loglog(w, numpy.abs(h))
    pyplot.title('Filter Frequency Response')
    pyplot.text(2e-3, 1e-5, str(N)+"-th order Butterworth filter")
    pyplot.grid(True)
    #
    ## filtered output
    ##zi = signal.lfiltic(b, a, x[0:5], x[0:5])
    ##(y, zi) = signal.lfilter(b, a, x, zi=zi)
    y = signal.lfilter(b, a, x)
    fig.add_subplot(235)
    pyplot.plot(t, y)
    pyplot.title('Filter output - Time Domain')
    pyplot.grid(True)
    #
    ## output spectrum
    yfreq = numpy.fft.fft(y)
    fig.add_subplot(236)
    pyplot.loglog(fft_freqs[0:nsamps/2], numpy.abs(yfreq)[0:nsamps/2])
    pyplot.title('Filter Output - Frequency Domain')
    pyplot.grid(True)
    
    pyplot.show()
#fig.suptitle(wave.wave_times[j].strftime("%d/%m/%y %H:%M:%S %f")) ## some constants ## generate input signal #freqs = [0.1, 0.5, 1, 4] #x = 0 #for i in range(len(freqs)): # x += numpy.cos(2*math.pi*freqs[i]*t) # ## input signal spectrum #pyplot.text(0.03, 0.01, "freqs: "+str(freqs)+" Hz") # ## design filter # ## filter frequency response # ## filtered output ##zi = signal.lfiltic(b, a, x[0:5], x[0:5]) ##(y, zi) = signal.lfilter(b, a, x, zi=zi) # ## output spectrum