0
$\begingroup$

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] 
$\endgroup$
6
  • $\begingroup$ What is your definition of the "fundamental period of a signal" for a signal consisting of multiple sinusoids? For example, what would you want to get from an audio clip? $\endgroup$ Commented Aug 19, 2024 at 16:09
  • $\begingroup$ I am working with clean periodic signals, so a fundamental period is the period for when the signal repeats itself i.e x(t)=x(t+T0), where the lowest value of T0 is the fundamental period for all t. A audio clip wouldnt have a fundamental period. $\endgroup$ Commented Aug 19, 2024 at 16:52
  • $\begingroup$ Ok. Then if you do an FFT of a perfectly periodic signal, the first peak that’s not DC would be the fundamental, wouldn’t it? $\endgroup$ Commented Aug 19, 2024 at 17:34
  • $\begingroup$ That is true, if the signal only consists of one sinusoid, which I explained in the post. This already works for me. When there are more sinusoids in the signal I get more than one peak in the frequency spectrum and therefore need to deduce a LCM for them such that a period that encapsulates all the frequencies can be found. $\endgroup$ Commented Aug 19, 2024 at 17:52
  • $\begingroup$ If there are many, yes, there will be more than one peak in the frequency spectrum. The first peak past DC is related to T0, if your signal is perfectly periodic. But from your response it seems that's not actually what you're looking for! Hence my previous question: what is the "fundamental frequency" you're referring to? Something that "encapsulates all the frequencies" isn't the same as the "fundamental frequency T0" $\endgroup$ Commented Aug 19, 2024 at 19:29

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.