-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinspect_binary_data.asv
97 lines (74 loc) · 2.28 KB
/
inspect_binary_data.asv
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
%% Reshape Binary Data
% Ephys
nchannels = 16;
ephysx = reshape(ephysx, [], 1);
ephysx = reshape(ephysx,[nchannels, round(length(ephysx)/nchannels)]); % Reshape binary vector to N channel matrix
%% Sniff
sniff_nchannels = 8;
adcx = reshape(adcx, [], 1);
adcx = reshape(adcx,[sniff_nchannels, round(length(adcx)/sniff_nchannels)]); % Reshape binary vector to N channel matrix
%% Get Sniff Signal
ch = 8;
sniff = adcx(ch, :); % Sniff signal should be in channel 8
% correct jumps in sniff signal
for x = 1:length(sniff)
if sniff(x) > 40000
sniff(x) = sniff(x) - 65520;
end
end
%% Correct jumps in Ephys Signal
for ch = 1:nchannels
for x = 1:length(ephysx(ch,:))
if ephysx(ch, x) > 40000
ephysx(ch, x) = ephysx(ch, x) - 65520;
end
end
end
%% Resample Sniff signals to 1khz
resample_factor = 30;
sniff = resample(sniff, 1, resample_factor);
sniff_smooth = smooth(sniff, 25, 'sgolay');
%% Find inhalation times
[pks, locs] = findpeaks(sniff_smooth, 'MinPeakProminence', 50);
%% Plot sniff peaks against signal
win_beg = 30000;
win_end = win_beg + 30000;
% Find nearest peak to window start and ending
abs_diff = abs(locs - window);
[~, end_idx] = min(abs_diff);
plot(sniff_smooth(1:window))
hold on
scatter(locs(1:idx), pks(1:idx))
%% Resample Ephys to 1khz
% Initialize resampled ephys matrix
N_rs = width(ephysx) / 30;
ephysx_rs = zeros(16, N_rs);
% Resample each channel in ephys data
for i = 1:16
ephysx_rs(i,:) = resample(ephysx(i,:), 1, resample_factor);
end
%% Visualize Ephys channels with inhalation times
% Window to plot
window = 30000;
window_start = 500000;
window_end = window_start + window;
% Set up plot
clf
cmap = colormap('jet');
colorIndices = round(linspace(1, size(cmap, 1), nchannels));
for ii = 3%:nchannels
% Plot traces
p = plot(ephysx(ii, window_start:window_end));
color = cmap(colorIndices(ii), :);
p.Color = color; % Set the color
% Add a label at the trace and legend
label_str = "Ch " + num2str(ii);
p.DisplayName = "Ch " + num2str(ii);
end_val = ephysx(ii,window); % Y position of label
t = text(window, end_val-5, label_str, 'HorizontalAlignment', 'left', 'VerticalAlignment', 'bottom');
t.Color = color;
hold on
end
%plot(sniff(window_start:window_end));
legend()
hold off