简体   繁体   中英

Why is my fourier transform negative in python? How do I fix it?

I am trying to code a Fourier Transform in Python so I can take the transform some data from a couple signals, but for for some reason, my results have a weird negative component to them. Searching around the web, I cannot figure out what I am doing wrong.

#Library function calls
import scipy.fft as ft #This library helps with performing the transform
import matplotlib.pyplot as plt #This will allow us to plot the data
import numpy as np #This will allow us to use arrays

#Import the textfiles made by LabView
time, radio, trial = np.loadtxt("trialC4.xls",float,unpack=True)
print("time:", time)
print("")
print("radio:", radio)
print("")
print("trial:", trial)

#Take the Fourier Transform of the signals
fft_radio = ft.rfft(radio)
fft_trial = ft.rfft(trial)

#Plot the signals
plt.plot(time,radio)
plt.plot(time,trial)
plt.show()
plt.plot(fft_radio)
plt.show()
plt.plot(fft_trial)
plt.show

Here are the results of this code

The signals before being transformed:
转换前的信号。

The first signal after transformation:
转换后的第一个信号。

The second signal after transformation:
变换后的第二个信号。

What do I need to do so the transformed signals are not negative?

Fourier transforms always go from complex numbers to complex numbers. Scalar values are also elements of the set of complex numbers (just with the imaginary component being 0).

And complex numbers live in the complex plane, where you can also have negative values for the imaginary and real component respective.

If you want the spectrum power density, you must take the absolute value of each channel (ie the magnitude), calculated as sqrt(Re(x)² + Im(x)²)

With numpy you can put them through a simple numpy.abs(fft(...))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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