DSPRelated.com
Forums

convert sum of two sines to square wave?

Started by Funky February 16, 2004
Suppose two 16 bit words define the frequency of two sine waves from 0.1Hz
to 20 Khz +/-0.1Hz which are then summed, amplified by "infinity" and then
clipped, giving a pulsed output. In theory, I think this could be done using
a DSP to do everything, but what's the best strategy?

Method 1:
1. Store a Nyquist sampled sinewave in Eprom
2. upsample the lower frequency to that of the higher frequency and add
together
3. MMmm... this is getting tricky

OK. Is there an algorithm that gives us the times for which a sampled
waveform crosses zero and whether the gradient is positve or negative?
If so, then EURIKA!! since this defines when the pulse should go positive or
negative.

Method 2:
1. Use a mathematical expression for sin(2*pi*f*t) to calculate the sum at
intervals of 1/(10*f) where f is the larger of the two and then iterate
further to find the zero crossing point and gradient.

I suspect there is a closed form mathematical expression that defines the
zero crossing points and the derivatives. Again, probably slow in
calculating but the easiest to understand.

Funky




"Funky" <hello@hello.com> wrote in message
news:c0re09$ifl$1@newsg1.svr.pol.co.uk...
> Suppose two 16 bit words define the frequency of two sine waves from 0.1Hz > to 20 Khz +/-0.1Hz which are then summed, amplified by "infinity" and then > clipped, giving a pulsed output. In theory, I think this could be done
using
> a DSP to do everything, but what's the best strategy? > > Method 1: > 1. Store a Nyquist sampled sinewave in Eprom > 2. upsample the lower frequency to that of the higher frequency and add > together > 3. MMmm... this is getting tricky > > OK. Is there an algorithm that gives us the times for which a sampled > waveform crosses zero and whether the gradient is positve or negative? > If so, then EURIKA!! since this defines when the pulse should go positive
or
> negative. > > Method 2: > 1. Use a mathematical expression for sin(2*pi*f*t) to calculate the sum at > intervals of 1/(10*f) where f is the larger of the two and then iterate > further to find the zero crossing point and gradient. >
> I suspect there is a closed form mathematical expression that defines the > zero crossing points and the derivatives. Again, probably slow in > calculating but the easiest to understand.
Sin (2*pi*(f1+f2)*t) + Sin (2*pi*(f1-f2)*t) = 2*Sin (2*pi*(f1)*t)Cos (2*pi*(f2)*t) So the Zeros of the RHS are easily calculated, as is the derivative which is just Cos (2*pi*(f1+f2)*t) + Cos (2*pi*(f1-f2)*t) evaluated at these points. Clever, eh?;)
> Funky > > > >
Funky wrote:

> Suppose two 16 bit words define the frequency of two sine waves from 0.1Hz > to 20 Khz +/-0.1Hz which are then summed, amplified by "infinity" and then > clipped, giving a pulsed output. In theory, I think this could be done using > a DSP to do everything, but what's the best strategy? > > Method 1: > 1. Store a Nyquist sampled sinewave in Eprom > 2. upsample the lower frequency to that of the higher frequency and add > together > 3. MMmm... this is getting tricky > > OK. Is there an algorithm that gives us the times for which a sampled > waveform crosses zero and whether the gradient is positve or negative? > If so, then EURIKA!! since this defines when the pulse should go positive or > negative. > > Method 2: > 1. Use a mathematical expression for sin(2*pi*f*t) to calculate the sum at > intervals of 1/(10*f) where f is the larger of the two and then iterate > further to find the zero crossing point and gradient. > > I suspect there is a closed form mathematical expression that defines the > zero crossing points and the derivatives. Again, probably slow in > calculating but the easiest to understand. > > Funky
It seems to me that the straightforward way is to fun two standard sinusoidal oscillators, sum them, and then set the output to 1 or -1 according to whether the sign of the result is positive or negative. Is there a reason not to do that? Jerry -- Engineering is the art of making what you want from things you can get. &#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;
Funky wrote:

   ...

> Sin (2*pi*(f1+f2)*t) + Sin (2*pi*(f1-f2)*t) = 2*Sin (2*pi*(f1)*t)Cos > (2*pi*(f2)*t) > > So the Zeros of the RHS are easily calculated, as is the derivative which is > just Cos (2*pi*(f1+f2)*t) + Cos (2*pi*(f1-f2)*t) evaluated at these points. > > Clever, eh?;)
What's the gain? You still have the sum of two sinusoidal oscillators. Your expression becomes more complex if you want to give the two basic sines different amplitudes. Jerry -- Engineering is the art of making what you want from things you can get. &#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;
Maybe I don't understand the problem, by why use trig functions (expensive)
at all if you just want to generate square waves?  That's an easy
integer-only operation and computationally cheap.  Am I missing something?

