LMMS
Loading...
Searching...
No Matches
Sine.h
Go to the documentation of this file.
1/*
2 dsp/Sine.h
3
4 Copyright 2003-4 Tim Goetze <tim@quitte.de>
5
6 http://quitte.de/dsp/
7
8 direct form I recursive sin() generator.
9
10*/
11/*
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License
14 as published by the Free Software Foundation; either version 2
15 of the License, or (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 02111-1307, USA or point your web browser to http://www.gnu.org.
26*/
27
28#ifndef _DSP_SINE_H_
29#define _DSP_SINE_H_
30
31namespace DSP {
32
33class Sine
34{
35 protected:
36 int z;
37 double y[2];
38 double b;
39
40 public:
42 {
43 b = 0;
44 y[0] = y[1] = 0;
45 z = 0;
46 }
47
48 Sine (double f, double fs, double phase)
49 {
50 set_f (f, fs, phase);
51 }
52
53 Sine (double omega, double phase = 0.)
54 {
55 set_f (omega, phase);
56 }
57
58 inline void set_f (double f, double fs, double phase)
59 {
60 set_f (f * M_PI / fs, phase);
61 }
62
63 inline void set_f (double w, double phase)
64 {
65 b = 2 * cos (w);
66 y[0] = sin (phase - w);
67 y[1] = sin (phase - w * 2);
68 z = 0;
69 }
70
71 /* advance and return 1 sample */
72 inline double get()
73 {
74 double s = b * y[z];
75 z ^= 1;
76 s -= y[z];
77 return y[z] = s;
78 }
79
80 double get_phase()
81 {
82 double x0 = y[z], x1 = b * y[z] - y[z^1];
83 double phi = asin (x0);
84
85 /* slope is falling, we're into the 2nd half. */
86 if (x1 < x0)
87 return M_PI - phi;
88
89 return phi;
90 }
91};
92
93} /* namespace DSP */
94
95#endif /* _DSP_SINE_H_ */
Sine(double f, double fs, double phase)
Definition Sine.h:48
double get_phase()
Definition Sine.h:80
double get()
Definition Sine.h:72
int z
Definition Sine.h:36
void set_f(double f, double fs, double phase)
Definition Sine.h:58
Sine()
Definition Sine.h:41
double b
Definition Sine.h:38
Sine(double omega, double phase=0.)
Definition Sine.h:53
double y[2]
Definition Sine.h:37
void set_f(double w, double phase)
Definition Sine.h:63
#define M_PI
Definition compat.h:149
UINT_D64 w
Definition inflate.c:942
unsigned s
Definition inflate.c:1555
unsigned f
Definition inflate.c:1572
Definition BiQuad.h:31