0

I share this python code that I extracted from here: Plotting a fast Fourier transform in Python. I am making a Fourier spectrum, and then I would like to 'pick' to get the value from a specific point on the plot. In the next picture you can see the 50 Hz frequency, but I would like to know the frequency value of the next peak. How could I find that?

enter image description here

import numpy as np
import matplotlib.pyplot as plt
import scipy.fftpack

N = 600
# sample spacing
T = 1.0 / 800.0
x = np.linspace(0.0, N*T, N)
y = np.sin(50.0 * 2.0*np.pi*x) + 0.5*np.sin(80.0 * 2.0*np.pi*x)
yf = scipy.fftpack.fft(y)
xf = np.linspace(0.0, 1.0/(2.0*T), N//2)

fig, ax = plt.subplots()
ax.plot(xf, 2.0/N * np.abs(yf[:N//2]))
plt.show()
tdy
  • 36,675
  • 19
  • 86
  • 83
Miguel
  • 55
  • 6

1 Answers1

0

Outcome of the following code resolves peaks as 50.836120401337794

import numpy as np
import matplotlib.pyplot as plt
import scipy.fftpack

# Set the parameters for the signal and the Fourier transform
N = 600
T = 1.0 / 800.0
x = np.linspace(0.0, N*T, N)
y = np.sin(50.0 * 2.0*np.pi*x) + 0.5*np.sin(80.0 * 2.0*np.pi*x)
yf = scipy.fftpack.fft(y)
xf = np.linspace(0.0, 1.0/(2.0*T), N//2)

# Find the indices of the peaks in the spectrum
peak_indices = np.argmax(np.abs(yf[:N//2]), axis=0)

# Get the corresponding frequency values from the xf array
peaks = xf[peak_indices]

# Print the frequency values of the peaks
print(peaks)

# Generate the Fourier spectrum plot
fig, ax = plt.subplots()
ax.plot(xf, 2.0/N * np.abs(yf[:N//2]))
plt.show()
Jacob Webb
  • 84
  • 1
  • 8