Author Topic: Square wave from sine, follow envelope?  (Read 7022 times)

0 Members and 1 Guest are viewing this topic.

Offline alexanderbrevigTopic starter

  • Frequent Contributor
  • **
  • Posts: 700
  • Country: no
  • Musician, developer and EE hobbyist
    • alexanderbrevig.com
Square wave from sine, follow envelope?
« on: August 18, 2017, 07:24:38 am »
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 out :) Then order, and test will commence. Thanks.
« Last Edit: August 18, 2017, 07:28:15 am by alexanderbrevig »
 

Offline ironmonkey

  • Contributor
  • Posts: 19
  • Country: co
Re: Square wave from sine, follow envelope?
« Reply #1 on: August 18, 2017, 07:40:45 am »
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.
 

Offline alexanderbrevigTopic starter

  • Frequent Contributor
  • **
  • Posts: 700
  • Country: no
  • Musician, developer and EE hobbyist
    • alexanderbrevig.com
Re: Square wave from sine, follow envelope?
« Reply #2 on: August 18, 2017, 07:50:36 am »
That won't follow any envelopes and is used in my design for generating the square. Thanks though :)
 

Offline Zero999

  • Super Contributor
  • ***
  • Posts: 19994
  • Country: gb
  • 0999
Re: Square wave from sine, follow envelope?
« Reply #3 on: August 18, 2017, 07:57:25 am »
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.
 

Offline alexanderbrevigTopic starter

  • Frequent Contributor
  • **
  • Posts: 700
  • Country: no
  • Musician, developer and EE hobbyist
    • alexanderbrevig.com
Re: Square wave from sine, follow envelope?
« Reply #4 on: August 18, 2017, 08:04:31 am »
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.
Thanks! 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!
 

Offline Rbastler

  • Frequent Contributor
  • **
  • Posts: 286
  • Country: it
  • Wörk Wörk
    • Rbastlers Blog
Re: Square wave from sine, follow envelope?
« Reply #5 on: August 18, 2017, 08:09:01 am »
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

http://rbastlerblog.jimdo.com/
Gamma spectrometer works. Now some yellow crystals need regenerating and testing.
 

Offline alexanderbrevigTopic starter

  • Frequent Contributor
  • **
  • Posts: 700
  • Country: no
  • Musician, developer and EE hobbyist
    • alexanderbrevig.com
Re: Square wave from sine, follow envelope?
« Reply #6 on: August 18, 2017, 08:24:55 am »
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.
I also want the amplitude to follow the sine.
 

Offline ironmonkey

  • Contributor
  • Posts: 19
  • Country: co
Re: Square wave from sine, follow envelope?
« Reply #7 on: August 18, 2017, 09:44:04 am »
That won't follow any envelopes and is used in my design for generating the square. Thanks though :)
Why not? It is a zero crossing detector, or I was deceived all this years? [emoji32]

Enviado desde mi MotoG3 mediante Tapatalk

 

Offline alexanderbrevigTopic starter

  • Frequent Contributor
  • **
  • Posts: 700
  • Country: no
  • Musician, developer and EE hobbyist
    • alexanderbrevig.com
Re: Square wave from sine, follow envelope?
« Reply #8 on: August 18, 2017, 10:24:52 am »
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. :)
 

Offline Zero999

  • Super Contributor
  • ***
  • Posts: 19994
  • Country: gb
  • 0999
Re: Square wave from sine, follow envelope?
« Reply #9 on: August 18, 2017, 10:41:54 am »
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.
 

Offline alexanderbrevigTopic starter

  • Frequent Contributor
  • **
  • Posts: 700
  • Country: no
  • Musician, developer and EE hobbyist
    • alexanderbrevig.com
Re: Square wave from sine, follow envelope?
« Reply #10 on: August 18, 2017, 11:58:58 am »
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.  :)
 

Offline Zero999

  • Super Contributor
  • ***
  • Posts: 19994
  • Country: gb
  • 0999
Re: Square wave from sine, follow envelope?
« Reply #11 on: August 18, 2017, 12:35:05 pm »
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.
Look 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.

Quote
BTW the first sine is 200mV peak around 4.5, even 50 works well in SPICE.  :)
Yes, 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
 

Offline Zero999

  • Super Contributor
  • ***
  • Posts: 19994
  • Country: gb
  • 0999
