Author Topic: Square wave to Sine wave but in DC range?  (Read 4259 times)

0 Members and 1 Guest are viewing this topic.

Offline ChrissTopic starter

  • Frequent Contributor
  • **
  • Posts: 534
  • Country: 00
Square wave to Sine wave but in DC range?
« on: March 07, 2017, 08:13:22 am »
Hi!
I'm searching for a solution how to convert the square wave which is coming from an Atmel uC to a sine wave, actually the sine wave should stay in the DC range. I don't need an AC sine wave.

The problem for me is the frequency of the square wave could be changed, depend on the setup by user, so the frequency of the sine wave should also flow the square wave frequency.

The frequency range should be from 10Hz maybe up to 10KHz.

Any idea?

Tanks.
My best regards.
 

Offline JPortici

  • Super Contributor
  • ***
  • Posts: 3525
  • Country: it
Re: Square wave to Sine wave but in DC range?
« Reply #1 on: March 07, 2017, 08:25:41 am »
anything that is over 0 Hz is technically AC.
(tecnically not even that as DC may also be thought as a pulse at an extreeeemely slow frequency ::D )

anyway. what are you trying to accomplish, exactly?
sounds like a waveform generator.. yours is not the usual way to do it if you wand different waves!
DCO perhaps?

anyway, you are correct. To do it like you are thinking you need a filter that tracks the output, a voltage controlled filter. There are plenty of schematics! its control input could be the output of a PWM module of your uC
the duty cycle of the PWM is the cutoff frequency. But it needs to be either of an high order or cut off at some octaves below the square wave to filter out as much harmonics as possible...

why don't you do it the other way around then? generate the sine wave in the analog domain with a VCF auto oscillating, square it with a comparator. control the cutoff with the MCU and measure the square frequency to keep the sinewave "in tune"

or... simplier... DDS, you know?
 

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 13119
Re: Square wave to Sine wave but in DC range?
« Reply #2 on: March 07, 2017, 08:39:15 am »
That would be extremely difficult to do with analog circuits.   A F=>V converter driving a low distortion  VCO with a PLL keeping it locked would be one approach but I don't think you'd get a three decade frequency range without switching timing capacitors.

The best approach would be if there is sourcecode  available to allow the firmware to be modified to control a DDS chip to generate both the sine and square waves, with filtering for the sine wave.

Otherwise, realistically, it either needs a fast DSP capable MCU that can run a DDS routine for the sinewave, and a software PLL to keep it locked to the squarewave, or if you've got FPGA development experience it could be implemented with a digital PLL and hardware DDS. 

Its not for the faint-hearted - if you haven't done some serious DSP projects before, you will struggle.
 

Offline JPortici

  • Super Contributor
  • ***
  • Posts: 3525
  • Country: it
Re: Square wave to Sine wave but in DC range?
« Reply #3 on: March 07, 2017, 08:53:52 am »
I'd wait and hear what the end application first.. you can do reasonably good DDS for sinewave, at audio frequencies, on almost any modern 8 bit mcu
 

Online BrianHG

  • Super Contributor
  • ***
  • Posts: 8111
  • Country: ca
Re: Square wave to Sine wave but in DC range?
« Reply #4 on: March 07, 2017, 08:58:51 am »
For an analog sine wave VCO with the range you want, take a look at some of the old function generator ICs like:
ICL8038
MAX038
XR-2206

Your other choice might be a voltage output DAC and feed it a sine waveform, or an MCU with internal software to do the same.
 

Offline Siwastaja

  • Super Contributor
  • ***
  • Posts: 8789
  • Country: fi
Re: Square wave to Sine wave but in DC range?
« Reply #5 on: March 07, 2017, 09:38:48 am »
If the noise/distortion requirements are not special, many modern $1 MCUs capable of producing fast PWM (i.e., internal timer running at around 100 MHz, or so) would be able to simply PWM the 20kHz sinewave from a lookup table, using a simple RC filter.

Many modern sub-$1 MCUs also include fast DACs, such as  even the chepest in STM32 series, offering 1 MSPS 12-bit DAC.
 

Offline Codebird

  • Regular Contributor
  • *
  • Posts: 161
  • Country: gb
Re: Square wave to Sine wave but in DC range?
« Reply #6 on: March 07, 2017, 09:55:30 am »
How siney-do you need? Square to sine is rather tricky, but square-to-triangle is comparatively easy.
 

Online Fungus

  • Super Contributor
  • ***
  • Posts: 17203
  • Country: 00
