Saturday, July 23, 2011

Another Another New Tone Circuit (I think the last)

Introduction
I am very excite! I finally have the solution to my tone circuit. It's been a long time, and a lot work. I've been pretty happy with my improvement in intuition when it comes to analog circuits. These are very simple, but it's amazing what you don't know after leaving undergrad. It's fun to get to know subjects like this. Anyway, let's talk about the new (final) circuit.

Why the Change?
You may realize that I just posted a new circuit with the same capabilities (i.e. adjustment of cutoff frequency and adjustment of attenuation floor). This was the last post, and the circuit was completely passive. I did put the circuit together, and it sounded like it worked, but there was an additional problem that I did not foresee.

If you think about it, if we're adjusting both the real value of the impedance in the shunt portion of the low pass filter and the feed portion, we are going to change the DC gain. And this is exactly what happened to me. There was a dramatic drop in volume as I changed the cutoff frequency. I'm sure it was the case that the cutoff frequency did in fact change, but the drop in volume was unacceptable.

Avoiding the Volume Change
The thought occurred to me that an active filter might be able to compensate for the tendency of the circuit's DC gain to change with cutoff frequency. So I conceived the following circuit.


If you've read my previous post about a standard active 1-pole low pass filter, you should find this schematic familiar. I had implemented the circuit without R3 before, but I decided that the absence of an attenuation floor adjustment was too different than the original tone circuit. But I realized that you can add the floor feature by adding R3 to the circuit. Notice that all the pots in the circuit are wired as variable resistors (3rd terminal floating). In my implementation, R1=R2 to maintain unity gain.

Here's an intuitive description of the circuit. Recall that the standard opamp inverter has a gain of -R2/R1 where R2 is the feedback resistor and R1 is the input resistor. This still applies, but now R2 is replaced by a feedback network with complex impedance.

First ignore R3 (let's say that it's turned all the way down). We're back to the original filter. For high frequencies, the capacitor becomes a short, and the feedback impedance is 0 (so the gain is 0). For low frequencies, the capacitor is an open circuit, so the feedback impedance is simply R2.

Now let's consider R3. At high frequencies, the capacitor will still be a short circuit, but the feedback network's impedance will now be R2//R3 (which is less than R2). So we will still attenuate high frequencies, but R2//R3 is still a lot more than 0. R3 provides a floor for the attenuation. At low frequencies, the capacitor becomes an open circuit and R3 has no effect on the circuit. We're back to an impedance of R2, just like before we added R3.

Adjusting the cutoff frequency is as simple as adjusting R1 and R2. If we maintain the condition that R1=R2, then the DC gain will always be unity. So let's use a dual ganged pot. Radd is there to make sure we can't adjust the resistances in the circuit all the way down to 0. A value of 100 still yields roughly 7kHz for a maximum cutoff frequency.

The values I used in testing were as follows:
R1 = R2 = 5K Log Pot
R3 = 500 K Log Pot
C = 0.22uF
Radd = 100

Performance
Here is the plot of the frequency response. Again, I have used an animated GIF to show different values for R1 and R2. Each graph should resemble the original tone circuit response that came in the guitar. However, R1 and R2 give us the ability to adjust the cutoff frequency, whereas it is constant in the original tone circuit. And again, you can see that the DC gain of the circuit remains 1 (0dB) regardless of cutoff frequency.


** hmm i can't get this gif to work. I wonder if blogspot has changed their picture policy. I got a GIF up here before without any trouble. I guess you'll have to take my word for it for now.**

A Couple of Considerations
As I mentioned briefly, we can't have 0 ohm resistors in the feedback or input paths. This is the reason for Radd. Radd puts an upper limit on our cutoff frequency as well.

