2

I'm trying to do some tests before I proceed analyzing some real dataset via FFT, and I've found the following problem.

First, I create a signal as the sum of two cosines and then use rfft to to the transformation (since it has only real values):

import numpy as np
import matplotlib.pyplot as plt
from scipy.fft import rfft, rfftfreq

# Number of sample points
N = 800
# Sample spacing
T = 1.0 / 800.0

x = np.linspace(0.0, N*T, N)
y = 0.5*np.cos(10*2*np.pi*x) + 0.5*np.cos(200*2*np.pi*x)

# FFT
yf = rfft(y)
xf = rfftfreq(N, T)

fig, ax = plt.subplots(1,2,figsize=(15,5))
ax[0].plot(x,y)
ax[1].plot(xf, 2.0/N*np.abs(yf))

As it can be seen from the definition of the signal, I have two oscillations with amplitude 0.5 and frequency 10 and 200. Now, I would expect the FFT spectrum to be something like two deltas at those points, but apparently increasing the frequency broadens the peaks:

FFT

From the first peak it can be infered that the amplitude is 0.5, but not for the second. I've tryied to obtain the area under the peak using np.trapz and use that as an estimate for the amplitude, but as it is close to a dirac delta it's very sensitive to the interval I choose. My problem is that I need to get the amplitude as exact as possible for my data analysis.

EDIT: As it seems to be something related with the number of points, I decided to increment (now that I can) the sample frequency. This seems to solve the problem, as it can be seen in the figure:

FFT with more points

However, it still seems strange that for a certain number of points and sample frequency, the high frequency peaks broaden...

  • Does this answer your question? [Detect peaks in list of numbers and record their positions](https://stackoverflow.com/questions/53914486/detect-peaks-in-list-of-numbers-and-record-their-positions) – goalie1998 Jan 26 '21 at 20:48
  • If you can (somehow) derive an analytical expression for the peak then you can infer its height by fitting, which might be more accurate than numerical integration. – Yang Yushi Jan 26 '21 at 21:09

2 Answers2

2

It is not strange , you have leakage of the frequency bins. When you discretize the signal (sampling) needed for the Fourier transfrom , frequency bins are created which are frequency intervals where the the amplitude is calculated. And each bin has wide which is given by the sample_rate / num_points . So , the less the number of bins the more difficult is to assign precise amplitudes to every frequency. Other problems in choosing the best sampling rate exist such as the shannon-nyquist theorem to prevent aliasing. https://en.wikipedia.org/wiki/Nyquist%E2%80%93Shannon_sampling_theorem . But depending on the problem sometimes there some custom rates used for sampling. E.g. when dealing with audio a sampling rate of 44,100 Hz is widely used , cause is based on the limits of the human hearing. So it depends also on nature of the data you want to perform analysis as you wrote. Anyway , since this question has also theoretical value , you can also check https://dsp.stackexchange.com for some useful info.

George
  • 328
  • 1
  • 9
0

I would comment to George's answer, but yet I cannot.

Maybe a starting point for your research are the properties of the Discrete Fourier Transform. The signal in the time domain is actual the cosines multiplied by a box window which transforms into the frequency domain as the convolution of the deltas with the sinc function. The sinc functions will smear the spectrum.

However, I am not sure we are observing spectral leakage here, since the window fits exactly to the full period of cosines. The discretization of the bins might still play a role here.

mss
  • 348
  • 2
  • 12
  • You are right , but I wasn't referring to this spectral leakage . I referred to the leakage of the frequency bins to others that are not supposed to . I called it spectral leakage since stack overflow it's not about the theoretical topics and I just name it like that while trying to give a small compact answer that is directly correlated with the problem he is facing , which is definitely the discretization of the bins. But sure the formal definition is the one you stated. That's the reason I suggested dsp.stackexhange.com for further analyzing this topic. – George Jan 26 '21 at 22:35