Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add complex support #366

Closed
wants to merge 23 commits into from
Closed

add complex support #366

wants to merge 23 commits into from

Conversation

v923z
Copy link
Owner

@v923z v923z commented Apr 20, 2021

This PR adds rudimentary complex support to ulab. Detailed discussion can be found in #364.

At the moment, complex arrays can be initialised from complex iterables. E.g.,

from ulab import numpy as np
a = np.array([1, 2+2j, 3, 4], dtype=np.complex)
print(a)

behaves as expected. In addition, the PR also implements the real and imaginary functions for ndarrays, i.e., the following works:

from ulab import numpy as np

a = np.array([1, 2+2j, 3, 4], dtype=np.complex)
b = np.array([1, 2, 3, 4], dtype=np.uint8)

print(a)
print(np.real(a))
print(np.imag(a))
print(np.real(b))
print(np.imag(b))

@wired8
Copy link
Contributor

wired8 commented Nov 10, 2021

Thanks for this. I've tried running on an ESP32 with MicroPython v1.17 but get kernel panics when doing any operations.
Sample stack trace:
ndarray_make_new_core
ndarray.c:?
ndarray_array_constructor

Any ideas what I may have done wrong?

@v923z
Copy link
Owner Author

v923z commented Nov 10, 2021

@wired8 This draft is based on an older version of ulab, so I am pretty sure that it's not going to fly. Do you need complex support? If so, what would you need it for? This whole issue came up in the context of eigenvalues and eigenvectors, but your requirements could be different, I don't know.

@wired8
Copy link
Contributor

wired8 commented Nov 11, 2021

@v923z thanks for the quick response.

Specifically I'm trying to port some filters from scipy.

The buttap function uses complex numbers.

def buttap(N):
    """Return (z,p,k) for analog prototype of Nth-order Butterworth filter.

    The filter will have an angular (e.g., rad/s) cutoff frequency of 1.

    See Also
    --------
    butter : Filter design function using this prototype

    """
    if abs(int(N)) != N:
        raise ValueError("Filter order must be a nonnegative integer")
    z = numpy.array([])
    m = numpy.arange(-N+1, N, 2)
    # Middle value is 0 to ensure an exactly real pole
    p = -numpy.exp(1j * np.pi * m / (2 * N))
    k = 1
    return z, p, k

@v923z
Copy link
Owner Author

v923z commented Nov 11, 2021

The exp function could be solved as cos() + 1j * sin(), somewhat similar to how the FFT is handled now. But I assume that, after you generate the filter coefficients, you want to pipe your data into lfilter, or something similar. Is that correct?
What I am trying to say is that complex is most probably only the first stage of what you need.

@wired8
Copy link
Contributor

wired8 commented Nov 11, 2021

Correct, data is then piped through one of low/high/band or bandstop filter.

@v923z
Copy link
Owner Author

v923z commented Nov 11, 2021

@wired8 I think I can relatively easily fix the complex code, but if you want to use it, we have to implement the filter functions in question. In order to not invest effort, where it is not needed, could you, please, point out which are the most important functions that you would need. Preferably, in order of importance.

EDIT: could you, please, open a new ticket for each function, with a link to the scipy documentation? Thanks!

@wired8
Copy link
Contributor

wired8 commented Nov 11, 2021

#447
#448
#449

Thanks @v923z

@v923z v923z mentioned this pull request Nov 23, 2021
@v923z
Copy link
Owner Author

v923z commented Nov 23, 2021

Closing this now, on-going work is in #456

@v923z v923z closed this Nov 23, 2021
@v923z v923z deleted the complex branch January 8, 2022 19:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants