pythonnumpyffttrigonometryamplitude

Numpy FFT issue when shifting data along vertical axis


I want to find the relationship between the y-axis of my data and the vertical axis of my FFT (amplitude). To do this I am testing how the amplitude of my FFT changes when I change the y-axis of my data. For example, I plotted sin(t) from 0 to 2*pi and took the FFT using Numpy's FFT package and got a frequency of approximately 1/(2*pi). I then added 1 to all of my y-values merely to shift my sine function up 1 unit everywhere; however, when I took the FFT, I got something that made no sense. Why is the FFT totally different when I merely shift my sine function upwards? Any help would be greatly appreciated. Thank you in advance.

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

t = np.linspace(0, 2*math.pi, 10)
y1 = np.sin(t)
y2 = np.sin(t) + 1
plt.plot(t, y2)    # y1 can be used instead
plt.xlabel('time')
plt.ylabel('height')
plt.show()

fft_power = fft.fft(y2)    # y1 can be used instead
rfft_power = fft.rfft(y2)    # y1 can be used instead

sample_spacing = 0.6981317

frequency = fft.fftfreq(len(fft_power), sample_spacing)
real_frequency = fft.rfftfreq(len(fft_power), sample_spacing)

plt.plot(real_frequency.real, rfft_power.real)
plt.xlabel('frequency')
plt.ylabel('amplitude')
plt.show()

initial data FFT shifted data FFT


Solution

  • Try this, it will convince you that all is working well:

    t = np.linspace(0, 2*math.pi, 10000)
    y2 = np.sin(200*t) + 1
    

    The 1 adds a very strong peak at 0 frequency. But the sin peak is also there.