
#  Maple Procedures for Analysis of Single Phase Thyristor.
#  The code is described in the Maple Technical Newsletter Issue 9.
#
# ===========================================================================
# Title of article: Harmonic Analysis of Phase Controlled Waveforms
# -----
# Authors: A. D. Rough and J. Richardson
# -------

# A.D. Rough is at the Department of Computing and Mathematics, Royal Naval
# Engineering College, Manadon, Plymouth PL5 3AQ, England.
# email: rough@manadon.ac.uk

# J. Richardson is at the Department of Electrical and Electronic Engineering,
# Royal Naval Engineering College, Manadon, Plymouth PL53AQ, England.

# ===========================================================================
# This session calculates the harmonic content of a single phase,
# phase controlled rectified sinewave for a firing angle y (degrees)
# and a frequency f (hertz) and plots the results.      

# Integer harmonics are defined by x.  The resultant magnitude of each 
# harmonic is rx. After the harmonic magnitudes a0,ax,bx,rx are determined, 
# the signal is reconstructed and plotted against the original for
# comparison.

# The commands with(plots,matrixplot):   matrixplot(c);
# plot the harmonic magnitudes rx against its rows (firing angle) and
# columns (harmonic number).  They are also plotted with `surfdata'
# which can also be obtained from the share library.

# (see issue no. 9 of the Maple Technical Newsletter for more details).

top:= y/(360*f):
bottom:= (y/180 - 1)/(2*f):

# Calculate harmonic magnitudes ``a0'', ``ax'' and ``bx''
a0:=2*f*int(-sin(2*Pi*f*t),t= bottom..0) + 
	2*f*int(sin(2*Pi*f*t), t=top..1/(2*f));
a0:= subs({sin(x*Pi)=0,cos(x*Pi)=(-1)^x},a0):
a0:= expand(a0);

ax:=2*f*int(-sin(2*Pi*f*t)*cos(2*x*Pi*f*t),t= bottom..0) +
	2*f*int(sin(2*Pi*f*t)*cos(2*x*Pi*f*t), t=top..1/(2*f));
ax:= expand(ax):
ax:= subs({sin(x*Pi)=0,cos(x*Pi)=(-1)^x},ax):
ax:= normal(ax):
ax:= factor(numer(ax))/denom(ax);

bx:= 2*f*int(-sin(2*Pi*f*t)*sin(2*x*Pi*f*t),t= bottom..0) +
	2*f*int(sin(2*Pi*f*t)*sin(2*x*Pi*f*t), t=top..1/(2*f));
bx:= expand(bx):
bx:= subs({sin(x*Pi)=0,cos(x*Pi)=(-1)^x},bx):
bx:= normal(bx):
bx:= factor(numer(bx))/denom(bx);

#  Note that the first harmonic x = 1 gives 0 in the denominators of
#  ``ax'' and ``bx''.  Consequently, these components must be calculated 
#  using the Maple limit function:

a1:=evalc(Re(limit(ax,x=1)));
b1:=limit(bx,x=1);

# Calculate resultant harmonic

rx:=sqrt((ax)^2 + (bx)^2);

#  Set frequency and firing angle
f:=1:
y:= 70:

#  Reconstruct signal
signal:= proc(t)
	a0/2 + sum(ax*cos(2*Pi*f*x*t) + bx*sin(2*Pi*f*x*t), x=2..10):
end:

#  Plot the controlled rectified single phase source wave
source:=proc(angle)
m:=signum(t-a):   n:=signum(t-b):
d(t,a,b):=1/2*(m-n):
e1(t):=-sin(2*Pi*t)*(subs({a=-1/2 + angle/360,b=0},d(t,a,b))):
e2(t):=sin(2*Pi*t)*(subs({a = angle/360,b=1},d(t,a,b))):
e3(t):=e1(t) + e2(t) ;
end:

# compare source wave and signal wave
plot({signal(t),source(70)},t=-1/2..1/2,style=LINE);

# Using matrixplot 
y := 'y';
c:= array(1..11,1..19,[]):
for j from 0 to 18 do
   c[1,j+1]:=evalf(subs(y=10*j,a0)):
   c[2,j+1]:=0:
   for i from 2 to 10 do
        c[i+1,j+1]:=evalf(subs({x=i,y=10*j},rx)):
   od:
od:

with(plots,matrixplot):
matrixplot(c);

e:=[seq([seq([i,j*10,c[i+1,j+1]],i=0..10)]  ,j=0..18)]:

# The surfdata routine should be loaded from the library.
# PLOT3D( GRID(op(e)) ); for Maple V
with(plots,surfdata): surfdata(e);
