
How can I make a square wave using Fourier synthesis?
3 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Image Analyst
on 5 Dec 2015
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')

2 Comments
More Answers (2)
Walter Roberson
on 5 Dec 2015
You need an infinite number of points to synthesize a square wave using fourier synthesis.
2 Comments
Aditya Sharma
on 26 Feb 2022
Edited: Walter Roberson
on 27 Feb 2022
%% Generation of squarewave from Fourier Synthesis
close all
clear all
clc
Fs = 400; %samples per second
Ts = 1 / Fs; % sampleing interval
t = 0; Ts:3-1*Ts;
fm= 3;
%squarewave using MATLAB function
x = square(2*pi*fm*t);
plot(t,x);
axis( [0 1 -2 2 ] )
hold on
y=0;
n= input(' enter no of samples ');
for i =1:2:n
y = y+ sin( 2*pi*fm )
1 Comment
Walter Roberson
on 27 Feb 2022
Edited: Walter Roberson
on 27 Feb 2022
Your code is missing an end statement.
You should be taking sin(2*pi*fm*t)
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!