DSPRelated.com
Forums

convert sum of two sines to square wave?

Started by Funky February 16, 2004
Funky wrote:

  ...


> 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
There is another way. Find each of the sines. (This need not be a call to a sine routine. Since they describe a smooth sinewave, it takes about a dozen lines of assembly code for each sine.) Add them. If the sum is positive, output 1; if negative, 0 or -1 as you choose. You can use the same continuous sine generator for (a+b) and (a-b), but why bother? Jerry -- Engineering is the art of making what you want from things you can get. �����������������������������������������������������������������������
"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?
Since the sine waves have the same amplitude, you can do the calculation with triangular waves that have the same peaks and zero crossings instead of using real sine waves. The result is the same. It's easy to calculate a triangular wave if you first turn the frequency into a slew rate per sample. Then you just keep track of the current value and slew*direction, adding the slew rate at every time step, and reflecting the wave against the +1 and -1 rails like a bouncing ball when you hit them, like this: v+=slew; if (v>1) { v=-v+2; slew=-slew; } else if(v<-1) { v=-v-2; slew=-slew; }
Matt Timmermans wrote:

> "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? > > > Since the sine waves have the same amplitude, you can do the calculation > with triangular waves that have the same peaks and zero crossings instead of > using real sine waves. The result is the same.
I doubt it. A few sketches on a scrap of paper leads me to believe otherwise. When one waveform is positive and the other negative, sine of the sum seems to depend on the waveform's shape. ... 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:4032e776$0$3072$61fed72c@news.rcn.com...
> Matt Timmermans wrote: > > Since the sine waves have the same amplitude, you can do the calculation > > with triangular waves that have the same peaks and zero crossings
instead of
> > using real sine waves. The result is the same. > > I doubt it. A few sketches on a scrap of paper leads me to believe > otherwise. When one waveform is positive and the other negative, sine of > the sum seems to depend on the waveform's shape.
The sign of the sum only depends on which waveform has the largest magnitude, and that is completely determined by which one is closest in phase to a peak.
Matt Timmermans wrote:

> "Jerry Avins" <jya@ieee.org> wrote in message > news:4032e776$0$3072$61fed72c@news.rcn.com... > >>Matt Timmermans wrote: >> >>>Since the sine waves have the same amplitude, you can do the calculation >>>with triangular waves that have the same peaks and zero crossings > > instead of > >>>using real sine waves. The result is the same. >> >>I doubt it. A few sketches on a scrap of paper leads me to believe >>otherwise. When one waveform is positive and the other negative, sine of >>the sum seems to depend on the waveform's shape. > > > The sign of the sum only depends on which waveform has the largest > magnitude, and that is completely determined by which one is closest in > phase to a peak.
More careful sketching shows that you're right. Thanks. 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;
"Paul Russell" <prussell@sonic.net> wrote in message
news:cowYb.1882$_3.30739@typhoon.sonic.net...
> 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.)
Could you explain how the method works or give me some references, keywords to google please. Funky
> Paul
"Jerry Avins" <jya@ieee.org> wrote in message
news:4032ca43$0$3104$61fed72c@news.rcn.com...
> Funky wrote: > > ... > > > > 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 > > There is another way. Find each of the sines. (This need not be a call > to a sine routine. Since they describe a smooth sinewave, it takes about > a dozen lines of assembly code for each sine.) Add them. If the sum is > positive, output 1; if negative, 0 or -1 as you choose. You can use the > same continuous sine generator for (a+b) and (a-b), but why bother? > > Jerry
This doesn't seem an efficient way of doing things since you're waisting time keeping track of the sum between zero crossings, even though the corresponding output is constant. Secondly, the zero crossing point in the output will have more jitter on it than necessary because of the time taken to compute sines, add and then check for the sign. The zero crossing points can be known _exactly_ in advance as those of sin(2*pi*(f1+f2)*t) and cos(2*pi*(f1-f2)*t) i.e t = n/(2*(f1+f2)); t = (1/2 +n )/(2*(f1-f2)) A timer is loaded with the value for the next zero crossing time interval and the output is inverted on each overflow interrupt. Simple. Thanks for the interest shown. Funky
> 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:
> > > Could you explain how the method works or give me some references, keywords > to google please. >
Something like this, perhaps: + Initialisation - Calculate the half period of each sine wave in samples (as float, or perhaps integer + fraction) - Using the initial phase of each sine wave, calculate the sample number of the first zero crossing for each sine wave, and the initial sign - Initialise the output state using the above information + For each sample - If this is a zero crossing for either of the two sine waves - Update the sign of the relevant sine wave(s) - Update the sample count(s) for the next zero crossing(s) - Update the output state Paul
"Paul Russell" <prussell@sonic.net> wrote in message
news:W0RYb.2060$_3.33958@typhoon.sonic.net...
> Funky wrote: > > > > > > Could you explain how the method works or give me some references,
keywords
> > to google please. > > > > Something like this, perhaps: > > + Initialisation > - Calculate the half period of each sine wave in samples (as float, > or perhaps integer + fraction) > - Using the initial phase of each sine wave, calculate the sample > number of the first zero crossing for each sine wave, and the initial sign > - Initialise the output state using the above information > > + For each sample > - If this is a zero crossing for either of the two sine waves > - Update the sign of the relevant sine wave(s) > - Update the sample count(s) for the next zero crossing(s) > - Update the output state > > Paul
But how do I use this to get the same output as multiplying the sum by infinity and clipping? Yes, I'm being stupid here. Funky
Funky wrote:
> > But how do I use this to get the same output as multiplying the sum by > infinity and clipping? > Yes, I'm being stupid here. > Funky >
The output only has 2 states: +1 and -1. (Actually there is probably at least one pathological condition where the output will always be zero, but I leave this kind of detail as an exercise for the reader.) Paul