-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_chirp_signals.m
140 lines (105 loc) · 3.57 KB
/
get_chirp_signals.m
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
%
% This file is part of pichim's controller tuning framework.
%
% This sofware is free. You can redistribute this software
% and/or modify this software under the terms of the GNU General
% Public License as published by the Free Software Foundation,
% either version 3 of the License, or (at your option) any later
% version.
%
% This software is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
%
% See the GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public
% License along with this software.
%
% If not, see <http:%www.gnu.org/licenses/>.
%
%%
function [exc, fchirp, sinarg] = get_chirp_signals(f0, f1, t1, Ts)
% [exc, fchirp, sinarg] = get_chirp_signals(f0, f1, t1, Ts)
% [exc, fchirp, sinarg] = get_chirp_signals(f0, f1, t1, time)
if (length(Ts) == 1)
% bf implementation
chirp = chirpInit(f0, f1, t1, Ts);
exc = zeros(chirp.N, 1);
fchirp = zeros(chirp.N, 1);
sinarg = zeros(chirp.N, 1);
while (true)
chirp = chirpUpdate(chirp);
if (chirp.isFinished)
break
end
exc(chirp.count) = chirp.exc;
fchirp(chirp.count) = chirp.fchirp;
sinarg(chirp.count) = chirp.sinarg;
end
else
% direct version
time = Ts;
beta = (f1 / f0)^(1.0 / t1);
k0 = 2.0 * pi / log(beta);
k1 = k0 * f0;
fchirp = f0 * beta.^(time);
sinarg = k0 * fchirp - k1;
% wrap sinarg to 0...2*pi
sinarg = mod(sinarg, 2.0 * pi);
% use cosine so that the angle will oscillate around 0 (integral of gyro)
exc = cos(sinarg);
% frequencies below 1 Hz will lead to the same angle magnitude as at 1 Hz (integral of gyro)
ind = fchirp < 1.0;
exc(ind) = fchirp(ind) .* exc(ind);
end
end
% initialize the chirp signal generator
% f0: start frequency in Hz
% f1: end frequency in Hz
% t1: signal length in seconds
% Ts: sampling time in seconds
function chirp = chirpInit(f0, f1, t1, Ts)
chirp.f0 = f0;
chirp.Ts = Ts;
chirp.N = floor(t1 / chirp.Ts);
chirp.beta = (f1 / f0)^(1.0 / t1);
chirp.k0 = 2.0 * pi / log(chirp.beta);
chirp.k1 = chirp.k0 * chirp.f0;
chirp = chirpReset(chirp);
end
% reset the chirp signal generator fully
function chirp = chirpReset(chirp)
chirp.count = 0;
chirp.isFinished = false;
chirpResetSignals(chirp);
end
% reset the chirp signal generator signals
function chirp = chirpResetSignals(chirp)
chirp.exc = 0.0;
chirp.fchirp = 0.0;
chirp.sinarg = 0.0;
end
% update the chirp signal generator
function chirp = chirpUpdate(chirp)
if (chirp.isFinished)
return
elseif (chirp.count == chirp.N)
chirp.isFinished = true;
chirpResetSignals(chirp);
return
else
chirp.fchirp = chirp.f0 * chirp.beta^(chirp.count * chirp.Ts);
chirp.sinarg = chirp.k0 * chirp.fchirp - chirp.k1;
% wrap sinarg to 0...2*pi
chirp.sinarg = mod(chirp.sinarg, 2.0 * pi);
% use cosine so that the angle will oscillate around 0 (integral of gyro)
chirp.exc = cos(chirp.sinarg);
% frequencies below 1 Hz will lead to the same angle magnitude as at 1 Hz (integral of gyro)
if (chirp.fchirp < 1.0)
chirp.exc = chirp.fchirp * chirp.exc;
end
chirp.count = chirp.count + 1;
return
end
end