-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoscwave_propmdl_3.m
113 lines (88 loc) · 2.51 KB
/
oscwave_propmdl_3.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
%% partial frequency (period) correction --> update freq each timepont & calculate predicted duration accordingly
clear all;clc;
close all;
fs = 1000;
Ts = 1/fs;
% ======= make stim =======
stim = zeros(110,1)';
ioilist = [150, 120, 130, 140, 120];% [200, 200, 300, 600, 250];
for i = 1:length(ioilist)
x = zeros(ioilist(i),1)';
x(1) = 2;
stim = [stim x];
end
fig = figure;
set(fig,'Position', [ 1001 965 1085 374]);
subplot(3,1,1)
hold on;
EK_xAxisMarker(find(stim>0), [0 0 1]);
% ======= init params =======
theta = 0;
freq_mdl = 5; % fs/samples << initial freq
freq = freq_mdl;
mdl_ioi = fs/freq_mdl;
W = .9;
% WP = .5;
tbl = table();
a = 1;
k = 1;
t = Ts;
wave = [];
freqk = [];
thetak = [];
radphak = [];
radphak(1) = t*(2*pi*freq)+theta;
while 1
if stim(k) > 0 % if you see a stimulus (sound)
if a == 1
ioi = k; % ioi = n samples
prev_theta = theta;
else
ioi = k - tbl.k(a-1);
prev_theta = thetak(k-1);
end
% -------------- update wave frequency --------------
freqnew = fs/(mdl_ioi + (ioi - mdl_ioi) * W); % avg ioi
disp(['freq was ' num2str(freq) ' --> ' num2str(freqnew)]);
% -------------- set required theta to match phase to prev --------------
set_theta = prev_theta - t*2*pi*(freqnew - freq);
% -------------- assign updated values
freq = freqnew;
theta = set_theta;
contin_ok = input('continue? (1/0): ');
if contin_ok ==0
break;
end
% -------------- save interval info --------------
tbl.ioi(a) = ioi;
tbl.freq(a) = freq;
tbl.k(a) = k;
tbl.W(a) = W;
a = a+1;
end
% -------------- save timepoint info --------------
thetak(k) = theta;
freqk(k) = freq;
wave(k) = cos(t*(2*pi*freq)+theta);
% -------------- plot wave generated up to now --------------
plot(wave, 'linewidth', 1.2, 'color', 'm');
% increase time
t = t+Ts;
if k == length(stim)
break
end
k = k+1;
end
EK_plotlabels('time', 'cos()', 'model wave',18);
subplot(3,1,2)
hold on;
EK_xAxisMarker(find(stim>0), [0 0 1]);
plot(freqk, 'linewidth', 2, 'color', 'g');
EK_plotlabels('time', 'model frequency', '',18);
subplot(3,1,3)
hold on;
EK_xAxisMarker(find(stim>0), [0 0 1]);
plot(thetak, 'linewidth', 2, 'color', 'g');
yline(0, '--k')
EK_plotlabels('time', 'model \theta', '',18);
%% func