Initial commit
This commit is contained in:
parent
25d009587e
commit
ef4a5b7fd1
20 changed files with 1680 additions and 0 deletions
5
snippets/numpy/__init__.py
Normal file
5
snippets/numpy/__init__.py
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
|
||||
from . import core
|
||||
from .core import *
|
||||
from . import lib
|
||||
from .lib import *
|
||||
5
snippets/numpy/core/__init__.py
Normal file
5
snippets/numpy/core/__init__.py
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
|
||||
from .multiarray import *
|
||||
from .numeric import *
|
||||
from .fromnumeric import *
|
||||
from .shape_base import *
|
||||
50
snippets/numpy/core/fromnumeric.py
Normal file
50
snippets/numpy/core/fromnumeric.py
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
|
||||
from .overrides import set_module
|
||||
from .multiarray import asarray
|
||||
|
||||
@set_module('numpy')
|
||||
def prod(arr):
|
||||
result = 1
|
||||
for x in arr:
|
||||
result = result * x
|
||||
return result
|
||||
|
||||
def size(a, axis=None):
|
||||
"""
|
||||
Return the number of elements along a given axis.
|
||||
Parameters
|
||||
----------
|
||||
a : array_like
|
||||
Input data.
|
||||
axis : int, optional
|
||||
Axis along which the elements are counted. By default, give
|
||||
the total number of elements.
|
||||
Returns
|
||||
-------
|
||||
element_count : int
|
||||
Number of elements along the specified axis.
|
||||
See Also
|
||||
--------
|
||||
shape : dimensions of array
|
||||
ndarray.shape : dimensions of array
|
||||
ndarray.size : number of elements in array
|
||||
Examples
|
||||
--------
|
||||
>>> a = np.array([[1,2,3],[4,5,6]])
|
||||
>>> np.size(a)
|
||||
6
|
||||
>>> np.size(a,1)
|
||||
3
|
||||
>>> np.size(a,0)
|
||||
2
|
||||
"""
|
||||
if axis is None:
|
||||
try:
|
||||
return a.size
|
||||
except AttributeError:
|
||||
return asarray(a).size
|
||||
else:
|
||||
try:
|
||||
return a.shape[axis]
|
||||
except AttributeError:
|
||||
return asarray(a).shape[axis]
|
||||
18
snippets/numpy/core/multiarray.py
Normal file
18
snippets/numpy/core/multiarray.py
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
from ulab import numpy as np
|
||||
|
||||
def asarray(a, dtype=None):
|
||||
if isinstance(a,(np.ndarray)):
|
||||
return a
|
||||
try:
|
||||
a = np.array(a)
|
||||
return a
|
||||
except Exception as e:
|
||||
if "can't convert complex to float" in e.args:
|
||||
try:
|
||||
a = np.array(a, dtype=np.complex)
|
||||
return a
|
||||
except:
|
||||
pass
|
||||
raise ValueError('Could not cast %s to array' % (a))
|
||||
|
||||
|
||||
3
snippets/numpy/core/numeric.py
Normal file
3
snippets/numpy/core/numeric.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
|
||||
from .multiarray import asarray
|
||||
|
||||
16
snippets/numpy/core/overrides.py
Normal file
16
snippets/numpy/core/overrides.py
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
|
||||
import sys
|
||||
|
||||
def set_module(module):
|
||||
"""Decorator for overriding __module__ on a function or class.
|
||||
Example usage::
|
||||
@set_module('numpy')
|
||||
def example():
|
||||
pass
|
||||
assert example.__module__ == 'numpy'
|
||||
"""
|
||||
def decorator(func):
|
||||
if module is not None:
|
||||
sys.modules[func.__globals__['__name__']] = module
|
||||
return func
|
||||
return decorator
|
||||
16
snippets/numpy/core/shape_base.py
Normal file
16
snippets/numpy/core/shape_base.py
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
from ulab import numpy as np
|
||||
from .multiarray import asarray
|
||||
|
||||
def atleast_1d(*arys):
|
||||
res = []
|
||||
for ary in arys:
|
||||
ary = asarray(ary)
|
||||
if not isinstance(ary,(np.ndarray)):
|
||||
result = ary.reshape((1,))
|
||||
else:
|
||||
result = ary
|
||||
res.append(result)
|
||||
if len(res) == 1:
|
||||
return res[0]
|
||||
else:
|
||||
return res
|
||||
3
snippets/numpy/lib/__init__.py
Normal file
3
snippets/numpy/lib/__init__.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
|
||||
from .function_base import *
|
||||
from .polynomial import *
|
||||
11
snippets/numpy/lib/function_base.py
Normal file
11
snippets/numpy/lib/function_base.py
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
from ulab import numpy as np
|
||||
from ..core.multiarray import asarray
|
||||
|
||||
def append(arr, values, axis=None):
|
||||
arr = asarray(arr)
|
||||
if axis is None:
|
||||
if len(arr.shape) != 1:
|
||||
arr.flatten()
|
||||
values.flatten()
|
||||
axis = len(arr.shape)-1
|
||||
return np.concatenate((arr, values), axis=axis)
|
||||
37
snippets/numpy/lib/polynomial.py
Normal file
37
snippets/numpy/lib/polynomial.py
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
|
||||
from ..core import (atleast_1d, asarray)
|
||||
from ..core.overrides import set_module
|
||||
from ulab import numpy as np
|
||||
|
||||
@set_module('numpy')
|
||||
def poly(seq_of_zeros):
|
||||
seq_of_zeros = atleast_1d(seq_of_zeros)
|
||||
sh = seq_of_zeros.shape
|
||||
|
||||
if len(sh) == 2 and sh[0] == sh[1] and sh[0] != 0:
|
||||
seq_of_zeros = eigvals(seq_of_zeros)
|
||||
elif len(sh) == 1:
|
||||
dt = seq_of_zeros.dtype
|
||||
# Let object arrays slip through, e.g. for arbitrary precision
|
||||
if dt != object:
|
||||
seq_of_zeros = seq_of_zeros #seq_of_zeros.astype(mintypecode(dt.char))
|
||||
else:
|
||||
raise ValueError("input must be 1d or non-empty square 2d array.")
|
||||
|
||||
if len(seq_of_zeros) == 0:
|
||||
return 1.0
|
||||
dt = seq_of_zeros.dtype
|
||||
a = np.ones((1,), dtype=dt)
|
||||
|
||||
for k in range(len(seq_of_zeros)):
|
||||
a = np.convolve(a, np.array([1, -seq_of_zeros[k]], dtype=dt))
|
||||
|
||||
if a.dtype == np.complex:
|
||||
# if complex roots are all complex conjugates, the roots are real.
|
||||
roots = asarray(seq_of_zeros, complex)
|
||||
p = np.sort_complex(roots)
|
||||
c = np.real(p) - np.imag(p) * 1j
|
||||
q = np.sort_complex(c)
|
||||
if np.all(p == q):
|
||||
a = a.real.copy()
|
||||
return a
|
||||
3
snippets/scipy/__init__.py
Normal file
3
snippets/scipy/__init__.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
|
||||
from . import signal
|
||||
from .signal import *
|
||||
2
snippets/scipy/signal/__init__.py
Normal file
2
snippets/scipy/signal/__init__.py
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
|
||||
from .filter_design import *
|
||||
1441
snippets/scipy/signal/filter_design.py
Normal file
1441
snippets/scipy/signal/filter_design.py
Normal file
File diff suppressed because it is too large
Load diff
17
snippets/tests/numpy/core/fromnumeric.py
Normal file
17
snippets/tests/numpy/core/fromnumeric.py
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import math
|
||||
import sys
|
||||
sys.path.append('.')
|
||||
|
||||
from snippets import numpy
|
||||
from ulab import numpy as np
|
||||
|
||||
a = np.array([[1,2,3],[4,5,6]])
|
||||
print(numpy.size(a))
|
||||
print(numpy.size(a,1))
|
||||
print(numpy.size(a,0))
|
||||
|
||||
print(numpy.prod([1, 10, 100, 5]))
|
||||
print(numpy.prod([]))
|
||||
print(numpy.prod([1.,2.]))
|
||||
|
||||
|
||||
6
snippets/tests/numpy/core/fromnumeric.py.exp
Normal file
6
snippets/tests/numpy/core/fromnumeric.py.exp
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
6
|
||||
3
|
||||
2
|
||||
5000
|
||||
1
|
||||
2.0
|
||||
9
snippets/tests/numpy/core/multiarray.py
Normal file
9
snippets/tests/numpy/core/multiarray.py
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
import math
|
||||
import sys
|
||||
sys.path.append('.')
|
||||
|
||||
from snippets import numpy
|
||||
|
||||
print (numpy.asarray([1]))
|
||||
print (numpy.asarray([1.0, 2.0, 3j]))
|
||||
print (numpy.asarray([4, 3, 1, (2-2j), (2+2j), (2-1j), (2+1j), (2-1j), (2+1j), (1+1j), (1-1j)]))
|
||||
3
snippets/tests/numpy/core/multiarray.py.exp
Normal file
3
snippets/tests/numpy/core/multiarray.py.exp
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
array([1.0], dtype=float64)
|
||||
array([1.0+0.0j, 2.0+0.0j, 0.0+3.0j], dtype=complex)
|
||||
array([(4.0+0.0j, 3.0+0.0j, 1.0+0.0j, 2.0-2.0j, 2.0+2.0j, 2.0-1.0j, 2.0+1.0j, 2.0-1.0j, 2.0+1.0j, 1.0+1.0j, 1.0-1.0j], dtype=complex)
|
||||
11
snippets/tests/numpy/lib/function_base.py
Normal file
11
snippets/tests/numpy/lib/function_base.py
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
from ulab import numpy as np
|
||||
from ..core.multiarray import asarray
|
||||
|
||||
def append(arr, values, axis=None):
|
||||
arr = asarray(arr)
|
||||
if axis is None:
|
||||
if len(arr.shape) != 1:
|
||||
arr.flatten()
|
||||
values.flatten()
|
||||
axis = len(arr.shape)-1
|
||||
return np.concatenate((arr, values), axis=axis)
|
||||
23
snippets/tests/scipy/signal/filter_design.py
Normal file
23
snippets/tests/scipy/signal/filter_design.py
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
import math
|
||||
import sys
|
||||
sys.path.append('.')
|
||||
|
||||
from snippets import scipy
|
||||
from ulab import numpy as np
|
||||
|
||||
a = [4, 3, 1, 2-2j, 2+2j, 2-1j, 2+1j, 2-1j, 2+1j, 1+1j, 1-1j]
|
||||
print('_cplxreal: ', scipy.cplxreal(a))
|
||||
|
||||
nyquistRate = 48000 * 2
|
||||
centerFrequency_Hz = 480.0
|
||||
lowerCutoffFrequency_Hz = centerFrequency_Hz/math.sqrt(2)
|
||||
upperCutoffFrequenc_Hz = centerFrequency_Hz*math.sqrt(2)
|
||||
wn = np.array([ lowerCutoffFrequency_Hz, upperCutoffFrequenc_Hz])/nyquistRate
|
||||
|
||||
#print('butter: ', scipy.butter(N=4, Wn=wn, btype='bandpass', analog=False, output='ba'))
|
||||
#print('butter: ', scipy.butter(N=4, Wn=wn, btype='bandpass', analog=False, output='zpk'))
|
||||
print('butter: ', scipy.butter(N=4, Wn=wn, btype='bandpass', analog=False, output='sos'))
|
||||
#print('butter: ', scipy.butter(N=4, Wn=wn, btype='bandstop', analog=False, output='ba'))
|
||||
#print('butter: ', scipy.butter(N=4, Wn=wn, btype='lowpass', analog=False, output='ba'))
|
||||
#print('butter: ', scipy.butter(N=4, Wn=wn, btype='highpass', analog=False, output='ba'))
|
||||
|
||||
1
snippets/tests/scipy/signal/filter_design.py.exp
Normal file
1
snippets/tests/scipy/signal/filter_design.py.exp
Normal file
|
|
@ -0,0 +1 @@
|
|||
butter: (array([9.375938694653579e-10, 0.0, -3.750375477861432e-09, 0.0, 5.625563216792147e-09, 0.0, -3.750375477861432e-09, 0.0, 9.375938694653579e-10], dtype=float64), array([1.0, -7.969992171296993, 27.79137077985833, -55.37836320535709, 68.97098091968704, -54.97798122059838, 27.39096409669159, -7.798371663621388, 0.9713924646368845], dtype=float64))
|
||||
Loading…
Reference in a new issue