Changeset 13180


Ignore:
Timestamp:
03/22/12 21:15:57 (13 years ago)
Author:
neise
Message:
test functions are using Plotter class, and SignalGenerator class in order to produce test vectors, before they were doing that themselves.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • fact/tools/pyscripts/pyfact/fir_filter.py

    r13143 r13180  
    66import numpy as np
    77from scipy import signal
     8
    89import matplotlib.pyplot as plt
    910
     
    9899
    99100    """
    100     npoints = 100
     101    from plotters import Plotter
     102    from generator import SignalGenerator
     103    generate = SignalGenerator()
     104    plot = Plotter('_test_SlidingAverage')
     105   
    101106    safilter = SlidingAverage(8)
    102     signal = np.zeros(npoints)
    103     signal[10:50] += 20.
    104 
    105     # add noise to the signal
    106     sigma = 1.5
    107     signal += np.random.randn(npoints) * sigma
     107    print safilter
    108108   
    109     print safilter
    110     #print 'signal in:  ', signal
    111     #print 'signal out: ', rsfilter(signal)
    112     x=range(npoints)
    113     plt.plot(x, signal, 'b', label='original')
    114     plt.plot(x, safilter(signal), 'r', label='filtered')
    115     plt.title(safilter.name)
    116     plt.xlabel('sample')
    117     plt.legend()
    118     plt.grid(True)
    119     plt.show()
    120 
     109    signal = generate('len 100 noise 1.5 step 20 10 50')
     110    filtered = safilter(signal)
     111    plot( [signal, filtered] , ['original', 'filtered'] )
     112   
     113    raw_input('press any key to go on')
     114    plt.close(plot.figure)
     115   
    121116
    122117def _test_CFD():
     
    124119   
    125120    """
     121    from plotters import Plotter
     122    from generator import SignalGenerator
     123    generate = SignalGenerator()
     124    plot = Plotter('_test_CFD')
    126125   
    127     filt = CFD(8, 0.6)
     126    sa = SlidingAverage(3)
     127    print 'I apply a weak smooting with a filter length of 3'
     128    cfd = CFD(8, 0.6)
     129    print cfd
    128130
    129     npoints = 100
    130     signal = np.ones(npoints) * 10.
    131     signal[20:30] += np.linspace(0., 100., 10)
    132     signal[30:90] += np.linspace(100., 0., 60)
    133     # add noise to the signal
    134     sigma = 1.5
    135     signal += np.random.randn(npoints) * sigma
     131    signal = generate('len 100 noise 1.5 bsl -20 triangle 30 30 8 50')
     132    filtered = cfd(sa(signal))
     133    plot( [signal, filtered] , ['original', 'filtered'] )
    136134   
    137     print filt
    138     #print 'signal in:  ', signal
    139     #print 'signal out: ', rsfilter(signal)
    140     x=range(npoints)
    141     plt.plot(x, signal, 'b.', label='original')
    142     plt.plot(x, filt(signal), 'r.', label='filtered')
    143     plt.title(filt.name)
    144     plt.xlabel('sample')
    145     plt.legend()
    146     plt.grid(True)
    147     plt.show()
    148 
     135    raw_input('press any key to go on')
     136    plt.close(plot.figure)
    149137
    150138def _test_RemoveSignal():
     
    152140   
    153141    """
     142    from plotters import Plotter
     143    from generator import SignalGenerator
     144    generate = SignalGenerator()
     145    plot = Plotter('_test_RemoveSignal')
    154146   
    155     rsfilter = RemoveSignal()
     147    remove_signal = RemoveSignal()
     148    print remove_signal
    156149
    157     npoints = 100
    158     signal = np.ones(npoints) * 10.
    159     signal[10:20] += np.linspace(0., 100., 10)
    160     signal[20:80] += np.linspace(100., 0., 60)
    161     # add noise to the signal
    162     sigma = 3.
    163     signal += np.random.randn(npoints) * sigma
     150    signal = generate('len 100 noise 2 bsl -20 triangle 20 30 8 40 50 30 spike 50 50 15 50 80 50')
     151    filtered = remove_signal(signal)
     152    plot( [signal, filtered] , ['original', 'filtered'] )
    164153   
    165     print rsfilter
    166     #print 'signal in:  ', signal
    167     #print 'signal out: ', rsfilter(signal)
    168     x=range(npoints)
    169     plt.plot(x, signal, 'b.', label='original')
    170     plt.plot(x, rsfilter(signal), 'r.', label='filtered')
    171     plt.title(rsfilter.name)
    172     plt.xlabel('sample')
    173     plt.legend()
    174     plt.grid(True)
    175     plt.show()
     154    raw_input('press any key to go on')
     155    plt.close(plot.figure)
    176156
    177157def _test(filter_type, sig, noise_sigma = 1.):
     158    from plotters import Plotter
    178159   
    179160    filt = filter_type
     
    182163    sig += np.random.randn(samples) * noise_sigma
    183164   
    184     print filt
    185     x=range(samples)
    186     plt.plot(x, sig, 'b.', label='original')
    187     plt.plot(x, filt(sig), 'r.', label='filtered')
    188     plt.title(filt.name)
    189     plt.xlabel('sample')
    190     plt.legend()
    191     plt.grid(True)
    192     plt.show()
    193    
     165    plot = Plotter('_test with ' + str(filt.name))
     166    plot( [sig, filt(sig)], ['original', 'filtered'] )
     167    raw_input('press any key to go on')
     168    plt.close(plot.figure)
    194169
    195170if __name__ == '__main__':
     
    197172   
    198173    _test_SlidingAverage()
     174    _test_CFD()
    199175    _test_RemoveSignal()
    200     _test_CFD()
     176   
    201177    tsig = np.ones(100)
    202178    _test(filter_type=SlidingAverage(8), sig=tsig, noise_sigma=3.)
Note: See TracChangeset for help on using the changeset viewer.