File: examples-hears_IIRfilterbank.txt

package info (click to toggle)
brian 1.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, stretch
  • size: 23,436 kB
  • sloc: python: 68,707; cpp: 29,040; ansic: 5,182; sh: 111; makefile: 61
file content (79 lines) | stat: -rw-r--r-- 2,944 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
.. currentmodule:: brian

.. index::
   pair: example usage; subplot
   pair: example usage; figure
   pair: example usage; show
   pair: example usage; flipud
   pair: example usage; imshow
   pair: example usage; vstack
   pair: example usage; linspace
   pair: example usage; whitenoise
   pair: example usage; IIRFilterbank

.. _example-hears_IIRfilterbank:

Example: IIRfilterbank (hears)
==============================

Example of the use of the class :class:`~brian.hears.IIRFilterbank` available in
the library.  In this example, a white noise is filtered by a bank of chebyshev
bandpass filters and lowpass filters which are different for every channels.
The centre frequencies of  the filters are linearly taken between 100kHz and
1000kHz and its bandwidth or cutoff frequency increases linearly with frequency.

::

    from brian import *
    from brian.hears import *
    
    sound = whitenoise(100*ms).ramp()
    sound.level = 50*dB
    
    ### example of a bank of bandpass filter ################
    nchannels = 50
    center_frequencies = linspace(200*Hz, 1000*Hz, nchannels)  #center frequencies 
    bw = linspace(50*Hz, 300*Hz, nchannels)  #bandwidth of the filters
    # The maximum loss in the passband in dB. Can be a scalar or an array of length
    # nchannels
    gpass = 1.*dB
    # The minimum attenuation in the stopband in dB. Can be a scalar or an array
    # of length nchannels
    gstop = 10.*dB
    #arrays of shape (2 x nchannels) defining the passband frequencies (Hz)
    passband = vstack((center_frequencies-bw/2, center_frequencies+bw/2))
    #arrays of shape (2 x nchannels) defining the stopband frequencies (Hz)
    stopband = vstack((center_frequencies-1.1*bw, center_frequencies+1.1*bw))
    
    filterbank = IIRFilterbank(sound, nchannels, passband, stopband, gpass, gstop,
                               'bandstop', 'cheby1')
    filterbank_mon = filterbank.process()
    
    figure()
    subplot(211)
    imshow(flipud(filterbank_mon.T), aspect='auto')    
    
    #### example of a bank of lowpass filter ################
    nchannels = 50
    cutoff_frequencies = linspace(100*Hz, 1000*Hz, nchannels)
    #bandwidth of the transition region between the en of the pass band and the
    #begin of the stop band 
    width_transition = linspace(50*Hz, 300*Hz, nchannels)
    # The maximum loss in the passband in dB. Can be a scalar or an array of length
    # nchannels
    gpass = 1*dB
    # The minimum attenuation in the stopband in dB. Can be a scalar or an array of
    # length nchannels
    gstop = 10*dB
    passband = cutoff_frequencies-width_transition/2
    stopband = cutoff_frequencies+width_transition/2
    
    filterbank = IIRFilterbank(sound, nchannels, passband, stopband, gpass, gstop,
                               'low','cheby1')
    filterbank_mon=filterbank.process()
    
    subplot(212)
    imshow(flipud(filterbank_mon.T), aspect='auto')    
    show()