![]() It is straightforward to generate their range based on the documentation of fftfreq alone: from numpy.fft import fftįrom numpy import arange, linspace, sin, pi as π You don't really need to rely on any of those functions operating on the frequencies. That's how the peaks ended up on the wrong side of the spectrum: You plotted the Fourier components referring to the positive half of the frequencies with respect to the negative half, so the peaks on the right are actually meant to be close to zero, not at the far end. In your code, you only shifted the frequencies. Though if you do, you must shift both arrays: the frequencies and the Fourier components. However, if you plot only half of the spectrum anyway, then you may as well not bother doing this at all. If plotting both halves, it may make sense to shift the frequency spectrum around, so that the negative half is to the left of the zero-component, the positive half to its right, meaning all values are in ascending order and ready to be plotted.įftshift does exactly that. ![]() In the spectrum returned by fft and the frequency range from fftfreq, it's at the very first array index. It's a measure of the average value of the signal, its offset from zero. There is one frequency that stands out: f = 0. For that reason, it often doesn't make sense to plot both halves of the spectrum, as they contain the same information. In that case, the Fourier transform has a special property: it's symmetric in the frequency domain, i.e. Input is usually samples of real numbers, as in the above example. It does however accept complex numbers as input. It doesn't care about the actual frequency values: the sampling interval is not passed in as a parameter. It makes the same assumption about the input sampling, that it's equidistant, and outputs the Fourier components in the same order as fftfreq. (You usually only want to plot one half, as you do in your code.) Note how the function actually needs to know very little about the data: just the number of samples and their spacing in the time domain.įft performs the actual (Fast) Fourier transformation. How can I make it so the frequency axis is properly scaled at all times?įftfreq returns the frequency range in the following order: the positive frequencies from lowest to highest, then the negative frequencies in reverse order of absolute value. Now keeping N = 1024 and setting limit = 100 also changes the result. As an example, setting N = 2048 gives the following plot.Īs you can see, the locations of the spikes have changed. Additionally, I have noticed that the frequency scaling is sensitive to the number of points N as well as the interval limits that I make limit. However, when I run this code, I get the following plot.Ĭlearly the spikes are not where they should be. Y = np.sin(2 * np.pi * 5 * x) + np.sin(2 * np.pi * x)įrom the function that is given,, it is clear there should be two spikes at frequencies 1 and 5. I am trying some sample code taking the FFT of a simple sinusoidal function.
0 Comments
Leave a Reply. |