Laboratory Exercise 12 Basic Digital Signal Processing This exercise is suggested for students who want to use the Audio CODEC on a Altera DE2-series board for their project in an introductory digital logic course. Students will design circuit that takes input from an Audio CODEC, alters the sound from the microphone by filtering out noise, and produces the resulting sound through the speakers. In addition to a DE2-series board, a microphone and speakers will be required. Background Sounds, such as speech and music, are signals that change over time. The amplitude of a signal determines the volume at which we hear it. The way the signal changes over time determines the type of sounds we hear. For example, an ’ah’ sound is represented by a waveform shown in Figure 1. Figure 1: A waveform for an ’ah’ sound. The waveform is an analog signal, which can be stored in a digital form by using a relatively small number of samples that represent the analog values at certain points in time. The process of producing such digital signals is called sampling. Figure 2: A sampled waveform for an ’ah’ sound. 1 The points in Figure 2 provide a sampled waveform. All points are spaced equally in time and they trace the original waveform. The Altera DE2-series board is equipped with an audio CODEC capable of sampling sound from a microphone and providing it as input to a circuit. By default, the CODEC provides 48000 samples per second, which is sufficient to accurately represent audible sounds. In this exercise you will create several designs that take input from an Audio CODEC on the Altera DE2-series board, record and process the sound from a microphone, and play it back through the speakers. To simplify your task, a simple system that can record and playback sounds on an Altera DE2-series board is provided for you. The system, shown in Figure 3, comprises a Clock Generator, an Audio CODEC Interface, and an Audio/Video Configuration modules. This interface is a simplified version of the Altera University Program Audio IP Cores you can find at http://university.altera.com. Audio CODEC Interface Audio/Video Configuration CLOCK_27 AUD_XCK CLOCK_50 Your Circuit I2C_SDAT I2C_SCLK AUD_BCLK AUD_ADCLRCK AUD_DACLRCK AUD_ADCDAT AUD_DACDAT read_ready write_ready read write readdata_left readdata_right writedata_left writedata_right Clock Generator Figure 3: Audio System for this exercise. The left-hand side of Figure 3 shows the inputs and outputs of the system. These I/O ports supply the clock inputs, as well as connect the Audio CODEC and Audio/Video Configuration modules to the corresponding peripheral devices on the Altera DE2-series board. In the middle, a set of signals to and from the Audio CODEC Interface module is shown. These signals allow your circuit, depicted on the right-hand side, to record sounds from a microphone and play them back via speakers. The system works as follows. Upon reset, the Audio/Video Configuration begins an autoinitialization sequence. The sequence sets up the audio device on the Altera DE2-series board to sample microphone input at a rate of 48kHz and produce output through the speakers at the same rate. Once the autoinitialization is complete, the Audio CODEC begins reading the data from the microphone once every 48000th of a second, and sends it to the Audio CODEC Interface core in the system. Once received, the sample is stored in a 128-element buffer in the Audio CODEC Interface core. The first element of the buffer is always visible on the readdata left and readdata right outputs when the read ready signal is asserted. The next element can be read by asserting the read signal, which ejects the current sample and a new one appears one or more clock cycles later, if read ready signal is asserted. To output sound through the speakers a similar procedure is followed. Your circuit should observe the write ready signal, and if asserted write a sample to the Audio CODEC by providing it at the writedata left and writedata right inputs and asserting the write signal. This operation stores a sample in a buffer inside of the Audio CODEC Interface, which will then send the sample to the speakers at the right time. A starter kit that contains this design is provided in a starterkit as part of this exercise. 2 Part I In this part of the exercise, you are to make a simple modification to the provided circuit to pass the input from the microphone to the speakers. You should take care to read data from and write data to the Audio CODEC Interface only when its ready signals are asserted. Compile your circuit and download it onto the Altera DE2-series board. Connect microphone and speakers to the Mic and Line Out ports of the DE2-series board and speak to the microphone to hear your voice through the speakers. After resetting the circuit, you should hear your own voice through the speakers when you talk to the microphone. Part II In this part, you will learn a basic signal processing technique known as filtering. Filtering is a process of adjusting a signal - for example, removing noise. Noise in a sound waveform is represented by small, but frequent changes to the amplitude of the signal. A simple logic circuit that achieves the task of noise-filtering is an averaging Finite Impulse Response (FIR) filter. The schematic diagram of the filter is shown in Figure 4. D Q 24 Data in D Q D Q D Q D QD Q D Q ÷8 ÷8÷8 ÷8 ÷8 ÷8 ÷8 + + + + + + + ÷8 24 Data out Figure 4: A simple averaging FIR filter. An averaging filter, like the one shown in Figure 4, removes noise from a sound by averaging the values of adjacent samples. In this particular case, it removes small deviations in sound by looking at changes in the adjacent 8 samples. When using low-quality microphones, this filter should remove the noise produced when you speak to the microphone, making your voice sound clearer. You are to implement the circuit shown in Figure 4 to process the sound from the microphone, and output the filtered sound through the speakers. Do you notice any difference between the quality of sound in this part as compared to Part I? NOTE: It is possible to obtain high-quality microphone with noise-cancelling capabilities. In such circumstances, you are unlikely to hear any effect from using this filter. If this is the case, we suggest introducing some noise into the sound by adding the output of the circuit in Figure 5 to the sample produced by the Audio CODEC. The circuit is a simple counter, whose value should be interpreted as a signed value. The circuit should be clocked by a 50MHz clock, and the enable signal should be driven high when the Audio CODEC module can both produce and accept a new sample. To hear the effect of the noise generator, add the values produced by the circuit to each sample of sound from the Audio CODEC in the circuit in part I. Part III The implementation of the averaging filter in part II may have been effective in removing some of the noise, and 3 module noise generator (clk, enable, Q); input clk, enable; output [23:0] Q; reg [2:0] counter; always@(posedge clk) if (enable) counter = counter + 1’b1; assign Q = {{10{counter[2]}}, counter, 11’d0}; endmodule Figure 5: Circuit to generate some noise. all of the noise produced by the noise generator. However, if your microphone is of low-quality or you increate the width of the counter in the noise generator, the filter in part II would be insufficient to remove the noise. The reason for this is because the filter in part II only looked at a very small time frame over which the sound waveform was changing. This can be remedied by making the filter larger, taking an average of more samples. In this part, you are to experiment with the size of the filter to determine the number of samples over which you have to average sound input to remove background noise. To do this more effectively, use the design of an averaging FIR filter shown in Figure 6. 24 Data in Q D Datain Dataout *(-1) + + ÷N 24 Data out FIFO of size N Accumulator Figure 6: N-sample averaging FIR filter. To compute the average of the last N samples, this circuit first divides the input sample by N. Then, the resulting value is stored it in a First-In First-out (FIFO) buffer of length N and added to the accumulator. To make sure the value in the accumulator is the average of the last N samples, the circuit subtracts the value that comes out of the FIFO, which represents the (n + 1)th sample. Implement, compule and download the circuit onto Altera DE2-series board. Connect microphone and speakers to the Mic and Line Out ports of the DE2-series board and speak to the microphone to hear your voice through the speakers. Experiment with different values of N to see what happens to your voice and any background noise, remembering to divide the samples by appropriate value. We recommend experimenting with values of N that are a power of 2, to make the division easier. If you have a portable music player, with a connector such that you can supply input to your circuit through the Mic port, try experimenting with different sizes of the filter and its effect on the song you play. Copyright c 2011 Altera Corporation. 4