gibbs phenomenon in matlab

To observe the Gibbs phenomenon in MATLAB, you can create a square wave and compute its Fourier series approximation using a finite number of terms.

Here is an example code that demonstrates the Gibbs phenomenon:

main.m
% Parameters
T = 1; % period of the square wave
N = 50; % number of Fourier series terms

% Create the square wave
t = linspace(0, T, 1000);
squareWave = sign(sin(2*pi*t/T));

% Compute Fourier series approximation
fourierApprox = zeros(size(t));
for n = 1:N
    amplitude = 4/(n*pi);
    harmonic = amplitude * sin(2*pi*n*t/T);
    fourierApprox = fourierApprox + harmonic;
end

% Plot the original square wave and its Fourier series approximation
figure;
hold on;
plot(t, squareWave, 'k', 'LineWidth', 2);
plot(t, fourierApprox, 'r--', 'LineWidth', 1.5);
hold off;
xlabel('Time');
ylabel('Amplitude');
legend('Square Wave', 'Fourier Series Approximation');
title('Gibbs Phenomenon');

687 chars
28 lines

This code generates a square wave using sign(sin(2*pi*t/T)) where T is the period. It then computes the Fourier series approximation using a finite number of terms specified by N. The Fourier series approximation is plotted alongside the original square wave using the plot function.

When you run this code, you will observe the Gibbs phenomenon which is the overshoot and ringing effect at the corners of the square wave. As you increase the number of Fourier series terms (N), the overshoot and ringing become more pronounced.

Remember, the Gibbs phenomenon is a phenomenon specific to Fourier series approximations of step-like functions, and may not be observed in other types of signals.

related categories

gistlibby LogSnag