# Scipy.signal noise with rfft compared to fft

I'm trying to get the fourier transform of a signal with real values, however the results I get with rfft are noiser than those with fft.

I wrote the following code:

import numpy as np
from scipy.fftpack import rfft, fft
import matplotlib.pyplot as plt
# Number of sample points

N = 600
# sample spacing
T = 1.0 / 800.0
x = np.linspace(0.0, N*T, N)
f1 = 50
f2 = 80
y = np.sin( f1* 2.0*np.pi*x) + 0.5*np.sin(f2 * 2.0*np.pi*x)
yf = fft(y)
xf = np.linspace(0.0, 1.0/(2.0*T), N/2)
plt.plot(xf, 2.0/N * np.abs(yf)[0:N/2])
#plt.savefig('sin.png')
plt.show()

yf2 = rfft(y)
xf2 = np.linspace(0.0, 1.0/(2.0*T), N)
plt.plot(xf2, 2.0/N * np.abs(yf2))
plt.savefig('sin2.png')
plt.show()



and I get the following results:

I thought that for real values I would get the same result with fft and rfft, do you know why there are some differences ?

• FYI scipy.fftpack is now considered legacy, new code should use scipy.fft.
– VMMF
Jun 15, 2023 at 15:34

yf3 = yf2[0:-2:2] + 1j*yf2[1:-1:2];
plt.plot(np.abs(yf3));
plt.show()