EGR 224L - Spring 208 7. Introduction Laboratory 7: Active Filters During this lab, you are going to use data files produced by two different low-pass filters to examine MATLAB s ability to predict transfer functions based on voltages collected at the input and output of the filter. You will get to hear the difference between the original signals and their filtered version, hear how aliasing can be reduced by using a good low pass filter, and see just how accurate estimates of the transfer function can be. For each circuit, you will be using two different input signals - one is a chirp signal that spans a broad range of frequencies while another is an actual song. 7.2 Getting Started Because you are using sound, you will need to run MATLAB on the PC in the lab. To log in, use the FROSHNN\Michael account (the password is empty). You will then need to start the 32-bit version of MATLAB on the local PC - not remotely. Single-click the icon to make sure it explicitly states it is the 32-bit version before double-clicking it to start it. Once started, make sure the working directory is empty. 7.3 Background 7.3. Passive Low-Pass Filter In a previous lab, you generated an experimental version of a Bode plot for an RC circuit by examining amplitude ratios and phase shifts between a sinusoidal input and the output across the capacitor. A unity-gain, first-order low-pass filter can thus be built as follows: R v in v out C The transfer function between the input and output voltages is: V out V in = sc sc + R = + src As a Fourier transform, the transfer function and its magnitude are given as: + jωrc + ω2 R 2 C 2 The maximum magnitude is, when ω = 0, and the half-power frequency is where (H(jω hp )) 2 = 2, so: + ωhp 2 R2 C 2 = 2 ω hp = RC Copyright 208, Gustafson et al. Lab 7
EGR 224L - Spring 208 7.3.2 Sallen-Key Low-Pass Filter While the filter above is simple and provides a means for diminishing signal components at frequencies higher than the half-power frequency, it is not a particularly sharp filter. That is, the rate of attenuation as a function of frequency is such that elements past the half-power frequency remain with some non-negligible amplitude. There are several methods available for obtaining a sharper cutoff. One simple active circuit that provides such properties is a Sallen and Key Filter. The specific configuration for a unity gain, second-order low-pass filter with a sharper cutoff than a simple first-order low pass filter is: C 2 v in R R 2 + v out C The transfer function between the input and output voltages is: If R = R 2 = R, C = C, and C 2 = 2C, this simplifies to: V out V in = V out = V in R R 2 C C 2 s 2 + (R + R 2 )C s + 2R 2 C 2 s 2 + 2RCs + = 2R 2 C 2 s 2 + RC s + From this, the natural frequency and damping ratio can be determined: 2R 2 C 2 ω n = RC 2 As a Fourier transform, the transfer function is given as: ζ = 2RCω n = 2 and the magnitude of the Fourier transform is: 2R 2 C 2 (jω) 2 + 2RC(jω) + = ( 2R 2 C 2 ω 2 ) 2 + (2RCω) 2 + 4ω4 R 4 C 4 The maximum magnitude is, when ω = 0, and the half-power frequencies are where (H(jω c )) 2 = 2, so: + 4ω 4 hp R4 C 4 = 2 4ωhpR 4 4 C 4 = ω hp = RC 2 74-85. Sallen, R. P.; E. L. Key 955-03). A Practical Method of Designing RC Active Filters. IRE Transactions on Circuit Theory 2 ): Copyright 208, Gustafson et al. Lab 7 2
EGR 224L - Spring 208 For this filter, then, the half-power frequency is the same as the natural frequency. Note that this is a repeated root, so there are actually two corners here - they just happen to be on top of each other. This is what leads to a sharper drop after the half-power frequency than is the case for a first-order filter. To compare, look at the following transfer functions and Bode plots. The first - represented by a solid line - is for a first-order filter with a half-power frequency of 20 krad/s - this can be generated with an RC series circuit with a kω resistor and a 50 nf capacitor. The second - represented by a dashed line - is for a second-order Sallen-Key filter with a half-power frequency of 20.797 krad/s - this can be generated with R=680 Ω and C= 50 nf. H st (s) = 5 0 5 s + H 2nd (s) = 2.32 0 9 s 2 + 6.8 0 5 s + Bode Diagram 0 0 First order Second order 0 Magnitude (db) 20 30 40 50 60 0 0 2 0 3 0 4 0 5 0 6 Frequency (rad/sec) Copyright 208, Gustafson et al. Lab 7 3
EGR 224L - Spring 208 7.3.3 Numerical Estimation of Transfer Function In a previous lab, you generated an experimental companion to a theoretical Bode magnitude plot by recording a circuit s response to eight single-frequency inputs then taking the amplitude ratio for each frequency. While this method works, it is quite inefficient. For this week, you will be using the concept of the transfer function: V out(jω) V in (jω) to generate a magnitude plot and a phase plot of an experimentally determined transfer function. MATLAB has a function called tfestimate that will produce a numerical estimate of the magnitude and phase of a transfer function given an input signal, an output signal, and possibly other information. The specific form of this command used for this lab is: [Hest, Fvals ] = tfestimate (Vin, Vout, [], [], [], Fs) where: 0 Vin - vector containing the input voltage values 0 Vout - vector containing the output voltage values 0 Fs - the sampling frequency for the voltage values, in Hz 0 Hest - vector containing complex numbers that contain the amplitudes and phases of the estimate of the transfer function 0 Fvals - vector containing the corresponding frequencies, in Hz, for the magnitudes and phases stored in Hest The square brackets in the third through fifth arguments are placeholders for parameters whose default values are fine for this experiment. Essentially, this command will determine the frequency content of the input and of the output using subsections of the data; it will then compute the magnitude ratio and phase difference between the input and the output and provide an approximation of the transfer function at particular frequencies contained in Fvals. One issue that comes into play is you must make sure the input signal has energy at as many frequencies as possible to give tfestimate values to work with. Using a single-frequency cosine as an input, for example, might lead to a disaster - if that frequency happens to exactly hit one of the frequencies tfestimate is using - since there would only be one input frequency on which to base an estimate for the response to all frequencies. This would be similar to estimating the acoustics of a concert hall by hitting a single tuning fork. It is important to note, however, that what MATLAB is really doing is assuming that your input is one period s worth of some periodic input - meaning T is t end t start and ω 0 = 2π T. Furthermore, the tfestimate command breaks the signal up into several windows, further reducing the possibility that a problem of this nature will occur. If, however, the input signal does not have any energy in the higher frequency ranges, the tfestimate program will not be able to compensate. You should therefore make sure that whatever signal gets used contains a wide range of frequencies. Ideally, the input signal would be an impulse function since that contains all frequencies at equal amounts. Barring that 2, generating a noise signal or a sweeping frequency signal with energy in the bands of interest works well. You will investigate the latter possibility in this lab. 2..and the Laws of Nature do a pretty nice job o barring that. Copyright 208, Gustafson et al. Lab 7 4
EGR 224L - Spring 208 7.4 Experimental Procedure 7.4. Downloading Data Files For this lab, you will need to download four data files. Open a web browser and point it to: http :// classes. pratt. duke. edu / FILES / EGR224 / DAQS / FilterFiles Right-click and save each of the files to MATLAB s working directory. These files contain four vectors each: 0 SoundIn - the voltages collected at the input of the signal 0 SoundOut - the voltages collected at the output of the filter 0 duration - the duration, in seconds, of the signal 0 samplerate - the sample rate, in samples per second, for the signals In each case, the sample rate was set to 40 khz, fast enough to capture signals below 20 khz. The volumes of the signals are such that the measured voltages are between - V and V; this is so the voltage measurements themselves can be sent directly to the sound command in MATLAB. 7.4.2 Let s Go Duke The first file you are going to analyze is in the DukeFightSong.m file. Start a new script and add the lines: clear load DukeFightSong sound ( SoundIn, samplerate ) pause sound ( SoundOut, samplerate ) then save this script as HearDuke.m. Run the script - the first time through, you will be hearing the original. Once that is done playing, hit return. The second time, you will be hearing the sound filtered through a first-order low-pass filter with a half-power frequency of 2000 rad/s. Among other things, you should notice that the drums stand out more. To analyze the frequency content, use the following script (also available on the Pundit page for the lab). The specific commands will be explained below. clear load DukeFightSong Filter constants R = 0000; C = 50 e -9; Experimental Transfer Function [EstH, EstF ] = tfestimate ( SoundIn, SoundOut, [], [], [], samplerate ); EstMag = abs ( EstH ); EstOmega = EstF *2* pi; Analytical Transfer Function s = tf ([ 0], []); H = / ( + s * R * C); [HMag, HPhase, HOmega ] = bode (H, {, max ( EstOmega )}); HMag = squeeze ( HMag ); Make plot figure (); clf semilogx ( EstOmega, 20* log0 ( EstMag ), b - ) hold on semilogx ( HOmega, 20* log0 ( HMag ), r - ) hold off xlabel ( \ omega, rad /s ); ylabel ( H, db ) legend ( Estimates, Theoretical, 0) The code above to get magnitudes and phases out of the tfestimate program, once the data set is loaded is: Copyright 208, Gustafson et al. Lab 7 5
EGR 224L - Spring 208 [EstH, EstF ] = tfestimate ( SoundIn, SoundOut, [], [], [], samplerate ); EstH will be a set of complex numbers representing both the magnitudes and phases of the transfer function while EstF will be the frequencies (in Hz) at which the estimates are given. The lines below this generate a vector of magnitudes of the transfer function estimates (EstMag) as well as a vector of the angular frequencies of the estimates (EstOmega). For the theoretical transfer function, you first need to set up a transfer function object in MATLAB using the tf command, then use the bode command to get an array of magnitudes, phases, and frequencies. The lines s = tf ([ 0], []); H = / ( + s * R * C); take care of this by first defining s as a transfer function: s = () s + (0) s 0 () s 0 and then using that to build the transfer function for the low-pass filter. The code above to get theoretical values for a transfer function H using the bode command is: [HMag, HPhase, HOmega ] = bode (H, {, max ( EstOmega )}); HMag = squeeze ( HMag ); where HMag and HPhase will be real numbers that give the magnitudes and phases, respectively, of the transfer function at angular frequencies denoted by HOmega. The limits on the Bode analysis are set to obtain frequencies between and the maximum value obtained by tfestimate - so make sure the tfestimate line runs first The bode command is capable of performing much more powerful tasks than we have asked for it, and as a result the output is more complicated than it needs to be for us. Specifically, the magnitude and phase vectors are three dimensional - one row, one column, and several layers. Before plotting with these then, you need MATLAB to squeeze out the extra singleton dimension - the command for this is: HMag = squeeze ( HMag ); The plot commands generate Bode plots for the experimental and analytical estimates and put them on the same graph. You should see that the experimental estimates are good for a certain range of frequencies - generally those near the middle of the graph. The low and high ends are not quite as good. Part of the reason for these errors is that the song you used does not contain a great deal of very high or very low frequencies. The next task will be to examine a file that does have those. Copyright 208, Gustafson et al. Lab 7 6
EGR 224L - Spring 208 7.4.3 Chirp Another file you have been given is the ChirpData file. This file contains a chirp signal that sweeps through frequencies from Hz to 0 khz over the course of 20 seconds. Because of that, it is a better candidate for providing information about the transfer function of the filter. First, you will want to listen to the signal and the filtered version of the signal. Save your HearDuke.m script as HearChirp.m and then change the file name loaded on line 2 to ChirpData. At the beginning, it may seem that nothing is playing - this is because the super-low frequencies in the signal are below the range of human hearing. Eventually, you will hear the signal. You may, in fact, want to move your headphones away from your ears for the latter parts of the signal as the high frequency parts are...very high. When you listed to the filtered version, you should note that the higher frequency parts now have a lower volume. To see the frequency information, go ahead and save your SeeDuke.m file as SeeChirp.m. Again, change the name of the file you are loading on line 2 of the program and run it. This time, you should see that there is a much cleaner experimentally determined transfer function. The extreme low and high ends are still not great, but the midrange should very closely match the analytically determined magnitudes. 7.4.4 2nd Order Filtering In both these cases, a first-order filter was used to eliminate the high-frequency information. Often it is preferable to use a higher-order filter, such as the Sallen-Key filter discussed above. The remaining two data files - which end in SK - were taken when passing the input signal through a Sallen-Key filter. Your job is to determine the resistor value used in the filter given that 50 nf and 00 nf capacitors were used. You should create Hear -type programs for each so you can hear how the second order filters eliminate the higher frequency sounds, then generate See -type programs. You will need to change not only the name of the file being loaded but also the structure of the transfer function. Type in a guess for the resistance value and run the See program; you can then keep adjusting your estimated R values until the experimentally determined magnitudes match the analytically determined ones. As you might imagine, using the Chirp data is probably more useful than using the Duke data because of the wider range of frequencies in the Chirp signal. 7.5 Assignment Your assignment is to go through this lab, determine good estimates for the resistance used for the Sallen-Key filter, and show the TAs the results from your four See programs. Copyright 208, Gustafson et al. Lab 7 7