After a long silence, this post is about the implementation of not-so-simple recursive circuits in the Faust language. We will see a few relevant examples and the three main approaches that we can follow to implement a circuit: Faust’s diagram expressions (basic syntaxt), the with environment with auxiliary function definition, and the letrec environment.

The Faust manual provides basic examples for the first, second, and third approaches. As we will see later, Faust’s basic syntax can be less concise and more complicated in some cases, whereas the remaining two approaches are easier. However, the letrec environment, despite being concise, is not always desirable if we want to generate diagrams that have little or no redundancy. In this post, we will implement a few circuits with feedback using all of the three approaches.

Let’s start with a simple one-pole lowpass filter, which is essentially a scaled-down input feeding into an integrator. In the basic syntax, the tilde operator lets the signal(s) to its left through and sendsthem back into a feedback path to fill the first available input(s) in the function. The operand or group of operands immediately after the tilde operator is applied to the feedback path. The tilde operator, unlike all other basic synthax operators, is left-assiociative and has highest priority. For example, if we write:

we are summing the first two inputs, then we are sending the result together with a third input into another “+” operator, and then we are summing the result to the output itself. Of course, any feedback loop in a digital system requires at least a one-sample delay, which is the default delay in Faust’s recursive composition. Suppose that we want to add another feedback loop in the previous function that is connected to the input of the first “+” operator. We also want to multiply that feedback signal by .5. Then we can write as follows:

Back to the lowpass filter, we can see the diagram below, kindly taken from the website of Julius Smith.

Following [Chamberlin 1985] for the design of the filter, we can write the function using basic syntax as follows:

Below, we can see the diagram generated by the Faust code. Note that the empty little square on a wire indicates a one-sample delay, representing the $$z^-1$$ operator in our case.

Block diagram generated through basic syntax.

Another way to implement the filter is by using an intermediate function and the with environment. I would also like to thank Oleg Nesterov who first introduced me to this technique. The intermediate function usually acts as container and defines elementary single or multiple feedback loops. The feedback loops that are sent back to the function can then be used anywhere in the inner code as they are identified by argument names, which are specified in the intermediate function (“loop”) definition:

Block diagram generated through intermediate function and the with environment.

The third way is through the letrec environment. Within this environment, we can define signals recursively, similarly to how recurrence equations are written:

Block diagram implemented through the letrec environment.

So far, we have implemented a somewhat elementary circuit. Now, we can try to implement a first-order lowpass filter with zero-delay feedback topology. The circuit below is taken from the book by Zavalishin on virtual analogue filters design.

Zavalishin's first-order zero-delay feedback lowpass with resolved topology.

As we can see, the implementation is not as straightforward as the previous case. It can be useful to name several points in the circuit to determine the fundamental signals to compose the whole circuit. Here, we introduce $$G = g/(1+g)$$, $$v$$ as the signal taken after the $$G$$ multiplication, $$s$$ as the state of the system, that is the output of the $$z^-1$$ operator, and $$y$$ as the output of the system. Hence, we have that:

\begin{align*} & v = G(x - s) \\ & y = v + s \\ & s = v + y \\ \end{align*}

If we substitute $$v$$ and $$y$$, then we have that:

\begin{align*} & y = G(x - s) + s \\ & s = 2G(x - s) + s \\ \end{align*}

and we can define two paths, one for the state, the other for the output of the system. Specifically, we can write the paths replacing all occurrences of $$s$$ with a wire, which we will then fill with feedback loops from the state path. It is convenient to define the state first, and the output second, as the tilde operator applies to signals to its left starting from the top:

Zavalishin's first-order zero-delay feedback lowpass diagram implemented with Faust's basic syntax.

As we can notice, the signal $$G(x - s) + s$$ repeats twice in the diagram. However, Faust’s optimisation will make sure that the signal is computed only once. Still, if we want the diagram to be closer to the original circuit, then we can write the following, copying the signal $$G(x - s)$$ internally to compose the remaining necessary signals:

Zavalishin's first-order zero-delay feedback lowpass improved diagram.

Now, we can implement the filter using an intermediate function:

Zavalishin's first-order zero-delay feedback lowpass diagram implemented through intermediate function.

And finally, we can use the letrec environment for a concise and elegant solution, although the diagram will show some redundancy:

Zavalishin's first-order zero-delay feedback lowpass diagram generated through the letrec environment.

For the last example, we will implement Martin Vicanek’s beautiful quadrature oscillator, a recursive self-oscillating system with two states. See the circuit below.

Here, we have a feedback system with two cross-coupled states. Hence, it is not as straightforward as with systems having only one state, for we must send each state back to the appropriate inputs. In this system, we need to define two state paths, which correspond to the two outputs of the system. Similarly to what we did earlier, we can define the states by composing the paths with the signals feeding into the $$z^-1$$ operators. Thus, the two states $$u_n$$ and $$v_n$$ are defined as follows:

\begin{align*} & u_n = w_n - k_1(v_n + k_2 \cdot w_n) \\ & v_n = v_n + k_2 \cdot w_n \\ & w_n = u_n - k_1 \cdot v_n \\ \end{align*}

If we substitute $$w_n$$, we have that:

\begin{align*} & u_n = u_n - k_1 \cdot v_n - k_1(v_n + k_2(u_n - k_1 \cdot v_n)) \\ & v_n = v_n + k_2(u_n - k_1 \cdot v_n) \\ \end{align*}

To start with, using basic syntax, we will simply put a wire wherever a state is fed back without distinguishing between $$u_n$$ or $$v_n$$:

This will lead to the following network, where the external inputs are feedback paths that need to be matched with the corresponding states.

Intermediate implementation stage of Vicanek's oscillator.

At this point, and without worrying about redundancy in the resulting diagram, the easiest thing to do is to send the two states to the feedback path and then copy and route them accordingly. We can do so using the “route” primitive, which we can call specifying the number of inputs, the number of outputs, and a set of input-output pairs to route the signals. Furthermore, we will also add a one-sample impulse to the $$u_n$$ state as its initial condition must be 1.

Vicanek's oscillator diagram implemented through Faust's basic syntax.

Next, we can see how to implement the oscillator using the second approach. It should be clear now:

Vicanek's oscillator diagram implemented through an intermediate function.

Lastly, we can see how to implement the system using letrec:

Vicanek's oscillator diagram implemented through the letrec environment.

Overall, it seems that the with and letrec environments are best to work with. Particularly the with environment with auixiliary function, it allows to define temporary or intermediate signals as we did for the quadrature oscillator. Using letrec, instead, that would not be possible as it would introduce a delay in the auxiliary path. The basic syntax, though, is still useful when we want to generate diagrams showing the entire network topology.