MATLAB: How to make a square wave using Fourier synthesis

fourier synthesis

I'm can't seem to figure out how to plot this.
f(t)=4/π ∑_n^∞ 1/n*sin(nωt)
n=1,3,5….
n=1=1Hz
t=0:0.001:3
Any help would be much appreciated, thanks.

Best Answer

Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
t=0:0.01:3;
omega = 2*pi;
f = zeros(1, length(t));
for n = 1 : 2 : 201
f = f + (4/pi) .* 1 ./ n * sin(n * omega * t);
end
plot(t, f, 'b*-', 'LineWidth', 2, 'MarkerSize', 10);
grid on;
xlabel('t', 'fontSize', fontSize);
ylabel('f', 'fontSize', fontSize);
title('Square Wave Approimation', 'fontSize', fontSize);
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
Related Question