APPENDIX: STREAM LANGUAGE EXAMPLES
The Reservoir, Inc. R-Stream 1.0 release contains several sample source codes, including a simple Brook program that sums all of the elements of two streams [11]. Figure A-1 is a block diagram of that program; the program code follows.

Figure A-1. Block diagram of example Brook program for pairwise sum of two streams.
/***---**--*
*
* Copyright (C) 2003 Reservoir Labs. All rights reserved.
*
*--**---***/
/*-----------------------------------------------*
* Stream Sum *
* *
* Get the sum of all the elements in a stream *
*-----------------------------------------------*/
extern int printf(char [], ...);
/*---------------------------------------------------------------
Simple kernel which computes the pair-wise sum of two streams
of elements. For example, the 5th element of the output stream
c is the sum of the 5th elements of the input streams a and b.
---------------------------------------------------------------*/
kernel void pairwiseSum(int a<>, int b<>, out int c<>) {
c = a + b;
}
/*---------------------------------------------------------------
Simple kernel which computes the total of a stream of elements.
Specifically, the reduction value total is set equal to the sum
of all elements in the input stream a.
---------------------------------------------------------------*/
kernel void computeTotal(int a<>, reduce int total) {
total += a;
}
/*---------------------------------------------------------------
Brook code which reads the contents from arrays a_array and
b_array into streams a and b using the streamReadAll stream
operator, applies pairwiseSum kernel to those streams, and
writes the output stream c back to the array c_array using the
streamWriteAll stream operator.
Also uses the computeTotal kernel to compute the total of all
elements in c and prints the result.
---------------------------------------------------------------*/
int main(void) {
int i;
int total = 0;
int a_array[100];
int b_array[100];
int c_array[100];
int sum = 0;
int a<>, b<>, c<>;
for(i = 0; i < 100; i++) {
a_array[i] = i;
b_array[i] = 100 + i;
sum += i + 100 + i;
}
streamReadAll(a, a_array);
streamReadAll(b, b_array);
pairwiseSum(a, b, c);
streamWriteAll(c, c_array);
computeTotal(c, total);
printf("\n\nReal sum = %d\n", sum);
printf("Calculated sum = %d\n", total);
if(sum == total) {
printf("PASSED!\n\n");
} else {
printf("FAILED!\n\n");
}
return (total != sum);
}
The following example of a frequency band detector, consisting of an A/D converter followed by a bandpass filter and four detectors, is taken from [19]. Figure A-2 shows a block diagram of the system; StreamIt code to implement the system follows.

Figure A-2. Block diagram of frequency band detector. (After [19].)
void -> void pipeline FrequencyBand {
float sFreq = 4000;
float cFreq = 500/(sFreq*2*pi);
float wFreq = 100/(sFreq*2*pi);
add BandPassFilter(1, cFreq-wFreq, cFreq+wFreq, 100);
add splitjoin {
split duplicate;
for (int i=0; i<4; i++) {
add pipeline {
add Detector(i/4);
add LEDOutput(i);
}
}
join roundrobin(0);
}
}
As an example of a kernel, the following code implements a low pass filter:
float -> float filter LowPassFilter (int N, float freq) {
float[N] weights;
init {
weights = calcWeights(N, freq);
}
work push 1 pop 1 peek N {
float result = 0;
for (int i=0; i<weights.length; i++) {
result =+ weights[i]*peek(i);
}
push(result);
pop();
}
}