ZHAW, SiSy, 2013, dqtm Laboratory 4: Three Topics In this laboratory 4 we wrap up the system’s topics and starting the signal related topics. Therefore we will be looking into several topics, namely: Analog-Digital Conversion and Aliasing Implementing a Discrete System Signal Measurement in Frequency Domain PART 1 Analog-Digital Conversion and Aliasing In order to process a continuous signal with a discrete system, we need first to sample and quantize this signal. This conversion is made by an analog to digital converter (ADC), which is nowadays often embedded in microcontroller ICs (also in the Arduino-Uno). Before we try out the function of the ADC, we want to experiment in Matlab, with distortions that the sampling can cause to a signal. (a) Start a new Matlab script, and define two cosine signals x1(t) and x2(t) with equal amplitude and frequencies of 1kHz und 7KHz. Use for this definition a time vector with fine resolution, such that: tstep < min{Period)} /100 (b) Calculate now in your Matlab script the corresponding discrete signals x1[n] and x2[n], that would result after sampling x1(t) and x2(t) with a sampling frequency Fs = 8kHz. sampling x(t) Ts x[n] (c) Prepare in Matlab plots of the “time continuous” signals x1(t) and x2(t) and of the time discrete signals x1[n] and x2[n] . Hint: in order to better visualize the continuous and discrete characteristics of the signals, use the commands plot() and stem(). (d) After the sampling, can you still distinguish the frequency of the discrete signals x1[n] and x2[n] ? If these were unknown signals at the input of an ADC, which frequency would you suppose for them, when observing the signals at the output of the ADC? Obs.: The effect you have observed here is called aliasing. (e) Read please the definition of the Sampling-Theorem, also known as Shannon-Theorem or Nyquist criterium. The sampling frequency Fs must be at least twice as big as the highest frequency fmax occurring in in the analog signal. Thus the following holds: Fs 2 . fmax If this criteria is not fulfilled, the signal will be distorted, because the signal components with frequencies above Fs/2 will be “shifted in the frequency domain” by the aliasing effect. In order to avoid this distortion it is usual to build before an ADC input, an analog antialiasing filter, which is a low pass filter with a cut-frequency around 40% of Fs. (f) In your Matlab script (without anti-aliasing filter), which Fs would be necessary to correctly sample x1(t) and x2(t). Verfiy your answer with a plot. Page 1/4 Document1 ZHAW, SiSy, 2013, dqtm PART 2 Implementing a Discrete System The figure below shows the hardware and firmware setup, that you will use to implement the discrete system. The analog input signal from the function generator should be first low-pass filtered before entering the ADC. The code in the microcontroller will drive the sampling and implement the difference equation describing our discrete signal processing. And finally the discrete output is again converted into an analog signal, which can be measured with an oscilloscope. Please follow the instructions below to build this setup step by step. Analog LPF ADC Microprocessor DAC Function Generator (FuGe) External IC TLC7528 AAF antialiasing filter Oscilloscope with code implementing difference equation ARDUINO Figure 1 Hardware and Firmware Setup for Implementation and Test of a Disctete System SETUP AD-DA (a) Place the DAC component TLC7528 in the breadboard of the Arduino and wire it according to the schematics below (left side of figure 1). Please pay attention on the position of the pins (indicated in the package view on right side of figure 1). Arduino Digital Connector Pins: 0 - 7: PortD[0 - 7] : DB0 - DB7 8 : PortB[0] TLC7528 CN Package Pinning Figure 2 Connecting the TLC7528 DAC with Arduino, and pinning information The integrated circuit TLC7528 contains two DAC with 8 bits resolution, with input buffers and latches. The output can be a current or a voltage signal proportional to the digital input value. We will be using only the DAC-A and select the voltage output signal or voltage mode (please notice that in this mode the analog output is available via pin-4 “REFA”. For more details about the modes, please refer to the component datasheet). Page 2/4 Document1 ZHAW, SiSy, 2013, dqtm (b) Open and load the Arduino the program exer_adda.ino . In this program we read the analog input with the embedded ADC and send the result directly to the output port D (with 8 bits). The ADC has a resolution of 10 bits, but the output port D (and also the DAC) has only 8 bits. Therefore in order not to lose the 2 MSBs (most significant bits) we need to shift the input ADC value by 2 bits before sending it to the portD. (c) Check now your settings by connecting the output of the FuGe (Function Generator) as analog signal to be sampled. Choose a sinus wave with 1kHz frequency, amplitude 2Vpp and offset of 1Vdc. Check on the program exer_adda.ino which pin is used for the analog input. The DAC works with the principle of a “Zero-Order-Holder”, which means it holds the sensed input value as a constant ouput value, until it changes. How do you expect the DAC output signal to look like? Check it with the oscilloscope. (d) The program exer_adda.ino also toggles the bit0 of the portB, which corresponds to pin 8 on the Arduino Board digital connector. Observe this output in the 2nd channel of the oscilloscope and determine the sampling frequency which is used for the AD and DA conversion. (e) Compare the port B pin 0 to the DAC outA signal. How could you also determine the sampling period by observing the DAC output signal? (f) What is the maximum frequency for the input signal which you can observe without aliasing with this sampling frequency? Verify your answer in the hardware setup1. (g) What is another frequency that gives the same output as 1kHz because of the aliasing effect? Verify your answer in the hardware setup. SETUP AAF (anti-aliasing filter) (h) Use the information of the sampling frequency you calculated in part (d) to calculate the value of a capacitor for an RC-passive low pass filter with a break-point frequency of about 0,4*Fs . Use for the resistor R=10kΩ. (i) Build in this low pass filter before the input of the ADC as an anti-aliasing filter. Observe its effect on the oscilloscope before the ADC and after the DAC. Most times analog signals are converted to digital signals in order to carry out signal processing steps with a microcontroller or a PC. Let us implement and test now a simple discrete system. (j) Change the Arduino program to implement a moving average filter with the following difference equation: yn un un 1 un 2 un 3 The filter takes the input signal and sums with the previous 3 samples before sending the output value to the port D. Hint: the loop routine is executed once for every Ts. Define variables to store your previous input values, and the commands for the update of these variables. Then calculate the sum and do not forget to shift the output because of the 8-bit width limitation on the port D output. Test your discrete system using a periodic square wave with 1kHz frequency as input signal. 1 For curiosity if you want to see how to vary the sampling frequency of the ADC, check the program exer_adda_extended.ino . The ADC-clock is derived from the main processor clock via a clock divider, with the commands CBI and SBI (clear and set bit) you can change the value of the clock divider and vary the frequency of the ADC-clock. The sampling period Ts is a multiple of the ADC clock period Tadc, because it takes about 12 clock cycles for each ADC conversion. The exact amount of clock cycles depends also on the sample and hold settings. Page 3/4 Document1 ZHAW, SiSy, 2013, dqtm PART 3 Signal Measurement in Frequency Domain In this exercise you experiment to measure a signal in the frequency domain using the FFT function from the Math Menu of the oscilloscope. (a) Connect the FuGe to the oscilloscope with a coaxial cable and set up the FuGe to generate a sine with frequency 100kHz and amplitude 2Vpp. Adapt the horizontal scale, such that 2 periods are visible in the display. (b) What would you expect for a measurement of the single sided amplitude spectrum of this signal? Sketch your answer in the graphics below. f [Hz] (c) Select now in the Math Menu the function FFT. A red display appears, showing the single sided spectrum of the input signal. Observe the displayed spectrum and check if the frequency and amplitude values are as expected. You can use the horizontal scale and horizontal position button to adapt the displayed range. (d) The vertical scale can be switched between logarithmic and linear (Lineare Eff) . Which units are available to display the amplitude value? (e) Change now the input signal from a sine to a periodic square with a 50% duty cycle. Verify first your settings checking the input signal in the time domain. Switch then to the measurement in frequency domain and fill out the table below by measuring the amplitude and frequency of the different harmonic components. Hint: for the measurement of the amplitude in dB, switch in the Math Menu the vertical scale dBV Eff. Harmonic Index k=0 k=1 Frequency [Hz] Amplitude (linear) V Amplitude dBV k=2 k=3 k=4 k=5 k=6 k=7 (f) Change now the duty cycle of the periodic square to 25%. Which differences do you observe in the corresponding amplitude spectrum? Page 4/4 Document1