# The av operator

The average operator average data samples.
```  Usage:
p0y = av(q0)
```

To see the av Dialog window mark the av with the left mouse button in the input text line in the igr calculator window.
Then press Edit->Operator. Now the av Dialog window appears. The average operator average data samples.
It can average scalars (rank 0, dimension 0), vectors (rank 1, dimension 0) and spectra (rank 0, dimension 1).

This operator is used to average the original n data samples.
The user must write in the Average number of samples: line the number a samples to be averaged for each sample returned.

```|<----------------    n     ------------------ >|
-------------------------------------------------
|<--  a  -->|<--  a  -->|<--  a  -->|<--  a  -->|
|           |
|  average  |
|  interval |
```
none
no averaging performed (default)

average
the average of the a samples is returned. This mode works on scalars, vectors and spectra.
This (average) is the most useful mode.
The figure below shows a result of averaging the data.
The Average number of samples: is here set to 20. The black curve is the original 300 samples, and the blue curve is the result of the av operator. In this example the average option is selected. The result contains 15 samples. The user can see which option of the av operator was selected. It prints av("quantity name")[avg20smpl.] . This means the average selection in the operator was selected, and the average interval was 20 samples. median
the median of the a samples is returned. The samples in the input average interval are stored in a data array. This array, of length n, is then sorted. The result is the array element at index n/2.
This mode works only on scalars.
To be correct an odd number of samples should be selected in the Average number of samples:.
The maximum number of samples in the Average number of samples: is 1024, (AV_MED_MAX).
The figure below shows a result of median averaging the data.
The Average number of samples: is here set to 21. The black curve is the original samples, and the blue and magenta curves are the result of the av operator. In this example the average option is selected for the blue curve and the median is selected for the magenta curve. In the median example the operator sorts the 21 input samples to be averaged. Then the 10'th element in the sorted array is selected. As seen in this example narrow spikes are removed in the median averaging. min
the minimum of the a samples is returned This mode works on scalars and vectors.

max
the maximum of the a samples is returned This mode works on scalars and vectors.

min&max
the minimum and maximum of the a samples are returned. This mode works only on scalars.

skip
the first of the the a values is returned This mode works on scalars and vectors.

The figure below shows the result of all averaging modes. The black curves are the same data as in the example above.

• none: black (original data)
• average : blue [avg21smpl.]
• median : magenta [median21smpl.]
• max : green [max21smpl.]
• min : red [min21smpl.]
• min&max : cyan [min&max21smpl.]
• skip: brown [skip21smpl.] ## More about the averaging

average , median , min and max
One sample is returned for each average interval. The sample returned is time tagged to the middle of the average interval.
```|<--  a  -->|<--  a  -->|<--  a  -->|<--  a -->|
|           |           |           |
t0          t1          t2          t3
```
min&max
The min&max averaging returns two samples when averaging the a samples.
```|<------------------    n    ----------------- >|
-------------------------------------------------
|<--  a  -->|<--  a  -->|<--  a  -->|<--  a  -->|

2           2           2           2<-return
```
The two samples returned for each average interval are the samples with maximum and minimum values. If the maximum value appears first, then the first value returned is the maximum value. If the minimum value appears first, then the first value returned is the minimum value. The two returned values are time-tagged equal to the sample 1/4 and 3/4 in the average interval.
```|<--  a  -->|<--  a  -->|<--  a  -->|<--  a  -->|
|     |     |     |     |     |     |     |
t0    t1    t2    t3    t4    t5    t6    t7
```
skip
The first sample is returned in each averaging interval. The sample returned is time tagged to the first sample in the average interval.

## about median filter/average

```An example contains 9 samples:
d, d, ... d
d = 22
d = 105
d = 31
d = 17
d = 25
d = 21
d = 44
d = 129
d = 21
Then the samples are sorted, (with qsort) giving:
d = 17
d = 21
d = 21
d = 22
d = 25
d = 31
d = 44
d = 105
d = 129
The median is d , 25

(The average of this example, the mean, is
(22+105+31+17+25+21+44+129+21)/9 = 46  )

```

## about vectors and spectra

A vector sample (s) contains 3 elements: si, si and si, (i is sample number).
```I.e for 6 samples:
sample 0:   s0 s0 s0
sample 1:   s1 s1 s1
sample 2:   s2 s2 s2
sample 3:   s3 s3 s3
sample 4:   s4 s4 s4
sample 5:   s5 s5 s5
The av operator works on each element at a time.
If the user writes average 3 number of samples
the output is two samples (r)
rj, rj and rj (j is sample number).

sample 0: r0 r0 r0
sample 1: r1 r1 r1

Where:  r0 =  (s0 + s1 + s2)/3
r1 =  (s3 + s4 + s5)/3

r0 =  (s0 + s1 + s2)/3
r1 =  (s3 + s4 + s5)/3

r0 =  (s0 + s1 + s2)/3
r1 =  (s3 + s4 + s5)/3
```
The averaging of spectra works in the same manner. This time each sample contains (usually) more than 3 elements, e.g. 128, then one sample (s) contains 128 elements si, si, si, si, si, ... , si

When the OK button is pressed the settings (in the Calculator) is updated and the av Dialog disappears.
When The Update button is pressed the settings is updated.
When the Cancel button is pressed the av Dialog disappears.

Updated Jan 03, 2007 by Bjřrn Lybekk