MatlabTM for computer exercises, they did not grasp well the basic details of .... Java and object-oriented programming, with emphasis on audio multimedia.
TEACHING SIGNAL PROCESSING USING JAVA REAL-TIME PROGRAMMING Sverre Holm, Svein Bøe, Nils Christophersen Department of Informatics, University of Oslo, P. O. Box 1080, N-0316 Oslo, Norway ABSTRACT Our beginner’s course in signal processing is taught to students who are computer literate, but who lack the traditional electrical engineering background in circuit theory. We started to notice that when they were taught using a high-level, batch-oriented language such as MatlabTM for computer exercises, they did not grasp well the basic details of sinusoids, convolution, the DFT etc. This led us to a search for better ways. The multimedia and real-time capabilities of JavaTM combined with the large processing capability of today’s PC, and in addition the real time input/output of the PC’s soundcard, now provide the key elements in our beginner’s course. The students have to work out all the details of loops and indices for the convolution, the sinusoids of the DFT, and time and frequency axes. Results have been improved, and also the enthusiasm of the students. In doing real time processing of their own voice signals, we now hear students commenting that this is fun to work with.
1. INTRODUCTION In recent years it has become customary to use high level languages such as Matlab in signal processing courses. This gives the students access to a lot of functions and makes advanced signal processing easily accessible. This trend can be found in e.g. [1] and many other intermediate level signal processing text books. Beginner’s courses in signal processing have also been influenced, and it is quite common to use Matlab in these courses also, see e.g. [2]. We have had several versions of our own beginner’s course, and have in recent years used Matlab-based exercises and the textbook DSP First [2]. This has worked reasonably well, but we started noticing that when the students came to more advanced courses, they seemed not to have comprehended the basic topics as well as desired. It was then hard to get as far as desired in the second course, as there seemed to lack a foundation in the very basics. This includes how to deal with complex exponentials, convolution, basic spectral analysis and so on. They were not familiar with the details. This parallels the experience of Donald Knuth, author of one of the most
INF 155 – Oct. 2002
influential books on computer algorithms “The Art of Computer Programming” who replied when he was asked about the best approach to learning algorithms: the high level or low level, detailed, point of view. His response was that we tend to forget the way we learnt ourselves, via going through all the details, and then we spare our students for it. In his opinion, this is a big mistake [3]. Our experience is that this is also applicable to the learning of signal processing. There has been a trend in recent years where signal processing is taught outside the traditional electrical engineering context. The book [4] was among the first to address this, emphasizing digital audio and applications to computer music. For this reason, the progression of topics in [2] is also unconventional as the Preface says: “Furthermore, this approach makes the subject much more accessible to students in other majors such as computer science and other engineering fields. This point is increasingly important because non-specialists are beginning to use DSP techniques routinely in many areas of science and technology.” However, we disagree with the authors of [2] when they spare the students for programming of the convolution. Although, a course based on Matlab could have required actual programming of the convolution sum, the approach in Appendix C of [2] is either to use the Matlab function filter() or to use the firfilt() function supplied. We teach signal processing in a computer science department and we also have students in mathematics, physics and geophysics attending our course. We wanted to develop a course that addresses computer literate students, but where a background in circuit theory is lacking. This led us to search for even better ways to get the students into the basic topics of signal processing. The key for us was the discovery of the multimedia and real-time capabilities of the recent version of the Java programming language. After Java became the computer language of instruction in the beginner’s course in our department, we started to develop ideas for how to use Java as a vehicle for teaching signal processing. Standard PC’s, whether they run under WindowsTM or Linux, also have sound cards that are excellent for demonstrating
1
many of the basic topics of signal processing. The modern computer also has a capacity which is large enough to do real-time signal processing on audio input and output streams. All this combined has opened up the possibility for doing signal processing in real time on audio signals that lend themselves well for both a mathematical understanding, as well as an intuitive understanding of the processing. Last, but not least, Java is free to download and use, making our approach affordable for anyone. Our approach can be compared to that of [5]. We agree when they say “Moving beyond a Matlab-only program to a real-time hardware implementation is highly desirable from a pedagocical point of view. Many practical issues and learning opportunities occur only when the student tries to adapt their newly acquired knowledge to the challenge of real-time DSP.” Our approach differs in that we use Java and the PC’s soundcard rather than the much more complex Texas Instruments C6x Digital Signal Processing Starter Kit based on the C-language and dedicated software and hardware. It also means that our course, using standard PC’s, does not require its own lab with PC’s dedicated to running external hardware.
graphical user interface for control of two sinusoids that were played through the computer’s soundcard. They also studied harmonies such as an octave, a fifth, and a fourth to hear if such combinations sounded better than two randomly selected frequencies. They also tried constructive and destructive interference, as well as aliasing when the requested frequency was higher than half the sampling frequency. 2.
Signal generator. The signal generator was extended to square and triangular waves and in addition to listening, they also plotted the waveforms. One of the challenges here is to get the units on the time axis right. They were asked to describe the quality of the sound from each waveform and made an effort to verify that the period that could be read from the plots, corresponded to the frequency heard.
3.
Spectrum. Building on the previous experience of programming sinusoids, the students had to program their own Discrete Fourier Transform, and plot the spectra for the waveforms from their waveform generator. They also studied the harmonic structure of the waveforms in order to correlate that with the audible impression. They had to implement both a linear and a decibel scale for amplitude and assess the advantage of the logarithmic scale when studying strong and weak signals together. Finally they looked at the effect of a sinusoid that had an even number of periods over the duration of the DFT (e.g. 10) and compared that spectrum to that of a non-even number of periods (e.g. 10.5) in order to generate a sidelobe structure.
4.
Sampling of own voice. The spectral analysis program was extended so that it could read samples from the soundcard’s A/D-converter. They tried 8, 11.025, and 22.05 kHz sampling rates in order to assess how much is lost with telephone quality sound. They also tried to find the range of their own voice to find out if they qualified as a bass or a soprano. They quickly discovered that the DFT was not very fast for a real-time application, so they were given an FFTclass to replace it. The display of this program with whistling as the input is shown in Fig. 1.
5.
Convolution. They wrote their own convolutionclass in order to filter a sound file. We had given them a test case of a sequence of input/output values for verification of their code. They first
Our approach also differs from the popular use in recent years of Java applets for demonstration of various aspects of signal processing. We emphasize the need for students to program themselves. Demos based on Java applets are good for motivation and for getting an overview of various topics, but real understanding requires one to go through all the necessary details in order to get a working program. This paper is structured as follows: In section II we outline the exercises we have developed for our course, then we go on to describe the student’s evaluation of the exercises, and finally sum up the experience from this approach.
2. BASIC EXERCISES Java became really useful for real-time signal processing with the introduction of The Java Sound API. It is part of the JavaTM 2 Standard Edition version 1.3 and higher, and we used version 1.3.1 in our course. Based on this API, we developed six exercises, of which the students had to have at least four approved in order to pass the exam. They are: 1.
Sinusoidal generator. The objective was to increase understanding of the frequency domain by studying sinusoids. They developed their own class for sinusoids that interfaced with the course’s package, in order to quickly produce a
INF 155 – Oct. 2002
2
implemented convolution on a finite length buffer, and then one using a circular buffer for
Figure 1: Real-time spectral analysis of whistling.
Figure 2: Spectrum of input signal with added interference in the form of a 3 kHz sinusoid.
filtering of continuous data streams. The filtering was first done with a moving average filter with a user specified length on a signal consisting of sinusoids added to the sound file data (see Figs. 2 and 3). They also could try a filter designed by truncation and/or weighting of the sinc-response of an ideal low-pass filter (see Fig. 4). A typical output spectrum with the interfering sinusoid suppressed is shown in Fig. 5. An example of the convolution code is shown below. A major point in implementation is to get the indices right and not to address arrays outside of their declared range.
Figure 3: Graphical user interface for setting parameters of interfering signal.
public double[] convolve () { double val; int kFirst, kLast; for (int n = 0; n