1 % my adjustable tone control simulation 2 % This is an active circuit with a parallel combination of 3 % R2 and (R3 in series with C1) in the feedback path. 4 % The input has R1 in series. R1=R2 and they are variable. 5 % R3 is also variable. C1 is constant. 6 % Normal Gain Equation is G = -R2/R1 7 8 % 0.22 uF capacitor 9 Ctone = 0.22e-6; 10 11 % Added Series Resistor to R1,R2 12 Radd = 100; 13 14 % Series Resistor with C1. Affects the floor of the filter 15 R3Val = 500e3; 16 % steps on the tone resistor 17 R3 = logspace(0, log10(R3Val)); 18 19 % Parallel Resistor and Input Resistor. Affect the cutoff frequency of the filter 20 R1a2Val = 5e3; 21 % steps on the series tone resistor 22 R1a2 = logspace(0,log10(R1a2Val)) + Radd; 23 24 % steps in frequency 25 freq = logspace(0, log10(20e3), 100); 26 27 % Impedance of C1 28 ZCtone = repmat( (-j./(2*pi*Ctone*freq))' , 1, size(R3,2) ); 29 30 % Resize R3 31 R3 = repmat(R3, size(ZCtone, 1) , 1); 32 33 % Series Impedance of Feedback Path 1 34 % Down = Frequency 35 % Across = R3 Changes 36 % Depth = R1 and R2 Change 37 Z1 = repmat( R3 + ZCtone, [1 1 size(R1a2,2)] ); 38 39 % Resize R1a2 for operations with Z1 40 R1a2 = reshape( R1a2, [1 1 size(Z1,3)]); 41 R1a2 = repmat(R1a2, [size(Z1,1) size(Z1,2), 1]); 42 43 % Calculate Impedance of entire feedback path 44 Z2 = ( R1a2 .* Z1 ) ./ ( R1a2 + Z1 ); 45 46 % Calculate the Gain 47 gain = abs(-Z2 ./ R1a2 ); 48 49 % Calculate Cutoff Frequency 50 % Analytical solution is difficult, so let's use numerical methods 51 % below is the equation for the gain in symbolic form 52 gainf = inline( 'abs(-1/( (1/(R3 - sqrt(-1)/(2*pi*f*C)) + 1/R2 ) ) / R1)', 'f', 'C', 'R1', 'R2', 'R3'); 53 % x(down) = R 54 % y(across) = constant Rtone / changing Rtone2 55 fcpoints = 20; 56 R1a2fc = logspace(0, log10(R1a2Val), fcpoints) + Radd; 57 R3fc = logspace(0, log10(R3Val), fcpoints); 58 59 fc = zeros(fcpoints); 60 mygain = zeros(fcpoints); 61 for ind = 1:fcpoints 62 for jnd = 1:fcpoints 63 % fc is usually around this value 64 fcEst = 1/(2*pi*R1a2fc(ind)*Ctone); 65 % use secant method to solve for gain = 1/sqrt(2) for each 66 % combination of R1aR2 and R3 (Ctone is constant) 67 R1a2fc(ind) 68 R3(jnd) 69 [fc(ind,jnd), mygain(ind, jnd)] = secantsolve(gainf, fcEst+10, fcEst-10, 1/sqrt(2), 0.01, Ctone, R1a2fc(ind), R1a2fc(ind), R3fc(jnd)); 70 end 71 end 72 % get rid of NaN 73 %mygain(isnan(mygain)) = 0; 74 %fc(isnan(fc)) = 0; 75 76 77 % create Gain Gif 78 figure(1) 79 filename = 'Gain.gif'; 80 for n = 1:size(gain,3) 81 semilogx(freq,20*log10(gain(:,:,n))); 82 title(sprintf('R1 and R2(KOhms) = %.3f ; R3(KOhms) = 0 - %.3f', R1a2(1,1,n)/1000, R3Val/1000)); 83 ylim([-12 0]); 84 xlabel('Frequency (Hz)'); 85 ylabel('Gain (dB)'); 86 drawnow 87 frame = getframe(1); 88 im = frame2im(frame); 89 [imind,cm] = rgb2ind(im,256); 90 if n == 1; 91 imwrite(imind,cm,filename,'gif', 'Loopcount',inf); 92 else 93 imwrite(imind,cm,filename,'gif','WriteMode','append'); 94 end 95 end 96 97 98 99 % plot fc of circuit 100 figure(2); 101 semilogx(R1a2fc,fc); 102 xlabel('R1 and R2 (Ohms)'); 103 ylabel('Cutoff Frequency (Hz)'); 104 title ('Cutoff Frequency'); 105 ylim([0 7e3]);