shelling (owner)

Revisions

gist: 215412 Download_button fork
public
Description:
EM theory assignment 2
Public Clone URL: git://gist.github.com/215412.git
Embed All Files: show embed
em_theory_assignment_2.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/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")
 
 
em_wave.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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