|
|
|
#tests.ipynb [notebook](tests.ipynb) [html](tests.html)
|
|
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
import sys
|
|
|
|
from __future__ import division
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
import numpy as np
|
|
|
|
from phasor.utilities.ipynb.displays import *
|
|
|
|
from phasor.utilities.ipynb.sympy import *
|
|
|
|
|
|
|
|
import declarative
|
|
|
|
```
|
|
|
|
|
|
|
|
Populating the interactive namespace from numpy and matplotlib
|
|
|
|
Sympy version: 1.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
import phasor.utilities.version as version
|
|
|
|
print(version.phasor_git_version())
|
|
|
|
|
|
|
|
from phasor.utilities.np import logspaced
|
|
|
|
import scipy.signal
|
|
|
|
import iirrational
|
|
|
|
```
|
|
|
|
|
|
|
|
b'2017-10-09 22:05:43 -0400 (8b71fced6ae3697741af9b5085e7f7509ba0dc98)'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
F_nyquist = 16384 / 2
|
|
|
|
F_AC = F_nyquist * 2 * np.arange(0, 1001) / 301
|
|
|
|
|
|
|
|
F_AC = logspaced(1, 7000, 500)
|
|
|
|
ZPKz = (
|
|
|
|
[
|
|
|
|
.9,
|
|
|
|
-.3,
|
|
|
|
.1+.95j,
|
|
|
|
.1-.95j,
|
|
|
|
#.999 * np.exp(1j * np.pi * .001),
|
|
|
|
#.999 * np.exp(1j * np.pi * -.001),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
#.9,
|
|
|
|
.99,
|
|
|
|
#.999 * np.exp(1j * np.pi * .002),
|
|
|
|
#.999 * np.exp(1j * np.pi * -.002),
|
|
|
|
#.97,
|
|
|
|
-0.03+.95j,
|
|
|
|
-.03-.95j,
|
|
|
|
.95 + .2j,
|
|
|
|
.95 - .2j,
|
|
|
|
#-.2,
|
|
|
|
],
|
|
|
|
10
|
|
|
|
)
|
|
|
|
|
|
|
|
ZPKz = (
|
|
|
|
[
|
|
|
|
.9,
|
|
|
|
-.3,
|
|
|
|
.1+.95j,
|
|
|
|
.1-.95j,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
#.9,
|
|
|
|
.99,
|
|
|
|
#.97,
|
|
|
|
-0.03+.95j,
|
|
|
|
-.03-.95j,
|
|
|
|
#-.2,
|
|
|
|
],
|
|
|
|
10
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
b,a = scipy.signal.zpk2tf(*ZPKz)
|
|
|
|
|
|
|
|
|
|
|
|
Fb = mplfigB(Nrows=2)
|
|
|
|
w, h = scipy.signal.freqz_zpk(*ZPKz , worN = F_AC / F_nyquist * np.pi)
|
|
|
|
F_exact = h
|
|
|
|
Fb.ax0.loglog(F_AC, abs(h), label = 'discrete_ZPK')
|
|
|
|
Fb.ax1.semilogx(F_AC, np.angle(h), label = 'discrete_ZPK')
|
|
|
|
|
|
|
|
w, h = scipy.signal.freqz(b, a , worN = F_AC / F_nyquist * np.pi)
|
|
|
|
#F_exact = h
|
|
|
|
Fb.ax0.loglog(F_AC, abs(h), label = 'discrete_BA')
|
|
|
|
Fb.ax1.semilogx(F_AC, np.angle(h), label = 'discrete_BA')
|
|
|
|
|
|
|
|
Fb.ax0.legend()
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<matplotlib.legend.Legend at 0x7f2b55882240>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
b
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
array([ 10. , -8. , 7.625 , -4.935 , -2.46375])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
R = np.random.randn(len(F_AC)) + 1j*np.random.randn(len(F_AC))
|
|
|
|
F_noise = F_exact * (1 + R / 10)
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
|
|
|
fitz = [iirrational.fit_zeros(
|
|
|
|
F_AC,
|
|
|
|
F_noise,
|
|
|
|
W = 10,
|
|
|
|
#npoles = 10,
|
|
|
|
nzeros = len(ZPKz[0]),
|
|
|
|
F_nyquist = F_nyquist,
|
|
|
|
ZPK=ZPKz,
|
|
|
|
)]
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
fitz = iirrational.fit_series(
|
|
|
|
F_AC,
|
|
|
|
F_noise,
|
|
|
|
W = 10,
|
|
|
|
npoles_seq = np.arange(5, 10),
|
|
|
|
nzeros_seq = 7,
|
|
|
|
F_nyquist= F_nyquist,
|
|
|
|
max_size = 100,
|
|
|
|
func = iirrational.fit_pzpz,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
#fits
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
fitz = [iirrational.fit_poles(
|
|
|
|
F_AC,
|
|
|
|
F_noise,
|
|
|
|
W = 10,
|
|
|
|
npoles = len(ZPKz[1]),
|
|
|
|
F_nyquist = F_nyquist,
|
|
|
|
ZPK=ZPKz,
|
|
|
|
)]
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
fitz = [iirrational.fit_pzpz(
|
|
|
|
F_AC,
|
|
|
|
F_noise,
|
|
|
|
W = 10,
|
|
|
|
npoles = 10,
|
|
|
|
nzeros = 10,
|
|
|
|
F_nyquist = F_nyquist,
|
|
|
|
#ZPK=ZPKz,
|
|
|
|
)]
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
Fb = mplfigB(Nrows=3)
|
|
|
|
|
|
|
|
Fb.ax0.loglog(F_AC, abs(F_exact), label = 'Fexact')
|
|
|
|
Fb.ax1.semilogx(F_AC, np.angle(F_exact), label = 'Fex')
|
|
|
|
|
|
|
|
Fb.ax0.loglog(F_AC, abs(F_noise), label = 'Fnoise')
|
|
|
|
Fb.ax1.semilogx(F_AC, np.angle(F_noise), label = 'Fnoise')
|
|
|
|
|
|
|
|
|
|
|
|
for fB in fitz[:4]:
|
|
|
|
Fb.ax0.loglog(
|
|
|
|
fB.F_bal,
|
|
|
|
abs(fB.data_fit),
|
|
|
|
label = 'fit {npoles} ({res})'.format(
|
|
|
|
npoles = len(fB.ZPK[1]),
|
|
|
|
res = fB.res,
|
|
|
|
),
|
|
|
|
#color = 'green',
|
|
|
|
)
|
|
|
|
Fb.ax1.semilogx(fB.F_bal, np.angle(fB.data_fit), label = 'fit')
|
|
|
|
Fb.ax2.semilogx(fB.F_bal, abs(fB.resP), label = 'fit')
|
|
|
|
|
|
|
|
Fb.ax0.legend(ncol = 3, fontsize = 6)
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<matplotlib.legend.Legend at 0x7f2b5306ff28>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
fits = [iirrational.fit_pzpz(
|
|
|
|
F_AC,
|
|
|
|
F_noise,
|
|
|
|
W = 10,
|
|
|
|
npoles = 10,
|
|
|
|
nzeros = 10,
|
|
|
|
F_nyquist = None,
|
|
|
|
)]
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
Fb = mplfigB(Nrows=3)
|
|
|
|
|
|
|
|
Fb.ax0.loglog(F_AC, abs(F_exact), label = 'Fexact')
|
|
|
|
Fb.ax1.semilogx(F_AC, np.angle(F_exact), label = 'Fex')
|
|
|
|
|
|
|
|
Fb.ax0.loglog(F_AC, abs(F_noise), label = 'Fnoise')
|
|
|
|
Fb.ax1.semilogx(F_AC, np.angle(F_noise), label = 'Fnoise')
|
|
|
|
|
|
|
|
|
|
|
|
for fB in fits[:4]:
|
|
|
|
Fb.ax0.loglog(
|
|
|
|
fB.F_bal,
|
|
|
|
abs(fB.data_fit),
|
|
|
|
label = 'fit {npoles} ({res})'.format(
|
|
|
|
npoles = len(fB.ZPK[1]),
|
|
|
|
res = fB.res,
|
|
|
|
),
|
|
|
|
#color = 'green',
|
|
|
|
)
|
|
|
|
Fb.ax1.semilogx(fB.F_bal, np.angle(fB.data_fit), label = 'fit')
|
|
|
|
Fb.ax2.semilogx(fB.F_bal, abs(fB.resP), label = 'fit')
|
|
|
|
|
|
|
|
Fb.ax0.legend(ncol = 3, fontsize = 6)
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<matplotlib.legend.Legend at 0x7f2b556c1518>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
scipy.linalg.svd??
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
|
|
|
``` |