python – Incorrect Fourier coefficients signs resulting from scipy.fft.fft
I analysed a triangle wave using the Fourier series by hand, and then using the scipy.fft package. What I’m getting are the same absolute values of the coefficients, but with opposite signs.
By hand:
I took the interval [-1,1], calculated the integral to get a0 = 1, then a1, a3 and a5 using the result of integration:
And all the bn are 0.
Then I simply constructed the series, where the 1st term is a0/2, the second a1×cos(n Pi t/T) and so on, and plotted these waves, which summed give a good approximation of the original signal.
The coefficients are:
a0 = 0.5
a1 = -0.4053
a3 = -0.0450
a5 = -0.0162
Scipy.fft:
I defined the sampling frequency fs=50, and created the space for the function with space=np.linspace(-1,1,2*fs).
Then defined the fft1 = fft.fft(triang(space)), where “triang” is the function which generates the signal. I immediately scaled the results by dividing by the number of samples (100) and multiplying each term except the 0th by 2. Next were the frequencies freq1 = fft.fftfreq(space.shape[-1], d=1/fs).
The resulting coefficients (the real parts pertaining to an) are:
a0 = 0.5051
a1 = 0.4091
a3 = 0.0452
a5 = 0.0161
As you can see, the absolute values are correct, but the signs are not. What am I missing?

And one bonus question if I may – when doing it this way (using scipy) and plotting each wave separately, should I add the phase angle from np.angle(fft1[n]) into each term, like a1×cos(n Pi t/T + theta) + b1×sin(n Pi t/T + theta)? I’d say yes, but in this example all the bn are 0, and so are the phase angles, so I couldn’t really test it.
Read more here: Source link


