-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJAT_MultidienTEST.m
218 lines (156 loc) · 5.19 KB
/
JAT_MultidienTEST.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
pcNAME = getenv('COMPUTERNAME');
switch pcNAME
case 'DESKTOP-EGFQKAI'
mainLOC = 'C:\Users\johna\OneDrive\Documents\Github\multidien_dIEA';
case 'DESKTOP-FAGRV5G'
mainLOC = 'C:\Users\Admin\Documents\Github\multidien_dIEA';
otherwise
end
cd(mainLOC)
%%
fooof_fileN = '10162654_fooof.mat';
% load(fooof_fileN)
%%
histo_fileN = 'UColorado_AAB_10162654_Histogram_Hourly.csv';
%%
data_fooof = load(fooof_fileN);
data_histogram = readtable(histo_fileN);
%%
histo_days_since_implant = days(data_histogram.RegionStartTime - data_fooof.metadata.Implant);
% don't worry that some values are negative - these are pre-implant
histo_detections_preZ = data_histogram.EpisodeStarts;
plot(histo_days_since_implant,histo_detections_preZ) %%%
xlabel('days since implant')
ylabel('detections')
%%
skipDATA = floor(diff(histo_days_since_implant));
startIND = find(skipDATA) + 1;
stopIND = startIND + skipDATA(find(skipDATA));
part1 = histo_detections_preZ(1:startIND(1)-1);
part1a = nan(stopIND(1)-startIND(1),1)
part2 = histo_detections_preZ(stopIND(1)+1:startIND(2)-1)
part2a = nan(stopIND(2)-startIND(2),1)
part3 = histo_detections_preZ(stopIND(2)+1:startIND(3)-1)
part3a = nan(stopIND(3)-startIND(3),1)
part4 = histo_detections_preZ(stopIND(3)+1:end)
allParts = [part1 ; part1a ; part2 ; part2a ; part3 ; part3a; part4]
%%
epoch_edges = data_fooof.epoch_days;
epoch_edges = vertcat(epoch_edges,min(histo_days_since_implant),max(histo_days_since_implant)+1);
epoch_edges = sort(epoch_edges);
%% z-score histogram counts within each epoch
histo_detections_zScored = [];
for iEpoch = 1:(length(epoch_edges)-1)
idx = histo_days_since_implant >= epoch_edges(iEpoch) & ...
histo_days_since_implant < epoch_edges(iEpoch+1);
preZ = histo_detections_preZ(idx);
if all(isnan(preZ))
continue
elseif any(isnan(preZ))
preZ = preZ(~isnan(preZ));
end
postZ = (preZ - mean(preZ,"omitnan")) / std(preZ,"omitnan");
% can't use "zscore" function because of NaN values
histo_detections_zScored = vertcat(histo_detections_zScored,postZ);
end
%%
hdzS = smoothdata(histo_detections_zScored,'gaussian',50);
close
[u1,d] = envelope(hdzS,50,'rms');
[u2,d] = envelope(hdzS,10,'peak');
figure;
% disp(any(isnan(histo_detections_zScored)))
plot(hdzS)
hold on
plot(u1)
plot(u2)
yline(mean(u2)-(std(u2)*0.3))
peakENV1 = u2;
peakENV1(u2 < mean(u2)-(std(u2)*0.3)) = 0;
plot(peakENV1)
legend('Raw','RMS','Peak')
%%
plot(peakENV1)
N = numel(peakENV1); % Number of data points
fftResult = fft(peakENV1); % Compute FFT
% Calculate the two-sided spectrum and then the single-sided spectrum
P2 = abs(fftResult/N);
P1 = P2(1:N/2+1);
P1(2:end-1) = 2*P1(2:end-1);
Fs = 0.24; % Sampling frequency (times per day)
f = Fs*(0:(N/2))/N; % Frequency vector
% [peakValues, peakLocations] = findpeaks(P1, 'MinPeakHeight', threshold);
[peakValues, peakLocations] = findpeaks(P1, 'MinPeakHeight', 0.3);
peakFrequencies = f(peakLocations); % Frequencies corresponding to the found peaks
figure;
plot(f, P1);
title('Single-Sided Amplitude Spectrum of Data');
xlabel('Frequency (cycles per day)');
ylabel('|P1(f)|');
hold on;
plot(peakFrequencies, peakValues, 'r*', 'MarkerSize', 10); % Mark peaks
hold off;
%%
[pxx,f] = periodogram(peakENV1,[],[],1);
pow2plot = pow2db(pxx);
% plot(f,10*log10(pxx))
plot(f(5:end),pow2plot(5:end))
pow2plotS = smoothdata(pow2plot,'sgolay',120)
xlabel('Cycles/Day')
ylabel('dB / (Cycles/Year)')
%%
histo_days_since_implant2 = histo_days_since_implant(8:end)
allParts2 = allParts(5:end)
histo_days_since_implant2 = days(histo_days_since_implant2)
oneday = seconds(days(1));
[pxx,f] = plomb(allParts2,histo_days_since_implant2,0.0417,10,'normalized');
f = f*oneday;
[pmax,lmax] = max(pxx);
f0 = f(lmax);
plot(f,pxx,f0,pmax,'o')
xlabel('Frequency (day^{-1})')
%%
close all
[pxx,w] = periodogram(hdzS);
pow2plot = pow2db(pxx);
pow2plotS = smoothdata(pow2plot,'sgolay',120);
% remove 0.05
ind2keep = w >= 0.1;
powDATA = pow2plotS(ind2keep);
wDATA = w(ind2keep);
plot(w,pow2plotS)
xlim([0.1 3])
meanPOW = mean(pow2plotS);
stdPOW = std(pow2plotS);
thrFac = 1.25;
thrPOW = meanPOW + (stdPOW * thrFac);
yline(thrPOW,'-k','threshold')
%%
close all
coefficients = polyfit(wDATA, powDATA, 2);
y_fit = polyval(coefficients, wDATA);
% x_fit = linspace(min(wDATA), max(wDATA), 100); % Generate points for a smooth plot
% y_fit = polyval(coefficients, x_fit); % Evaluate polynomial
residuals = powDATA - y_fit;
meanPOW = mean(residuals);
stdPOW = std(residuals);
thrFac = 1.25;
thrPOW = meanPOW + (stdPOW * thrFac);
plot(wDATA,residuals)
yline(thrPOW,'-k','threshold')
%%
close all
detS = detrend(powDATA)
plot(detS)
%%
close all
periodogram(histo_detections_zScored);
%1) hourly histogram event counts
%2) z-score between programming changes
%3) periodogram
%4) find significant periodogram peaks
%5) inverse wavelet transform to reconstruct each peak (effectively bandpassing at significant frequencies)
%6) find period with highest phase locking value
%7) use hilbert transform to calculate phase angle of each scheduled recording
%8) classify scheduled recordings as peak/trough/rising/falling phase
% The paper from Penn has some specifics but is missing other details and has things I would do differently.