Skip to content

Instantly share code, notes, and snippets.

@t-nissie
Last active January 3, 2016 14:56
Show Gist options
  • Save t-nissie/0325fc29d833e0aa6f4d to your computer and use it in GitHub Desktop.
Save t-nissie/0325fc29d833e0aa6f4d to your computer and use it in GitHub Desktop.
三角波を生成する方法

三角波を生成する方法

周期T三角波を生成するにはabs(mod(i,T)-T/2)-T/4とする。 ここで、i=0,1,2,3,...。 また、Tは4の倍数である必要がある。

この文章は Gist https://gist.github.com/t-nissie/0325fc29d833e0aa6f4d に置いてある。 git clone https://gist.github.com/0325fc29d833e0aa6f4d.git triangle_waveもしくは git clone git@gist.github.com:0325fc29d833e0aa6f4d.git triangle_waveでクローンできる。

GNUPLOTスクリプト

triangle_wave.gp:

#!/usr/bin/env gnuplot
##
set title 'triangle wave with {/Times-Italic T}=16'
T =                                             16
set key bottom
set xrange [0:T*2]
set samples   T*2+1
set yrange [-T:T]
set terminal postscript landscape enhanced color dashed "Times-Roman" 27
set output "triangle_wave.eps"
set xlabel '{/Times-Italic i}'
set xtics 4
set ytics 4
set grid
plot     int(x)%T          title '    mod({/Times-Italic i},{/Times-Italic T})'                      with linespoints lt 2 pt 2 lw 3,\
         int(x)%T-T/2      title '    mod({/Times-Italic i},{/Times-Italic T})-{/Times-Italic T}/2'  with linespoints lt 3 pt 3 lw 3,\
     abs(int(x)%T-T/2)     title 'abs(mod({/Times-Italic i},{/Times-Italic T})-{/Times-Italic T}/2)' with linespoints lt 4 pt 4 lw 3,\
     abs(int(x)%T-T/2)-T/4 title 'abs(mod({/Times-Italic i},{/Times-Italic T})-{/Times-Italic T}/2)-{/Times-Italic T}/4'\
                                                                                                     with linespoints lt 1 pt 1 lw 3,\
     0 t '' with lines lt 1 lc 9
set output   # ensure to close the output file
!epstopdf.pl triangle_wave.eps

実行方法と実行結果

UNIXの場合は gnuplot triangle_wave.gp 、 Windowsの場合は gnuplot.exe triangle_wave.gp などと実行する。 実行結果は図のとおりになる。

figure

図: 三角波を生成する方法。この図はtriangle_wave.gpを実行して得られる。

まとめ

  • 周期Tの三角波を生成する方法abs(mod(i,T)-T/2)-T/4を紹介
  • i=0,1,2,3,...Tは4の倍数
  • 剰余 (Fortran: mod(i,T), C: i%T) を巧妙に使う
  • 実はi, Tともに実数でも可
  • 生成した三角波でヒステリシスループを描く
# -*-Makefile-*-
# gfm2html.rb: https://gist.github.com/t-nissie/c415b7da3a694e82d6b5
##
MD = 00triangle_wave.md
all: triangle_wave.pdf index.html
triangle_wave.pdf: triangle_wave.gp
gnuplot $<
triangle_wave.gp: $(MD)
sed -n "/env gnuplot/,/epstopdf\.pl /p" $< > $@
index.html: $(MD)
gfm2html.rb --readme -l ja -n 'Takeshi Nishimatsu' -s style.css \
-j https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js $(MD) > $@
clean:
rm -f triangle_wave.eps triangle_wave.pdf triangle_wave.gp triangle_wave.dat index.html
/* -*-CSS-*-
* style.css for README.html of feram
* Time-stamp: <2014-07-07 13:51:49 takeshi>
* Author: Takeshi NISHIMATSU
*/
body {
color: black;
font-family: verdana, arial, helvetica, sans-serif;
}
h1, h2, h3, h4, h6 {
font-family: verdana, arial, helvetica, sans-serif;
}
h1 {
color: #dd0000;
background-color: #fff0f0;
font-size: 240%;
}
h2 {
border-top: red 5px solid;
border-bottom: red 1px solid;
padding-left: 8px;
background-color: #fff0f0;
}
h3 {
border-top: red 2px solid;
border-bottom: red 1px solid;
padding-left: 4px;
}
h4 {
border-top: red 1px solid;
padding-left: 4px;
background-color: #fff0f0;
}
h5 {
font-size: larger;
font-family: courier, verdana, arial, helvetica, sans-serif;
padding-top: 10px;
color: darkred;
}
pre {
font-family: monospace, courier, verdana, arial, helvetica, sans-serif;
padding-right: 0.5em;
padding-left: 0.5em;
padding-top: 0.1ex;
padding-bottom: 0.1ex;
margin-left: 0.5em;
margin-right: 1.0em;
white-space: pre;
color: darkred;
background-color: #f3f3f3;
}
p img {
width: 60%;
margin: auto;
display: block;
}
div.figure div.figcaption {
width: 60%;
margin: auto;
display: block;
}
div.navi {
text-align: right;
margin-right: 1.0em;
}
div.contents {
margin-left: 10%;
}
img{
width: 30%;
margin: auto;
margin-top: 3.0em;
display: block;
}
figure figcaption{
width: 60%;
margin: auto;
margin-bottom: 3.0em;
display: block;
}
table {
border: blue 2px solid;
text-align: center;
margin: auto;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment