Computation of the current average of input values

This example is in the directory: /home/lbesnard/Polychrony/V4.19/Linux/Examples/elementary/average

Description

It contains the description of a simple SIGNAL example that computes the current average of input values. We describe in this file a way to obtain a quick simulation of the program.

Signal programming

Here is the program, defined in the file average.SIG:

function average =
     ( ? integer input;
       ! real avg;
     )
   (| sum := sum$ init 0 + input
    | n := n$ init 0 + 1
    | avg := real(sum)/real(n)
    |)
   where
        integer sum, n;
   end;

Compile it, and generate C code (for example), as follows:

signal -lis -tra -c average.SIG

Resulting files are produced in the directory average (which is the name of the process):

cd average

You can generate a Makefile for compiling and linking the generated C files:

genMake C average

Then, to produce an executable, run the command:

make

Simulation

By default, read and write of external signals (inputs and outputs) are done via input and output files (communication functions are defined in the file average_io.c).

For a given input X, you have to define a file RX.dat that contains the sequence of input values for X. For a given output Y, the execution of the program will produce a file WY.dat.

For example, with the following values in

Rinput.dat (corresponding to the input "input"):

2
-2
5
7
3
0
9

run the program as follows:

average

and you will obtain as result the file Wavg.dat (corresponding to the output "avg"):

2.000000
0.000000
1.666667
3.000000
3.000000
2.500000
3.428571

Another way to obtain a quick simulation of the program is to use "intrinsic" input-output interactive functions (refer to chapter XIII in the SIGNAL Reference Manual):

function average_simulation = ( ? ! )
     (| input := read("input: ")
      | avg := average(input)
      | write("avg: ", avg)
      |)
     where
     integer input;
     real avg;
     function average =
              ( ? integer input;
                ! real avg; )
              (| sum := sum$ init 0 + input
               | n := n$ init 0 + 1
               | avg := real(sum)/real(n)
               |)
              where
              integer sum, n;
              end;
     end
;

With this program defined in the file average_simulation.SIG:

signal -lis -tra -c average_simulation.SIG
cd average_simulation
genMake C average_simulation
make

Then run the program as follows:

average_simulation

A possible interactive session is:

input: 2
avg: 2.000000
input: -2
avg: 0.000000
input: 5
avg: 1.666667
input: 7
avg: 3.000000
input: 3
avg: 3.000000
input: 0
avg: 2.500000
input: 9
avg: 3.428571
input: 

(you can interrupt by ^c)
 All Files

Generated on Mon Jul 25 16:55:00 2011 for Polychrony, by  doxygen 1.6.1. This page has been designed by the Espresso team ( contact )