Digital Signal Processing Reference
In-Depth Information
Example 5.3: Sine Generation Using a Difference Equation (
sinegenDE
)
This example also generates a sinusoidal tone using an alternative difference equa-
tion. See also Example 5.2, which generates/adds two tones. Consider the second-
order difference equation obtained in Chapter 4:
()
=
(
)
+
(
)
+
(
)
y n
Ay n
-
1
By n
-
2
Cx n
-
1
where
B
=-
1. Apply an impulse at
n
=
1, so that
x
(
n
-
1)
=
x
(0)
=
1, and 0 other-
wise. For
n
=
1,
()
=
()
+
()
+
()
=
y
1
y
0
y
1
x
0
C
with
y
(0)
=
0 and
y
(
-
1)
=
0. For
n
2,
()
=
(
)
--
(
)
y n
Ay n
-
1
y n
2 .
The coefficients
A
=
2cos(
w
T
) and
C
=
sin (
w
T
) are calculated for a given sampling
period
T
=
1/
F
s
and a desired frequency
w
.
f
=
1.5kHz
(
)
=
14
A
=
2
cos
w
T
0 765
.
Æ¥
A
2
=
12 540
,
()
==
y
1
C
0 924
.
Æ¥ =
C
2
14
15 137
,
()
=
()
=
Æ
()
¥=
y
2
y
1
0 707
.
y
2
2
14
11 585
,
f
=
2kHz
(
)
=
A
=
2
cos
sin
w
T
0
()
==
(
)
=Æ ¥
y
1
C
w
T
1
C
2
14
=
16 384
,
()
=
()
-
()
==
y
2
y
1
y
0
C
0
Figure 5.21 shows a listing of the program
sinegenDE.c
, which generates a sine
wave using this alternative difference equation. This difference equation is calcu-
lated within the ISR using an alternative scheme to the implementation in Example
5.2. The coefficient
A
=
0, and the array
y[3]
, which contains
y
(0),
y
(1), and
y
(2),
generate a 2-kHz sine wave.
Build and run this project as
sinegenDE
. Verify that the output is a 2-kHz tone.
Change the array to
y[3]={0,15,137,11,585}
and
A=12,540
. Rebuild/run the
program and verify a 1.5-kHz tone generated at the output. A 3-kHz tone can be
generated using
A
=-
23,170 and
y[3]={0,11,585,0}
.
Search WWH ::
Custom Search