MATLAB: GIBBS phenomenon & sum of squared differences

fourier seriesgibbs phenomenonsquare wavesum of the squared differences

I obtained the attached m-file from MATLAB Central that demonstrates GIBBS phenomenon. I modified the code to track the sum of the squared differences denoted by the variable err. I carried out the Fourier series to 1000 terms. I varied the parameter N which varies the time step. In the pdf file are my plots. The 2nd plot is err vs 1 to C which represents the number of terms in the Fourier series. I plotted 3 cases for N=101, 1001, & 10001.
It appears err flattens out around 0.5 if N is too small. I believe this is due to the time step is not small enough to track the higher harmonics of the Fourier series. Is there anyone out there know why the value is 0.5 or know of any informative literature on this effect?
Appreciate any response

Best Answer

Hi Jeff,
No, it's not really about the zeroth order coefficient. At a discontinuity, a fourier series converges at value halfway between the upper and lower values at the discontinuity. This is not unreasonable. So it converges at height 1/2 in this case. However, right now the comparison is with X = 1 a that point. You get (1/2)^2, times two for the two side of the pulse, = 1/2.
For comparison purposes you need to modify X.
Right after
X(t>=-Tau/2 & t<=Tau/2) = A; % Original signal
add the lines
X(t==-Tau/2 | t==Tau/2) = A/2;
sum(find(X==A/2))-N-1 % should equal 0
Floating point numbers for comparisons are never a great idea, so the second line is a just safety check to make sure that it worked. With that change the error will come down considerably. For C = 1000 N = 2001 it's down around .05.