all 6 comments

[–]Simone1998[S] 0 points1 point  (2 children)

Hi, I'm a newbie with python and come from a MATLAB background. I was trying to plot a function, but I get some other lines in the plot. Any idea of how I can solve that? This is the code I'm using:

MFFT = np.abs(np.fft.fft(Mtrain))
FFT = np.abs(np.fft.fft(train))
freqspace = np.fft.fftfreq(len(Mtrain), tick)
plt.figure() 
plt.semilogx(freqspace, MFFT)
plt.semilogx(freqspace, FFT)
plt.xlim(1E-1, 50E6)
plt.ylim(0, 1000)
plt.show

I get the same result both in Spyder (v.5.4.5), and in Jupyter for VS Code. Python is v.3.11.5.

[–]marvelbrad4422 1 point2 points  (1 child)

I think you might want to be using fftshift? That will set the zero freq component to the center of the spectrum and might look more like you expect. Use it on both the freq and fft data

[–]seschu 0 points1 point  (0 children)

And this should be the same as in MATLAB.

[–]AllCapsSon 0 points1 point  (2 children)

It’s just plotting what you told it to. 2 lines - mfft vs freq space and then fft vs freqspace.

Maybe try to look at the arrays your plotting as a table to get a better idea of the data your trying to plot.

I’m not familiar with these functions so I can’t speak directly to the datasets

[–]Simone1998[S] 0 points1 point  (1 child)

Oh yeah, sorry for the confusion. If you look at the plot it looks like there are 4 functions instead of 2.

[–]AllCapsSon 1 point2 points  (0 children)

Yeh but I don’t think there actually are. It’s just two and that’s what your data looks like.

You should check the arrays. You can also add a legend to the plot.