Skip to content

Commit 79fe7a8

Browse files
committed
IIR tutorial added
1 parent 2da3eb1 commit 79fe7a8

File tree

3 files changed

+114
-11
lines changed

3 files changed

+114
-11
lines changed
Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
# delta function) with the filter. FIR filters convolve an input signal with the impulse response.
2626
# Each timepoint in the output signal is expressed as:
2727
#
28-
# ..math::
28+
# .. math::
2929
#
30-
# y(n) = \sum_{k=0}^M b_k x(n-k) - \sum_{k=1}^N a_k y(n-k)
30+
# y(n) = \sum_{k=0}^M b_k x(n-k)
3131
#
3232
# Where each output (:math:`y(n)`) is the sum of the product of the filter coefficients
3333
# (i.e. values from the impulse response, :math:`b_k`) and past values of the input signal
@@ -36,9 +36,9 @@
3636
#
3737
# This formula also has a frequency representation:
3838
#
39-
# ..math::
39+
# .. math::
4040
#
41-
# H(z) = \sum_{k=0}^Mb_kz^{-k}
41+
# H(z) = \sum_{k=0}^M b_k z^{-k}
4242
#
4343

4444
###################################################################################################
@@ -51,7 +51,7 @@
5151
#
5252
# 1. Considering the frequency and impulse response
5353
# 2. Using simulation data to understand filter effects
54-
# 3. Manually inspecting the filtered signal.
54+
# 3. Manually inspecting the filtered signal
5555
#
5656
# The impulse and frequency response (:math:`b_k`) are found below for an alpha bandpass filter.
5757
#
@@ -111,11 +111,9 @@
111111
# - Direction of Computation
112112
#
113113
# The design, application, and evaluation of a filter may be performed using the
114-
# :func:`~.filter_signal` function. The ``verbose`` argument prints the filter parameters to the
115-
# console. Alternatively, these parameters may be saved by passing a path and filename to the
116-
# ``save_properties`` argument. Futhermore, the ``plot_properties`` argument plots the impulse and
117-
# frequency response.
118-
#
114+
# :func:`~.filter_signal` function. The ``plot_properties`` argument plots the impulse and
115+
# frequency response. The filter response and parameters may be saved by passing a path and
116+
# filename to the ``save_properties`` argument.
119117

120118
sig_filt = filter_signal(sig, fs, pass_type, f_range, filter_type='fir',
121-
plot_properties=True, verbose=True)
119+
plot_properties=True, print_transitions=True)

tutorials/filt/plot_3_IIR.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
"""
2+
IIR Filters
3+
===========
4+
5+
Design, apply, and evaluate IIR Filters.
6+
7+
This tutorial covers the design and application of Infinite Impulse Response (IIR) filters
8+
(``neurodsp.filt.iir``).
9+
10+
"""
11+
###################################################################################################
12+
13+
import numpy as np
14+
from neurodsp.sim import sim_combined
15+
from neurodsp.filt import filter_signal
16+
from neurodsp.filt.iir import design_iir_filter, apply_iir_filter
17+
from neurodsp.filt.utils import compute_frequency_response
18+
from neurodsp.plts import plot_filter_properties, plot_time_series
19+
20+
###################################################################################################
21+
# Introduction
22+
# ------------
23+
#
24+
# In contrast to FIR filters, IIR filters produce an impulse response that is dependent on past and
25+
# values of the impulse and impulse response (i.e. recursion or feedback), producing a response that
26+
# is never zero. Due to this, the IIR filters are not typically implemented using convolution. The
27+
# math introduced in the FIR tutorial may be extended to IIR filters, with a second expression
28+
# representing feedback:
29+
#
30+
# .. math::
31+
#
32+
# y(n) = \sum_{k=0}^M b_k x(n-k) - \sum_{k=1}^N a_k y(n-k)
33+
#
34+
# .. math::
35+
#
36+
# H(z) = \frac{\sum_{k=0}^M b_k z^{-k}}{\sum_{k=1}^N a_k z^{-k}}
37+
#
38+
39+
40+
###################################################################################################
41+
# Design
42+
# ------
43+
#
44+
# Neurodsp supports the butterworth IIR filter. The order of this filter controls how smooth (low
45+
# orders) or steep (high order) the roll-off of the transition band is. Below, we will design an
46+
# IIR filter and plot the frquency response.
47+
#
48+
49+
# Settings
50+
fs = 1000
51+
pass_type = 'bandpass'
52+
f_range = (1, 50)
53+
butterworth_order = 2
54+
55+
# Get the second-order series (sos) representatino of the filter
56+
sos = design_iir_filter(fs, pass_type, f_range, butterworth_order)
57+
58+
###################################################################################################
59+
# Apply
60+
# -----
61+
#
62+
# The filter we previously designed may now be applied to a signal. Inspecting the filtered signal,
63+
# relative to the original signal, is recommended.
64+
#
65+
66+
# Simulate
67+
n_seconds = 1
68+
69+
components = {'sim_powerlaw' : {'exponent' : 0},
70+
'sim_oscillation' : {'freq' : 10}}
71+
72+
variances = [0.1, 1]
73+
74+
sig = sim_combined(n_seconds, fs, components, variances)
75+
76+
# Apply the filter
77+
sig_filt = apply_iir_filter(sig, sos)
78+
79+
# Plot
80+
times = np.arange(0, len(sig)/fs, 1/fs)
81+
plot_time_series(times, [sig, sig_filt], ['Raw', 'Filtered'])
82+
83+
###################################################################################################
84+
# Reporting
85+
# ---------
86+
#
87+
# In addition, to careful filter design, `Widman et al., 2015 <https://pubmed.ncbi.nlm.nih.gov/25128257/>`_
88+
# recommends reporting the following filter parameters:
89+
#
90+
# - Filter Pass-Type
91+
# - Cutoff Frequency and Definition
92+
# - Filter Order
93+
# - Transition Bandwidth
94+
# - Passband Ripple and Stopband Attenuation
95+
# - Filter Delay and Causality
96+
# - Direction of Computation
97+
#
98+
# The design, application, and evaluation of a filter may be performed using the
99+
# :func:`~.filter_signal` function. The ``plot_properties`` argument plots the frequency response.
100+
# The frqeuency response and parameters may be saved by passing a path and filename to the
101+
# ``save_properties`` argument.
102+
#
103+
104+
sig_filt = filter_signal(sig, fs, pass_type, f_range, filter_type='iir',
105+
butterworth_order=2, plot_properties=True, print_transitions=True)

0 commit comments

Comments
 (0)