import numpy import numpy.fft import matplotlib.pyplot as plt def f(x): return 1./(1.+numpy.cos(x)**2) x = numpy.linspace(0, 2.0*numpy.pi, 2048) coeff = numpy.fft.rfft(f(x)) rec5 = numpy.fft.irfft(coeff[:5],2048) rec10 = numpy.fft.irfft(coeff[:10],2048) rec50 = numpy.fft.irfft(coeff[:50],2048) plt.figure() plt.plot(x, f(x), x, rec5, x, rec10, x, rec50, ) plt.legend(('f(x)', '5', '10', '50')) def g(x): return x**-12 - x**-6 x = numpy.linspace(1, 5, 2048) coeff = numpy.fft.rfft(g(x)) rec5 = numpy.fft.irfft(coeff[:5],2048) rec10 = numpy.fft.irfft(coeff[:10],2048) rec50 = numpy.fft.irfft(coeff[:50],2048) plt.figure() plt.plot(x, g(x), x, rec5, x, rec10, x, rec50, ) plt.legend(('g(x)', '5', '10', '50')) plt.show()