square to sine wave converter

Started by 11-90-an, July 01, 2020, 12:41:21 PM

Previous topic - Next topic

11-90-an

https://www.engineersgarage.com/contributions/waveform-converter-circuits/

can any of you folks with oscilloscopes verify the content of this? If there is a way that this may be tailored to guitar signal frequencies (which are more inconsistent and non repetitive), please share...  ;D
flip flop flip flop flip

Kevin Mitchell

#1
The following is the opposite of what you're asking... Please forgive me  :'(
Refer to a basic comparator circuit and assume we're using a rail to rail opamp like a LM368.

Let's put a reference voltage on the non-inverted input (let's say half of the supply voltage, 4.5v). No feedback resistors for this demo. Could be done with a simple resistor divider.

If we apply a sine wave to the inverted input, when the voltage is lower than the reference at the + input the output will be high. Once the sine is higher than the + input voltage the output then goes low.

I threw this together for ya.
Circuit Simulator


And finally after all of that I'm realizing you want the opposite  :'(
  • SUPPORTER
This hobby will be the deaf of me


anotherjim

I'm afraid that square to sine is a problem you will hit a brick wall with. Filtering out all of the harmonics to leave a pure sine only works properly near a specific frequency. Below that frequency, harmonics creep back in and above it, the remaining fundamental gets quieter. This was a huge problem for the old transistor organs which is why the Hammond system which produces purer tones in the first place was so popular for so long.


ElectricDruid

Jim is right. Even that Square-to-triangle circuit produces a decreasing amplitude as the frequency climbs, because the slope stays the same. You can adjust the slope to suit the input frequency, of course, but then you need to know what the input frequency is. It works ok if you do know - the Roland Juno106 uses a circuit like this to generate ramp waves from squares.

If you *did* have a triangle wave of a fixed amplitude, you can waveshape it into a sine by using a tanh-curve function, like the input pair of an OTA, or a differential pair of transistors. This page is probably the definitive round-up of the various ways it can be done:

http://www.timstinchcombe.co.uk/index.php?pge=trisin

Most of these methods are very sensitive to the input amplitude. This is fine if you're building a VCO and you've got a fixed amplitude output, since you can trim the circuit for the best sine quality. As an effects circuit, it's basically a soft-drive distortion.

antonis

A 45 years old square to sine converter..
(110 op-amp projects  Ray M. Marston, page 77)

"I'm getting older while being taught all the time" Solon the Athenian..
"I don't mind  being taught all the time but I do mind a lot getting old" Antonis the Thessalonian..

Eb7+9

Quote from: antonis on July 01, 2020, 06:30:26 PM

A 45 years old square to sine converter..


I wonder what ramp input means ... I thought for a minute somebody had broken the laws of physics 45 years ago lol

PRR

  • SUPPORTER

R.G.

This is something I've chased for a few decades now.

As noted, most sine SHAPERS are very, very amplitude sensitive, and just don't work well at all without the single magic amplitude. This includes especially triangle to sine shapers.
As noted, square to triangle converters have decreasing amplitude with square wave frequency. This makes converting to triangle then to sine especially difficult.
Simple filters just don't work well except at the filter's special magic frequency.
Complex filters can be made that use PLLs to multiply a square wave frequency enough to make a tracking filter that will follow the input frequency to make the filter "magic" frequency track the input frequency. Note the word "complex".
The best way I've found is to use a phase locked loop ("PLL") to multiply an incoming square wave by 8, 16 or some such amount then to use binary dividers to generate a sine wave at the same frequency as the incoming square wave. This works. You get a sine wave to any degree of accuracy you care to make the PLL and dividers and summer network work at. 1% or less distortion is not difficult at all. You get a fixed amplitude sine wave at the frequency of the incoming square wave. It's a generated sine, no relation other than frequency to the original signal.  But you do get a sine.

This takes a CD4046 (or one of the followons), a CD4024 (or other similar) binary divider, and a set of carefully chosen precision resistors to add up the binary divider outputs. It's not terribly demanding, and it's the only reliable way I've found to generate a reasonably good sine at the frequency of a varying frequency analog signal that's been squared up.
R.G.

In response to the questions in the forum - PCB Layout for Musical Effects is available from The Book Patch. Search "PCB Layout" and it ought to appear.

jonny.reckless

#9
Generating a good quality sine wave from a square or a triangle is always going to be tricky if you want variable frequency or amplitude for that matter. I find it's easier to generate a sine wave first, using a sinusoidal oscillator such as a quadrature oscillator. Once you have a nice sine wave, it's relatively easy to generate square, triangle and sawtooth from it; certainly much easier than going the other way which always results in audible artifacts in my experience.
Here's a basic LM13700 based sinusoidal LFO design which works from about 0.1Hz to 20Hz, you can adjust it to suit. It's not correctly temperature compensated so I wouldn't use it for an audio oscillator without additional compensation.


R.G.

Before direct digital synthesis, the way to make a multiwaveform generator was to use a precision and symmetrical triangle wave generator with a ramp up/down integrator, and switch the direction at the peak and valley points. The simplest way to do that was with a Schmitt trigger to flip the ramp direction. This is used in most simple LFO sweep generators in effects, and gets you the square at the same time.
With a fixed triangle amplitude, you have the ingredients for a decent sine wave by shaping the triangle. That's what's inside the older three-waveform generator chips like the 8038 and its ilk. It's what was in some pretty fancy low distortion lab stuff from the 70s and 80s as well, all cleaned up and polished for the dance.
Generating a low distortion sine from scratch is tough enough that the AGC technique to do it with a Wien bridge is what started Hewlett Packard. Generating modest distortion sines is not too tough. Generating a square wave from this is really simple - just a comparator.
This is a path-sensitive process though. If you have to start with a random signal and square it up to get the square wave, you can't generate the sine you wanted first, then convert it. I was forever chasing taking a guitar signal, and making clean(ish) sines from it for further processing.
Yeah, if you get to choose your starting point, picka sine and make the square follow it.
DDS makes this all academic. Numerically controlled oscillators can be made with arbitrarily low sine distortion if you want to take the trouble to gen up the hardware, notably to get long enough accumulators and big enough waveform tables. It's even not too bad on simple old 8-bit PICs. Some PICs have includes an NCO hardware set to make this even easier. I did my first NCO by register banging inside a PIC without special NCO periperals. Made my head hurt.
And of course, if you have an NCO, you can make the waveform be whatever you want, not limited to sine, triangle, ramp, pulse, PWM, square, and so on. Just put it into the tables.

But still, going from square to sine is going to take some work.
R.G.

In response to the questions in the forum - PCB Layout for Musical Effects is available from The Book Patch. Search "PCB Layout" and it ought to appear.

11-90-an

Ok... thanks for all th input guys...

Just wondering, you guys were saying that the square to sine wave converters depend highly on amplitude and frequency... but isn't it that the output of a flip flop consists of 1's and 0's... the 0's are gnd and the 1's are 9v...(or the voltage given) so wouldn't the amplitude be consistent but only the frequency changing? And would it make sense to convert the square wave to triangle then triangle to sine? I'm honestly wondering if what i'm saying makes sense...  :(
flip flop flip flop flip

anotherjim

Yes, the square output amplitude is consistent, but that's the trouble - the circuit to convert it needs to know the frequency in order to create the required slope/shape of a different wave of the same frequency. If the new ramp is too fast, it "clips", if it's too slow, it doesn't reach the right amplitude in time. Finding the frequency isn't too hard if your square wave is stable (frequency to voltage converter), but it's almost impossible to get that good squarewave from complex audio tones (if that's what you're thinking).

A digital system can record the input into a short "buffer" memory, find the frequency (averaging several cycles to find the fundamental pitch - like a tuner does) and output a new wave to match. The Roland synth mentioned uses analogue waveshaping, but it knows the frequency of the note so uses a predetermined digital value into a DAC to adjust the slope timing of the ramp wave to match.



Rob Strand

#13
QuoteJust wondering, you guys were saying that the square to sine wave converters depend highly on amplitude and frequency... but isn't it that the output of a flip flop consists of 1's and 0's... the 0's are gnd and the 1's are 9v...(or the voltage given) so wouldn't the amplitude be consistent but only the frequency changing? And would it make sense to convert the square wave to triangle then triangle to sine? I'm honestly wondering if what i'm saying makes sense..
So the problem with square to triangle is the amplitude becomes frequency dependent.

With the PLL + tracking filter, the PLL tracks and multiplies-up the input frequency.   The output of the PLL is say square.   However you don't use that square signal as audio.   It is the *clock* for a tracking filter, usually a switched capacitor filter.   You feed the original square signal into the filter and the both the frequency and the amplitude are preserved at the output of the filter.

With the digital sine-wave case,  the PLL part is similar to the previous case.   The clock feeds a digital sine generator.   However in this case you need an envelope detector which extracts the level of the input signal; essentially a DC level representing the input amplitude.   The digital sine generator level is modulated by they DC level.   In the simplest case you can use analog switches connected to the DC amplitude level, then to the digital sine-wave weighting resistors where they are mixed together.
Send:     . .- .-. - .... / - --- / --. --- .-. -
According to the water analogy of electricity, transistor leakage is caused by holes.

anotherjim

In a simpler version, use the PLL VCO control voltage for an analogue tracking filter. IIRC, the EDP Gnat synth uses a tracking 4046 to impart portamento on the basic DCO square wave pitch. It then extracts the VCO CV as a key follow CV for the filter. Any 4046 based tracking circuit absolutely relies of a good, simple input, which a synth it gets.


ElectricDruid

Quote from: anotherjim on July 02, 2020, 05:07:21 AM
In a simpler version, use the PLL VCO control voltage for an analogue tracking filter. IIRC, the EDP Gnat synth uses a tracking 4046 to impart portamento on the basic DCO square wave pitch. It then extracts the VCO CV as a key follow CV for the filter. Any 4046 based tracking circuit absolutely relies of a good, simple input, which in a synth it gets.

These PLL solutions are certainly clever, but ultimately they swap a frequency/amplitude problem for a tracking problem. As we know, making a PLL track a guitar input is not easy, and can only ever be done for single notes (at least with analog circuits, or without a hexaphonic pick-up).

I do think it's funny that getting from a square to a sine should be so hard, when getting from a sine to a square is so easy.

R.G.

Speculating even further down what might have been related to the OP's original question -

If you're trying to make a sine wave tracker for guitar inputs, you can take advantage of the spectrum of the input waveform. A guitar string contains a fundamental frequency F1, and varying amounts of F2 (second harmonic), F3, F4... and some mix of non related frequencies that give it a metallic ringing quality. You can conceptually construct a filter bank with 1/3 octave or narrower filters that would isolate the fundamental by gating the filter bank output by detecting output per filter and using logic chips to select the lowest active filter output as the fundamental. A guitar has a range of four octaves of fundamental, so you can cover the range with 12 1/3 octave filters.
Conceptually.
Just that filter bank and logic does a reasonably good job of filtering single strings to a sine(ish) output. From there you can go square easily, and add a tracking PLL. The PLL can be made to generate 2x, 3x, 4x and so on higher overtones and to glade to the next tone it detects, as well as being gated by the select-a-frequency logic of the filter bank

On another aside, if you're doing PLL tracking, you can be sly about generating sines and use one of the waveform generator chips like the 8038 and ... um, 2206, was it? ... as the VCO in the PLL to have the chip make the sine as a side effect of its tracking. Most of a PLL's performance is dependent on the phase detector and filter, not on the VCO so much.
R.G.

In response to the questions in the forum - PCB Layout for Musical Effects is available from The Book Patch. Search "PCB Layout" and it ought to appear.

anotherjim


mozz

You could probably make a square to sine convertor for guitar that would clean up 1 frequency yet leave the harmonics there. How wide a bandwidth for that 1 freq i don't know.
  • SUPPORTER

ElectricDruid

Quote from: anotherjim on July 23, 2020, 04:46:39 PM
Following a link elsewhere led to this...
https://worldradiohistory.com/UK/Elektor/80s/Elektor-301Circuits-79-179.pdf
Circuit 101 on p19 of the pdf.

Yep, basic integrator. Same problem - amplitude goes down as frequency goes up.