Fourier (four): Fourier transform (FFT) to find the period of the time series

  • Fourier series : It is a periodic and continuous function in the time domain , and a non-periodic discrete function in the frequency domain.
  • Fourier transform: The Fourier transform we will talk about next is to convert a non-periodic continuous signal in the time domain into a non-periodic continuous signal in the frequency domain .

First, let's understand what frequency is:

        The general interpretation is ----- frequency, which is the number of times periodic changes are completed per unit of time . It is a quantity that describes the frequency of periodic motion. It is often represented by the symbol f or ν, and the unit is one second of a second. Hz is the basic unit of frequency. , usually the number of actions completed in 1 second. For example, if you can eat 3 steamed buns in 1 second, it will be recorded as 3Hz (3 Hz) when you eat steamed buns. For example, if your eyelids can twitch 10 times in 1 second, it will be recorded as 10 Hz (10 Hz).

import numpy as np
import matplotlib.pyplot as plt
import numpy as np

fs = 100  # frequency: 100  Hz
Fs = 1000 # sampling frequency: 1000 Hz

dt = 1/Fs # sampling period
N = 2048

T = N * dt # span

t = np.linspace(0, T, N, endpoint = False) # time
data = np.cos(2 * np.pi * fs * t) + np.random.normal(scale = 0.2, size = len(t))

# data = np.random.randint(6, 10, 300)  # 生成随机数
X = np.fft.fft(data) # Discrete Fourier Transform by fft
X = np.abs(X)
plt.plot(data[:100])
plt.show()
plt.plot(X)
plt.show()
print(1)

mean_X = np.mean(X)
distance = (X-mean_X) ** 2
mean_distance = np.mean(distance)
frequency = [i for i in range(len(distance)) if distance[i] > 0.8 * mean_distance]

length = len(X)
if len(frequency) > 2:
    if frequency[0] == 0:
        period = length // frequency[1]  # length个点中,完成了frequency[1]个周期
        if period >= 0.5 * length:
            print("none")
        print(period)
    else:
        period = length // frequency[1]
        if period >= 0.5 * length:
            print("none")
        print(period)
else:
    print("none")

Guess you like

Origin blog.csdn.net/weixin_39910711/article/details/124642188