def test_polyvalfromroots(self): # check exception for broadcasting x values over root array with # too few dimensions assert_raises(ValueError, poly.polyvalfromroots, [1], [1], tensor=False) # check empty input assert_equal(poly.polyvalfromroots([], [1]).size, 0) assert_(poly.polyvalfromroots([], [1]).shape == (0,)) # check empty input + multidimensional roots assert_equal(poly.polyvalfromroots([], [[1] * 5]).size, 0) assert_(poly.polyvalfromroots([], [[1] * 5]).shape == (5, 0)) # check scalar input assert_equal(poly.polyvalfromroots(1, 1), 0) assert_(poly.polyvalfromroots(1, np.ones((3, 3))).shape == (3,)) # check normal input) x = np.linspace(-1, 1) y = [x ** i for i in range(5)] for i in range(1, 5): tgt = y[i] res = poly.polyvalfromroots(x, [0] * i) assert_almost_equal(res, tgt) tgt = x * (x - 1) * (x + 1) res = poly.polyvalfromroots(x, [-1, 0, 1]) assert_almost_equal(res, tgt) # check that shape is preserved for i in range(3): dims = [2] * i x = np.zeros(dims) assert_equal(poly.polyvalfromroots(x, [1]).shape, dims) assert_equal(poly.polyvalfromroots(x, [1, 0]).shape, dims) assert_equal(poly.polyvalfromroots(x, [1, 0, 0]).shape, dims) # check compatibility with factorization ptest = [15, 2, -16, -2, 1] r = poly.polyroots(ptest) x = np.linspace(-1, 1) assert_almost_equal(poly.polyval(x, ptest), poly.polyvalfromroots(x, r)) # check multidimensional arrays of roots and values # check tensor=False rshape = (3, 5) x = np.arange(-3, 2) r = np.random.randint(-5, 5, size=rshape) res = poly.polyvalfromroots(x, r, tensor=False) tgt = np.empty(r.shape[1:]) for ii in range(tgt.size): tgt[ii] = poly.polyvalfromroots(x[ii], r[:, ii]) assert_equal(res, tgt) # check tensor=True x = np.vstack([x, 2 * x]) res = poly.polyvalfromroots(x, r, tensor=True) tgt = np.empty(r.shape[1:] + x.shape) for ii in range(r.shape[1]): for jj in range(x.shape[0]): tgt[ii, jj, :] = poly.polyvalfromroots(x[jj], r[:, ii]) assert_equal(res, tgt)
def Q(racines, i): temp = racines.copy() x_t = temp.pop(i) coef = 1 / nppol.polyvalfromroots(x_t, temp) return (coef * nppol.polyfromroots(temp))
def freqz_zpk(z, p, k, worN=None, whole=False): r""" Compute the frequency response of a digital filter in ZPK form. Given the Zeros, Poles and Gain of a digital filter, compute its frequency response:: :math:`H(z)=k \prod_i (z - Z[i]) / \prod_j (z - P[j])` where :math:`k` is the `gain`, :math:`Z` are the `zeros` and :math:`P` are the `poles`. Parameters ---------- z : array_like Zeroes of a linear filter p : array_like Poles of a linear filter k : scalar Gain of a linear filter worN : {None, int, array_like}, optional If None (default), then compute at 512 frequencies equally spaced around the unit circle. If a single integer, then compute at that many frequencies. If an array_like, compute the response at the frequencies given (in radians/sample). whole : bool, optional Normally, frequencies are computed from 0 to the Nyquist frequency, pi radians/sample (upper-half of unit-circle). If `whole` is True, compute frequencies from 0 to 2*pi radians/sample. Returns ------- w : ndarray The normalized frequencies at which `h` was computed, in radians/sample. h : ndarray The frequency response. See Also -------- freqs : Compute the frequency response of an analog filter in TF form freqs_zpk : Compute the frequency response of an analog filter in ZPK form freqz : Compute the frequency response of a digital filter in TF form Notes ----- .. versionadded: 0.19.0 Examples -------- >>> from scipy import signal >>> z, p, k = signal.butter(4, 0.2, output='zpk') >>> w, h = signal.freqz_zpk(z, p, k) >>> import matplotlib.pyplot as plt >>> fig = plt.figure() >>> plt.title('Digital filter frequency response') >>> ax1 = fig.add_subplot(111) >>> plt.plot(w, 20 * np.log10(abs(h)), 'b') >>> plt.ylabel('Amplitude [dB]', color='b') >>> plt.xlabel('Frequency [rad/sample]') >>> ax2 = ax1.twinx() >>> angles = np.unwrap(np.angle(h)) >>> plt.plot(w, angles, 'g') >>> plt.ylabel('Angle (radians)', color='g') >>> plt.grid() >>> plt.axis('tight') >>> plt.show() """ z, p = map(atleast_1d, (z, p)) if whole: lastpoint = 2 * pi else: lastpoint = pi if worN is None: N = 512 w = numpy.linspace(0, lastpoint, N, endpoint=False) elif isinstance(worN, int): N = worN w = numpy.linspace(0, lastpoint, N, endpoint=False) else: w = worN w = atleast_1d(w) zm1 = exp(1j * w) h = k * polyvalfromroots(zm1, z) / polyvalfromroots(zm1, p) return w, h
def test_polyvalfromroots(self): # check exception for broadcasting x values over root array with # too few dimensions assert_raises(ValueError, poly.polyvalfromroots, [1], [1], tensor=False) # check empty input assert_equal(poly.polyvalfromroots([], [1]).size, 0) assert_(poly.polyvalfromroots([], [1]).shape == (0,)) # check empty input + multidimensional roots assert_equal(poly.polyvalfromroots([], [[1] * 5]).size, 0) assert_(poly.polyvalfromroots([], [[1] * 5]).shape == (5, 0)) # check scalar input assert_equal(poly.polyvalfromroots(1, 1), 0) assert_(poly.polyvalfromroots(1, np.ones((3, 3))).shape == (3,)) # check normal input) x = np.linspace(-1, 1) y = [x**i for i in range(5)] for i in range(1, 5): tgt = y[i] res = poly.polyvalfromroots(x, [0]*i) assert_almost_equal(res, tgt) tgt = x*(x - 1)*(x + 1) res = poly.polyvalfromroots(x, [-1, 0, 1]) assert_almost_equal(res, tgt) # check that shape is preserved for i in range(3): dims = [2]*i x = np.zeros(dims) assert_equal(poly.polyvalfromroots(x, [1]).shape, dims) assert_equal(poly.polyvalfromroots(x, [1, 0]).shape, dims) assert_equal(poly.polyvalfromroots(x, [1, 0, 0]).shape, dims) # check compatibility with factorization ptest = [15, 2, -16, -2, 1] r = poly.polyroots(ptest) x = np.linspace(-1, 1) assert_almost_equal(poly.polyval(x, ptest), poly.polyvalfromroots(x, r)) # check multidimensional arrays of roots and values # check tensor=False rshape = (3, 5) x = np.arange(-3, 2) r = np.random.randint(-5, 5, size=rshape) res = poly.polyvalfromroots(x, r, tensor=False) tgt = np.empty(r.shape[1:]) for ii in range(tgt.size): tgt[ii] = poly.polyvalfromroots(x[ii], r[:, ii]) assert_equal(res, tgt) # check tensor=True x = np.vstack([x, 2*x]) res = poly.polyvalfromroots(x, r, tensor=True) tgt = np.empty(r.shape[1:] + x.shape) for ii in range(r.shape[1]): for jj in range(x.shape[0]): tgt[ii, jj, :] = poly.polyvalfromroots(x[jj], r[:, ii]) assert_equal(res, tgt)
def freqs_zpk(z, p, k, worN=None): """ Compute frequency response of analog filter. Given the zeros `z`, poles `p`, and gain `k` of a filter, compute its frequency response:: (jw-z[0]) * (jw-z[1]) * ... * (jw-z[-1]) H(w) = k * ---------------------------------------- (jw-p[0]) * (jw-p[1]) * ... * (jw-p[-1]) Parameters ---------- z : array_like Zeroes of a linear filter p : array_like Poles of a linear filter k : scalar Gain of a linear filter worN : {None, int, array_like}, optional If None, then compute at 200 frequencies around the interesting parts of the response curve (determined by pole-zero locations). If a single integer, then compute at that many frequencies. Otherwise, compute the response at the angular frequencies (e.g. rad/s) given in `worN`. Returns ------- w : ndarray The angular frequencies at which `h` was computed. h : ndarray The frequency response. See Also -------- freqs : Compute the frequency response of an analog filter in TF form freqz : Compute the frequency response of a digital filter in TF form freqz_zpk : Compute the frequency response of a digital filter in ZPK form Notes ----- .. versionadded: 0.19.0 Examples -------- >>> from scipy.signal import freqs_zpk, iirfilter >>> z, p, k = iirfilter(4, [1, 10], 1, 60, analog=True, ftype='cheby1', ... output='zpk') >>> w, h = freqs_zpk(z, p, k, worN=np.logspace(-1, 2, 1000)) >>> import matplotlib.pyplot as plt >>> plt.semilogx(w, 20 * np.log10(abs(h))) >>> plt.xlabel('Frequency') >>> plt.ylabel('Amplitude response [dB]') >>> plt.grid() >>> plt.show() """ k = np.asarray(k) if k.size > 1: raise ValueError('k must be a single scalar gain') if worN is None: w = findfreqs(z, p, 200, kind='zp') elif isinstance(worN, int): N = worN w = findfreqs(z, p, N, kind='zp') else: w = worN w = atleast_1d(w) s = 1j * w num = polyvalfromroots(s, z) den = polyvalfromroots(s, p) h = k * num / den return w, h