How can you generate a sine wave using integers only?

Click For Summary

Discussion Overview

The discussion revolves around generating a sine wave using integer arithmetic within a grid defined by a large power of two, specifically focusing on the challenges faced when transitioning from floating-point to integer calculations. Participants explore various mathematical approaches and algorithms relevant to this problem.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes a method to generate a quadrature signal using a recursive formula derived from a waveguide-form oscillator, but encounters unexpected results when using integers.
  • Another participant questions the derivation of the variable k and notes that as k increases, the resulting numbers may become excessively large, which could affect calculations.
  • A suggestion is made to consider the digital differential analyzer (DDA) as a potential method for generating the sine wave.
  • There is a clarification that while DDA typically uses floating-point numbers, it can also be implemented with integer arithmetic.
  • Further discussion reveals a modified expression for k, with a rationale provided for its derivation, indicating that for large N, k approaches N/2 - 1.
  • Another participant acknowledges that a variation of the DDA, specifically the Bresenham algorithm, can work with integers, providing an alternative approach to the problem.

Areas of Agreement / Disagreement

Participants express differing views on the applicability of certain algorithms and the implications of using integer arithmetic versus floating-point arithmetic. There is no consensus on the best approach to generate the sine wave using integers, and multiple competing ideas are presented.

Contextual Notes

Some assumptions about the behavior of the algorithms and the mathematical expressions used remain unresolved, particularly regarding the transition from floating-point to integer calculations and the implications of large values of N.

intervoxel
Messages
192
Reaction score
1
TL;DR
A recursive generator of a quadrature signal which fits exactly in a NxN grid, where N is a large power of two.
I need to recursively generate a quadrature signal which fits exactly into a grid NxN, where N is a large power of two.
After unsuccessful research, I decided to develop my own solution, starting from the waveguide-form oscillator taken from Pete Symons' book 'Digital wave generation, p. 100'.

\begin{gather}
\begin{bmatrix}\hat{ y_1}\\\hat{y_2}\end{bmatrix}
=
\begin{bmatrix}
k &k-1\\
k+1&k
\end{bmatrix}
\begin{bmatrix} y_1\\y_2\end{bmatrix}
\end{gather}
where k=cos(\omega T).

This formula works perfectly when using floating point numbers.

In the case of the NxN grid, we have \omega T=2\pi/N, and the amplitude is N/2.
For N>>1, we have k=N/2-1.
The difference equations then become
\begin{gather}
\hat{ y_1}=\frac{2((N/2-1)y_1 - y_2)}{N}
\end{gather}
and
\begin{gather}
\hat{ y_2}=\frac{2((N-1)y_1 + (N/2-1)y_2))}{N}
\end{gather}
If we initialize N=128 and y_1=N/2, y_2=0, we get for the first iteration the values \hat{y_1}=63 and \hat{y_2}=127 (it was expected a low value!). That is, y_2>N/2.

What went wrong? Thank you for any help.
 
Physics news on Phys.org
I don't know where k=N/2 - 1 comes from, but with k getting larger your numbers will get too large as well. This is true for floating point numbers as well.
 
Thank you for answering. DDA uses floats not pure integers.
 
mfb said:
I don't know where k=N/2 - 1 comes from, but with k getting larger your numbers will get too large as well. This is true for floating point numbers as well.
Of course the modified k grows, it is an integer constant.
The rationale for the modified expression for k is:
lim_{N\to\infty} \frac{N}{2}sin(2\pi/N)=\pi
sin^2\theta+cos^2\theta=1
\overline{k}=\sqrt{\frac{N^2}{4}-\pi^2}
for N>>1 we have
\sqrt{\frac{N^2}{4}-\pi^2}\approx \frac{N}{2}-1
 
intervoxel said:
Thank you for answering. DDA uses floats not pure integers.
From the Wikipedia article: "The DDA method can be implemented using floating-point or integer arithmetic. " I know that it is so, because a research institute I once worked for implemented a version using only digital logic chips back in the late 60's.
 
  • Like
Likes   Reactions: intervoxel
You are right, a DDA variation called Bresenheim algorithm for circles works with integers. Thank you.
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 14 ·
Replies
14
Views
3K
Replies
4
Views
5K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 43 ·
2
Replies
43
Views
3K