Re: Square wave to Sine wave but in DC range?
« Reply #7 on: March 07, 2017, 10:12:34 am »
Hi!
I'm searching for a solution how to convert the square wave which is coming from an Atmel uC to a sine wave, actually the sine wave should stay in the DC range. I don't need an AC sine wave.

The problem for me is the frequency of the square wave could be changed, depend on the setup by user, so the frequency of the sine wave should also flow the square wave frequency.

The frequency range should be from 10Hz maybe up to 10KHz.

Any idea?

There's lots of tricks but it depends on what it will be used for.

If you want a really nice sine wave for audio work you can get a signal generator, eg.: http://www.ebay.com/itm/331866160373
 

Offline w2aew

  • Super Contributor
  • ***
  • Posts: 1780
  • Country: us
  • I usTa cuDnt speL enjinere, noW I aR wuN
    • My YouTube Channel
Re: Square wave to Sine wave but in DC range?
« Reply #8 on: March 07, 2017, 04:14:22 pm »
A common technique used in old analog function generators was a two step process.  Convert the square wave into a triangle/sawtooth waveform using an integrator (easy with an op amp), and follow that with a PieceWise Linear wave shaping circuit, often using a multiple diode shaping circuit, where diodes are used to progressively shape the triangle into a near sine wave.  Here are some sites to go check out:

http://www-personal.engin.umd.umich.edu/~fmeral/ELECTRONICS%20II/03%25c3-Diodes/02a-Diode%20Waveshape%20Illus.pdf
http://www.timstinchcombe.co.uk/index.php?pge=trisin
http://www.ti.com/lit/an/snoa665c/snoa665c.pdf
http://www.electronics.dit.ie/staff/ypanarin/Lecture%20Notes/DT021-4/5SignalShaping.pdf

YouTube channel: https://www.youtube.com/w2aew
FAE for Tektronix
Technical Coordinator for the ARRL Northern NJ Section
 
The following users thanked this post: orolo

Offline Zero999

  • Super Contributor
  • ***
  • Posts: 19962
  • Country: gb
  • 0999
Re: Square wave to Sine wave but in DC range?
« Reply #9 on: March 07, 2017, 07:21:51 pm »
Hi!
I'm searching for a solution how to convert the square wave which is coming from an Atmel uC to a sine wave, actually the sine wave should stay in the DC range. I don't need an AC sine wave.
What does that mean? Do you mean you want the sine wave biased at half the supply voltage, never going below zero?

As you're using a micro controller anyway, why not simply generate a sine using PWM or some sort of DAC?

If that's not possible, say you can't change the code on the existing MCU, then add another one to work out the frequency of the square wave and output the appropriate sinewave via PWM or a DAC.
 

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 13119
Re: Square wave to Sine wave but in DC range?
« Reply #10 on: March 07, 2017, 07:56:23 pm »
A common technique used in old analog function generators was a two step process.  Convert the square wave into a triangle/sawtooth waveform using an integrator (easy with an op amp), and follow that with a PieceWise Linear wave shaping circuit, often using a multiple diode shaping circuit, where diodes are used to progressively shape the triangle into a near sine wave.

The quality of a piecewise linear sine shaper output is critically dependent on the input amplitude.

Unfortunately, the effective gain of the square to triangle integrator is inversely proportional to frequency. The traditional way to handle that was to gang the adjustment of the integrator components with that of the squarewave oscillator, so the integrator gain tracked with the oscillator frequency, but that's no good for an external signal. 

One is left with the problem of designing an AGC circuit that can handle a 1000:1 range of input levels while maintaining near constant output.  Even if you do forward correction with a F to V circuit and an analog multiplier that's still tricky, and you'll probably need a PID servo loop to trim the gain. :scared:  |O

Some improvement can be gained at the expense of considerable complexity by switching integrator capacitors by frequency decade (or even simply hi/lo), with some hysterisis so it doesn't jitter.

Any deviation  from an exact 50% duty cycle will cause the integrator output to 'walk' until it rails and distorts, so you'll also need another PID servo loop to inject a centering bias. 

Both PID loops will need tuning to handle the maximum possible rate of change of input frequency.

That's an Eurocard protoboard or two of OPAMPs and precision passives, with a considerable price tag.  Its not surprising we are pushing the O.P towards digital methods.
« Last Edit: March 07, 2017, 10:47:04 pm by Ian.M »
 

Offline sentry7

  • Regular Contributor
  • *
  • Posts: 138
  • Country: us
Re: Square wave to Sine wave but in DC range?
« Reply #11 on: March 07, 2017, 08:06:09 pm »
Any deviation  from an exact 50% duty cycle will cause the integrator output to 'walk' until it rails and distorts, so you'll also need another PID servo loop to inject a centering bias. 
If we're talking about an ordinary op amp integrator, this walk can be stopped with a large discharge resistor in the feedback loop. That doesn't stop the frequency dependent gain, but at least it doesn't clip.
 

Offline bson

  • Supporter
  • ****
  • Posts: 2457
  • Country: us
Re: Square wave to Sine wave but in DC range?
« Reply #12 on: March 07, 2017, 08:13:57 pm »
You could also find a simple DDS synthesizer and clock it with a multiple of your signal.
 

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 13119
Re: Square wave to Sine wave but in DC range?
« Reply #13 on: March 07, 2017, 08:17:07 pm »
Any deviation  from an exact 50% duty cycle will cause the integrator output to 'walk' until it rails and distorts, so you'll also need another PID servo loop to inject a centering bias. 
If we're talking about an ordinary op amp integrator, this walk can be stopped with a large discharge resistor in the feedback loop. That doesn't stop the frequency dependent gain, but at least it doesn't clip.
Yes, you can stop the walk that way, but it leaves a residual DC offset, which will distort the sine shaper output.
 

Offline Christe4nM

  • Supporter
  • ****
  • Posts: 252
  • Country: nl
Re: Square wave to Sine wave but in DC range?
« Reply #14 on: March 07, 2017, 10:38:23 pm »
Unless we know more about the exact application it's hard to suggest solutions that fit for the OP. To me the first post reads as "I need a sine wave generator, of which the output frequency is the same as that of an incoming square wave."

If so: you need a frequency detector for a square wave input. (Quite easily done in a microcontroller I think); you need a sine wave generator (for these low frequencies also very doable with a microcontroller either with PWM output or a DAC). And finally a way to get the output frequency matched to the input frequency. (Which again seems to me very doable in that same microcontroller)

Unless I'm overlooking something when I say it's pretty doable with a microcontroller?

However, this is all assuming I understand the OP correctly.

So please tell us a bit more about what you want to achieve. What kind of requirements do you have on the sine wave? (like amplitude, distortion etc.) Where does that square wave come from (a microcontroller yes, but what kind of circuit is it part of) do you have any control over that microcontroller?
Would it be possible to use the microcontroller generating the square wave to also generate the sine wave? If so, do you still need that square wave, or was it's only purpose as reference frequency for the sine? Etc...
 

Online BrianHG

  • Super Contributor
  • ***
  • Posts: 8111
  • Country: ca
Re: Square wave to Sine wave but in DC range?
« Reply #15 on: March 08, 2017, 01:35:10 am »
A common technique used in old analog function generators was a two step process.  Convert the square wave into a triangle/sawtooth waveform using an integrator (easy with an op amp), and follow that with a PieceWise Linear wave shaping circuit, often using a multiple diode shaping circuit, where diodes are used to progressively shape the triangle into a near sine wave.

The quality of a piecewise linear sine shaper output is critically dependent on the input amplitude.

Unfortunately, the effective gain of the square to triangle integrator is inversely proportional to frequency. The traditional way to handle that was to gang the adjustment of the integrator components with that of the squarewave oscillator, so the integrator gain tracked with the oscillator frequency, but that's no good for an external signal. 

One is left with the problem of designing an AGC circuit that can handle a 1000:1 range of input levels while maintaining near constant output.  Even if you do forward correction with a F to V circuit and an analog multiplier that's still tricky, and you'll probably need a PID servo loop to trim the gain. :scared:  |O

Some improvement can be gained at the expense of considerable complexity by switching integrator capacitors by frequency decade (or even simply hi/lo), with some hysterisis so it doesn't jitter.

Any deviation  from an exact 50% duty cycle will cause the integrator output to 'walk' until it rails and distorts, so you'll also need another PID servo loop to inject a centering bias. 

Both PID loops will need tuning to handle the maximum possible rate of change of input frequency.

That's an Eurocard protoboard or two of OPAMPs and precision passives, with a considerable price tag.  Its not surprising we are pushing the O.P towards digital methods.

Funny, the ICs I mentioned have all that built in...  The first 2 will also give the full desired frequency range in 1 sweep.

 

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 13119
Re: Square wave to Sine wave but in DC range?
« Reply #16 on: March 08, 2017, 02:08:51 am »
Funny, the ICs I mentioned have all that built in...  The first 2 will also give the full desired frequency range in 1 sweep.
For an analog sine wave VCO with the range you want, take a look at some of the old function generator ICs like:
ICL8038
MAX038
XR-2206

Unfortunately they are all obsolete so you would have find someone reputable who's still go a few N.O.S., and none of them can directly generate triangle and sine waves from an external squarewave.  The MAX038 is the closest as it has a phase detector and its VCO is designed to support phase locking it to an external signal, but you are still going to need to steer its base frequency close to the frequency of the waveform its to be locked to.  That drops the size and complexity maybe to half a Eurocard.:)
 

Online BrianHG

  • Super Contributor
  • ***
  • Posts: 8111
  • Country: ca
Re: Square wave to Sine wave but in DC range?
« Reply #17 on: March 08, 2017, 02:39:04 am »
I guess then I would use a cheap 64Mhz pic with dac.  Use the timer input to time the source square wave & playback from either a huge sine table, or computing the sine sending it to the dac output pin running at high Khz.  It will be something like an under 3$ solution, but, you will need to write some C code to get it running.
 

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 13119
Re: Square wave to Sine wave but in DC range?
« Reply #18 on: March 08, 2017, 03:05:57 am »
You dont really need a built-in DAC - just an 8 bit port and an external R/2R ladder into a low pass filter and buffer.

A 64MHz 8 bit PIC only gets you 16 MIPS,  which isn't a lot if you need to reproduce a 10KHz sine wave.  1600 instructions per cycle, and how many samples do you need to get an acceptable waveshape without having to design a 'brickwall' filter?  I would expect anythong under 32 is going to be crappy, which drops it to 50 instructions per sample - just possible if you code in assembler.  Bear in mind you have the period measurement and sample rate adjustment to keep it locked to the input waveform running  psuedo-concurrently.

A small dsPIC would be as better choice.  dsPIC33FJ06GS001looks like a good candidate. in 18 pin package, 40 MIPS from internal oscillator, with input capture and output compare on a 16 bit timer to sync to the squarewave and generate the sampling clock, and a 10 bit buffered DAC to generate the sine wave.
 

Offline David Hess

  • Super Contributor
  • ***
  • Posts: 17167
  • Country: us
  • DavidH
Re: Square wave to Sine wave but in DC range?
« Reply #19 on: March 08, 2017, 04:10:40 pm »
A common technique used in old analog function generators was a two step process.  Convert the square wave into a triangle/sawtooth waveform using an integrator (easy with an op amp), and follow that with a PieceWise Linear wave shaping circuit, often using a multiple diode shaping circuit, where diodes are used to progressively shape the triangle into a near sine wave.

The quality of a piecewise linear sine shaper output is critically dependent on the input amplitude.

Unfortunately, the effective gain of the square to triangle integrator is inversely proportional to frequency. The traditional way to handle that was to gang the adjustment of the integrator components with that of the squarewave oscillator, so the integrator gain tracked with the oscillator frequency, but that's no good for an external signal. 

One is left with the problem of designing an AGC circuit that can handle a 1000:1 range of input levels while maintaining near constant output.  Even if you do forward correction with a F to V circuit and an analog multiplier that's still tricky, and you'll probably need a PID servo loop to trim the gain.

It is not quite this bad because it is not done that way.  Instead the function generator is phase locked to the reference by adjusting the integrator's input current.  Phase locking over a 1:1000 range is completely feasible this way.  The output waveform after triangle to sine conversion is then only dependent on the performance of the function generator itself which handles the switching and amplitude compensation.  I still would not recommend this method though because there are better ways.  Note that at least in high performance designs, the integrator used is *not* the standard operational amplifier integrator which has bandwidth limitations; instead a diode bridge based current switch is used to drive an integration capacitor.  I wonder how far this design could be pushed with modern parts.

Software DDS is the obvious solution and up to 10kHz, I think this is feasible with careful programming.  I have done this before with older lower performance microcontroller but not to produce a sine wave output would would mean using a precomputed lookup table but that should not be a problem.

The alternative I would consider if a square wave output of 50 or 100 times the desired frequency is available is to drive an external switched capacitor filter which is clocked at 50 or 100 times the frequency of interest.  The microcontroller drives the switched capacitor filter at 50 or 100 times the desired frequency and an external divide-by-50 or divide-by-100 drives the switched capacitor input from its clock.  The antialiasing filter on the output will need to be switched to divide the frequency range up.
 

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 13119
Re: Square wave to Sine wave but in DC range?
« Reply #20 on: March 08, 2017, 07:52:17 pm »
The switched capacitor filter idea is worth looking at - its clock doesn't need to be an accurate 50x or 100x the squarewave, it just has to track it well enough to reliably filter the third harmonic.  F=>V circuit controlling a VCO would do it, maybe with some automatic hi/lo range switching to reduce that 1000:1 frequency range to 32:1.   Its not a great option compared to programming a suitable MCU, but for a one-off if you don't already have the toolchain for something suitable, it could make sense.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf