0

I am trying to use numpy fft to plot some data from a dataframe :

plt.plot(np.fft.fft(df_valid_daily_activity.stepsDaily))

I get this : enter image description here

I don't understand why the plot is so steep in the beginning and then seems to stabilise? Also I get this warning :

Casting complex values to real discards the imaginary part
  return array(a, dtype, copy=False, order=order)

example of the data I am trying to plot :

2      12693.0
3      18387.0
4      18360.0
5      11684.0
6      12722.0
        ...   
273    27836.0
274    15566.0
280     7836.0
281    17787.0
284     7739.0
Name: stepsDaily, Length: 199, dtype: float64 

Any ideas why ? Thanks!

Edit: tried subtracting mean - still looks weird enter image description here

2
  • The 0-th coefficient is the mean of the transformed time series. You can subtract the mean from the data before applying FFT to get rid of it. Commented Aug 13, 2020 at 12:54
  • Hey! I just tried that, it returns a mirrored graph, is that normal ? Commented Aug 13, 2020 at 12:57

2 Answers 2

1

The function you’re using is a full complex Fourier transform: when applied to real data it will be symmetrical about zero. Two things you could do: use np.fft.fftshift to shift the data such that the zero frequency is in the middle (or use np.fft.fftfreq to calculate the frequencies) or use np.fft.rfft which is a transform for real data and will return half the full FFT.

It would be good to know your intended use of the FFT. Most people (myself included) are really only interested in what frequencies are present in the data. For that a plot of the magnitude squared (usually on a logarithmic scale) of the FFT can be used.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi! thanks for answering. I am trying to look for cycles in lifelog data, fx I want to see if a person has similarities in their daily behaviours such as when they are active during the day. But I am a little confused about how the Fourier transform works
I have a dataframe which contains steps taken per hour for x days, and I would like to see if there are any peaks within 24 hours over the x days, if that makes sense
Stackoverflow is probably not the best place to ask algorithmic specific questions. Regardless, a Fourier transform can be thought of as a decomposition of your signal into sine waves of different frequencies. For example, if every morning the person walks his/her dog then you’ll see a peak in the FFT at a frequency of 1 / 24 hrs. If this person walks the dog morning, noon, and, night you’ll see another peak at 1 / 6 hrs. That being said, I don’t think the Fourier transform is the best tool for your problem.
1

I guess you should try it with logscales plots. At first, I suggest using numpy.fft.fftshift to Shift the zero-frequency component to the center of the spectrum.

import random
import matplotlib.pyplot as plt
import numpy as np
f = [random.randint(5000, 20000) for i in range(300)]
ff = np.fft.fftshift(f)

Then you can plot them in 'semilogx', 'semilogy', and 'loglog' scale.

Semi Log X: Semi Log X Semi Log Y: Semi Log Y Log Scale Both: Log Scale Both

1 Comment

Hey! I tried with np.fft.fftshift, but I am not quite sure how to interpret, is there a way to specify periods to look for in the plot ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.