Simple example: Echo

When you make a sound in a large room, the sound takes time to get reflected by the walls. The sound you hear is the sound you make coupled with the echo, coupled with echoes of echoes. A very simple model of echoes is specified by a delay and an attenuation factor. The model assumes that you are standing in the center of a square room. The delay is the time taken for sound to travel from you to the walls and back to you. The attenuation factor is the ratio of the amplitude of the reflected sound to the original sound.

A convenient way to start designing many applications is to draw a diagram showing the streams and the operations on them. The diagram for the echo is shown below.

Echo: attenuate and delay heard sound.

Echo: attenuate and delay heard sound.

A function that returns the stream of heard sounds from the stream of spoken sounds is:

def make_echo(spoken, D, A):
    echo = Stream('echo')
    heard = spoken + echo
    prepend([0]*D, in_stream=f_mul(heard, A), out_stream=echo)
    return heard

The parameters of the make_echo function are the stream of spoken sounds, the delay D, and the attenuation factor, A. The heard sound is the sum of the spoken sound and the echo. The attenuated heard sound is the product of the heard sound and the attenuation factor, A, i.e., it is

f_mul(heard, A)

The echo is the attenuated heard sound which is delayed by D time units. The echo only starts after the speaker’s initial sound carries to the wall and back, and so initially there is no echo, which is represented by silence: a list of D zeroes. The prepend function prepends the first argument, [0] * D, to the input stream to get the output stream. The prepend function is part of the IoTPy library. (Later, we will see that this code can be written by using initial values of streams rather than prepend.)

With D=2 and A=0.5, and spoken = [1, 0, 0, 0, 0, 0, 0], heard will become [1, 0, 0.5, 0, 0.25, 0, 0.125]. The heard stream stops when the spoken stream stops. We will discuss termination later.

Next, lets look at wrappers and decorators for merge agents that have multiple input streams and a single output stream. Merge: Multiple-in, 1-out.