DSPRelated.com
Forums

Square signal

Started by hyperpoetuk March 12, 2003
Hi all :)

Does anyone know how to generate a square wave as a sum of sine
waves using matlab code? I know how to generate a square wave using
the "square" command but I have NO IDEA how to do it using fourier
series.
Won't someone PLEEEAAASE help me out? I'm really desperate here. :(

Sara Lauren




Hi... here's how to generate a square wave using it's Fourier
series:

The Fourier series of a square wave of period = 2T (ignoring a
constant scaling factor) is

x = 1/1 * sin(2*pi*1*t/T) + 1/3 * sin(2*pi*3*t/T)
+ 1/5 * sin(2*pi*5*t/T) + ... + 1/(2*n+1) *
sin(2*pi*(2n+1)*t/T)
+ ...
The Fourier coefficients are all zero for n even.

Here's how to do it in MATLAB:

T = 1; % for a square wave of time period = 2*T = 2
secs

t = 0:0.001:2; % time base ranges from 0 to 2 secs -> 1 period of
wave

n = 1:2:39; % taking odd n to compute sine waves.
% the higher the value of n, the better the
square shape

wave = zeros(1,length(t));
s = wave;
% initialise arrays to store computed sine values
and
% running sum of Fourier series

for i = n % Perform summation specified by equation
wave = 1/i*sin(i*pi*t/T);
s = s + wave;
end

plot(t, s); % and finally...
Regards
AK

On Wed, 12 Mar 2003 hyperpoetuk wrote :
>Hi all :)
>Does anyone know how to generate a square wave as a sum of sine
>waves using matlab code? I know how to generate a square wave
>using
>the "square" command but I have NO IDEA how to do it using
>fourier
>series.
>Won't someone PLEEEAAASE help me out? I'm really desperate here.
>:(
>Sara Lauren
>
>_____________________________________
>Note: If you do a simple "reply" with your email client, only the
>author of this message will receive your answer. You need to do
>a "reply all" if you want your answer to be distributed to the
>entire group.
>_____________________________________
>About this discussion group:
>To Join:
>To Post:
>To Leave:
>Archives:
>http://www.yahoogroups.com/group/matlab
>More DSP-Related Groups:
>http://www.dsprelated.com/groups.php3
>br /> >.

__________________________________________________________
Great Travel Deals, Airfares, Hotels on
http://r.rediff.com/r?www.journeymart.com/rediff/travel.asp&&sign&&jmart