001/* Demo for a signal plotter. 002 003 @Copyright (c) 1997-2014 The Regents of the University of California. 004 All rights reserved. 005 006 Permission is hereby granted, without written agreement and without 007 license or royalty fees, to use, copy, modify, and distribute this 008 software and its documentation for any purpose, provided that the 009 above copyright notice and the following two paragraphs appear in all 010 copies of this software. 011 012 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 013 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 014 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 015 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 016 SUCH DAMAGE. 017 018 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 020 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 021 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 022 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 023 ENHANCEMENTS, OR MODIFICATIONS. 024 025 PT_COPYRIGHT_VERSION_2 026 COPYRIGHTENDKEY 027 */ 028package ptolemy.plot.demo; 029 030import ptolemy.plot.Plot; 031import ptolemy.plot.PlotApplet; 032 033/////////////////////////////////////////////////////////////////// 034//// PlotFourierSeries 035 036/** 037 Plot a Fourier series approximation to a square wave. 038 This is a demonstration of the use of the Plot class. 039 040 @author Edward A. Lee 041 @version $Id$ 042 @since Ptolemy II 0.2 043 @Pt.ProposedRating red (eal) 044 @Pt.AcceptedRating red (cxh) 045 */ 046@SuppressWarnings("serial") 047public class PlotFourierSeries extends PlotApplet { 048 /////////////////////////////////////////////////////////////////// 049 //// public methods //// 050 051 /** 052 * Return a string describing this applet. 053 */ 054 @Override 055 public String getAppletInfo() { 056 return "PlotFourierSeries 1.1: Demo of PlotApplet.\n" 057 + "By: Edward A. Lee\n " + "($Id$)"; 058 } 059 060 /** 061 * Initialize the applet. 062 */ 063 @Override 064 public void init() { 065 super.init(); 066 067 Plot plot = (Plot) plot(); 068 069 plot.setTitle("Fourier Series Approximation to a Square Wave"); 070 plot.setXRange(0, 400); 071 plot.setMarksStyle("none"); 072 plot.addLegend(0, "ideal"); 073 plot.addLegend(1, "1 sinusoid"); 074 075 for (int j = 2; j <= 10; j++) { 076 plot.addLegend(j, j + " sinusoids"); 077 } 078 079 boolean first = true; 080 plot.addPoint(0, 0.0, 0.0, false); 081 082 for (int i = 0; i <= 400; i++) { 083 double approximation = 0.0; 084 085 for (int j = 1; j <= 10; j++) { 086 double sig = 4.0 087 * Math.sin(i * 2.0 * Math.PI * (2 * j - 1) / 400.0) 088 / (Math.PI * (2 * j - 1)); 089 approximation += sig; 090 plot.addPoint(j, i, approximation, !first); 091 } 092 093 first = false; 094 095 if (i <= 200) { 096 plot.addPoint(0, i, 1.0, true); 097 } 098 099 if (i >= 200) { 100 plot.addPoint(0, i, -1.0, true); 101 } 102 } 103 104 plot.addPoint(0, 400.0, 0.0, true); 105 } 106}