-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwave.h
118 lines (103 loc) · 1.88 KB
/
wave.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//wave generator
#ifndef ___KAL_WAVE_H
#define ___KAL_WAVW_H
#include "config.h"
#include "utilize.h"
//type
#define CONST 0
#define SIN 1
#define TRIANGLE 2
#define CHARPSIN 3
#define RECTANGLE 4
namespace kal{
class wave{
public:
double t;
int type;//@todo:色々な波形を追加
double ave;
double amp;
double f;
double phase;
double output;
wave();
wave(double ave_in,double amp_in,double f_in,double phase_in,int type);
double update();
};
wave::wave(){
t=0.0;
ave=0.0;
amp=0.0;
f=0.0;
output=0.0;
phase=0.0;
type = SIN;
}
wave::wave(double ave_in,double amp_in,double f_in,double phase_in=0.0,int type_in = SIN){
ave=ave_in;
amp=fabs(amp_in);
f=f_in;
output=0.0;
type = type_in;
if(type == CHARPSIN){
f = 0.0;
}
phase = phase_in;
range(-2.0*PI,2.0*PI,phase);
if(phase<0.0){
phase = 2.0*PI+phase;
}
t = phase/2.0/PI/f;
}
double wave::update(){
t+=Ts;
//t = t%((int)(1.0/f));//tの∞積分を防止
switch (type){
case CONST:
output = ave;
break;
case SIN:
if(t>=1.0/f){
t = 0.0;
}
output = amp*sin(2.0*PI*f*t) + ave;
break;
case TRIANGLE:
if(t>=1.0/f){
t = 0.0;
}
if(0.0<=t && t<=1.0/4.0/f){
output = amp/(1.0/f/4) * t + ave;
}
else if(1.0/4.0/f<t && t<=3.0/4.0/f){
output = -amp/(1.0/f/4.0) * t + (ave + 2.0*amp);
}
else if(3.0/4.0/f<t && t<=1.0/f){
output = amp/(1.0/f/4.0) * t + (ave - 4.0*amp);
}
break;
case CHARPSIN:
f += 0.0005;
output = amp*sin(2.0*PI*f*t) + ave;
if(f>5.0){
f = 0.0;
t = 0.0;
}
break;
case RECTANGLE:
if(t>=1.0/f){
t = 0.0;
}
if(0.0<=t && t<=1.0/2.0/f){
output = amp + ave;
}
else if(1.0/2.0/f<t && t<=1.0/f){
output = -amp + ave;
}
break;
default:
break;
}
return output;
}
}
#endif