Re: Square wave from sine, follow envelope?
« Reply #12 on: August 18, 2017, 06:18:04 pm »
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.
« Last Edit: August 18, 2017, 06:21:12 pm by Hero999 »
 
The following users thanked this post: alexanderbrevig

Offline basinstreetdesign

  • Frequent Contributor
  • **
  • Posts: 461
  • Country: ca
Re: Square wave from sine, follow envelope?
« Reply #13 on: August 19, 2017, 07:07:34 am »
+1, Hero999
Yeah, that's about it, except put a buffer between R8 and R9.   :-+
STAND BACK!  I'm going to try SCIENCE!
 

Offline Zero999

  • Super Contributor
  • ***
  • Posts: 19994
  • Country: gb
  • 0999
Re: Square wave from sine, follow envelope?
« Reply #14 on: August 19, 2017, 09:15:14 am »
+1, Hero999
Yeah, that's about it, except put a buffer between R8 and R9.   :-+
Yes, you're right. Without a buffer C1 will discharge via R9.
 

Offline alexanderbrevigTopic starter

  • Frequent Contributor
  • **
  • Posts: 700
  • Country: no
  • Musician, developer and EE hobbyist
    • alexanderbrevig.com
Re: Square wave from sine, follow envelope?
« Reply #15 on: August 20, 2017, 06:13:12 pm »
Thank you so much guys! Especially you Hero999 :D

Attached 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?
 

Offline Zero999

  • Super Contributor
  • ***
  • Posts: 19994
  • Country: gb
  • 0999
Re: Square wave from sine, follow envelope?
« Reply #16 on: August 20, 2017, 06:35:24 pm »
Looks good apart from the last part. What does D2, C5 and R11 do? It looks like a bodge.
« Last Edit: August 20, 2017, 07:57:47 pm by Hero999 »
 

Offline alexanderbrevigTopic starter

  • Frequent Contributor
  • **
  • Posts: 700
  • Country: no
  • Musician, developer and EE hobbyist
    • alexanderbrevig.com
Re: Square wave from sine, follow envelope?
« Reply #17 on: August 20, 2017, 06:47:06 pm »
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".
 

Offline Zero999

  • Super Contributor
  • ***
  • Posts: 19994
  • Country: gb
  • 0999
Re: Square wave from sine, follow envelope?
« Reply #18 on: August 20, 2017, 08:34:18 pm »
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".
But 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.

 

Offline alexanderbrevigTopic starter

  • Frequent Contributor
  • **
  • Posts: 700
  • Country: no
  • Musician, developer and EE hobbyist
    • alexanderbrevig.com
Re: Square wave from sine, follow envelope?
« Reply #19 on: August 20, 2017, 08:52:11 pm »
But 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?
I 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.

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.
I 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 it :) Thanks again for your help and input Hero999!
 

Offline Zero999

  • Super Contributor
  • ***
  • Posts: 19994
  • Country: gb
  • 0999
Re: Square wave from sine, follow envelope?
« Reply #20 on: August 20, 2017, 09:04:25 pm »
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.
 

Offline alexanderbrevigTopic starter

  • Frequent Contributor
  • **
  • Posts: 700
  • Country: no
  • Musician, developer and EE hobbyist
    • alexanderbrevig.com
Re: Square wave from sine, follow envelope?
« Reply #21 on: August 20, 2017, 09:09:54 pm »
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... ;)
 

Offline StillTrying

  • Super Contributor
  • ***
  • Posts: 2850
  • Country: se
  • Country: Broken Britain
Re: Square wave from sine, follow envelope?
« Reply #22 on: August 20, 2017, 09:18:04 pm »
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.
.  That took much longer than I thought it would.
 
The following users thanked this post: Zero999, alexanderbrevig

Offline Zero999

  • Super Contributor
  • ***
  • Posts: 19994
  • Country: gb
  • 0999
Re: Square wave from sine, follow envelope?
« Reply #23 on: August 20, 2017, 11:36:35 pm »
I think it would be easier if you just used single supply op-amps.
 

Offline alexanderbrevigTopic starter

  • Frequent Contributor
  • **
  • Posts: 700
  • Country: no
  • Musician, developer and EE hobbyist
    • alexanderbrevig.com
Re: Square wave from sine, follow envelope?
« Reply #24 on: August 21, 2017, 02:14:58 am »
Hero999, that is absolutely perfect!
Much easier and still exactly what I want :) Thanks again!
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf