Square wave from sine, follow envelope? - Page 1
Forum section: electronics / projects

author: alexanderbrevig
author_rank: Frequent Contributor
timestamp: August 18, 2017, 07:24:38 am
content: Hi everyone!While I played around with some custom VST plugin I stumbled upon something I though was kind of cool:I made a square wave effect that followed the envelope (and frequency of course) of the input signal.Then, I tried translating that to LTSPICE just as a challenge (I rarely touch analog) and this is what I came up with.Am I trying to solve something that has proven solution? It kind of felt harder than it should've. (I want to power off of a 9V battery in the end).Attached is the images and the LTSPICE.EDIT: Just to mention, as it will probably be clear to those of you who know this stuff better than me (*most of you): The OpAmps were chosen on a 'does it work' basis.Next step: to breadboard it! But I want to wait before I get some feedback. Maybe there are better solutions, and maybe you can suggest some nice opamps to try outThen order, and test will commence. Thanks.

author: ironmonkey
author_rank: Contributor
timestamp: August 18, 2017, 07:40:45 am
content: It's a already solved problem. Try in spice an opamp with its inverting input to 0 V and its non inverting input to the signal, without any feedback and see the result.

author: alexanderbrevig
author_rank: Frequent Contributor
timestamp: August 18, 2017, 07:50:36 am
content: That won't follow any envelopes and is used in my design for generating the square. Thanks though

author: Zero999
author_rank: Super Contributor
timestamp: August 18, 2017, 07:57:25 am
content: The negative power supply to U3 is connected incorrectly. R7 and C3 are connected in series with the negative. Simulators often overlook supply currents to amplifiers, so this may work in SPICE but I doubt it will in real life.

author: alexanderbrevig
author_rank: Frequent Contributor
timestamp: August 18, 2017, 08:04:31 am
content: Quote from: Hero999 on August 18, 2017, 07:57:25 amThanks! That's actually just something I tried for fun to try to remove some of the biased 4.5V on there. But I won't simply copy this to a PCB and go for it. First I will make a prototype of some kind, to fine tune some components by ear first.Any other insight Hero999? Is there an easier way to do this?Thanks again!

author: Rbastler
author_rank: Frequent Contributor
timestamp: August 18, 2017, 08:09:01 am
content: To Make a square out of a sind wave, I use Inverter ICs like the 40106 that have a schmitttrigger input. 4093 nand works also.Sent from my A0001 using Tapatalk

author: alexanderbrevig
author_rank: Frequent Contributor
timestamp: August 18, 2017, 08:24:55 am
content: Quote from: Rbastler on August 18, 2017, 08:09:01 amI also want the amplitude to follow the sine.

author: ironmonkey
author_rank: Contributor
timestamp: August 18, 2017, 09:44:04 am
content: Quote from: alexanderbrevig on August 18, 2017, 07:50:36 amWhy not? It is a zero crossing detector, or I was deceived all this years? Enviado desde mi MotoG3 mediante Tapatalk

author: alexanderbrevig
author_rank: Frequent Contributor
timestamp: August 18, 2017, 10:24:52 am
content: It will replicate the frequency, but going rail to rial (what the opamp can manage at least). So a small signal at 1kHz and a large signal at 1kHz will both produce the same 1kHz square.My design modulates the amplitude of the square to be closer to that of the input sine.I've gotten a tip to try to use a JFET in the feedback so to avoid changing the Vcc of the final opamp. I'll give it a go later.

author: Zero999
author_rank: Super Contributor
timestamp: August 18, 2017, 10:41:54 am
content: You need a proper comparator. What you have there will kind of work, but using the peak detector to power U3 is a bodge. It will work as long as the amplitude of the sine wave is high enough to power U3 and the output will be unpredictable, since the OP07 does not have a rail-to-rail output. Try it with a 0.5V peak sine wave and it will fail. If SPICE tells you it'll work, then it's lying to you.Change U3 to a proper comparator IC with an open collector output, such as the LM393 or LM311, rather than abusing an op-amp as a comparator. Power U3 from +9V, rather than the output of U1 and connect a suitable pull-up resistor, say 2k2, from the output of U3 to the output of U1, so its output voltage follows U1.

author: alexanderbrevig
author_rank: Frequent Contributor
timestamp: August 18, 2017, 11:58:58 am
content: It probably is a bodge and that's why I'm asking here. It didn't feel right.I'll definitely try a comparator, not quite sure what you meant for the amplitude control though. I'll take a look for sure.BTW the first sine is 200mV peak around 4.5, even 50 works well in SPICE.

author: Zero999
author_rank: Super Contributor
timestamp: August 18, 2017, 12:35:05 pm
content: Quote from: alexanderbrevig on August 18, 2017, 11:58:58 amLook up comparators. Note the output stage of a comparator and how it's normally used with a pull-up resistor, which usually goes to +V. The trick is, the pull-up doesn't have to go to +V. It can go to any voltage more positive than the negative pin on the IC, up to the maximum voltage rating of the output transistor.QuoteYes, it's a lie. Simulators often take various short cuts with modelling the output stage. I've encountered this before, except it was the reverse: SPICE told me something wouldn't work, when it did in real life.https://www.eevblog.com/forum/projects/better-ltspice-tl072-model/msg999066/#msg999066

author: Zero999
author_rank: Super Contributor
timestamp: August 18, 2017, 06:18:04 pm
content: There were some strange things in there. Why do you need a gain of 1.1? I took it out to demonstrate than the gain and bias section isn't needed if unity is acceptable.I removed one of the op-amps configured as a comparator and replaced it and the variable power supply bodge, with the LM393.

author: basinstreetdesign
author_rank: Frequent Contributor
timestamp: August 19, 2017, 07:07:34 am
content: +1, Hero999Yeah, that's about it, except put a buffer between R8 and R9.

author: Zero999
author_rank: Super Contributor
timestamp: August 19, 2017, 09:15:14 am
content: Quote from: basinstreetdesign on August 19, 2017, 07:07:34 amYes, you're right. Without a buffer C1 will discharge via R9.

author: alexanderbrevig
author_rank: Frequent Contributor
timestamp: August 20, 2017, 06:13:12 pm
content: Thank you so much guys! Especially you Hero999Attached is my modified solution with the comparator (lesson learned, so much easier to work with!).The square now maintains approx the same amplitude, and seems to handle well.Next step is to make a PCB for this and tweak the values some more, with a real input signal!Thanks again!EDIT: how do I attach an image from attachment here so it's large?

author: Zero999
author_rank: Super Contributor
timestamp: August 20, 2017, 06:35:24 pm
content: Looks good apart from the last part. What does D2, C5 and R11 do? It looks like a bodge.

author: alexanderbrevig
author_rank: Frequent Contributor
timestamp: August 20, 2017, 06:47:06 pm
content: Makes 4.5v be the low side from the comparator. Without it it goes from 0 to ~4.7 when I only want ~0.2.It probably is a bodge. I don't know much and I tried to search for 'subtracting voltage' and the like but didn't find a solution. So I came up with my own "solution".

author: Zero999
author_rank: Super Contributor
timestamp: August 20, 2017, 08:34:18 pm
content: Quote from: alexanderbrevig on August 20, 2017, 06:47:06 pmBut it also messes up the squarewave slightly and cause small signals to disappear altogether. If you don't need a precision rectifier, then why not change the feedback loop on U1 so it no longer goes via D1?The .asc file you've attached must be different to the one you used when you ran the simulation and took the plot. Here's what the .asc you've attached gives me. I'm using the latest version of LTSpice XVII.

author: alexanderbrevig
author_rank: Frequent Contributor
timestamp: August 20, 2017, 08:52:11 pm
content: Quote from: Hero999 on August 20, 2017, 08:34:18 pmI need the sample and hold so that C1 holds the charge in order to make the next square as square as possible going down (and up).As far as I can tell by simulation the diode does not mess anything up, even for small signals. The voltage U2 provides is biased at 4.5 and will never go below that. More than enough to bias the diode.Not sure if I've said, but this is going to be used as a guitar effect so instrumentation accuracy is of no concern for me.Quote from: Hero999 on August 20, 2017, 08:34:18 pmI use the same version and the same .asc, but I observed the biased signal (before C6 for insig, the cap is to make audible data) so it looks different. I did it to make both waves visible to show that their amplitudes are similar.I think I'll design a quick PCB and just try itThanks again for your help and input Hero999!

author: Zero999
author_rank: Super Contributor
timestamp: August 20, 2017, 09:04:25 pm
content: Oh I see now, as you just get a Schottky drop off the output of the comparator.Why not simply use a +/-4.5 power supply for simulation purposes? It would speed things up, even if you use the op-amp virtual earth circuit in real life. The op-amp models will have all sorts of approximations similar to that, in order to make it quicker to simulate.If the signal level is too high, then why not simply reduce the gain?I suggest you build it without D2, C5 & R11 first (if you're doing a board you can always leave space for them, even if you might not fit them). You'll find the circuit behaves slightly differently if real life. For example, the output stage of the comparator will not go all the way down to 0V.

author: alexanderbrevig
author_rank: Frequent Contributor
timestamp: August 20, 2017, 09:09:54 pm
content: It will be easy enough to bypass D2,C5,R11 so I'll lay out with them on there.Even if the comparator won't go all the way down to 0 is a pretty big difference between a 200mV signal, and a 4.7V signal.This is the whole reason for making this in the first place. A square wave that has an amplitude similar to the input.To be honest about that voltage setup... It's because I was not sure how to do it properly. Glad I did, as it seems as though I could not get it working properly without buffering the virtual ground.Expect to learn a lot more trying to build this thing! Hehe.Give me a microcontroller any day over this analog stuff...

author: StillTrying
author_rank: Super Contributor
timestamp: August 20, 2017, 09:18:04 pm
content: If it's any use, putting a value into the LT Sine source's "Theta{1/s}" box (~+30 or ~-30) produces a sine that varies in amplitude with time.

author: Zero999
author_rank: Super Contributor
timestamp: August 20, 2017, 11:36:35 pm
content: I think it would be easier if you just used single supply op-amps.

author: alexanderbrevig
author_rank: Frequent Contributor
timestamp: August 21, 2017, 02:14:58 am
content: Hero999, that is absolutely perfect!Much easier and still exactly what I wantThanks again!

author: alexanderbrevig
author_rank: Frequent Contributor
timestamp: August 22, 2017, 11:23:08 pm
content: As I've saidI just tried diptrace and I like it a lot!Here's my layout of this thing: (as small as possible because why not.,..)

author: Ian.M
author_rank: Super Contributor
timestamp: August 23, 2017, 04:21:03 am
content: Here's another approach to the problem:I'm assuming its fed by a buffer amp with a very low output impedance.Detect the peaks by the reversal of the current through a capacitor, and use that to trigger a sample & hold circuit to hold the output till the next peak. On a waveform with alternating positive and negative peaks that will give you a squarewave that follows the envelope. However on more complex waveforms it gets a bit messy.The devil is however in the details - to cover the whole audio frequency range (say 50Hz to 20Khz) you need an analog switch with a very high ratio of off resistance to on resistance.Ltspice sim attached. Change the .param freq to change the frequency as it alters the simulation length to match. You'll need74hct.liband the74HCT symbols, specifically the quad XOR74HCT86, from the LTwiki files area.A practical implementation would either run everything at 5V single supply using rail to rail CMOS OPAMPs and 74HC logic, or use 4000 series CMOS logic to avoid the need for a 5V logic supply. If using logic with symmetrical thresholds, omit R4 and readjust the R3*C3 time constant (which controls the sampling gate time) to suit. Also the peak detector comparator will need a trace of hysteresis added to reduce chatter due to noise. Too much compromises its ability to follow small signals and skews the sampling point away from the peak.

author: dorin
author_rank: Contributor
timestamp: August 23, 2017, 09:52:16 am
content: Not sure if it matters for your application, but what you are getting there with these circuits is a very distorted square wave. To get a clean square wave, the circuit would need to be much more complicated.

author: Ian.M
author_rank: Super Contributor
timestamp: August 23, 2017, 10:10:51 am
content: I've had some further thoughts on the sampling at peak approach:If one uses a two channel SPDT switch, e.g CD4053, and two hold caps, one can discard the quad XOR and drive the switch direct from the comparator.It would alternately connect one cap to the input and the other to the output buffer amp, swapping them on every signal peak. Because the cap connected to the input follows the signal for the whole interval between peaks, it avoids the problem of trying to charge the hold cap very quickly before the signal has dropped too far from the peak and will give much sharper edges. That gets it down to a three chip solution: Comparator, CD4053, and a dual FET input OPAMP to buffer input and output.

author: alexanderbrevig
author_rank: Frequent Contributor
timestamp: August 23, 2017, 10:35:16 am
content: Quote from: dorin on August 23, 2017, 09:52:16 amDoes not matter at all for my application (a guitar effect). We like distortion

author: alexanderbrevig
author_rank: Frequent Contributor
timestamp: September 15, 2017, 05:52:15 pm
content: Just wanted to say, I made it and it works!Clips out some of the lowest playing, and acts as a gate in turn. Not sure yet if I like it or not.I wonder if it is the first zener from ground or what? Hmm.

author: Zero999
author_rank: Super Contributor
timestamp: September 16, 2017, 07:26:29 pm
content: Quote from: alexanderbrevig on September 15, 2017, 05:52:15 pmI don't know. Which schematic did you build? None of the ones posted, have a zener diode.