Upgrade to Linux now!!

*Transfer Function Plotter

Instrumentation Amplifier Transfer Function Plotter in Perl

#!/usr/bin/perl
#
# Instrumentation Amplifier Transfer Function Plotter
# Copyright (C) 2005  Peter Harlow
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#

@limits = (50, 0);

# Subroutine to take input parameters, apply them to the transfer function
# and write the resulting data point to a text file
sub simulate
{

  local($xval, $Vinput, $Vbias, $gain, $lowerlimit, $upperlimit,
        $filehandle, $a, $b) = @_;
  local $Vout = 0;
  # Only output a data point if we are in the oprating range
  if (($xval >= $lowerlimit) && ($xval <= $upperlimit))
  {

    $Vout = ($gain * $Vinput) - (( $gain - 1) * $Vbias);

    # If Vout exceeds the minimum or maximum logged so far,
    # update the appropriate variable
    # $_[] references the input array by name
    if ($Vout < $_[7]) {$_[7] = $Vout;}
    if ($Vout > $_[8]) {$_[8] = $Vout;}

    # Write point pair to text file
    print $filehandle ($xval . " " . $Vout . "    " . "\n");
  }

}

# Open output files
open(PARMS, '>parms.txt')  || die "File Error\n";         # Parameters for Gnuplot
open(DATASET1, '>dataset1.txt')  || die "File Error\n";   # Temperature data
open(DATASET2, '>dataset2.txt')  || die "File Error\n";   # Pressure data
open(DATASET3, '>dataset3.txt')  || die "File Error\n";   # Humidity data

# Set operating parameters for temperature sensor amplifier
$temperature_Vb = 0;
$temperature_g = 5;
$temperature_upper = 0.896;
$temperature_lower = 0.251;

# Set operating parameters for pressure sensor amplifier
$pressure_Vb = 5;
$pressure_g = 1.3;
$pressure_upper = 4.475;
$pressure_lower = 2;

# Set operating parameters for humidity sensor amplifier
$humidity_Vb = -2.5;
$humidity_g = 1.1;
$humidity_upper = 4.1;
$humidity_lower = 0.8;


for ($count = 0 ; $count < 5001 ; $count++)

{
  $c = $count / 1000;
  simulate($c, $c, $temperature_Vb, $temperature_g, $temperature_lower,
           $temperature_upper, "DATASET1", $limits[0], $limits[1]);

  simulate($c, $c, $pressure_Vb, $pressure_g, $pressure_lower,
           $pressure_upper, "DATASET2", $limits[0], $limits[1]);

  simulate($c, $c, $humidity_Vb, $humidity_g, $humidity_lower,
           $humidity_upper, "DATASET3", $limits[0], $limits[1]);
}


# Output Gnuplot parameters for on-screen display
print PARMS 'set terminal x11' . "\n";
print PARMS 'set data style lines' . "\n";
print PARMS 'set grid' . "\n";
print PARMS 'set title "Amplifier Transfer Functions"' . "\n";
print PARMS 'set xlabel "Vi"' . "\n";
print PARMS 'set ylabel "Vo"' . "\n";
print PARMS 'set yrange [0:5]' . "\n";
print PARMS 'plot "dataset1.txt" title "Temperature", "dataset2.txt" title "Pressure", ' .
            '"dataset3.txt" title "Humidity"'. "\n";
print PARMS 'pause -1'. "\n";

close(PARMS)  || die "File Error\n";
close(DATASET1)  || die "File Error\n";
close(DATASET2)  || die "File Error\n";
close(DATASET3)  || die "File Error\n";

# Print the maximum and minimum outputs, along with their range to the command line

print "Vmin = " . $limits[0] . "  Vmax = " . $limits[1] . "  Range = " .
      ($limits[1] - $limits[0] . "\n");

print "Return to exit \n";

if (system ('gnuplot parms.txt 2> /dev/null') != 0) {die "Could not launch Gnuplot \n";}

open(PARMS, '>parms.txt')  || die "File Error\n";

# Output Gnuplot parameters for png file generation
print PARMS 'set terminal png' . "\n";
print PARMS 'set data style lines' . "\n";
print PARMS 'set grid' . "\n";
print PARMS 'set title "Amplifier Transfer Functions"' . "\n";
print PARMS 'set xlabel "Vi"' . "\n";
print PARMS 'set ylabel "Vo"' . "\n";
print PARMS 'set yrange [0:5]' . "\n";
print PARMS 'plot "dataset1.txt" title "Temperature", "dataset2.txt" title "Pressure", ' .
            '"dataset3.txt" title "Humidity"'. "\n";

close(PARMS)  || die "File Error\n";

if (system ('gnuplot parms.txt > tferfunc1.png 2> /dev/null') != 0)
     {die "Could not launch Gnuplot \n";}

# End

This script should be executed in an empty directory. It will produce several output files - the plot data as dataset1.txt, dataset2.txt and dataset3.txt, the parameter file for Gnuplot as parms.txt and the output graph as tferfunc1.png.

A printable copy of this document in OpenDocument format may be found at:
http://www.catnip.co.uk/opendocument/tferfunc.odt.

Read more about Perl:

Perl.com: The Source for Perl
The Perl Directory - perl.org
Comprehensive Perl Archive Network
ActiveState - ActivePerl Product Family

Valid XHTML 1.1! Valid CSS! Powered by PHP! Get Firefox!