Skip to content

Instantly share code, notes, and snippets.

@shelling
Created October 21, 2009 19:58
Show Gist options
  • Select an option

  • Save shelling/215412 to your computer and use it in GitHub Desktop.

Select an option

Save shelling/215412 to your computer and use it in GitHub Desktop.
EM theory assignment 2
#!/usr/bin/env ruby
$:.unshift "./lib"
require 'em_wave'
require 'rubygems'
require 'mrplot'
require 'mrplot/magick'
require 'mrplot/plots/xy'
include MRPlot
plot = XYPlot.new(
Label.new("EM Theory Assignment 2"),
Label.new("the reflection and trasmission coefficient of TE and TM wave")
)
interface = EMWave::Interface.new({ :eps_r1 => 1, :eps_r2 => 4 })
include EMWave::InterfaceHelper
plot.datasets.concat( [
DataSet.new(ContinousData.new { |thita| interface.te_reflection_at( thita ).abs }, "TE reflection" ),
DataSet.new(ContinousData.new { |thita| interface.te_transmission_at( thita ).abs }, "TE transmission" ),
DataSet.new(ContinousData.new { |thita| interface.tm_reflection_at( thita ).abs }, "TM reflection" ),
DataSet.new(ContinousData.new { |thita| interface.tm_transmission_at( thita ).abs }, "TM transmission" ),
])
for i in 0...plot.datasets.length
plot.datasets[i].style.color = Color.new( 0.5+Math.sin(i)/2, 1-(1.0/(i+1)), 0.5+Math.cos(i)/2 )
end
# Set the plot space
# plot.space.xrange = 0..90
# plot.space.yrange = 0..1
plot.space.xrange = 0..90
plot.space.yrange = 0..1
plot.space.res = 2000
plot.axes.xstep = 90
plot.axes.ystep = 0.1
plot.axes.xlabel = "thita"
plot.axes.ylabel = "ratio"
# plot.axes.xdiv = 5
# plot.axes.ydiv = 0.1
# Set the grid
plot.grid.xstep = 5
plot.grid.ystep = 0.2
plot.grid.xdiv = 4
plot.grid.ydiv = 4
plot.grid.style.solid = true
plot.grid.style.set_color(Color.new(0.8,0.8,0.8))
plot.grid.division_style.solid = true
plot.grid.division_style.set_color(Color.new(0.9,0.9,0.94))
# Align the history to the top - left
plot.history.alignment = :top_left
# write to file
gc = RMagickContext.new(Size.new(1024, 768))
plot.draw(gc, Rect.new(0,0,1024,768), Border.new(100,100,100,100))
gc.write("assignment_2_2_ab.png")
plot.datasets.clear
plot.description = Label.new "the axial ratio of transmission and reflection"
plot.space.yrange = 0..100
plot.axes.ystep = 5
plot.grid.ystep = 5
plot.datasets.push DataSet.new(ContinousData.new { |thita| interface.reflection_axial_ratio(thita) }, "Reflection Axial Ratio" )
plot.datasets.push DataSet.new(ContinousData.new { |thita| interface.transmission_axial_ratio(thita) }, "Transmission Axial Ratio" )
for i in 0...plot.datasets.length
plot.datasets[i].style.color = Color.new( 0.5+Math.sin(i)/2, 1-(1.0/(i+1)), 0.5+Math.cos(i)/2 )
end
gc = RMagickContext.new(Size.new(1024, 768))
plot.draw(gc, Rect.new(0,0,1024,768), Border.new(100,100,100,100))
gc.write("assignment_2_2_cd.png")
module EMWave
module InterfaceHelper
include Math
def te_reflection( ita1, ita2, thita1, thita2 )
thita1 = angle2arc thita1
thita2 = angle2arc thita2
( ita2 * cos(thita1) - ita1 * cos(thita2) ) / ( ita2 * cos(thita1) + ita1 * cos(thita2) )
end
def te_transmission( ita1, ita2, thita1, thita2 )
thita1 = angle2arc thita1
thita2 = angle2arc thita2
( 2 * ita2 * cos(thita1) ) / ( ita2 * cos(thita1) + ita1 * cos(thita2) )
end
def tm_reflection( ita1, ita2, thita1, thita2 )
thita1 = angle2arc thita1
thita2 = angle2arc thita2
( ita2 * cos(thita2) - ita1 * cos(thita1) ) / ( ita2 * cos(thita2) + ita1 * cos(thita1) )
end
def tm_transmission( ita1, ita2, thita1, thita2 )
thita1 = angle2arc thita1
thita2 = angle2arc thita2
( 2 * ita2 * cos(thita1) ) / ( ita2 * cos(thita2) + ita1 * cos(thita1) )
end
def transmit_angle( n1, n2, incident_angle )
asin( ( n1 / n2 ) * sin( angle2arc ( incident_angle ) ) )
end
end
class Interface
include Math
include InterfaceHelper
attr_accessor :eps_r1, :eps_r2, :mu_r1, :mu_r2
def initialize( params = {} )
@eps_r1 = params[:eps_r1] ? params[:eps_r1] : 1
@eps_r2 = params[:eps_r2] ? params[:eps_r2] : 1
@mu_r1 = ( params[:mu_r1] or 1 )
@mu_r2 = ( params[:mu_r2] or 1 )
end
# return impedance of medium one
#
def ita1
120 * PI * sqrt( @mu_r1 / @eps_r1 )
end
# return impedance of medium two
#
def ita2
120 * PI * sqrt( @mu_r2.to_f / @eps_r2 )
end
def n1
return @n1 if @n1
@n1 = sqrt(@eps_r1 * @mu_r1)
end
def n2
return @n2 if @n2
@n2 = sqrt(@eps_r2 * @mu_r2)
end
# return reflection of TE mode / perpendicular polarization / horization polarization
#
def te_reflection_at( incident_angle )
te_reflection( ita1, ita2, incident_angle, transmit_angle( n1, n2, incident_angle ) )
end
def te_transmission_at( incident_angle )
te_transmission( ita1, ita2, incident_angle, transmit_angle( n1, n2, incident_angle ) )
end
def tm_reflection_at( incident_angle )
tm_reflection( ita1, ita2, incident_angle, transmit_angle(n1, n2, incident_angle) )
end
def tm_transmission_at( incident_angle )
tm_transmission( ita1, ita2, incident_angle, transmit_angle(n1, n2, incident_angle) )
end
def reflection_axial_ratio( incident_angle )
20 * log( te_reflection_at(incident_angle).abs / tm_reflection_at(incident_angle).abs )
end
def transmission_axial_ratio( incident_angle )
20 * log( te_transmission_at(incident_angle).abs / tm_transmission_at(incident_angle).abs ).abs
end
end
end
module Math
def angle2arc(angle)
angle * PI / 180
end
def arc2angle(arc)
arc * 180 / PI
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment