Circuits Ci it II EE221 Optimization of Filter Parameters Instructor: Kevin D D. Donohue Iteration, Objective/Error j Functions, Visualization Matlab and Parameter Optimization: If equations can be developed to relate design parameters to the error/objective criteria, and they can be solved directly, the problem is finished. Unfortunately in many cases, the large number of parameters or the th nonlinear li relationship l ti hi b between t th them, makes k a di directt ((closed-form) l d f ) solution difficult or impossible. The performance equations can, however, be examined through programs such as Matlab, programs, Matlab and optimization performed with a creative strategy for iterating through guesses of critical parameters until error reaches a practical minimum. Matlab functions: Available Matlab functions can be combined together to create your own function useful for your optimization task. Example: Create a Matlab function that evaluates transfer function samples of a first-order low-pass filter over an array of frequency axis points (in Hz), for a given cutoff frequency (in Hz), and the gain at DC (in dB). dB) Function for Use in Iteration Process 1) Use a text editor to create a file lpf1.m p (Matlab program p files always end in .m). 2) Then enter the following text. function h = lpf1(f,fc,gdb) % This function evaluates complex p p points of a first % order low-pass filter with cut-off frequency fc % in Hertz, and a gain in dB at DC of gdb. The % function syntax is: % % tflp = lpf1(f, fc, gdb) % % where f is an array of points in Hertz where the % function is eavluated at, at and tfpl is the array of % complex evaluation points. s = j*2*pi*f; % compute complex frequencies along j omega axis. gdc = 10^(gdb/20); / % convert gain in dB to linear gain. h = gdc./(s/(2*pi*fc) + 1 ); % Evaluate TF point by point 3) Save the file in your “working directory” and execute in Matlab just like any other f function. If f you type help h l lpf1 l f1 in Matlab, M l b the h first f sequence of f comments will ll print to the screen. Function for Use in Iteration Process 4) All variables defined inside the function will be undefined when the function is through (local variables). Create an analogous function for a high-pass filter: 1) Create file with a text editor called hpf1.m and enter: function h = hpf1(f,fc,gdb) % This function evaluates complex points of a first % order high-pass filter with cut-off frequency fc % in Hertz, and a gain in dB at infinity of gdb. % The function syntax is: % % tfhp = hpf1(f, fc, gdb) % % where f is an array of points in Hertz where the % function is evaluated at, and tfhp is the array of % complex evaluation points. % s = j*2*pi*f; % compute complex frequencies along j omega axis. ginf=10^(gdb/20); % convert gain in dB to linear gain. h = ginf*(s/(2*pi*fc))./(s/(2*pi*fc) + 1 ); % Evaluate TF point by point Script for Optimization Matlab Scripts: Matlab functions can be combined together to create a series of commands to be executed. Example: Create a Matlab script to examine the mean and variance error b t between a cascaded d d llow-pass and d high-pass hi h filt filter, and dab band-pass-like d lik transfer function specification consisting of a flat 10 dB passband extending from 5 kHz to 50 kHz with a 20 dB per decade roll-off at either end of f the p passband. The gain g in the passband p is 10 dB. Only y consider the error from 500 Hz to 500 kHz. 1)Strategy: Since the major portion of overlap between the passbands of the LPF and HPF will exist between the cut-off cut off frequencies frequencies, set the gains equal to about 5.6 dB each (should put the combined gain close to 10db). Now consider minimizing error with respect to the cut-off frequencies of the LPF and the HPF. The g gain can be adjusted j later if assumption p on the passband gain is not accurate. 2) Create a file in a text editor called ex6.m, and enter: f =logspace(log10(500),log10(500e3), logspace(log10(500),log10(500e3), 512); % define axis for evaluation % Create target function (Described in Example) ht = zeros(1,512); % Set up array to fill with function points Script for Optimization dB % Loop through each linear section piece-wise, to create target shape for k=1:512 % For points less than 5kHz create a line on a log scale if( f(k) < 5e3) , ht(k) = -10 + 20*log10(f(k)/500); Target TF 10 % For points greater than 5kHz and less than 50kHz elseif(f(k) < 5e4), 5 ht(k) = 10; 0 % for points greater than 50kHz create a line on a % log scale -5 else -10 ht(k) = 10 - 20*log10(f(k)/5e4); end -15 10 10 10 10 end Hertz % Note the above code has nothing to do with your project % It should not be seen in any of your codes figure(1) semilogx(f,ht) % Check to see if it did what we wanted it to grid title('Target ( g TF') ) xlabel('Hertz') ylabel('dB') pause % Script will pause until the keyboard is struck % Set up nested loops to vary the cutoff frequencies for HPF and LPF % Tr Try a neighborhood of freq frequencies encies aro around nd f f= 5 kH kHz and 50 kH kHz. fhc = linspace(3500,7500,25); % Select a set of high-pass cut-offs flc = linspace(35000, 55000, 25); % Select a set of low-pass cut-offs 2 3 4 5 6 10 Script for Optimization % S Set t up arrays t to store t computed t d error t terms em = zeros(25,25); % mean error (bias) ev = zeros(25,25); % variance error (consistency of fit) % Fix Gain at 5.6 dB => 10^(5.6/20) = 1.9055 and optimize for low-pass % and high-pass cutoff % Note, this may not be the best gain, but is just an initial guess. for k=1:25, for n=1:25, h = lpf1(f,flc(k),5.6).*hpf1(f,fhc(n),5.6); % Compute Resultant TF % Compute errors em(k,n) = mean(ht-20*log10(abs(h))); ev(k,n) = std(ht-20*log10(abs(h))); %This is actually square root of the % variance, which is just as good in terms of finding a minimum point. end end [k,n] = find(abs(em) == min(min(abs(em))))%find mean error index close to 0 % look at optimal TF h = lpf1(f,flc(k),5.6).*hpf1(f,fhc(n),5.6); % Compute Resultant TF % points at optimum semilogx(f,20*log10(abs(h)),'g') hold on semilogx(f, ht,':r') hold off titl ('C title('Compare target t t with ith d design i TF f for minimum i i mean error') ') xlabel('Hertz') Script for Optimization % To get a better view, try an intensity plot of the same data: figure(3) % Plot Mean Error imagesc(fhc, flc, abs(em)) colormap('gray') colorbar l b title('mean error') xlabel(' High-pass cut-off') ylabel(' Low-pass cut-off') figure(4) % Plot Variance (standard deviation) Error imagesc(fhc, flc, ev) colormap('gray') colorbar xlabel(' High-pass cut-off') ylabel(' Low-pass cut-off') ylabel( cut-off ) title('standard deviation error') pause 3) Save file and type ex6 at Matlab prompt. These commands will execute and when finished, all variables defined in the script will still be available in the Matlab workspace. Script for Optimization ylabel('dB') l b l('dB') flcmm = flc(k) % Convert index to frequency value fhcmm = fhc(n) % Convert index to frequency value pause [k,n] = find(ev == min(min(ev))) % find index of minimum variance error % Compute Resultant TF points at optimum h = lpf1(f,flc(k),5.6).*hpf1(f,fhc(n),5.6) figure(2) semilogx(f,20*log10(abs(h)),'g') hold on semilogx(f, ht,':r') hold off title('Compare target with design TF for minimum variance error') xlabel('Hertz') ylabel('dB') flcsd = flc(k) % Convert index to frequency value fhcsd = fhc(n) % Convert index to frequency value pause figure(3) mesh(fhc, flc, abs(em)) % Look at graphic distribution of error. title('mean error') figure(4) mesh(fhc, flc, ev) titl (' t d d d title('standard deviation i ti error') ') pause Script for Optimization % Changing g g the color map p to something g other than g gray y may y help p in % observing changes in levels. Use the 'jet' color scheme To get a % better view, try an intensity plot of the same data: figure(5) imagesc(fhc flc, imagesc(fhc, flc abs(em)) colormap('jet') colorbar title('mean error') xlabel(' High High-pass pass cut cut-off') off') ylabel(' Low-pass cut-off') figure(6) imagesc(fhc, flc, ev) colormap('jet') colorbar xlabel(' High-pass cut-off') ylabel(' Low-pass cut-off') title('standard deviation error') 3) Save file and type ex6 at Matlab prompt. These commands will execute. When finished, all variables defined in the script will still be available in the Matlab workspace. Optimization Results 4) The results of this program should yield a variance error at k=3 and n=16, which corresponds to cutoffs at 6000 Hz and 41667 Hz. Further improvement can be obtained by fixing the cutoffs and varying the gain. The following is the output of the previous script: M Mean Error E 10 dB 5 0 -5 -10 -15 2 10 3 4 10 5 6 10 10 10 Hertz Compare target with design TF for minimum variance error 15 10 5 dB k= 12 n= 8 flcmm = 4.4167e+004 fhcmm = 4.6667e+003 Variance Error k= 9 n= 16 flcsd = 4 1667e+004 4.1667e+004 fhcsd = 6000 Compare target with design TF for minimum mean error 15 0 -5 -10 -15 2 10 3 10 4 10 Hertz 5 10 6 10 Optimization Iterated Error Surfaces (mesh) Optimization Iterated Error Surfaces (Intensity Image) R lt f l map ‘‘gray’’ Result for color Optimization Iterated Error Surfaces (Intensity Image) R Result lt for f color l map ‘jet’ ‘j t’ 4 4 standard deviation error x 10 mean error x 10 2.2 35 3.5 2.4 3.5 2 2.2 1.8 1.6 1.4 1.2 4.5 1 Low-pass cu ut-off Low-pass cut--off 2 4 4 1.8 1.6 4.5 1.4 0.8 0.6 5 1.2 5 1 0.4 0.2 5.5 3500 4000 4500 5000 5500 6000 High-pass cut-off 6500 7000 7500 0.8 5.5 3500 4000 4500 5000 5500 6000 High-pass cut-off 6500 7000 7500 Parameter Optimization BPF Example: Example: Create a Matlab script that evaluates matching a desired transfer function shape by adding 3 second-order band-pass filters and varies the mid frequency bandwidth to variance error. The test points are att 100 100, 500 500, 1000 1000, 2000 2000, 3000 3000, and d 5000 H Hz and d correspond d tto TF magnitudes of 0, 6, 10, 2, 6, and -1 dB. See script opt1.m opt1 m on course web site http://www.engr.uky.edu/~donohue/ee221/mfile.html Since shape is the criterion, criterion only need to evaluate variance error: >> errv(k) = std(ht-tfg)^2; % Compute error for this bandwidth where ht is the vector of test point magnitudes for the target TF and tgf is the vector of corresponding points evaluated on the kth synthesized filter Parameter Optimization BPF Example: Target TF and Initial Guess Desired Circuit TF (black) and Test Circuit (red), bandwidth on middle filter = 600 Hz 12 dB % Initial guess for parameters of 3 BPFs in parallel 10 % BPF1 8 f01 = 1000 1000; % C Center t F Frequency H Hz 6 b1 = 600; % Bandwidth Hz g1 = 10; % Gain at center frequency in dB 4 % BPF2 2 f02 = 3000; % Center Frequency Hz 0 b2 = 2000; % Bandwidth Hz -2 g2 = 6; % Gain at center frequency in dB 10 % BPF3 % The third BPF helps fit the shape at the low end % to slow down the roll off from the middle BPF f03 = 300; % Center Frequency Hz b3 = 500; % Bandwidth Hz g3 = 4; % Gain at center frequency in dB 2 3 10 Hz 4 10 Parameter Optimization BPF Example: With all other parameters fixed, vary bandwidth of middle BPF (BPF1) from 200 to 2000 Hz in linear steps of 10. Various iterations: Desired Circuit TF (black) and Test Circuit (red), bandwidth on middle filter = 450 Hz 12 Desired Circuit TF (black) and Test Circuit (red), bandwidth on middle filter = 800 Hz 12 10 8 8 6 6 dB dB 10 4 4 2 2 0 0 -2 2 10 3 10 Hz 4 10 Desired Circuit TF (black) and Test Circuit (red), bandwidth on middle filter = 1100 Hz 12 -2 2 10 4 10 Desired Circuit TF (black) and Test Circuit (red), bandwidth on middle filter = 1750 Hz 12 10 10 8 8 6 6 dB dB 3 10 Hz 4 4 2 2 0 0 -2 2 10 3 10 Hz 4 10 -2 2 10 3 10 Hz 4 10 Parameter Optimization BPF Example Iteration corresponding to minimum variance error. Error as a function of filter parameter, with minimum error at 810 Hz Desired Circuit TF (black) and Test Circuit (red), bandwidth on middle filter = 810 Hz 12 8 7.5 10 7 8 6 6 dB Variance error 6.5 5.5 4 5 2 4.5 X: 810 Y: 3.76 4 3.5 200 400 600 800 1000 1200 1400 Bandwidth of Filter 1 in Hz 0 1600 1800 2000 -2 2 10 3 10 Hz 4 10