I am currently working on a way to deduce the fundamental period of a signal, where the only available information is the signal array. To do this i utilize the numpy FFT, and this I've gotten to work with signals consisting of single sinusoids. The method I use for this is as follows.
import numpy as np import matplotlib.pyplot as plt from scipy import signal %matplotlib widget def f(t): return np.cos(2*np.pi*t) def get_period(f): t = np.linspace(-1500,1500,100000) h = np.fft.fft(f(t)) h = np.fft.fftshift(h) N = len(h) omega_fft = np.fft.fftfreq(N,d=t[1]-t[0]) omega_fft = np.fft.fftshift(omega_fft) # getting peaks peaks = signal.find_peaks(h) peaks = peaks[0][len(peaks[0])//2:] f0 = omega_fft[peaks] T = 1/f0 return T[0] Im not sure if this is the smartes way of doing this, but it works with a high enough resolution. When multiple sinusoids are available, i would use the np.lcm.resduce, but this cant handle floating point numbers for obvious reasons. Is there a smart way of doing this as fractions, or a generally smarter way of doing this in general?
The concept:
def get_period(f): t = np.linspace(-1500,1500,100000) h = np.fft.fft(f(t)) h = np.fft.fftshift(h) N = len(h) omega_fft = np.fft.fftfreq(N,d=t[1]-t[0]) omega_fft = np.fft.fftshift(omega_fft) # getting peaks peaks = signal.find_peaks(h) peaks = peaks[0][len(peaks[0])//2:] if len(peaks) > 1: T = np.lcm.reduce(1/omega_fft[peaks]) else: f0 = omega_fft[peaks] T = 1/f0 return T[0]