<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://fbswiki.org/wiki/index.php?action=history&amp;feed=atom&amp;title=Figure_5.21%3A_Simulation_of_noise_cancellation</id>
	<title>Figure 5.21: Simulation of noise cancellation - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://fbswiki.org/wiki/index.php?action=history&amp;feed=atom&amp;title=Figure_5.21%3A_Simulation_of_noise_cancellation"/>
	<link rel="alternate" type="text/html" href="http://fbswiki.org/wiki/index.php?title=Figure_5.21:_Simulation_of_noise_cancellation&amp;action=history"/>
	<updated>2026-04-24T21:53:49Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.35.1</generator>
	<entry>
		<id>http://fbswiki.org/wiki/index.php?title=Figure_5.21:_Simulation_of_noise_cancellation&amp;diff=1341&amp;oldid=prev</id>
		<title>Murray: Created page with &quot;{{Figure |Chapter=Dynamic Behavior |Figure number=5.21 |Sort key=521 |Figure title=Simulation of noise cancellation |GitHub URL=https://github.com/murrayrm/fbs2e-python/blob/m...&quot;</title>
		<link rel="alternate" type="text/html" href="http://fbswiki.org/wiki/index.php?title=Figure_5.21:_Simulation_of_noise_cancellation&amp;diff=1341&amp;oldid=prev"/>
		<updated>2024-11-25T00:48:30Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;{{Figure |Chapter=Dynamic Behavior |Figure number=5.21 |Sort key=521 |Figure title=Simulation of noise cancellation |GitHub URL=https://github.com/murrayrm/fbs2e-python/blob/m...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Figure&lt;br /&gt;
|Chapter=Dynamic Behavior&lt;br /&gt;
|Figure number=5.21&lt;br /&gt;
|Sort key=521&lt;br /&gt;
|Figure title=Simulation of noise cancellation&lt;br /&gt;
|GitHub URL=https://github.com/murrayrm/fbs2e-python/blob/main/example-5.18-noise_cancel.py&lt;br /&gt;
}}&lt;br /&gt;
[[Image:figure-5.21-noise_cancel.png]]&lt;br /&gt;
&lt;br /&gt;
Figure 5.21: Simulation of noise cancellation. The upper left figure shows the headphone signal without noise cancellation, and the lower left figure shows the signal with noise cancellation. The right figures show the parameters &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;b&amp;lt;/math&amp;gt; of the filter.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
# example-5.18-noise_cancel.py - Noise cancellation&lt;br /&gt;
# RMM, 24 Nov 2024&lt;br /&gt;
#&lt;br /&gt;
# Figure 5.21: Simulation of noise cancellation. The upper left figure&lt;br /&gt;
# shows the headphone signal without noise cancellation, and the lower left&lt;br /&gt;
# figure shows the signal with noise cancellation. The right figures show&lt;br /&gt;
# the parameters a and b of the filter.&lt;br /&gt;
&lt;br /&gt;
import control as ct&lt;br /&gt;
import numpy as np&lt;br /&gt;
import matplotlib.pyplot as plt&lt;br /&gt;
from math import pi&lt;br /&gt;
ct.use_fbs_defaults()&lt;br /&gt;
&lt;br /&gt;
#&lt;br /&gt;
# System dynamics&lt;br /&gt;
#&lt;br /&gt;
&lt;br /&gt;
# Headphone dynamics&lt;br /&gt;
headphone_params = {'a0': -0.75, 'b0': 0.9}&lt;br /&gt;
def headphone_update(t, z, n, params):&lt;br /&gt;
    return params['a0'] * z[0] + params['b0'] * n[0]&lt;br /&gt;
headphone = ct.nlsys(&lt;br /&gt;
    headphone_update, inputs='n', states='z', params=headphone_params,&lt;br /&gt;
    name='headphone')&lt;br /&gt;
&lt;br /&gt;
# Filter dynamics&lt;br /&gt;
def filter_update(t, w, u, params):&lt;br /&gt;
    n, a, b = u&lt;br /&gt;
    return a * w + b * n&lt;br /&gt;
filter = ct.nlsys(&lt;br /&gt;
    filter_update, inputs=['n', 'a', 'b'], states='w', name='filter')&lt;br /&gt;
&lt;br /&gt;
# Controller dynamics&lt;br /&gt;
control_params = {'alpha': 1}&lt;br /&gt;
def control_update(t, x, u, params):&lt;br /&gt;
    n, e, w = u&lt;br /&gt;
    a, b = x&lt;br /&gt;
    return [&lt;br /&gt;
        params['alpha'] * w * e,&lt;br /&gt;
        params['alpha'] * n * e&lt;br /&gt;
    ]&lt;br /&gt;
control = ct.nlsys(&lt;br /&gt;
    control_update, inputs=['n', 'e', 'w'], states=['a', 'b'], name='control',&lt;br /&gt;
    params=control_params)&lt;br /&gt;
&lt;br /&gt;
# Create summing junction to add all of the signal together&lt;br /&gt;
summer = ct.summing_junction(inputs=['z', 'S', '-w'], outputs='e')&lt;br /&gt;
&lt;br /&gt;
# Interconnected system&lt;br /&gt;
sys = ct.interconnect(&lt;br /&gt;
    [headphone, filter, control, summer], name='noise_cancel',&lt;br /&gt;
    inputs=['S', 'n'], outputs=['e', 'a', 'b'])&lt;br /&gt;
&lt;br /&gt;
# Create the signal and noise&lt;br /&gt;
timepts = np.linspace(0, 200, 2000)&lt;br /&gt;
signal = np.sin(0.1 * 2 * pi * timepts) # sinewave with frequency 0.1 Hz&lt;br /&gt;
noise = ct.white_noise(timepts, 5)      # white noise with covariance 5&lt;br /&gt;
&lt;br /&gt;
# Set up the plotting grid to match the layout in the book&lt;br /&gt;
fig = plt.figure(constrained_layout=True)&lt;br /&gt;
gs = fig.add_gridspec(3, 2)&lt;br /&gt;
&lt;br /&gt;
# No noise cancellation&lt;br /&gt;
resp_off = ct.input_output_response(&lt;br /&gt;
    sys, timepts, [signal, noise], params={'alpha': 0})&lt;br /&gt;
&lt;br /&gt;
ax = fig.add_subplot(gs[0, 0])&lt;br /&gt;
ax.plot(resp_off.time, resp_off.outputs[0])&lt;br /&gt;
ax.axis('tight')&lt;br /&gt;
ax.axis([0, 200, -5, 5])&lt;br /&gt;
ax.set_ylabel(&amp;quot;No cancellation&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
resp_on = ct.input_output_response(&lt;br /&gt;
    sys, timepts, [signal, noise], params={'alpha': 1e-2})&lt;br /&gt;
&lt;br /&gt;
ax = fig.add_subplot(gs[1, 0])&lt;br /&gt;
ax.plot(resp_on.time, resp_on.outputs[0], label='e')&lt;br /&gt;
# ax.plot(resp_on.time, signal, label='S')&lt;br /&gt;
ax.axis('tight')&lt;br /&gt;
ax.axis([0, 200, -5, 5])&lt;br /&gt;
ax.set_ylabel(&amp;quot;Cancellation&amp;quot;)&lt;br /&gt;
ax.set_xlabel(&amp;quot;Time $t$ [s]&amp;quot;)&lt;br /&gt;
# ax.legend()&lt;br /&gt;
&lt;br /&gt;
ax = fig.add_subplot(gs[0, 1])&lt;br /&gt;
ax.plot(resp_on.time, resp_on.outputs[1])&lt;br /&gt;
ax.axis('tight')&lt;br /&gt;
ax.axis([0, 200, -1.1, 0])&lt;br /&gt;
ax.set_ylabel(&amp;quot;$a$&amp;quot;, rotation=0)&lt;br /&gt;
&lt;br /&gt;
ax = fig.add_subplot(gs[1, 1])&lt;br /&gt;
ax.plot(resp_on.time, resp_on.outputs[2])&lt;br /&gt;
ax.axis('tight')&lt;br /&gt;
ax.axis([0, 200, 0, 1.1])&lt;br /&gt;
ax.set_ylabel(&amp;quot;$b$&amp;quot;, rotation=0)&lt;br /&gt;
ax.set_xlabel(&amp;quot;Time $t$ [s]&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Save the figure&lt;br /&gt;
plt.savefig(&amp;quot;figure-5.21-noise_cancel.png&amp;quot;, bbox_inches='tight')&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;/div&gt;</summary>
		<author><name>Murray</name></author>
	</entry>
</feed>