Searching \ for '[EE]: Square Wave Pulse Converted to Sine Wave Pul' in subject line. ()
Help us get a faster server
FAQ page: massmind.org/techref/method/math.htm?key=sine
Search entire site for: 'Square Wave Pulse Converted to Sine Wave Pul'.

Exact match. Not showing close matches.
'[EE]: Square Wave Pulse Converted to Sine Wave Pul'
2001\05\17@164831 by

Hi PICList,

I need to generate a sine wave signal for input to an existing electronic
circuit.  The signal will vary in frequency as a function of some other inputs
to a 16F877.  I'm thinking I could generate a 0V to 5V output pulse with the
PIC but need a circuit to convert this signal to a +/- sine wave. The
peak-to-peak voltage has to be a least 2.0V.

I've experimented with a cap in series with the PIC output, and a resistor to
ground.  This generates a nice positive and negative spike with clean zero
crossings but this is not good enough.  The device I am sending the signal to
is also looking at the slew rate of the signal.  I need a relatively genuine
sine wave.

Thanks in advance if you have an ideas.

Freddie.

=====
Best Regards,
"If it can't be done with a PIC, it can't be done :+)"

__________________________________________________
Do You Yahoo!?
Yahoo! Auctions - buy the things you want at great prices
http://auctions.yahoo.com/

--

>
>I've experimented with a cap in series with the PIC output, and a resistor to
>ground.  This generates a nice positive and negative spike with clean zero
>crossings but this is not good enough.  The device I am sending the signal to
>is also looking at the slew rate of the signal.  I need a relatively genuine
>sine wave.

That's to be expected, as you have built a differentiator, rather than an
integrator.

There really is no good way to do this.
Fourier tells you that a sine wave is an infinite series of even harmonics,
and a square wave is an infinite series of odd harmonics.  You're trying to
make a sine purse out of a square ear. :)

You need a filter, with fair Q, on the frequency of interest, or more bits,
with which to synthesize a sine wave using an R/2R ladder, or some similar
approach.

--
Dave's Engineering Page: http://www.dvanhorn.org

I would have a link to FINDU here in my signature line, but due to the
inability of sysadmins at TELOCITY to differentiate a signature line from
the text of an email, I am forbidden to have it.

--

On Thu, 17 May 2001, Freddie Leaf wrote:

> Hi PICList,
>
> I need to generate a sine wave signal for input to an existing electronic
> circuit.  The signal will vary in frequency as a function of some other inputs
> to a 16F877.  I'm thinking I could generate a 0V to 5V output pulse with the
> PIC but need a circuit to convert this signal to a +/- sine wave. The
> peak-to-peak voltage has to be a least 2.0V.
>
> I've experimented with a cap in series with the PIC output, and a resistor to
> ground.  This generates a nice positive and negative spike with clean zero
> crossings but this is not good enough.  The device I am sending the signal to
> is also looking at the slew rate of the signal.  I need a relatively genuine
> sine wave.
>
> Thanks in advance if you have an ideas.

Instead of filtering a single pulse, you'd be better off filtering a pulse
stream. All you need to do is generate a series of pulses whose widths are
proportional to the sine wave amplitude. Low-pass filter this stream, and you're
done. Since you're using the 'F877, you can use the on chip PWM peripheral to
generate the pulse for you. To obtain the sinusoidal proportional widths, you
can try this:

http://www.dattalo.com/technical/software/pic/picsine.html

Scott

--

Hi Dave,

I think this has come up before but I can't shut up :-) Why do you say that
a sine wave contains harmonics? My guess is that you are saying that in
practicality, when you try to create a sine wave, you can't totally avoid
harmonics and you usually end up with mostly even ones, which may be true.
Nonetheless, a pure sine wave has no harmonics because that is what Fourier
series are based upon, sine waves. I think it is especially misleading in
this case to state that a sine wave is an infinite series of harmonics,
too, because this will lead people who are trying to create sine waves to
think that they must ensure the presence of such harmonics, when in reality
it is best to reduce them as much as possible because as long as they are
present, you don't have a perfect sine wave (the total harmonic distortion
is greater than 0).

I agree, though, that what Freddie was trying to do is not practical and he
needs to make some kind of rough ADC (R2R ladder or PWM much faster than
than the sine wave freq followed by a simple low pass filter, as Scott just
suggested).

Sean

At 04:09 PM 5/17/01 -0500, you wrote:
There really is no good way to do this.
>Fourier tells you that a sine wave is an infinite series of even harmonics,
>and a square wave is an infinite series of odd harmonics.  You're trying to
>make a sine purse out of a square ear. :)

--

Freddie,

Have you tried using the cap and/or resistor circuit with a ramp
going into it instead of the pulse or square wave?
Without going to filtering, this may be the best bet.  I know that
in the past I've seen a circuit with a stack of diodes that simulate
a sine wave from a square wave by turning on at various amplitudes
to imitate the sine function.   But they're ineffecient at best, and
they don't produce a very good sine wave.

Also, you may want to calculate a lookup table of sine values for
the first quadrant, and then output them in sequence to get the sine
wave output.  L little passive filtering after that and you should
wind up with a relatively good sine wave.   The best part is that to
get a higher frequency waveform, just speed up the sequence.

Regards,

Jim

On Thu, 17 May 2001, Freddie Leaf wrote:

{Quote hidden}

jimjpes.com

--

At 05:32 PM 5/17/01 -0400, Sean H. Breheny wrote:
>Hi Dave,
>
>I think this has come up before but I can't shut up :-) Why do you say that
>a sine wave contains harmonics? My guess is that you are saying that in
>practicality, when you try to create a sine wave, you can't totally avoid
>harmonics and you usually end up with mostly even ones, which may be true.
>Nonetheless, a pure sine wave has no harmonics because that is what Fourier
>series are based upon, sine waves. I think it is especially misleading in
>this case to state that a sine wave is an infinite series of harmonics,
>too, because this will lead people who are trying to create sine waves to
>think that they must ensure the presence of such harmonics, when in reality
>it is best to reduce them as much as possible because as long as they are
>present, you don't have a perfect sine wave (the total harmonic distortion
>is greater than 0).

Sorry, brain short.  :)
Correct. Sine is F only.
Square is F plus 3F, 5F.... in decreasing infinitum

>I agree, though, that what Freddie was trying to do is not practical and he
>needs to make some kind of rough ADC (R2R ladder or PWM much faster than
>than the sine wave freq followed by a simple low pass filter, as Scott just
>suggested).

Scott suggested PWM, which works too. You keep the PWM frequencies way up
out of the desired frequency, and you can filter it reasonably easy.

--
Dave's Engineering Page: http://www.dvanhorn.org

I would have a link to FINDU here in my signature line, but due to the
inability of sysadmins at TELOCITY to differentiate a signature line from
the text of an email, I am forbidden to have it.

--

On Thu, 17 May 2001 16:39:19 -0500, David VanHorn wrote:

{Quote hidden}

Even harmonics make up a *triangle* wave.

f + 2f + 4f + 6f ...

Dan

--

>  Have you tried using the cap and/or resistor circuit with a ramp
>  going into it instead of the pulse or square wave?
>  Without going to filtering, this may be the best bet.  I know that
>  in the past I've seen a circuit with a stack of diodes that simulate
>  a sine wave from a square wave by turning on at various amplitudes
>  to imitate the sine function.   But they're ineffecient at best, and
>  they don't produce a very good sine wave.

This is a circuit I've used in various forms. The op amps can be
changed to low power types and the -12V to -5V. It does put out
a pretty good sine wave

http://home.clear.net.nz/pages/joecolquitt/tri2sine.html

--

>-12V to -5V

Sorry, +/-12 to +/- 5

--

Didn't Don Lancaster do something about PICs generating sine waves? I
seem to remember a bunch of articles about "Magic Sine Waves". I'm sure
you can find it on his website at http://www.tinaja.com . I'd be
interested if you do find something there, so keep us posted please!

Hope this helps,
Josh
joshymb.sympatico.ca

Freddie Leaf wrote:
{Quote hidden}

--

You can generate a sine wave using PWM and a technique called direct digital
synthesis.

What frequency do you need?

Bob Ammerman
RAm Systems
(contract development of high performance, high function, low-level
software)

{Original Message removed}

Hi Freddie,

I4ve been working in a wave generator since last thursday. I4ve posted a
message called "page boundary on wave generator". So, that was a generator
for any wave, whith that circuitry I was able to reproduce any signal such
as a sine wave.
For a precise signal values you could use a r-2r ladder or a DAC(chip
based). If you want I can send to you a schematic that I used. This is
compound by a DAC0808(National) and an AmpOp (741). The final signal
On the software you can create a table with your function (sine). As a
rule the sine wave is compound by 4 quadrants, so your table could be formed
by a quarter-wave only to improve your signal resolution and reduce your
table length.
Create a table with a first quadrant, and obtain the second one reading
your table on the reverse way. To the third and fourth quadrants, complement
your values (step= 128 - table_value).
To vary your frequency, just vary your sample rate! With a Faster sample
To smooth your wave, use a low pass filter to cut-off high order harmonics
(remember the fourier series?) tune your filter to cut the third harmonic of
interested.(Thanks Michael Brown).

Eduardo Lopes.

{Quote hidden}

_________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com.

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics

> Fourier tells you that a sine wave is an infinite series of even
harmonics,

This was befer your first coffee or do you use a very special sine wave?

Wouter

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics

I am interested to know how you get harmonic content in a pure sine wave ???

Is this a new section of physics (maybe plutonian physics?)

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics

2001\05\18@041949 by
the low pass filter is to integrate the small steps between samples. We
really can't create a perfect pure signal wave from a digital source,
doesn't matter how small the steps are!
_________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com.

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics

> I need to generate a sine wave signal for input to an existing electronic
> circuit.  The signal will vary in frequency as a function of some other
inputs
> to a 16F877.  I'm thinking I could generate a 0V to 5V output pulse with
the
> PIC but need a circuit to convert this signal to a +/- sine wave. The
> peak-to-peak voltage has to be a least 2.0V.
>
> I've experimented with a cap in series with the PIC output, and a resistor
to
> ground.  This generates a nice positive and negative spike with clean zero
> crossings but this is not good enough.  The device I am sending the signal
to
> is also looking at the slew rate of the signal.  I need a relatively
genuine
> sine wave.

You didn't say what the frequency range is.  If the frequency requirement is
low enough, you can low pass filter the PWM output to make a D/A.  I've done
this for voice audio frequencies.  For higher frequencies, use a parallel
D/A driven from an I/O port.

********************************************************************
Olin Lathrop, embedded systems consultant in Littleton Massachusetts
(978) 742-9014, olinembedinc.com, http://www.embedinc.com

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics

> Fourier tells you that a sine wave is an infinite
> series of even harmonics,

Huh!?  Sine waves are the basis functions in Fourier analisys.  A sine wave
IS a pure single frequency.

> and a square wave is an infinite series of odd harmonics.  You're trying
to
> make a sine purse out of a square ear. :)
>
> You need a filter, with fair Q, on the frequency of interest,

You can make a filter with a sharp enough rollof to reduce the harmonics of
a square wave below the level you care about.  However, the amplitude of the
resulting sine wave will be very dependent on frequency.  This will probably
be an issue since he has to vary the frequency on the fly.

********************************************************************
Olin Lathrop, embedded systems consultant in Littleton Massachusetts
(978) 742-9014, olinembedinc.com, http://www.embedinc.com

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics

Dear Scott Dattalo,

Josh Koffman <joshyMB.SYMPATICO.CA> on 2001-05-17 07:30:50 PM recalled:
> Didn't Don Lancaster do something about PICs generating sine waves?

Scott Dattalo <scottDATTALO.COM> on 2001-05-17 04:25:59 PM mentioned:
> Subject:     Re: [EE]: Square Wave Pulse Converted to Sine Wave Pulse
> http://www.dattalo.com/technical/software/pic/picsine.html

Hey, very cool. Thanks for putting it online.

Yes, I agree that it's much easier to generate high-quality, variable-frequency,
sine waves using a DAC rather than try to filter a square wave.
The 8 bit audio DAC chips are falling below \$5. Or you could make a low-res DAC
with the ``R-2R resistor ladder'' (maybe 3 or 4 bits). Or if your desired
frequency band is low enough, a lowpass-filtered PWM can give 7 bits of accuracy
pretty easily.

Have you heard about "Magic Sinewaves" ? It's a way of generating sine waves
from a 1 bit digital output, very similar to PWM, except that PWM has to use
*many* more edges to generate equivalent-quality sinewaves. Lancaster claims
that using fewer edges also reduces power transistor "switching loss".

Don  Lancaster's Magic Sinewave Library
http://www.tinaja.com/magsn01.html

Is there some connection between PWM, "magic sinewaves", and sigma-delta
converters ?

The only flaw I see with "Magic Sinewaves" is that it seems to work only with a
fixed number of samples/cycle. If I'm trying to generate several sinewaves
(DTMF), it looks like I'm forced to store a separate table for each possible
frequency (DTMF uses 8 frequencies), and output them independently on 2
different pins. (With PWM, I can sum 2 analog values from a single table
(different offsets) and emit it from a single PWM output). Am I missing
something clever ?

--
David Cary

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics

On Fri, 18 May 2001, David Cary wrote:

> Have you heard about "Magic Sinewaves" ? It's a way of generating sine waves
> from a 1 bit digital output, very similar to PWM, except that PWM has to use
> *many* more edges to generate equivalent-quality sinewaves. Lancaster claims
> that using fewer edges also reduces power transistor "switching loss".
>
>   Don  Lancaster's Magic Sinewave Library
>   http://www.tinaja.com/magsn01.html

Yep, definitely heard about it. There ain't nothin' magic about it though.

> Is there some connection between PWM, "magic sinewaves", and sigma-delta
> converters ?

In fact, there is. Take a look at:

http://www.dattalo.com/technical/theory/sqwave.html

In particular, the section titled: "011 Harmonic Content of a Periodic Pulse
Train" discusses this very issue. Within this section are links to a couple of
Octave (the GNU version of MATLAB) programs that assist in the synthesis and
analysis of "magic sine waves".

>
> The only flaw I see with "Magic Sinewaves" is that it seems to work only with a
> fixed number of samples/cycle. If I'm trying to generate several sinewaves
> (DTMF), it looks like I'm forced to store a separate table for each possible
> frequency (DTMF uses 8 frequencies), and output them independently on 2
> different pins. (With PWM, I can sum 2 analog values from a single table
> (different offsets) and emit it from a single PWM output). Am I missing
> something clever ?

The difference between PWM synthesis of sine waves and harmonic cancellation
techniques (like magic sine waves), is that the latter attempts to maximize the
"carrier" frequency while the former attempts to minimize it. If you're
controlling "things" that can't switch fast (say like large inverters) then you
want to minimize the switching and would choose the magic sinewave approach. On
the other hand, if you have a hardware PWM and just want to create a variable
frequency tone, then you'd use the PWM technique.

Another difference is that the PWM synthesis is flexible. It's conducive to
dynamic algorithms (e.g. lookup tables). However, the pulse stream for a magic
sinewave has to be pre-calculated - or I should say that generating optimum
pulse streams is computationally expensive.

Scott

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics

On Fri, 18 May 2001, Scott Dattalo (incorrectly) wrote:

> The difference between PWM synthesis of sine waves and harmonic cancellation
> techniques (like magic sine waves), is that the latter attempts to maximize the
> "carrier" frequency while the former attempts to minimize it.

That's back asswards. The PWM synthesis wants to maximize the carrier frequency,
while the magic sine waves want to minimize it. E.g. The number of transitions
for one cycle of a magic sinewave is minimized...

Scott

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics

filter. A simple square->sine lowpass has a series R and a C to GND for
each stage. A 3 stage filter may be enough for tone signalling purposes
(for humans to hear), if the first stage has a 3dB point lower than your
lowest frequency. It will turn your output into a triangle wave which will
be further 'rounded' by the next two stages which are dimensioned as
lowpass filters with 3dB corners at the highest frequency you output.

These filters will shift the phase of your output wrt. the square wave
and the amplitude will depend on the frequency (and may become less than
2V).

Peter.

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics

Bloody fascinating Scott...

...killed an hour and a half on that one page and I still don't really have
it, although I've got a sort of an "almost there" sort of buzz...

...could be just brain cells burning out...

...now where did I put those aspirin...

If I got all that correctly, in practical terms, "Magic" sinewaves are
better when you are switching lots of power (like a motor driver or power
amp) and PWM is better when power efficiency is less important but you DO
need:
A) lower uP clock speed and code complexity or
B) higher output frequencies or
C) more even variations in amplitude such as when you need
D) summation of multiple frequencies to one output (digital mixing).

Is that right?

Has anyone made a motor driver that uses "Magic" sinewaves?

---
jamesnewtonpiclist.com 1-619-652-0593
PIC/PICList FAQ: http://www.piclist.com or .org

{Original Message removed}
David, magic sines are a form of sigma-delta conversion. By working on the
width of each output pulse for magic sine you can obtain room to insert
another magic sine into the gaps. You will probably want to do this at the
same time with oversampling to be able to remove the lower beat frequency
in the analog filter. By this time you will be generating something that
so closely resembles PWM that it is probably best to do it with PWM from
the start.

Peter

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.

On Fri, 18 May 2001, James Newton wrote:

> Bloody fascinating Scott...

Isn't it fun contorting square waves!

{Quote hidden}

Mike Keitz made one. Where's Mike?

There are some practical problems with Magic Sinewaves. On a pic at least, they
can only be implemented in software. This requires isochronous code.

There are (at least) two ways to implement them in a pic. One is to put the MS
into a ROM table, the other is to encode the MS directly in instruction delays.
The look up table requires ~10-15 cycles per edge, while the direct encoding
only requires 3 (or one if you're really twisted and demented - 1). I suppose a
co-routine solution would be appropriate too.

Another problem, or perhaps side effect, is that since the patterns are hard
coded, there is less amplitude variability/control. Also, it's more difficult to
control the generated frequency. I suppose you could add vriable dely in the
looup table, but the quantization effects would cause large frequency jumps.
(BTW, a better way to control the frequency is to PLL the RC oscillator on a PIC
- something else Mike has done).

Scott

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email listservmitvma.mit.edu with SET PICList DIGEST in the body

>Isn't it fun contorting square waves!

It's funnier to square sinusoidal waves :oD

>Mike Keitz made one. Where's Mike?

Tell me too :o)

>There are some practical problems with Magic Sinewaves. On a pic at least, they
>can only be implemented in software. This requires isochronous code.

And?

>(BTW, a better way to control the frequency is to PLL the RC oscillator on a PIC
>- something else Mike has done).

Nice idea...I'm working on it :oD

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email listservmitvma.mit.edu with SET PICList DIGEST in the body

On Sun, 20 May 2001, Alexandre Domingos F. Souza wrote:

> >(BTW, a better way to control the frequency is to PLL the RC oscillator on a PIC
> >- something else Mike has done).
>
>         Nice idea...I'm working on it :oD

What've you done so far?

Mike's approach was to vary the capacitance using a varactor diode. He
controlled the varactor's bias voltage using filtered PWM.

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email listservmitvma.mit.edu with SET PICList DIGEST in the body

Thanks to everyone who responded to my post.  I have so many different
approaches to consider now.  I'll update the list when I complete the project.
Thanks again for everyone's input!

--- Freddie Leaf <freddie_leafyahoo.com> wrote:
{Quote hidden}

=====
Best Regards,
"If it can't be done with a PIC, it can't be done :+)"

__________________________________________________
Do You Yahoo!?
Yahoo! Auctions - buy the things you want at great prices
http://auctions.yahoo.com/

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email listservmitvma.mit.edu with SET PICList DIGEST in the body