I have implemented the 3Blue1Brown description of the Fourier transform in Python + numpy for irregular and unclassified data, as described here.
import numpy as np import matplotlib.pyplot as plt def polarToRectangular(radii, angles): return radii * np.exp(1j * angles) def frequencyGenerator(time, steps = 100): 𝛿 = time.max() - time.min() M = np.arange(1, steps + 1)(:, np.newaxis) return M / 𝛿 def easyFourierTransform(time, values, frequency = None, steps = 100): if frequency is None: ft = frequencyGenerator(time, steps) frequency = ft.reshape(ft.shape(0)) else: ft = frequency(:, np.newaxis) # sorting the inputs order = np.argsort(time) ts = np.array(time)(order) Xs = np.array(values)(order) 𝜃 = (ts - time.min()) * 2 * np.pi * ft Y = polarToRectangular(Xs, 𝜃)(:, :-1) * np.diff(ts) amplitude = np.abs(Y.sum(axis=1)) return frequency, amplitude
I'm thinking maybe I can suggest that this be added to numpy / scypy. However, I am not sure if this small code snippet is qualified to be added upstream. I was wondering if you could help me know:
- Is this code correct? Does the Fourier transform really return? I want to be sure that there are no logical errors.
- Is this a performance code or is there any way to improve performance?
- Is the formatting good enough? Should I comply with the PEP8 standard or numpy / scipy require different best practices?
- How can I add typing annotations? Especially to make sure the dimensions of the ndarrays are fine.
- Is this novel or has it been done before? (not necessarily relevant to this forum but it's still my question)
I would appreciate if you could help me with the above points. Thanks in advance.
Keywords: non-uniform, uniform, uneven, sampled, distributed