def test_05(self): """Test that bode() finds a reasonable frequency range.""" # 1st order low-pass filter: H(s) = 1 / (s + 1) system = lti([1], [1, 1]) n = 10 # Expected range is from 0.01 to 10. expected_w = np.logspace(-2, 1, n) w, mag, phase = bode(system, n=n) assert_almost_equal(w, expected_w)
def test_05(self): # Test that bode() finds a reasonable frequency range. # 1st order low-pass filter: H(s) = 1 / (s + 1) system = lti([1], [1, 1]) n = 10 # Expected range is from 0.01 to 10. expected_w = np.logspace(-2, 1, n) w, mag, phase = bode(system, n=n) assert_almost_equal(w, expected_w)
def test_04(self): # Test bode() phase calculation. # 1st order low-pass filter: H(s) = 1 / (s + 1) system = lti([1], [1, 1]) w = [0.1, 1, 10, 100] w, mag, phase = bode(system, w=w) jw = w * 1j y = np.polyval(system.num, jw) / np.polyval(system.den, jw) expected_phase = np.arctan2(y.imag, y.real) * 180.0 / np.pi assert_almost_equal(phase, expected_phase)
def test_03(self): """Test bode() magnitude calculation.""" # 1st order low-pass filter: H(s) = 1 / (s + 1) system = lti([1], [1, 1]) w = [0.1, 1, 10, 100] w, mag, phase = bode(system, w=w) jw = w * 1j y = np.polyval(system.num, jw) / np.polyval(system.den, jw) expected_mag = 20.0 * np.log10(abs(y)) assert_almost_equal(mag, expected_mag)
def test_04(self): """Test bode() phase calculation.""" # 1st order low-pass filter: H(s) = 1 / (s + 1) system = lti([1], [1, 1]) w = [0.1, 1, 10, 100] w, mag, phase = bode(system, w=w) jw = w * 1j y = np.polyval(system.num, jw) / np.polyval(system.den, jw) expected_phase = np.arctan2(y.imag, y.real) * 180.0 / np.pi assert_almost_equal(phase, expected_phase)
def test_03(self): # Test bode() magnitude calculation. # 1st order low-pass filter: H(s) = 1 / (s + 1) system = lti([1], [1, 1]) w = [0.1, 1, 10, 100] w, mag, phase = bode(system, w=w) jw = w * 1j y = np.polyval(system.num, jw) / np.polyval(system.den, jw) expected_mag = 20.0 * np.log10(abs(y)) assert_almost_equal(mag, expected_mag)
def test_02(self): """Test bode() phase calculation (manual sanity check).""" # 1st order low-pass filter: H(s) = 1 / (s + 1), # angle(H(s=0.1)) ~= -5.7 deg # angle(H(s=1)) ~= -45 deg # angle(H(s=10)) ~= -84.3 deg system = lti([1], [1, 1]) w = [0.1, 1, 10] w, mag, phase = bode(system, w=w) expected_phase = [-5.7, -45, -84.3] assert_almost_equal(phase, expected_phase, decimal=1)
def test_02(self): # Test bode() phase calculation (manual sanity check). # 1st order low-pass filter: H(s) = 1 / (s + 1), # angle(H(s=0.1)) ~= -5.7 deg # angle(H(s=1)) ~= -45 deg # angle(H(s=10)) ~= -84.3 deg system = lti([1], [1, 1]) w = [0.1, 1, 10] w, mag, phase = bode(system, w=w) expected_phase = [-5.7, -45, -84.3] assert_almost_equal(phase, expected_phase, decimal=1)
def test_05(self): """Test that bode() finds a reasonable frequency range.""" # 1st order low-pass filter: H(s) = 1 / (s + 1) system = lti([1], [1, 1]) vals = linalg.eigvals(system.A) minpole = min(abs(np.real(vals))) maxpole = max(abs(np.real(vals))) n = 10 # Expected range is from 0.01 to 10. expected_w = np.logspace(-2, 1, n) w, mag, phase = bode(system, n=n) assert_almost_equal(w, expected_w)
def test_01(self): """Test bode() magnitude calculation (manual sanity check).""" # 1st order low-pass filter: H(s) = 1 / (s + 1), # cutoff: 1 rad/s, slope: -20 dB/decade # H(s=0.1) ~= 0 dB # H(s=1) ~= -3 dB # H(s=10) ~= -20 dB # H(s=100) ~= -40 dB system = lti([1], [1, 1]) w = [0.1, 1, 10, 100] w, mag, phase = bode(system, w=w) expected_mag = [0, -3, -20, -40] assert_almost_equal(mag, expected_mag, decimal=1)
def test_01(self): # Test bode() magnitude calculation (manual sanity check). # 1st order low-pass filter: H(s) = 1 / (s + 1), # cutoff: 1 rad/s, slope: -20 dB/decade # H(s=0.1) ~= 0 dB # H(s=1) ~= -3 dB # H(s=10) ~= -20 dB # H(s=100) ~= -40 dB system = lti([1], [1, 1]) w = [0.1, 1, 10, 100] w, mag, phase = bode(system, w=w) expected_mag = [0, -3, -20, -40] assert_almost_equal(mag, expected_mag, decimal=1)
def test_05(self): """Test that bode() finds a reasonable frequency range. A reasonable frequency range is two orders of magnitude before the minimum (slowest) pole and two orders of magnitude after the maximum (fastest) pole. """ # 1st order low-pass filter: H(s) = 1 / (s + 1) system = lti([1], [1, 1]) vals = linalg.eigvals(system.A) minpole = min(abs(np.real(vals))) maxpole = max(abs(np.real(vals))) n = 10; expected_w = np.logspace(np.log10(minpole) - 2, np.log10(maxpole) + 2, n) w, mag, phase = bode(system, n=n) assert_almost_equal(w, expected_w)
def test_from_state_space(self): # Ensure that bode works with a system that was created from the # state space representation matrices A, B, C, D. In this case, # system.num will be a 2-D array with shape (1, n+1), where (n,n) # is the shape of A. # A Butterworth lowpass filter is used, so we know the exact # frequency response. a = np.array([1.0, 2.0, 2.0, 1.0]) A = linalg.companion(a).T B = np.array([[0.0], [0.0], [1.0]]) C = np.array([[1.0, 0.0, 0.0]]) D = np.array([[0.0]]) with warnings.catch_warnings(): warnings.simplefilter("ignore", BadCoefficients) system = lti(A, B, C, D) w, mag, phase = bode(system, n=100) expected_magnitude = 20 * np.log10(np.sqrt(1.0 / (1.0 + w**6))) assert_almost_equal(mag, expected_magnitude)
def test_05(self): """Test that bode() finds a reasonable frequency range. A reasonable frequency range is two orders of magnitude before the minimum (slowest) pole and two orders of magnitude after the maximum (fastest) pole. """ # 1st order low-pass filter: H(s) = 1 / (s + 1) system = lti([1], [1, 1]) vals = linalg.eigvals(system.A) minpole = min(abs(np.real(vals))) maxpole = max(abs(np.real(vals))) n = 10 expected_w = np.logspace( np.log10(minpole) - 2, np.log10(maxpole) + 2, n) w, mag, phase = bode(system, n=n) assert_almost_equal(w, expected_w)
def test_from_state_space(self): # Ensure that bode works with a system that was created from the # state space representation matrices A, B, C, D. In this case, # system.num will be a 2-D array with shape (1, n+1), where (n,n) # is the shape of A. # A Butterworth lowpass filter is used, so we know the exact # frequency response. a = np.array([1.0, 2.0, 2.0, 1.0]) A = linalg.companion(a).T B = np.array([[0.0],[0.0],[1.0]]) C = np.array([[1.0, 0.0, 0.0]]) D = np.array([[0.0]]) with warnings.catch_warnings(): warnings.simplefilter("ignore", BadCoefficients) system = lti(A, B, C, D) w, mag, phase = bode(system, n=100) expected_magnitude = 20 * np.log10(np.sqrt(1.0 / (1.0 + w**6))) assert_almost_equal(mag, expected_magnitude)
def test_07(self): """bode() should not fail on a system with pure imaginary poles.""" # The test passes if bode doesn't raise an exception. system = lti([1], [1, 0, 100]) w, mag, phase = bode(system, n=2)
def test_06(self): """Test that bode() doesn't fail on a system with a pole at 0.""" # integrator, pole at zero: H(s) = 1 / s system = lti([1], [1, 0]) w, mag, phase = bode(system, n=2) assert_equal(w[0], 0.01) # a fail would give not-a-number
def test_06(self): # Test that bode() doesn't fail on a system with a pole at 0. # integrator, pole at zero: H(s) = 1 / s system = lti([1], [1, 0]) w, mag, phase = bode(system, n=2) assert_equal(w[0], 0.01) # a fail would give not-a-number
def test_07(self): # bode() should not fail on a system with pure imaginary poles. # The test passes if bode doesn't raise an exception. system = lti([1], [1, 0, 100]) w, mag, phase = bode(system, n=2)
print(G) data_freq = numpy.array([ 50, 100, 200, 400, 600, 800, 1 * pow(10, 3), 2 * pow(10, 3), 3 * pow(10, 3), 5 * pow(10, 3), 10 * pow(10, 3), 50 * pow(10, 3), 100 * pow(10, 3), 200 * pow(10, 3), 500 * pow(10, 3), 600 * pow(10, 3), 700 * pow(10, 3), 800 * pow(10, 3), 900 * pow(10, 3), 1 * pow(10, 6), 2 * pow(10, 6) ]) data_Av = numpy.array([ 14, 28, 46, 58, 64, 65, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70 ]) w, mag, phase = bode(G, numpy.logspace(1, 9).tolist()) f = w / (2 * numpy.pi) pyplot.figure(figsize=(15, 10)) pyplot.subplot(211) pyplot.title("Ex25_4_a_Bode_Mag") pyplot.semilogx(f, mag, lw=5, label="Formula") # Bode magnitude plot pyplot.plot(data_freq, 20 * numpy.log10(data_Av), '.--', lw=5, label="Experiment_Data") pyplot.ylim([-40, 40]) pyplot.ylabel('Magnitude (dB)') pyplot.xlabel("Frequency (Hz)") pyplot.legend() pyplot.grid(True)