Brian

"Jerry Avins" <jya@ieee.org> wrote in message
news:40314fc1$0$3090$61fed72c@news.rcn.com...
> Funky wrote: > > > Suppose two 16 bit words define the frequency of two sine waves from
0.1Hz
> > to 20 Khz +/-0.1Hz which are then summed, amplified by "infinity" and
then
> > clipped, giving a pulsed output. In theory, I think this could be done
using
> > a DSP to do everything, but what's the best strategy? > > > > Method 1: > > 1. Store a Nyquist sampled sinewave in Eprom > > 2. upsample the lower frequency to that of the higher frequency and add > > together > > 3. MMmm... this is getting tricky > > > > OK. Is there an algorithm that gives us the times for which a sampled > > waveform crosses zero and whether the gradient is positve or negative? > > If so, then EURIKA!! since this defines when the pulse should go
positive or
> > negative. > > > > Method 2: > > 1. Use a mathematical expression for sin(2*pi*f*t) to calculate the sum
at
> > intervals of 1/(10*f) where f is the larger of the two and then iterate > > further to find the zero crossing point and gradient. > > > > I suspect there is a closed form mathematical expression that defines
the
> > zero crossing points and the derivatives. Again, probably slow in > > calculating but the easiest to understand. > > > > Funky > > It seems to me that the straightforward way is to fun two standard > sinusoidal oscillators, sum them, and then set the output to 1 or -1 > according to whether the sign of the result is positive or negative. > Is there a reason not to do that? > > Jerry > -- > Engineering is the art of making what you want from things you can get. > &#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295; >
Brian Reinhold wrote:

> Maybe I don't understand the problem, by why use trig functions (expensive) > at all if you just want to generate square waves? That's an easy > integer-only operation and computationally cheap. Am I missing something? >
Yes, I think it's trivial. You only need to know the periods (and initial phases) of the two sine waves and then you have all the information you need. You need to take care of the fractional component of the period with sufficient resolution, but that's not /too/ hard. No need for any trig. Paul
Paul Russell wrote:

> Brian Reinhold wrote: > >> Maybe I don't understand the problem, by why use trig functions >> (expensive) >> at all if you just want to generate square waves? That's an easy >> integer-only operation and computationally cheap. Am I missing >> something? >> > > Yes, I think it's trivial. You only need to know the periods (and > initial phases) of the two sine waves and then you have all the > information you need. You need to take care of the fractional component > of the period with sufficient resolution, but that's not /too/ hard. No > need for any trig. > > Paul
The assumption again seems to be that the amplitudes of the two sine waves are equal. Is that confirmed? Jerry -- Engineering is the art of making what you want from things you can get. &#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;
"Jerry Avins" <jya@ieee.org> wrote in message
news:40326df7$0$3094$61fed72c@news.rcn.com...
> Paul Russell wrote: > > > Brian Reinhold wrote: > > > >> Maybe I don't understand the problem, by why use trig functions > >> (expensive) > >> at all if you just want to generate square waves? That's an easy > >> integer-only operation and computationally cheap. Am I missing > >> something? > >> > > > > Yes, I think it's trivial. You only need to know the periods (and > > initial phases) of the two sine waves and then you have all the > > information you need. You need to take care of the fractional component > > of the period with sufficient resolution, but that's not /too/ hard. No > > need for any trig. > > > > Paul > > The assumption again seems to be that the amplitudes of the two sine > waves are equal. Is that confirmed?
Yes, the amplitudes are equal.
> Jerry > -- > Engineering is the art of making what you want from things you can get. > &#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295; >
Funky wrote:

> > Yes. I still think you need to exploit the trig identity that sum of sines > equals product of sin and cos, unless you're thinking of another way.
I don't think so. Given the intial phases and the periods all you need is a simple state machine. (As others have noted, it gets more complicated if the amplitudes of the two sine waves are different, but apparently that is not the case in this instance.) Paul
"Paul Russell" <prussell@sonic.net> wrote in message
news:PztYb.1835$_3.30431@typhoon.sonic.net...
> Brian Reinhold wrote: > > > Maybe I don't understand the problem, by why use trig functions
(expensive)
> > at all if you just want to generate square waves? That's an easy > > integer-only operation and computationally cheap. Am I missing
something?
> > > > Yes, I think it's trivial. You only need to know the periods (and > initial phases) of the two sine waves and then you have all the > information you need. You need to take care of the fractional component > of the period with sufficient resolution, but that's not /too/ hard. No > need for any trig.
Yes. I still think you need to exploit the trig identity that sum of sines equals product of sin and cos, unless you're thinking of another way. Funky
> Paul > >