简体   繁体   English

如何使用 np.fft.fft function 进行傅里叶变换?

[英]How can I take the Fourier Transform using np.fft.fft function?

I want to generate two sinusoidal signals where =5 Hz and f2=3 Hz, the time duration of the signal is =1.我想生成两个正弦信号,其中 =5 Hz 和 f2=3 Hz,信号的持续时间为 =1。 x1(t)=sin(2) X2(t)=sin(2(1+f2)) X()=1()+2() So, I will try to analyze the frequency domain respresentation of this signal. x1(t)=sin(2) X2(t)=sin(2(1+f2)) X()=1()+2()因此,我将尝试分析该信号的频域表示。 How can I take the Fourier Transform using np.fft.fft function by using DFT size =64, sampling frequency =64 and =1 that is the time duration that you will have samples.如何使用 np.fft.fft function 通过使用 DFT 大小 =64、采样频率 =64 和 =1(即您将拥有样本的持续时间)进行傅里叶变换。 In the end, I want to plot () and () which is the Fourier Transform of x(t).最后我要plot()和()也就是x(t)的傅里叶变换。 Thanks, If you help me, I will be grateful to you.谢谢,如果你能帮助我,我会很感激你。

In the end, it will be seen how to distinguish two signals.最后,将看到如何区分两个信号。 Thanks for everything for everyone.感谢为大家所做的一切。

It is not entirely clear what the final signal is you want to do a Fourier Transform on, but I made the following assumptions:尚不完全清楚您想对其进行傅立叶变换的最终信号是什么,但我做了以下假设:

  • your time domain signal x(t)=sin(21)+sin(22) .您的时域信号x(t)=sin(21)+sin(22) From what your wrote it wasn't entirely clear从你写的内容来看还不完全清楚
  • you want to have the FFT of above x(t)你想要x(t)以上的 FFT
  • your total time T=1s (you wrote T=1 , units are important)你的总时间T=1s (你写了T=1 ,单位很重要)
  • your sampling frequency fs=64 sample/s (you wrote fs=64 , units are important)你的采样频率fs=64 sample/s (你写的fs=64 ,单位很重要)

With these givens, you can do something like the following:有了这些给定的信息,您可以执行以下操作:

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

# Entering your input variables
fs = 64
T = 1
f1 = 3
f2 = 5

# Creating the time and frequency axis
time = np.arange(0, T, 1/fs)
freq = np.arange(0, fs/2, 1/T)

# Creating the time domain x signal
x1 = np.sin(2*math.pi*f1*time)
x2 = np.sin(2*math.pi*f2*time)
x = x1 + x2

# Making the FFT, while normalizing to make sure the amplitude is correct
X = np.fft.fft(x)/len(time)*2

# Plotting the time domain + frequency domain signals on a subplot. For the
# frequency domain plot, we only take the first half of the frequency axis
# (going up to fs/2) because we're dealing with a real signal.
fig, axs = plt.subplots(2,1)
axs[0].plot(time, x)
axs[0].set_xlabel('Time [s]')
axs[0].set_ylabel('Amplitude')
axs[1].plot(freq, abs(np.split(X,2)[0]), "x")
axs[1].set_xlabel('Frequency [Hz]')
axs[1].set_ylabel('Amplitude')
plt.show()

Out of this, you get the following plot:由此,您将获得以下 plot:

在此处输入图像描述

As you can see, you clearly see peaks with amplitude 1 on 3 and 5 Hz: :)如您所见,您清楚地看到在 3 赫兹和 5 赫兹上振幅为 1 的峰值::)

Now, it's possible I didn't perfectly understand what your x(t) signal needed to be, but you can easily change the definitions of x1 , x2 and x and get the output you want.现在,我可能没有完全理解您的x(t)信号需要什么,但您可以轻松更改x1x2x的定义并获得您想要的 output 。

Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM