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

Viewed 279

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 signals before being transformed.

The first signal after transformation:
The first signal after transformation.

The second signal after transformation:
The second signal after transformation.

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

1 Answers

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 (i.e. the magnitude), calculated as sqrt(Re(x)² + Im(x)²)

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

Related