If our goal is to allow R3 to completely nullify the effects of the capacitor in the circuit when turned all the way up, then R3 needs to be much larger than R2. The value of R2 changes (obviously, that's the point), so often it will be small in the first place. The worst case is when we turn R2 all the way up (moving the cutoff frequency as low as it will go). At that point, putting R3 in parallel with R2 can affect the maximum attenuation floor realizable by the system. So even though the problem would be less evident at higher cutoff frequencies, the ability to nullify the capacitor would worsen as we adjusted down. Therefore, it is important to make R3 >> R2.

Of course, the price we pay for this adjustment is less precision on the R3 adjustment. However, as it turns out, R2 should only be 5K, so the original tone pot 500K will work nicely. I just ran the numbers for 250K, 100K, and 50K. At the lowest cutoff frequency, 250K looks good in the passband (a drop of less than .2 dB), but 100K and 50K have passband gains of more like -.5dB and -.8dB. That would probably be noticeable. But you'd have to listen to it.

Cutoff Frequency Selection
I graphed the cutoff frequency for 400 logarithmically spaced combinations of R1,R2 and R3 (20 values each). Here are the results.

The different traces are for different values of R3. As you can see, adjusting R3 has little effect on the cutoff frequency of the circuit. It does have a little more effect at higher cutoff frequency settings, but I don't think it's anything to be worried about. There are a couple of outliers, but this is probably a numerical artifact when R3 is set so that the attenuation floor is above -3dB. Obviously cutoff frequency has no meaning in this case, since it is defined as the frequency at which the circuit's gain is -3dB.

Calculating the Cutoff Frequency
One thing to mention is that the cutoff frequency is not as easy to calculate for this more complicated circuit as it is for the simple Active 1-pole filter. I worked out the math for a while, but it was clear that an analytical solution to the equation would be very difficult to obtain. So I had to solve for it numerically. I used the secant method, similar to Newton's Method, but without a requirement for the derivative of the function in question. I fixed the values for all the circuit's components, constraining the equation to one free variable. The code is included below.

Conclusion
I have put this together on a breadboard, and the results are excellent. Here are a couple of pictures of my setup. I just finished making that desk a couple of weeks ago. (designed in 3d autocad and put together with pocket hole screws). This is the first project on the new project desk. Yes I know the top is particle board. It is meant to be a project desk, not a piece of fine furniture.


I didn't have a dual ganged pot for the 5K, so I used 2 single ganged and one of my wife's thousands of hair rubber bands as a pulley belt. Not the best in the world, but it worked well enough for testing. I'm digging having the new desk. It's 72" by 26". Designed to fit in that space.

I'm very excited about finally solving this problem. Now I need to get a microcontroller and program the rest of the circuit. That will be another blog post to reveal why I need a microcontroller. Basically, I'm going digital on the blending pots.

Code
Here's the main simulation code.
% my adjustable tone control simulation % This is an active circuit with a parallel combination of % R2 and (R3 in series with C1) in the feedback path. % The input has R1 in series. R1=R2 and they are variable. % R3 is also variable. C1 is constant. % Normal Gain Equation is G = -R2/R1 % 0.22 uF capacitor Ctone = 0.22e-6; % Added Series Resistor to R1,R2 Radd = 100; % Series Resistor with C1. Affects the floor of the filter R3Val = 500e3; % steps on the tone resistor R3 = logspace(0, log10(R3Val)); % Parallel Resistor and Input Resistor. Affect the cutoff frequency of the filter R1a2Val = 5e3; % steps on the series tone resistor R1a2 = logspace(0,log10(R1a2Val)) + Radd; % steps in frequency freq = logspace(0, log10(20e3), 100); % Impedance of C1 ZCtone = repmat( (-j./(2*pi*Ctone*freq))' , 1, size(R3,2) ); % Resize R3 R3 = repmat(R3, size(ZCtone, 1) , 1); % Series Impedance of Feedback Path 1 % Down = Frequency % Across = R3 Changes % Depth = R1 and R2 Change Z1 = repmat( R3 + ZCtone, [1 1 size(R1a2,2)] ); % Resize R1a2 for operations with Z1 R1a2 = reshape( R1a2, [1 1 size(Z1,3)]); R1a2 = repmat(R1a2, [size(Z1,1) size(Z1,2), 1]); % Calculate Impedance of entire feedback path Z2 = ( R1a2 .* Z1 ) ./ ( R1a2 + Z1 ); % Calculate the Gain gain = abs(-Z2 ./ R1a2 ); % Calculate Cutoff Frequency % Analytical solution is difficult, so let's use numerical methods % below is the equation for the gain in symbolic form gainf = inline( 'abs(-1/( (1/(R3 - sqrt(-1)/(2*pi*f*C)) + 1/R2 ) ) / R1)', 'f', 'C', 'R1', 'R2', 'R3'); % x(down) = R % y(across) = constant Rtone / changing Rtone2 fcpoints = 20; R1a2fc = logspace(0, log10(R1a2Val), fcpoints) + Radd; R3fc = logspace(0, log10(R3Val), fcpoints); fc = zeros(fcpoints); mygain = zeros(fcpoints); for ind = 1:fcpoints for jnd = 1:fcpoints % fc is usually around this value fcEst = 1/(2*pi*R1a2fc(ind)*Ctone); % use secant method to solve for gain = 1/sqrt(2) for each % combination of R1aR2 and R3 (Ctone is constant) R1a2fc(ind) R3(jnd) [fc(ind,jnd), mygain(ind, jnd)] = secantsolve(gainf, fcEst+10, fcEst-10, 1/sqrt(2), 0.01, Ctone, R1a2fc(ind), R1a2fc(ind), R3fc(jnd)); end end % get rid of NaN %mygain(isnan(mygain)) = 0; %fc(isnan(fc)) = 0; % create Gain Gif figure(1) filename = 'Gain.gif'; for n = 1:size(gain,3) semilogx(freq,20*log10(gain(:,:,n))); title(sprintf('R1 and R2(KOhms) = %.3f ; R3(KOhms) = 0 - %.3f', R1a2(1,1,n)/1000, R3Val/1000)); ylim([-12 0]); xlabel('Frequency (Hz)'); ylabel('Gain (dB)'); drawnow frame = getframe(1); im = frame2im(frame); [imind,cm] = rgb2ind(im,256); if n == 1; imwrite(imind,cm,filename,'gif', 'Loopcount',inf); else imwrite(imind,cm,filename,'gif','WriteMode','append'); end end % plot fc of circuit figure(2); semilogx(R1a2fc,fc); xlabel('R1 and R2 (Ohms)'); ylabel('Cutoff Frequency (Hz)'); title ('Cutoff Frequency'); ylim([0 7e3]);


and the Secant Method Code

function [x, y] = secantsolve(myfun, guess1, guess0, goal, maxerror, C, R1, R2, R3) % [x, y] = secantsolve(myfun, guess1, guess0, goal, maxerror, C, R1, R2, R3) % secant solver equation % xj = xi - (xi-xh)/(yi-yh)*yi xj = guess1; xi = guess0; yj = myfun(guess1, C, R1, R2, R3); while abs(yj - goal) > maxerror && xi > 0 % shift x values xh = xi; xi = xj; % evaluate at x values yi = yj - goal; yh = myfun(xh, C, R1, R2, R3) - goal; % iterate xj = xi - (xi-xh)/(yi-yh)*yi yj = myfun(xj, C, R1, R2, R3) end x = xj; y = yj;


As always thanks for reading,

Bill

Saturday, June 25, 2011

Another New Guitar Tone Circuit

Introduction
After a long hiatus for which I will not beat myself up, I have gotten back to the guitar circuit.  Since my last post, I have gotten married, moved in to a new house, and dealt with several car issues.  So things have been pretty busy, but I have managed in the last couple of days to put some work in on the guitar circuit.  Here's a new simulation for a circuit that I plan to breadboard soon, maybe tonight.  My wife is studying for the BAR exam coming up, so I'm basically left to my own devices to amuse myself.

The New Circuit
The circuit is really not all that groundbreaking.  I realized a long time ago (as documented in this blog) that the pickup's internal resistance played a large part in determining the cutoff frequency for the tone circuit in my guitar.  Since I've been planning an active circuit with the pickups feeding opamp buffers instead of the normal passive circuit, I've had to reconsider what I want the tone circuit to be.

Having done a lot of design, research, and simulation on active circuits, I put one together to test.  Unfortunately, I wasn't all that happy with the results.  I realized that in all my effort to make the cutoff frequency adjustable, I had managed to change the way that the tone circuit modified the sound.  I though that it would be better that way, but it just didn't adjust the same way.  Not that that should have been surprising.  So I decided to try to preserve the original functionality while also adding the ability to adjust the cutoff frequency.

This is the simplest circuit imaginable.  So much so, that I don't really feel bad about not having a diagram.  Also I don't currently have access to Autocad.
  1. Take the original tone circuit, consisting of a tone pot in series with a capacitor and separate it from the rest of the original passive circuit.
  2. Now I'll put in all the opamp buffers, the summing amplifier, the rotary switches, all the fun stuff I've been desigining.  So we replace the rest of the circuit.
  3. Now we need to put the tone control back in the circuit.  But wait, we don't want to load down the pickups at the beginning.  That's one of the things that we tried to accomplish with all this opamp buffering in the first place.  
  4. We can replace the 10Kish pickup equivalent resistance with an actual resistor to bring back the tone circuit, but I still need to be able to adjust the frequency.
  5. To adjust the frequency, replace the pickup equivalent resistance with a series resistor (100 ohm) and a series tone pot (20 Kohm).
So our tone circuit goes in this order: 
  1. Opamp voltage source  
  2. fixed 100 ohm resistor
  3. 20Kohm variable resistor (2 terminals of a pot)
  4. Output voltage tap
  5. 50Kohm variable resistor (2 terminals of a second pot)
  6. 0.22 uF Capacitor
  7. Ground
Why use a 50K tone pot in the original tone pot position you ask?  Well as I discussed in the first Guitar Tone Analysis post, you really only use the first 30K or so of the tone pot in the first place.  At last that's what my simulations say.  We'll have to see how this sounds.  A 50K pot is a lot closer to 30K than 500K, so we should have a lot more control of our tone.  Also, I will use a log pot for reasons mentioned in the same article.

Simulation Results
I decided that the best way to display the results of the simulation would be an animated gif.  We have three variables here:  frequency (x), Rtone(y), and Rtone2(z).  Rtone is the original tone pot.  I kept its name for some continuity in my code.  So what we will see in the gif is a series of images that represent the same thing that I showed in the original post.

I plotted the magnitude of the gain from 0 Hz to 20KHz on a semilogx plot.  Each successive plot represents a different setting on the new tone pot (Rtone2).  Remember that the input of the filter is a series combination of a 100 Ohm resistor and the pot.  100 Ohms is about where the cutoff frequency gets too high to be useful.  In fact, it may still be still too high a cutoff frequency at that point.  I didn't listen to it, I just estimated. 

Note that when we hit about 11KOhms on the new tone pot (Rtone2) the graph should be virtually identical to the original graph.  I've standardized the y scale so that seeing the graphs back to back will make sense.  And without further ado, here's the graph.


Ok, a brief discussion.  All of the graphs look simliar.  This makes sense.  We didn't really change much in the circuit.  But it is obvious that the cutoff frequency does change as we adjust the value of Rtone2.  Also the original behavior of the flattening out of the frequency response by adjusting Rtone is preserved. 

Here is a plot of the cutoff frequency with respect to Rtone2 with Rtone set equal to 0.  So we're basically looking at a single pole filter while adjusting the input resistor. 


I tend to think of this plot as the starting point.  Here are all of the cutoff frequencies that we can achieve with an unaltered low-pass filter.  From there, we can flatten out the attenuation using the other pot.

Conclusion
I really make no claim that this circuit is in any way revolutionary.  Ok maybe not particularly creative either.  But I was interested to see the theory behind how it might work, and it will definitely accomplish the design goals that I wanted.  Whether it will sound good remains to be seen.  However, I believe that it can hardly sound bad, since it should function exactly like the original tone circuit if you just leave the new pot alone.  I'll probably do  this with concentric pots.  I'll have to think about it and maybe swap out some wafers.  For the breadboarding, I'll just use separate pots.

I did pick up some code from the interwebs that allowed me to create the gif file from inside Matlab.  So that's a nice bit of code to have.  The whole source code follows.

% my adjustable tone control simulation % 0.022 uF capacitor Ctone = 0.22e-6; % 500 Kohms Original Tone Pot Rtone = 50e3; % steps on the tone resistor toneres = logspace(0, log10(Rtone)); %toneres = linspace(0, Rtone, 50); %toneres = logspace(1,log10(30e3)); %toneres = linspace(0,30e3,50); % Added series tone pot to take the place of Rpickup Rtone2ser = 100; Rtone2 = 20e3; % steps on the series tone resistor toneres2 = logspace(0,log10(Rtone2)) + Rtone2ser; % steps in frequency freq = logspace(log10(20), log10(20e3), 100); ZCtone = repmat( (-j./(2*pi*Ctone.*freq))' , [1 size(toneres,2) size(toneres2,2)]); Ztone = repmat(toneres,[size(ZCtone,1) 1 size(ZCtone,3)]) + ZCtone; Ztone2 = ones(1,1,size(Ztone,3)); Ztone2(1,1,:) = toneres2; Ztone2 = repmat(Ztone2, [size(Ztone,1) size(Ztone,2), 1]); gain = abs(Ztone ./ (Ztone + Ztone2) ); % fc = 1/ (2pi * Ctone * sqrt( Rtone2^2 + Rtone2*Rtone - Rtone^2) % we set up the matrices so that % x(down) = constant Rtone2 / changing Rtone % y(across) = constant Rtone / changing Rtone2 Rtone_fc = repmat(toneres', [1 size(toneres,2)]); Rtone2_fc = repmat(toneres2, [size(toneres2,2) 1]); fc = 1./(2*pi*Ctone*sqrt(Rtone2_fc.^2 + Rtone2_fc.*Rtone_fc - Rtone_fc.^2)); % create Gain Gif figure(1) filename = 'Gain.gif'; for n = 1:size(gain,3) semilogx(freq,20*log10(gain(:,:,n))); title(sprintf('Rtone2(KOhms) = %.3f ; Rtone(KOhms) = 0 - %.3f', Ztone2(1,1,n)/1000, Rtone/1000)); ylim([-12 0]); xlabel('Frequency (Hz)'); ylabel('Gain (dB)'); drawnow frame = getframe(1); im = frame2im(frame); [imind,cm] = rgb2ind(im,256); if n == 1; imwrite(imind,cm,filename,'gif', 'Loopcount',inf); else imwrite(imind,cm,filename,'gif','WriteMode','append'); end end % plot fc of circuit figure(4); semilogx(toneres2,real(fc(1,:))); xlabel('Rtone2 (Ohms)'); ylabel('Cutoff Frequency (Hz)'); title ('Cutoff Frequency with Rtone = 0');


Thanks for reading,
Bill